1 /***************************************************************************
2 escape.cpp - escaping of strings
4 begin : Sun Nov 14 1999
5 copyright : (C) 1999 by Intra2net AG
6 email : info@intra2net.com
7 ***************************************************************************/
18 #include <stringfunc.hxx>
22 std::string iso_to_utf8(const std::string& isostring)
26 iconv_t i2utf8 = iconv_open ("UTF-8", "ISO-8859-1");
28 if (iso_to_utf8 == (iconv_t)-1)
29 throw runtime_error("iconv can't convert from ISO-8859-1 to UTF-8");
31 size_t in_size=isostring.size();
32 size_t out_size=in_size*4;
34 char *buf = (char *)malloc(out_size+1);
36 throw runtime_error("out of memory for iconv buffer");
38 const char *in = isostring.c_str();
40 iconv (i2utf8, &in, &in_size, &out, &out_size);
42 buf[isostring.size()*4-out_size]=0;
52 std::string utf8_to_iso(const std::string& utf8string)
56 iconv_t utf82iso = iconv_open ("ISO-8859-1","UTF-8");
58 if (utf82iso == (iconv_t)-1)
59 throw runtime_error("iconv can't convert from UTF-8 to ISO-8859-1");
61 size_t in_size=utf8string.size();
62 size_t out_size=in_size;
64 char *buf = (char *)malloc(out_size+1);
66 throw runtime_error("out of memory for iconv buffer");
68 const char *in = utf8string.c_str();
70 iconv (utf82iso, &in, &in_size, &out, &out_size);
72 buf[utf8string.size()-out_size]=0;
77 iconv_close (utf82iso);
82 std::string iso_to_html(const std::string& isostring, bool showerr_bug)
84 string result = isostring;
86 // TODO: This needs to be removed soon by a proper
87 // HTML quoted chars engine. Then we can also remove ü from i18n files.
89 replace_all (result, "\"", """);
90 replace_all (result, "&", "&");
91 replace_all (result, "<", "<");
92 replace_all (result, ">", ">");
95 replace_all (result, "ä", "ä");
96 replace_all (result, "ö", "ö");
97 replace_all (result, "ü", "ü");
98 replace_all (result, "Ä", "Ä");
99 replace_all (result, "Ö", "Ö");
100 replace_all (result, "Ü", "Ü");
101 replace_all (result, "ß", "ß");
106 bool replace_all(string &base, const char *ist, const char *soll)
110 return replace_all(base,&i,&s);
113 bool replace_all(string &base, const string &ist, const char *soll)
116 return replace_all(base,&ist,&s);
119 bool replace_all(string &base, const string *ist, const string *soll)
121 return replace_all(base,*ist,*soll);
124 bool replace_all(string &base, const char *ist, const string *soll)
127 return replace_all(base,&i,soll);
130 bool replace_all(string &base, const string &ist, const string &soll)
132 bool found_ist = false;
133 string::size_type a=0;
135 while((a=base.find(ist,a))!=string::npos)
137 base.replace(a,ist.size(),soll);
145 string to_lower(const string &src)
149 string::size_type pos = 0, end = dst.size();
150 for (pos = 0; pos < end; pos++)
151 dst[pos] = tolower(dst[pos]);
156 string to_upper(const string &src)
160 string::size_type pos = 0, end = dst.size();
161 for (pos = 0; pos < end; pos++)
162 dst[pos] = toupper(dst[pos]);
167 string nice_unit_format (int input) {
171 while (size > 1000) {
180 tmp = float(tmp)/float(10);
185 out.setf (ios::fixed);
189 out << size << i18n(" KBytes");
192 out << size << i18n(" MBytes");
195 out << size << i18n(" Gbytes");
198 out << size << i18n(" Bytes");
205 string escape(const string &s)
211 while ((p=out.find_first_of("\"\\",p))!=out.npos)
218 while ((p=out.find_first_of("\r",p))!=out.npos)
220 out.replace(p,1,"\\r");
225 while ((p=out.find_first_of("\n",p))!=out.npos)
227 out.replace(p,1,"\\n");
236 string descape(const string &s, int startpos, int &endpos)
240 if (s.at(startpos) != '"')
241 throw out_of_range("value not type escaped string");
243 out=s.substr(startpos+1);
244 string::size_type p=0;
246 // search for the end of the string
247 while((p=out.find("\"",p))!=out.npos)
252 // the " might be escaped with a backslash
253 while(e>=0 && out.at(e)=='\\')
255 if (escaped == false)
269 // we now have the end of the string
272 // tell calling prog about the endposition
275 // descape all \ stuff inside the string now
277 while((p=out.find_first_of("\\",p))!=out.npos)
282 out.replace(p,2,"\r");
285 out.replace(p,2,"\n");
296 string escape_shellarg(const string &input)
302 string::const_iterator it, it_end = input.end();
303 for (it = input.begin(); it != it_end; it++) {