libi2ncommon: (gerd) initial submission, not in use yet
[libi2ncommon] / src / stringfunc.cpp
1 /***************************************************************************
2                           escape.cpp  -  escaping of strings
3                              -------------------
4     begin                : Sun Nov 14 1999
5     copyright            : (C) 1999 by Intra2net AG
6     email                : info@intra2net.com
7  ***************************************************************************/
8
9 #include <iostream>
10 #include <string>
11 #include <sstream>
12 #include <stdexcept>
13
14 #include <stdlib.h>
15 #include <iconv.h>
16 #include <i18n.h>
17
18 #include <stringfunc.hxx>
19
20 using namespace std;
21
22 std::string iso_to_utf8(const std::string& isostring)
23 {
24     string result;
25     
26     iconv_t i2utf8 = iconv_open ("UTF-8", "ISO-8859-1");
27     
28     if (iso_to_utf8 == (iconv_t)-1)
29         throw runtime_error("iconv can't convert from ISO-8859-1 to UTF-8");
30         
31     size_t in_size=isostring.size();
32     size_t out_size=in_size*4;
33     
34     char *buf = (char *)malloc(out_size+1);
35     if (buf == NULL)
36         throw runtime_error("out of memory for iconv buffer");
37
38     const char *in = isostring.c_str();
39     char *out = buf;
40     iconv (i2utf8, &in, &in_size, &out, &out_size);
41             
42     buf[isostring.size()*4-out_size]=0;
43     
44     result=buf;
45                 
46     free(buf);
47     iconv_close (i2utf8);
48     
49     return result;
50 }
51
52 std::string utf8_to_iso(const std::string& utf8string)
53 {
54     string result;
55     
56     iconv_t utf82iso = iconv_open ("ISO-8859-1","UTF-8");
57     
58     if (utf82iso == (iconv_t)-1)
59         throw runtime_error("iconv can't convert from UTF-8 to ISO-8859-1");
60         
61     size_t in_size=utf8string.size();
62     size_t out_size=in_size;
63     
64     char *buf = (char *)malloc(out_size+1);
65     if (buf == NULL)
66         throw runtime_error("out of memory for iconv buffer");
67
68     const char *in = utf8string.c_str();
69     char *out = buf;
70     iconv (utf82iso, &in, &in_size, &out, &out_size);
71             
72     buf[utf8string.size()-out_size]=0;
73     
74     result=buf;
75                 
76     free(buf);
77     iconv_close (utf82iso);
78
79     return result;
80 }
81
82 std::string iso_to_html(const std::string& isostring)
83 {
84     string result = isostring;
85     
86     replace_all (result, "&", "&amp;");
87     replace_all (result, "\"", "&quot;");
88     replace_all (result, "ä", "&auml;");
89     replace_all (result, "ö", "&ouml;");
90     replace_all (result, "ü", "&uuml;");
91     replace_all (result, "Ä", "&Auml;");
92     replace_all (result, "Ö", "&Ouml;");
93     replace_all (result, "Ü", "&Uuml;");
94     replace_all (result, "ß", "&szlig;");
95     replace_all (result, "<", "&lt;");
96     replace_all (result, ">", "&gt;");
97     
98     return result;
99 }
100
101 bool replace_all(string &base, const char *ist, const char *soll)
102 {
103     string i=ist;
104     string s=soll;
105     return replace_all(base,&i,&s);
106 }
107
108 bool replace_all(string &base, const string &ist, const char *soll)
109 {
110     string s=soll;
111     return replace_all(base,&ist,&s);
112 }
113
114 bool replace_all(string &base, const string *ist, const string *soll)
115 {
116     return replace_all(base,*ist,*soll);
117 }
118
119 bool replace_all(string &base, const char *ist, const string *soll)
120 {
121     string i=ist;
122     return replace_all(base,&i,soll);
123 }
124
125 bool replace_all(string &base, const string &ist, const string &soll)
126 {
127     bool found_ist = false;
128     string::size_type a=0;
129
130     while((a=base.find(ist,a))!=string::npos)
131     {
132         base.replace(a,ist.size(),soll);
133         a=a+soll.size();
134         found_ist = true;
135     }
136     
137     return found_ist;
138 }
139
140 string to_lower(const string &src)
141 {
142     string dst = src;
143
144     string::size_type pos = 0, end = dst.size();
145     for (pos = 0; pos < end; pos++)
146         dst[pos] = tolower(dst[pos]);
147
148     return dst;
149 }
150
151 string to_upper(const string &src)
152 {
153     string dst = src;
154
155     string::size_type pos = 0, end = dst.size();
156     for (pos = 0; pos < end; pos++)
157         dst[pos] = toupper(dst[pos]);
158
159     return dst;
160 }
161
162 string nice_unit_format (int input) {
163     float size = input;
164     int sizecount = 0;
165
166     while (size > 1000) {
167         size = size / 1000;
168         sizecount++;
169     }
170
171     float tmp;                       // round
172     tmp = size*10;
173     tmp += 0.5;
174     tmp = int (tmp);
175     tmp = float(tmp)/float(10);
176     size = tmp;
177
178     ostringstream out;
179
180     out.setf (ios::fixed);
181     out.precision(2);
182     switch (sizecount) {
183     case 1:
184         out << size << i18n(" KBytes");
185         break;
186     case 2:
187         out << size << i18n(" MBytes");
188         break;
189     case 3:
190         out << size << i18n(" Gbytes");
191         break;
192     default:
193         out << size << i18n(" Bytes");
194         break;
195     }
196
197     return out.str();
198 }
199
200