1 //
2 // C++ Implementation: UtilFunctions
3 //
4 // Description: This file contain the implementation of some usefull conversion
5 //              functions.
6 //
7 //
8 // Author: Max Magalh�s Velasques <maxvelasques@gmail.com>, (C) 2006
9 //
10 // Copyright: See COPYING file that comes with this distribution
11 //
12 //
13 
14 #include "wxDFast.h"
15 
int2wxstr(long value,int format)16 wxString MyUtilFunctions::int2wxstr(long value,int format)
17 {
18     wxString temp,string;
19     string << wxT("%0") << format << wxT("ld");
20     temp = temp.Format(string,value);
21     return temp;
22 }
23 
TimeString(long value)24 wxString MyUtilFunctions::TimeString(long value)
25 {
26     wxString tmp;
27     int hour,min,sec;
28     long time;
29     time = (long) (value / 1000);
30     hour = (int) (time/3600);
31     time = time - (hour * 3600);
32     min = (int) (time/60);
33     sec = (time%60);
34     if (hour == 0)
35        tmp.Printf(wxT("%01dm %01ds"), min, sec);
36     else
37        tmp.Printf(wxT("%01dh %01dm %01ds"),hour, min, sec);
38     return(tmp);
39 }
40 
TimeString(wxLongLong value)41 wxString MyUtilFunctions::TimeString(wxLongLong value)
42 {
43     wxString tmp;
44     int hour,min,sec;
45     long time;
46     time = (value / 1000).ToLong();
47     hour = (int) (time/3600);
48     time = time - (hour * 3600);
49     min = (int) (time/60);
50     sec = (time%60);
51     if (hour == 0)
52        tmp.Printf(wxT("%01dm %01ds"), min, sec);
53     else
54        tmp.Printf(wxT("%01dh %01dm %01ds"),hour, min, sec);
55     return(tmp);
56 }
57 
GetLine(wxString text,int line)58 wxString MyUtilFunctions::GetLine(wxString text, int line)
59 {
60     wxString str = text;
61     int pos = str.Find(wxT("\n"));
62     int i;
63     if (pos == 0)
64     {
65         str=str.Mid(1);
66         pos = str.Find(wxT("\n"));
67     }
68     for (i=1; i<line;i++)
69     {
70         str = str.Mid(pos+1);
71         pos = str.Find(wxT("\n"));
72     }
73     return str.Mid(0,pos-1);
74 }
75 
str2wxstr(char * str)76 wxString MyUtilFunctions::str2wxstr(char *str)
77 {
78     wxString retorno;
79     unsigned int i;
80     for (i = 0 ; i < strlen(str); i++)
81         retorno.Append(str[i],1);
82     return retorno;
83 }
84 
str2wxstr(char str)85 wxString MyUtilFunctions::str2wxstr(char str)
86 {
87     wxString retorno;
88     retorno.Append(str,1);
89     return retorno;
90 }
91 
ByteString(long size)92 wxString MyUtilFunctions::ByteString(long size)
93 {
94     wxString str;
95     if ( size < 1024)
96 //        str.Printf(wxT("%0.1f Bytes"),(double) size);
97         str.Printf(wxT("0.0 kB"));
98     if (( size >= 1024) && (size < 1048576))
99         str.Printf(wxT("%0.1f kB"),((double) size) /((double)1024));
100     if ( size >= 1048576)
101         str.Printf(wxT("%0.1f mB"),((double) size)/((double)1048576));
102     return(str);
103 }
104 
ByteString(wxLongLong size)105 wxString MyUtilFunctions::ByteString(wxLongLong size)
106 {
107     wxString str;
108     if ( size < 1024)
109 //        str.Printf(wxT("%0.1f Bytes"),(double) size);
110         str.Printf(wxT("0.0 kB"));
111     if (( size >= 1024) && (size < 1048576))
112         str.Printf(wxT("%0.1f kB"),( wxlonglongtodouble(size)) /((double)1024));
113     if ( size >= 1048576)
114         str.Printf(wxT("%0.1f MB"),( wxlonglongtodouble(size))/((double)1048576));
115     return(str);
116 }
117 
wxstrtolonglong(wxString string)118 wxLongLong MyUtilFunctions::wxstrtolonglong(wxString string)
119 {
120     wxString tmp = string.Trim().Trim(FALSE);
121     char carac;
122     wxLongLong result = 0;
123     int sign=1;
124     if (string.Length() > 0)
125     {
126         if (tmp.GetChar(0) == '-')
127             sign = -1;
128         for (unsigned int i = 0; i < tmp.Length(); i++)
129         {
130             carac = tmp.GetChar(i);
131             if ((carac >= '0') && (carac <= '9'))
132                 result = result * 10LL + carac-'0';
133             else
134                 continue;
135         }
136     }
137     return result*sign;
138 }
139 
wxlonglongtodouble(wxLongLong value)140 double MyUtilFunctions::wxlonglongtodouble(wxLongLong value)
141 {
142     double d = value.GetHi();
143     d *= 1.0 + (double)ULONG_MAX;
144     d += value.GetLo();
145     return d;
146 }
147 
GenerateAuthString(wxString user,wxString pass)148 wxString MyUtilFunctions::GenerateAuthString(wxString user, wxString pass)
149 {
150     static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
151 
152     wxString buf;
153     wxString toencode;
154 
155     buf.Printf(wxT("Basic "));
156 
157     toencode.Printf(wxT("%s:%s"),user.c_str(),pass.c_str());
158 
159     size_t len = toencode.length();
160     const wxChar *from = toencode.c_str();
161     while (len >= 3) { // encode full blocks first
162         buf << wxString::Format(wxT("%c%c"), base64[(from[0] >> 2) & 0x3f], base64[((from[0] << 4) & 0x30) | ((from[1] >> 4) & 0xf)]);
163         buf << wxString::Format(wxT("%c%c"), base64[((from[1] << 2) & 0x3c) | ((from[2] >> 6) & 0x3)], base64[from[2] & 0x3f]);
164         from += 3;
165         len -= 3;
166     }
167     if (len > 0) { // pad the remaining characters
168         buf << wxString::Format(wxT("%c"), base64[(from[0] >> 2) & 0x3f]);
169         if (len == 1) {
170             buf << wxString::Format(wxT("%c="), base64[(from[0] << 4) & 0x30]);
171         } else {
172             buf << wxString::Format(wxT("%c%c"), base64[(from[0] << 4) & 0x30] + ((from[1] >> 4) & 0xf), base64[(from[1] << 2) & 0x3c]);
173         }
174         buf << wxString::Format(wxT("="));
175     }
176 
177     return buf;
178 }
179 
180 #ifdef __WXMSW__
GetProgramFilesDir()181 wxString MyUtilFunctions::GetProgramFilesDir()
182 {
183     wxString result = wxEmptyString;
184     wxRegKey regKey;
185     wxString idName(wxT("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion"));
186 
187     regKey.SetName(idName);
188     regKey.QueryValue(wxT("ProgramFilesDir"),result);
189     regKey.Close();
190 
191     return result;
192 }
193 
GetMyDocumentsDir()194 wxString MyUtilFunctions::GetMyDocumentsDir()
195 {
196     wxString result = wxEmptyString;
197     wxRegKey regKey;
198     wxString idName(wxT("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"));
199 
200     regKey.SetName(idName);
201     regKey.QueryValue(wxT("Personal"),result);
202     regKey.Close();
203 
204     return result;
205 }
206 
GetDefaultBrowser()207 wxString MyUtilFunctions::GetDefaultBrowser()
208 {
209     wxString result = wxEmptyString;
210     wxRegKey regKey;
211     wxString idName(wxT("HKEY_CLASSES_ROOT\\http\\shell\\open\\command"));
212 
213     regKey.SetName(idName);
214     regKey.QueryValue(wxEmptyString,result);
215     regKey.Close();
216     if (!result.IsEmpty())
217     {
218         result.Replace(wxT("\"%1\""),wxEmptyString);
219         result.Replace(wxT("%1"),wxEmptyString);
220     }
221 
222     return result;
223 }
224 
225 #endif
226