Merge branch 'master' of https://github.com/calf-studio-gear/calf
[calf.git] / src / utils.cpp
blob0c07d8ffcf4c5878aceb27d82a9b84da2ea0845e
1 /* Calf DSP Library
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
20 #include <config.h>
21 #include <calf/osctl.h>
22 #include <calf/utils.h>
23 #include <stdio.h>
24 #include <sstream>
26 using namespace std;
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;
40 return sb.data;
43 void decode_map(dictionary &data, const string &src)
45 osctl::string_buffer sb(src);
46 osc_stream<osctl::string_buffer> str(sb);
47 uint32_t count = 0;
48 str >> count;
49 string tmp, tmp2;
50 data.clear();
51 for (uint32_t i = 0; i < count; i++)
53 str >> tmp;
54 str >> tmp2;
55 data[tmp] = tmp2;
59 std::string xml_escape(const std::string &src)
61 string dest;
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])+";";
66 else
67 dest += src[i];
69 return dest;
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)
79 std::string str;
80 FILE *f = fopen(src.c_str(), "rb");
81 if (!f)
82 throw file_exception(src);
83 while(!feof(f))
85 char buffer[1024];
86 int len = fread(buffer, 1, sizeof(buffer), f);
87 if (len < 0)
89 fclose(f);
90 throw file_exception(src);
92 str += string(buffer, len);
94 fclose(f);
95 return str;
98 std::string i2s(int value)
100 char buf[32];
101 snprintf(buf, sizeof(buf), "%d", value);
103 return std::string(buf);
106 std::string f2s(double value)
108 stringstream ss;
109 ss << value;
110 return ss.str();
113 std::string ff2s(double value)
115 string s = f2s(value);
116 if (s.find('.') == string::npos)
117 s += ".0";
118 return s;
121 std::string indent(const std::string &src, const std::string &indent)
123 std::string dest;
124 size_t pos = 0;
125 do {
126 size_t epos = src.find("\n", pos);
127 if (epos == string::npos)
128 break;
129 dest += indent + src.substr(pos, epos - pos) + "\n";
130 pos = epos + 1;
131 } while(pos < src.length());
132 if (pos < src.length())
133 dest += indent + src.substr(pos);
134 return dest;
137 //////////////////////////////////////////////////////////////////////////////////
139 file_exception::file_exception(const std::string &f)
140 : message(strerror(errno))
141 , filename(f)
142 , container(filename + ":" + message)
144 text = container.c_str();
147 file_exception::file_exception(const std::string &f, const std::string &t)
148 : message(t)
149 , filename(f)
150 , container(filename + ":" + message)
152 text = container.c_str();
156 /* Returns a list of files in a directory (except the ones that begin with a dot) */
157 vector <direntry> list_directory(const string &path)
159 std::vector <direntry> out;
160 DIR *dir;
161 class dirent *ent;
162 dir = opendir(path.empty() ? "." : path.c_str());
163 while ((ent = readdir(dir)) != NULL) {
164 direntry f;
165 const string file_name = ent->d_name;
166 const string full_file_name = path + "/" + file_name;
167 if (file_name[0] == '.') continue;
168 f.name = file_name;
169 f.directory = path;
170 f.full_path = full_file_name;
171 out.push_back(f);
173 closedir(dir);
174 return out;