2 * Various utility functions.
3 * Copyright (C) 2007 Krzysztof Foltman
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
23 #include <calf/osctl.h>
24 #include <calf/utils.h>
28 using namespace osctl
;
30 namespace calf_utils
{
32 string
encode_map(const dictionary
&data
)
34 osctl::string_buffer sb
;
35 osc_stream
<osctl::string_buffer
> str(sb
);
36 str
<< (uint32_t)data
.size();
37 for(dictionary::const_iterator i
= data
.begin(); i
!= data
.end(); i
++)
39 str
<< i
->first
<< i
->second
;
44 void decode_map(dictionary
&data
, const string
&src
)
46 osctl::string_buffer
sb(src
);
47 osc_stream
<osctl::string_buffer
> str(sb
);
52 for (uint32_t i
= 0; i
< count
; i
++)
60 std::string
xml_escape(const std::string
&src
)
63 for (size_t i
= 0; i
< src
.length(); i
++) {
64 // XXXKF take care of string encoding
65 if (src
[i
] < 0 || src
[i
] == '"' || src
[i
] == '<' || src
[i
] == '>' || src
[i
] == '&')
66 dest
+= "&"+i2s((uint8_t)src
[i
])+";";
73 std::string
load_file(const std::string
&src
)
76 FILE *f
= fopen(src
.c_str(), "rb");
78 throw file_exception(src
);
82 int len
= fread(buffer
, 1, sizeof(buffer
), f
);
84 throw file_exception(src
);
85 str
+= string(buffer
, len
);
90 std::string
i2s(int value
)
93 sprintf(buf
, "%d", value
);
95 return std::string(buf
);
98 std::string
f2s(double value
)
105 std::string
ff2s(double value
)
107 string s
= f2s(value
);
108 if (s
.find('.') == string::npos
)
113 std::string
indent(const std::string
&src
, const std::string
&indent
)
118 size_t epos
= src
.find("\n", pos
);
119 if (epos
== string::npos
)
121 dest
+= indent
+ src
.substr(pos
, epos
- pos
) + "\n";
123 } while(pos
< src
.length());
124 if (pos
< src
.length())
125 dest
+= indent
+ src
.substr(pos
);