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)
84 string result = isostring;
86 replace_all (result, "&", "&");
87 replace_all (result, "\"", """);
88 replace_all (result, "ä", "ä");
89 replace_all (result, "ö", "ö");
90 replace_all (result, "ü", "ü");
91 replace_all (result, "Ä", "Ä");
92 replace_all (result, "Ö", "Ö");
93 replace_all (result, "Ü", "Ü");
94 replace_all (result, "ß", "ß");
95 replace_all (result, "<", "<");
96 replace_all (result, ">", ">");
101 bool replace_all(string &base, const char *ist, const char *soll)
105 return replace_all(base,&i,&s);
108 bool replace_all(string &base, const string &ist, const char *soll)
111 return replace_all(base,&ist,&s);
114 bool replace_all(string &base, const string *ist, const string *soll)
116 return replace_all(base,*ist,*soll);
119 bool replace_all(string &base, const char *ist, const string *soll)
122 return replace_all(base,&i,soll);
125 bool replace_all(string &base, const string &ist, const string &soll)
127 bool found_ist = false;
128 string::size_type a=0;
130 while((a=base.find(ist,a))!=string::npos)
132 base.replace(a,ist.size(),soll);
140 string to_lower(const string &src)
144 string::size_type pos = 0, end = dst.size();
145 for (pos = 0; pos < end; pos++)
146 dst[pos] = tolower(dst[pos]);
151 string to_upper(const string &src)
155 string::size_type pos = 0, end = dst.size();
156 for (pos = 0; pos < end; pos++)
157 dst[pos] = toupper(dst[pos]);
162 string nice_unit_format (int input) {
166 while (size > 1000) {
175 tmp = float(tmp)/float(10);
180 out.setf (ios::fixed);
184 out << size << i18n(" KBytes");
187 out << size << i18n(" MBytes");
190 out << size << i18n(" Gbytes");
193 out << size << i18n(" Bytes");