+ AutoHell: make LV2 GUI .so loadable again
[calf.git] / src / utils.cpp
blob0a5dacf44d28396a9e80442c03ee86d7bba1f85c
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., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <assert.h>
22 #include <config.h>
23 #include <calf/osctl.h>
24 #include <calf/utils.h>
25 #include <sstream>
27 using namespace std;
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;
41 return sb.data;
44 void decode_map(dictionary &data, const string &src)
46 osctl::string_buffer sb(src);
47 osc_stream<osctl::string_buffer> str(sb);
48 uint32_t count = 0;
49 str >> count;
50 string tmp, tmp2;
51 data.clear();
52 for (uint32_t i = 0; i < count; i++)
54 str >> tmp;
55 str >> tmp2;
56 data[tmp] = tmp2;
60 std::string xml_escape(const std::string &src)
62 string dest;
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])+";";
67 else
68 dest += src[i];
70 return dest;
73 std::string load_file(const std::string &src)
75 std::string str;
76 FILE *f = fopen(src.c_str(), "rb");
77 if (!f)
78 throw file_exception(src);
79 while(!feof(f))
81 char buffer[1024];
82 int len = fread(buffer, 1, sizeof(buffer), f);
83 if (len < 0)
84 throw file_exception(src);
85 str += string(buffer, len);
87 return str;
90 std::string i2s(int value)
92 char buf[32];
93 sprintf(buf, "%d", value);
95 return std::string(buf);
98 std::string f2s(double value)
100 // XXXKF might not work with some locale settings
101 stringstream ss;
102 ss << value;
103 return ss.str();
106 std::string indent(const std::string &src, const std::string &indent)
108 std::string dest;
109 size_t pos = 0;
110 do {
111 size_t epos = src.find("\n", pos);
112 if (epos == string::npos)
113 break;
114 dest += indent + src.substr(pos, epos - pos) + "\n";
115 pos = epos + 1;
116 } while(pos < src.length());
117 if (pos < src.length())
118 dest += indent + src.substr(pos);
119 return dest;