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
21 #include <calf/osctl.h>
22 #include <calf/utils.h>
27 using namespace osctl
;
29 namespace calf_utils
{
31 string
encode_map(const dictionary
&data
)
33 osctl::string_buffer sb
;
34 osc_stream
<osctl::string_buffer
> str(sb
);
35 str
<< (uint32_t)data
.size();
36 for(dictionary::const_iterator i
= data
.begin(); i
!= data
.end(); i
++)
38 str
<< i
->first
<< i
->second
;
43 void decode_map(dictionary
&data
, const string
&src
)
45 osctl::string_buffer
sb(src
);
46 osc_stream
<osctl::string_buffer
> str(sb
);
51 for (uint32_t i
= 0; i
< count
; i
++)
59 std::string
xml_escape(const std::string
&src
)
62 for (size_t i
= 0; i
< src
.length(); i
++) {
63 // XXXKF take care of string encoding
64 if (src
[i
] < 0 || src
[i
] == '"' || src
[i
] == '<' || src
[i
] == '>' || src
[i
] == '&')
65 dest
+= "&"+i2s((uint8_t)src
[i
])+";";
72 std::string
to_xml_attr(const std::string
&key
, const std::string
&value
)
74 return " " + key
+ "=\"" + xml_escape(value
) + "\"";
77 std::string
load_file(const std::string
&src
)
80 FILE *f
= fopen(src
.c_str(), "rb");
82 throw file_exception(src
);
86 int len
= fread(buffer
, 1, sizeof(buffer
), f
);
88 throw file_exception(src
);
89 str
+= string(buffer
, len
);
94 std::string
i2s(int value
)
97 snprintf(buf
, sizeof(buf
), "%d", value
);
99 return std::string(buf
);
102 std::string
f2s(double value
)
109 std::string
ff2s(double value
)
111 string s
= f2s(value
);
112 if (s
.find('.') == string::npos
)
117 std::string
indent(const std::string
&src
, const std::string
&indent
)
122 size_t epos
= src
.find("\n", pos
);
123 if (epos
== string::npos
)
125 dest
+= indent
+ src
.substr(pos
, epos
- pos
) + "\n";
127 } while(pos
< src
.length());
128 if (pos
< src
.length())
129 dest
+= indent
+ src
.substr(pos
);
133 //////////////////////////////////////////////////////////////////////////////////
135 file_exception::file_exception(const std::string
&f
)
136 : message(strerror(errno
))
138 , container(filename
+ ":" + message
)
140 text
= container
.c_str();
143 file_exception::file_exception(const std::string
&f
, const std::string
&t
)
146 , container(filename
+ ":" + message
)
148 text
= container
.c_str();