summaryrefslogtreecommitdiff
path: root/library
Unidiff
Diffstat (limited to 'library') (more/less context) (ignore whitespace changes)
-rw-r--r--library/backend/stringutil.cpp414
-rw-r--r--library/backend/timeconversion.cpp237
2 files changed, 651 insertions, 0 deletions
diff --git a/library/backend/stringutil.cpp b/library/backend/stringutil.cpp
new file mode 100644
index 0000000..b5fbd3e
--- a/dev/null
+++ b/library/backend/stringutil.cpp
@@ -0,0 +1,414 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include <qtopia/stringutil.h>
22#include <qregexp.h>
23#include <qstringlist.h>
24
25namespace Qtopia
26{
27
28
29
30/*
31 Very, very simple Latin-1 only collation guaranteed to displease anyone
32 who actually uses the non-ASCII characters.
33 */
34
35static const char collationHack[] = {
360x00, //C-@
370x01, //C-A
380x02, //C-B
390x03, //C-C
400x04, //C-D
410x05, //C-E
420x06, //C-F
430x07, //C-G
440x08, //C-H
450x09, //C-I
460x0a, //C-J
470x0b, //C-K
480x0c, //C-L
490x0d, //C-M
500x0e, //C-N
510x0f, //C-O
520x10, //C-P
530x11, //C-Q
540x12, //C-R
550x13, //C-S
560x14, //C-T
570x15, //C-U
580x16, //C-V
590x17, //C-W
600x18, //C-X
610x19, //C-Y
620x1a, //C-Z
630x1b, //C-[
640x1c, //C-\
650x1d, //C-]
660x1e, //C-^
670x1f, //C-_
68' ', //
69'!', //!
70'"', //"
71'#', //#
72'$', //$
73'%', //%
74'&', //&
75'\'', //'
76'(', //(
77')', //)
78'*', //*
79'+', //+
80',', //,
81'-', //-
82'.', //.
83'/', ///
840x80, //0
850x81, //1
860x82, //2
870x83, //3
880x84, //4
890x85, //5
900x86, //6
910x87, //7
920x88, //8
930x89, //9
94':', //:
95';', //;
96'<', //<
97'=', //=
98'>', //>
99'?', //?
100'@', //@
101'A', //A
102'B', //B
103'C', //C
104'D', //D
105'E', //E
106'F', //F
107'G', //G
108'H', //H
109'I', //I
110'J', //J
111'K', //K
112'L', //L
113'M', //M
114'N', //N
115'O', //O
116'P', //P
117'Q', //Q
118'R', //R
119'S', //S
120'T', //T
121'U', //U
122'V', //V
123'W', //W
124'X', //X
125'Y', //Y
126'Z', //Z
127'[', //[
128'\\', //\
129']', //]
130'^', //^
131'_', //_
132'`', //`
133'A', //a
134'B', //b
135'C', //c
136'D', //d
137'E', //e
138'F', //f
139'G', //g
140'H', //h
141'I', //i
142'J', //j
143'K', //k
144'L', //l
145'M', //m
146'N', //n
147'O', //o
148'P', //p
149'Q', //q
150'R', //r
151'S', //s
152'T', //t
153'U', //u
154'V', //v
155'W', //w
156'X', //x
157'Y', //y
158'Z', //z
159'{', //{
160'|', //|
161'}', //}
162'~', //~
163'', //
1640x80, //C-M-@
1650x81, //C-M-A
1660x82, //C-M-B
1670x83, //C-M-C
1680x84, //C-M-D
1690x85, //C-M-E
1700x86, //C-M-F
1710x87, //C-M-G
1720x88, //C-M-H
1730x89, //C-M-I
1740x8a, //C-M-J
1750x8b, //C-M-K
1760x8c, //C-M-L
1770x8d, //C-M-M
1780x8e, //C-M-N
1790x8f, //C-M-O
1800x90, //C-M-P
1810x91, //C-M-Q
1820x92, //C-M-R
1830x93, //C-M-S
1840x94, //C-M-T
1850x95, //C-M-U
1860x96, //C-M-V
1870x97, //C-M-W
1880x98, //C-M-X
1890x99, //C-M-Y
1900x9a, //C-M-Z
1910x9b, //C-M-[
1920x9c, //C-M-\
1930x9d, //C-M-]
1940x9e, //C-M-^
1950x9f, //C-M-_
196' ', // 
197'¡', //¡
198'¢', //¢
199'£', //£
200'¤', //¤
201'¥', //¥
202'¦', //¦
203'§', //§
204'¨', //¨
205'©', //©
206'A', //ª
207'«', //«
208'¬', //¬
209'­', //­
210'®', //®
211'¯', //¯
212'O', //°
213'±', //±
214'²', //²
215'³', //³
216'´', //´
217'µ', //µ
218'P', //¶
219'·', //·
220'¸', //¸
221'¹', //¹
222'O', //º
223'»', //»
224'¼', //¼
225'½', //½
226'¾', //¾
227'¿', //¿
228'A', //À
229'A', //Á
230'A', //Â
231'A', //Ã
232'A', //Ä
233'A', //Å
234'A', //Æ
235'C', //Ç
236'E', //È
237'E', //É
238'E', //Ê
239'E', //Ë
240'I', //Ì
241'I', //Í
242'I', //Î
243'I', //Ï
244'D', //Ð
245'N', //Ñ
246'O', //Ò
247'O', //Ó
248'O', //Ô
249'O', //Õ
250'O', //Ö
251'×', //×
252'O', //Ø
253'U', //Ù
254'U', //Ú
255'U', //Û
256'U', //Ü
257'Y', //Ý
258'T', //Þ
259'S', //ß
260'A', //à
261'A', //á
262'A', //â
263'A', //ã
264'A', //ä
265'A', //å
266'A', //æ
267'C', //ç
268'E', //è
269'E', //é
270'E', //ê
271'E', //ë
272'I', //ì
273'I', //í
274'I', //î
275'I', //ï
276'D', //ð
277'N', //ñ
278'O', //ò
279'O', //ó
280'O', //ô
281'O', //õ
282'O', //ö
283'÷', //÷
284'O', //ø
285'U', //ù
286'U', //ú
287'U', //û
288'U', //ü
289'Y', //ý
290'T', //þ
291'Y', //ÿ
292};
293
294
295
296
297
298static void hackString ( QString &s )
299{
300 int len = s.length();
301 const QChar* uc = s.unicode();
302 for ( int i = 0; i < len; i++ ) {
303 if ( !uc++->row() )
304 s[i] = collationHack[s[i].cell()];
305 }
306}
307
308QString buildSortKey( const QString & s )
309{
310 QString res = s;
311 hackString( res );
312 return res;
313}
314
315QString buildSortKey( const QString & s1, const QString & s2 )
316{
317 QString res = s1 + QChar( '\0' ) + s2;
318 hackString( res );
319 return res;
320}
321
322QString buildSortKey( const QString & s1, const QString & s2,
323 const QString & s3 )
324{
325 QString res = s1 + QChar( '\0' ) + s2 + QChar( '\0' ) + s3;
326 hackString( res );
327 return res;
328}
329
330static inline QChar coll( QChar u )
331{
332 return u.row() ? u : QChar(collationHack[ u.cell() ]);
333}
334
335
336int compare( const QString & s1, const QString & s2 )
337{
338 const QChar* u1 = s1.unicode();
339 const QChar* u2 = s2.unicode();
340
341 if ( u1 == u2 )
342 return 0;
343 if ( u1 == 0 )
344 return 1;
345 if ( u2 == 0 )
346 return -1;
347 int l=QMIN(s1.length(),s2.length());
348 while ( l-- && coll(*u1) == coll(*u2) )
349 u1++,u2++;
350 if ( l==-1 )
351 return ( s1.length()-s2.length() );
352 return u1->unicode() - u2->unicode();
353}
354
355QString simplifyMultiLineSpace( const QString &multiLine )
356{
357 QString result;
358 QStringList lines = QStringList::split("\n", multiLine);
359 for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it ) {
360 if ( it != lines.begin() )
361 result += "\n";
362 result += (*it).simplifyWhiteSpace();
363 }
364 return result;
365}
366
367QString escapeString( const QString& plain )
368{
369 QString tmp(plain);
370 int pos = tmp.length();
371 const QChar *uc = plain.unicode();
372 while ( pos-- ) {
373 unsigned char ch = uc[pos].latin1();
374 if ( ch == '&' )
375 tmp.replace( pos, 1, "&amp;" );
376 else if ( ch == '<' )
377 tmp.replace( pos, 1, "&lt;" );
378 else if ( ch == '>' )
379 tmp.replace( pos, 1, "&gt;" );
380 else if ( ch == '\"' )
381 tmp.replace( pos, 1, "&quot;" );
382 }
383 return tmp;
384}
385
386QString plainString( const char* escaped, unsigned int length )
387{
388 return plainString( QString::fromUtf8( escaped, length ) );
389}
390
391QString plainString( const QCString& string )
392{
393 // We first have to pass it through a ::fromUtf8()
394 return plainString( string.data(), string.length() );
395}
396
397QString plainString( const QString& string )
398{
399 QString tmp( string );
400 int pos = -1;
401 while ( (pos = tmp.find( "&", pos +1 ) ) != -1 ) {
402 if ( tmp.find( "&amp;", pos ) == pos )
403 tmp.replace( pos, 5, "&" );
404 else if ( tmp.find( "&lt;", pos ) == pos )
405 tmp.replace( pos, 4, "<" );
406 else if( tmp.find( "&gt;", pos ) == pos )
407 tmp.replace( pos, 4, ">" );
408 else if ( tmp.find( "&quot;", pos ) == pos )
409 tmp.replace( pos, 6, "\"" );
410 }
411 return tmp;
412}
413
414} // namespace QPC
diff --git a/library/backend/timeconversion.cpp b/library/backend/timeconversion.cpp
new file mode 100644
index 0000000..ef7762d
--- a/dev/null
+++ b/library/backend/timeconversion.cpp
@@ -0,0 +1,237 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include <qglobal.h>
22#include <qtopia/timeconversion.h>
23#include <qregexp.h>
24#include <stdlib.h>
25
26QString TimeConversion::toString( const QDate &d )
27{
28 QString r = QString::number( d.day() ) + "." +
29 QString::number( d.month() ) + "." +
30 QString::number( d.year() );
31 //qDebug("TimeConversion::toString %s", r.latin1());
32 return r;
33}
34
35QDate TimeConversion::fromString( const QString &datestr )
36{
37 int monthPos = datestr.find('.');
38 int yearPos = datestr.find('.', monthPos+1 );
39 if ( monthPos == -1 || yearPos == -1 ) {
40 qDebug("fromString didn't find . in str = %s; mpos = %d ypos = %d", datestr.latin1(), monthPos, yearPos );
41 return QDate();
42 }
43 int d = datestr.left( monthPos ).toInt();
44 int m = datestr.mid( monthPos+1, yearPos - monthPos - 1 ).toInt();
45 int y = datestr.mid( yearPos+1 ).toInt();
46 QDate date ( y,m,d );
47 //qDebug("TimeConversion::fromString ymd = %s => %d %d %d; mpos = %d ypos = %d", datestr.latin1(), y, m, d, monthPos, yearPos);
48 return date;
49}
50
51time_t TimeConversion::toUTC( const QDateTime& dt )
52{
53 time_t tmp;
54 struct tm *lt;
55
56#if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
57 _tzset();
58#else
59 tzset();
60#endif
61
62 // get a tm structure from the system to get the correct tz_name
63 tmp = time( 0 );
64 lt = localtime( &tmp );
65
66 lt->tm_sec = dt.time().second();
67 lt->tm_min = dt.time().minute();
68 lt->tm_hour = dt.time().hour();
69 lt->tm_mday = dt.date().day();
70 lt->tm_mon = dt.date().month() - 1; // 0-11 instead of 1-12
71 lt->tm_year = dt.date().year() - 1900; // year - 1900
72 //lt->tm_wday = dt.date().dayOfWeek(); ignored anyway
73 //lt->tm_yday = dt.date().dayOfYear(); ignored anyway
74 lt->tm_wday = -1;
75 lt->tm_yday = -1;
76 // tm_isdst negative -> mktime will find out about DST
77 lt->tm_isdst = -1;
78 // keep tm_zone and tm_gmtoff
79 tmp = mktime( lt );
80 return tmp;
81}
82
83QDateTime TimeConversion::fromUTC( time_t time )
84{
85 struct tm *lt;
86
87#if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
88 _tzset();
89#else
90 tzset();
91#endif
92 lt = localtime( &time );
93 QDateTime dt;
94 dt.setDate( QDate( lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday ) );
95 dt.setTime( QTime( lt->tm_hour, lt->tm_min, lt->tm_sec ) );
96 return dt;
97}
98
99
100int TimeConversion::secsTo( const QDateTime &from, const QDateTime &to )
101{
102 return toUTC( to ) - toUTC( from );
103}
104
105QCString TimeConversion::toISO8601( const QDate &d )
106{
107 time_t tmp = toUTC( d );
108 struct tm *utc = gmtime( &tmp );
109
110 QCString str;
111 str.sprintf("%04d%02d%02d", (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday );
112 return str;
113}
114
115QCString TimeConversion::toISO8601( const QDateTime &dt )
116{
117 time_t tmp = toUTC( dt );
118 struct tm *utc = gmtime( &tmp );
119
120 QCString str;
121 str.sprintf("%04d%02d%02dT%02d%02d%02dZ",
122 (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday,
123 utc->tm_hour, utc->tm_min, utc->tm_sec );
124 return str;
125}
126
127QDateTime TimeConversion::fromISO8601( const QCString &s )
128{
129
130#if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
131 _tzset();
132#else
133 tzset();
134#endif
135
136 struct tm *thetime = new tm;
137
138 QCString str = s.copy();
139 str.replace(QRegExp("-"), "" );
140 str.replace(QRegExp(":"), "" );
141 str.stripWhiteSpace();
142 str = str.lower();
143
144 int i = str.find( "t" );
145 QCString date;
146 QCString timestr;
147 if ( i != -1 ) {
148 date = str.left( i );
149 timestr = str.mid( i+1 );
150 } else {
151 date = str;
152 }
153
154// qDebug("--- parsing ISO time---");
155 thetime->tm_year = 100;
156 thetime->tm_mon = 0;
157 thetime->tm_mday = 0;
158 thetime->tm_hour = 0;
159 thetime->tm_min = 0;
160 thetime->tm_sec = 0;
161
162// qDebug("date = %s", date.data() );
163
164 switch( date.length() ) {
165 case 8:
166 thetime->tm_mday = date.right( 2 ).toInt();
167 case 6:
168 thetime->tm_mon = date.mid( 4, 2 ).toInt() - 1;
169 case 4:
170 thetime->tm_year = date.left( 4 ).toInt();
171 thetime->tm_year -= 1900;
172 break;
173 default:
174 break;
175 }
176
177 int tzoff = 0;
178 bool inLocalTime = FALSE;
179 if ( timestr.find( 'z' ) == (int)timestr.length() - 1 )
180 // UTC
181 timestr = timestr.left( timestr.length() -1 );
182 else {
183 int plus = timestr.find( "+" );
184 int minus = timestr.find( "-" );
185 if ( plus != -1 || minus != -1 ) {
186 // have a timezone offset
187 plus = (plus != -1) ? plus : minus;
188 QCString off = timestr.mid( plus );
189 timestr = timestr.left( plus );
190
191 int tzoffhour = 0;
192 int tzoffmin = 0;
193 switch( off.length() ) {
194 case 5:
195 tzoffmin = off.mid(3).toInt();
196 case 3:
197 tzoffhour = off.left(3).toInt();
198 default:
199 break;
200 }
201 tzoff = 60*tzoffhour + tzoffmin;
202 } else
203 inLocalTime = TRUE;
204 }
205
206 // get the time:
207 switch( timestr.length() ) {
208 case 6:
209 thetime->tm_sec = timestr.mid( 4 ).toInt();
210 case 4:
211 thetime->tm_min = timestr.mid( 2, 2 ).toInt();
212 case 2:
213 thetime->tm_hour = timestr.left( 2 ).toInt();
214 default:
215 break;
216 }
217
218 int tzloc = 0;
219 time_t tmp = time( 0 );
220 if ( !inLocalTime ) {
221 // have to get the offset between gmt and local time
222 struct tm *lt = localtime( &tmp );
223 tzloc = mktime( lt );
224 struct tm *ut = gmtime( &tmp );
225 tzloc -= mktime( ut );
226 }
227// qDebug("time: %d %d %d, tzloc=%d, tzoff=%d", thetime->tm_hour, thetime->tm_min, thetime->tm_sec,
228 // tzloc, tzoff );
229
230 tmp = mktime( thetime );
231 tmp += 60*(-tzloc + tzoff);
232
233 delete thetime;
234
235 return fromUTC( tmp );
236}
237