/
utils.cpp
177 lines (159 loc) · 4.36 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Calf DSP Library
* Various utility functions.
* Copyright (C) 2007 Krzysztof Foltman
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <calf/osctl.h>
#include <calf/utils.h>
#include <stdio.h>
#include <sstream>
using namespace std;
using namespace osctl;
namespace calf_utils {
string encode_map(const dictionary &data)
{
osctl::string_buffer sb;
osc_stream<osctl::string_buffer> str(sb);
str << (uint32_t)data.size();
for(dictionary::const_iterator i = data.begin(); i != data.end(); ++i)
{
str << i->first << i->second;
}
return sb.data;
}
void decode_map(dictionary &data, const string &src)
{
osctl::string_buffer sb(src);
osc_stream<osctl::string_buffer> str(sb);
uint32_t count = 0;
str >> count;
string tmp, tmp2;
data.clear();
for (uint32_t i = 0; i < count; i++)
{
str >> tmp;
str >> tmp2;
data[tmp] = tmp2;
}
}
std::string xml_escape(const std::string &src)
{
string dest;
for (size_t i = 0; i < src.length(); i++) {
// XXXKF take care of string encoding
if (src[i] < 0 || src[i] == '"' || src[i] == '<' || src[i] == '>' || src[i] == '&')
dest += "&"+i2s((uint8_t)src[i])+";";
else
dest += src[i];
}
return dest;
}
std::string to_xml_attr(const std::string &key, const std::string &value)
{
return " " + key + "=\"" + xml_escape(value) + "\"";
}
std::string load_file(const std::string &src)
{
std::string str;
FILE *f = fopen(src.c_str(), "rb");
if (!f)
throw file_exception(src);
while(!feof(f))
{
char buffer[1024];
int len = fread(buffer, 1, sizeof(buffer), f);
if (len < 0)
{
fclose(f);
throw file_exception(src);
}
str += string(buffer, len);
}
fclose(f);
return str;
}
std::string i2s(int value)
{
char buf[32];
snprintf(buf, sizeof(buf), "%d", value);
return std::string(buf);
}
std::string f2s(double value)
{
stringstream ss;
ss << value;
return ss.str();
}
std::string ff2s(double value)
{
string s = f2s(value);
if (s.find('.') == string::npos)
s += ".0";
return s;
}
std::string indent(const std::string &src, const std::string &indent)
{
std::string dest;
size_t pos = 0;
do {
size_t epos = src.find("\n", pos);
if (epos == string::npos)
break;
dest += indent + src.substr(pos, epos - pos) + "\n";
pos = epos + 1;
} while(pos < src.length());
if (pos < src.length())
dest += indent + src.substr(pos);
return dest;
}
//////////////////////////////////////////////////////////////////////////////////
file_exception::file_exception(const std::string &f)
: message(strerror(errno))
, filename(f)
, container(filename + ":" + message)
{
text = container.c_str();
}
file_exception::file_exception(const std::string &f, const std::string &t)
: message(t)
, filename(f)
, container(filename + ":" + message)
{
text = container.c_str();
}
/* Returns a list of files in a directory (except the ones that begin with a dot) */
vector <direntry> list_directory(const string &path)
{
std::vector <direntry> out;
DIR *dir;
struct dirent *ent;
dir = opendir(path.empty() ? "." : path.c_str());
while ((ent = readdir(dir)) != NULL) {
direntry f;
const string file_name = ent->d_name;
const string full_file_name = path + "/" + file_name;
if (file_name[0] == '.') continue;
f.name = file_name;
f.directory = path;
f.full_path = full_file_name;
out.push_back(f);
}
closedir(dir);
return out;
}
}