author | zecke <zecke> | 2002-09-10 12:19:50 (UTC) |
---|---|---|
committer | zecke <zecke> | 2002-09-10 12:19:50 (UTC) |
commit | 1e5c6143299f900d08f7e972d32297cf1ad0e25b (patch) (side-by-side diff) | |
tree | 8a08802cbf3950c80d689b80eb3644ac99c8cce3 /library | |
parent | 6b77a1cdb9536b1c135eb86d53a6b2c22c19b0a4 (diff) | |
download | opie-1e5c6143299f900d08f7e972d32297cf1ad0e25b.zip opie-1e5c6143299f900d08f7e972d32297cf1ad0e25b.tar.gz opie-1e5c6143299f900d08f7e972d32297cf1ad0e25b.tar.bz2 |
cvs is too stupid to remove and add without commit in between
-rw-r--r-- | library/backend/stringutil.cpp | 414 | ||||
-rw-r--r-- | library/backend/timeconversion.cpp | 237 |
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 @@ +/********************************************************************** +** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** +** This file is part of the Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#include <qtopia/stringutil.h> +#include <qregexp.h> +#include <qstringlist.h> + +namespace Qtopia +{ + + + +/* + Very, very simple Latin-1 only collation guaranteed to displease anyone + who actually uses the non-ASCII characters. + */ + +static const char collationHack[] = { +0x00, //C-@ +0x01, //C-A +0x02, //C-B +0x03, //C-C +0x04, //C-D +0x05, //C-E +0x06, //C-F +0x07, //C-G +0x08, //C-H +0x09, //C-I +0x0a, //C-J +0x0b, //C-K +0x0c, //C-L +0x0d, //C-M +0x0e, //C-N +0x0f, //C-O +0x10, //C-P +0x11, //C-Q +0x12, //C-R +0x13, //C-S +0x14, //C-T +0x15, //C-U +0x16, //C-V +0x17, //C-W +0x18, //C-X +0x19, //C-Y +0x1a, //C-Z +0x1b, //C-[ +0x1c, //C-\ +0x1d, //C-] +0x1e, //C-^ +0x1f, //C-_ +' ', // +'!', //! +'"', //" +'#', //# +'$', //$ +'%', //% +'&', //& +'\'', //' +'(', //( +')', //) +'*', //* +'+', //+ +',', //, +'-', //- +'.', //. +'/', /// +0x80, //0 +0x81, //1 +0x82, //2 +0x83, //3 +0x84, //4 +0x85, //5 +0x86, //6 +0x87, //7 +0x88, //8 +0x89, //9 +':', //: +';', //; +'<', //< +'=', //= +'>', //> +'?', //? +'@', //@ +'A', //A +'B', //B +'C', //C +'D', //D +'E', //E +'F', //F +'G', //G +'H', //H +'I', //I +'J', //J +'K', //K +'L', //L +'M', //M +'N', //N +'O', //O +'P', //P +'Q', //Q +'R', //R +'S', //S +'T', //T +'U', //U +'V', //V +'W', //W +'X', //X +'Y', //Y +'Z', //Z +'[', //[ +'\\', //\ +']', //] +'^', //^ +'_', //_ +'`', //` +'A', //a +'B', //b +'C', //c +'D', //d +'E', //e +'F', //f +'G', //g +'H', //h +'I', //i +'J', //j +'K', //k +'L', //l +'M', //m +'N', //n +'O', //o +'P', //p +'Q', //q +'R', //r +'S', //s +'T', //t +'U', //u +'V', //v +'W', //w +'X', //x +'Y', //y +'Z', //z +'{', //{ +'|', //| +'}', //} +'~', //~ +'', // +0x80, //C-M-@ +0x81, //C-M-A +0x82, //C-M-B +0x83, //C-M-C +0x84, //C-M-D +0x85, //C-M-E +0x86, //C-M-F +0x87, //C-M-G +0x88, //C-M-H +0x89, //C-M-I +0x8a, //C-M-J +0x8b, //C-M-K +0x8c, //C-M-L +0x8d, //C-M-M +0x8e, //C-M-N +0x8f, //C-M-O +0x90, //C-M-P +0x91, //C-M-Q +0x92, //C-M-R +0x93, //C-M-S +0x94, //C-M-T +0x95, //C-M-U +0x96, //C-M-V +0x97, //C-M-W +0x98, //C-M-X +0x99, //C-M-Y +0x9a, //C-M-Z +0x9b, //C-M-[ +0x9c, //C-M-\ +0x9d, //C-M-] +0x9e, //C-M-^ +0x9f, //C-M-_ +' ', // +'¡', //¡ +'¢', //¢ +'£', //£ +'¤', //¤ +'¥', //¥ +'¦', //¦ +'§', //§ +'¨', //¨ +'©', //© +'A', //ª +'«', //« +'¬', //¬ +'', // +'®', //® +'¯', //¯ +'O', //° +'±', //± +'²', //² +'³', //³ +'´', //´ +'µ', //µ +'P', //¶ +'·', //· +'¸', //¸ +'¹', //¹ +'O', //º +'»', //» +'¼', //¼ +'½', //½ +'¾', //¾ +'¿', //¿ +'A', //À +'A', //Á +'A', //Â +'A', //Ã +'A', //Ä +'A', //Å +'A', //Æ +'C', //Ç +'E', //È +'E', //É +'E', //Ê +'E', //Ë +'I', //Ì +'I', //Í +'I', //Î +'I', //Ï +'D', //Ð +'N', //Ñ +'O', //Ò +'O', //Ó +'O', //Ô +'O', //Õ +'O', //Ö +'×', //× +'O', //Ø +'U', //Ù +'U', //Ú +'U', //Û +'U', //Ü +'Y', //Ý +'T', //Þ +'S', //ß +'A', //à +'A', //á +'A', //â +'A', //ã +'A', //ä +'A', //å +'A', //æ +'C', //ç +'E', //è +'E', //é +'E', //ê +'E', //ë +'I', //ì +'I', //í +'I', //î +'I', //ï +'D', //ð +'N', //ñ +'O', //ò +'O', //ó +'O', //ô +'O', //õ +'O', //ö +'÷', //÷ +'O', //ø +'U', //ù +'U', //ú +'U', //û +'U', //ü +'Y', //ý +'T', //þ +'Y', //ÿ +}; + + + + + +static void hackString ( QString &s ) +{ + int len = s.length(); + const QChar* uc = s.unicode(); + for ( int i = 0; i < len; i++ ) { + if ( !uc++->row() ) + s[i] = collationHack[s[i].cell()]; + } +} + +QString buildSortKey( const QString & s ) +{ + QString res = s; + hackString( res ); + return res; +} + +QString buildSortKey( const QString & s1, const QString & s2 ) +{ + QString res = s1 + QChar( '\0' ) + s2; + hackString( res ); + return res; +} + +QString buildSortKey( const QString & s1, const QString & s2, + const QString & s3 ) +{ + QString res = s1 + QChar( '\0' ) + s2 + QChar( '\0' ) + s3; + hackString( res ); + return res; +} + +static inline QChar coll( QChar u ) +{ + return u.row() ? u : QChar(collationHack[ u.cell() ]); +} + + +int compare( const QString & s1, const QString & s2 ) +{ + const QChar* u1 = s1.unicode(); + const QChar* u2 = s2.unicode(); + + if ( u1 == u2 ) + return 0; + if ( u1 == 0 ) + return 1; + if ( u2 == 0 ) + return -1; + int l=QMIN(s1.length(),s2.length()); + while ( l-- && coll(*u1) == coll(*u2) ) + u1++,u2++; + if ( l==-1 ) + return ( s1.length()-s2.length() ); + return u1->unicode() - u2->unicode(); +} + +QString simplifyMultiLineSpace( const QString &multiLine ) +{ + QString result; + QStringList lines = QStringList::split("\n", multiLine); + for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it ) { + if ( it != lines.begin() ) + result += "\n"; + result += (*it).simplifyWhiteSpace(); + } + return result; +} + +QString escapeString( const QString& plain ) +{ + QString tmp(plain); + int pos = tmp.length(); + const QChar *uc = plain.unicode(); + while ( pos-- ) { + unsigned char ch = uc[pos].latin1(); + if ( ch == '&' ) + tmp.replace( pos, 1, "&" ); + else if ( ch == '<' ) + tmp.replace( pos, 1, "<" ); + else if ( ch == '>' ) + tmp.replace( pos, 1, ">" ); + else if ( ch == '\"' ) + tmp.replace( pos, 1, """ ); + } + return tmp; +} + +QString plainString( const char* escaped, unsigned int length ) +{ + return plainString( QString::fromUtf8( escaped, length ) ); +} + +QString plainString( const QCString& string ) +{ + // We first have to pass it through a ::fromUtf8() + return plainString( string.data(), string.length() ); +} + +QString plainString( const QString& string ) +{ + QString tmp( string ); + int pos = -1; + while ( (pos = tmp.find( "&", pos +1 ) ) != -1 ) { + if ( tmp.find( "&", pos ) == pos ) + tmp.replace( pos, 5, "&" ); + else if ( tmp.find( "<", pos ) == pos ) + tmp.replace( pos, 4, "<" ); + else if( tmp.find( ">", pos ) == pos ) + tmp.replace( pos, 4, ">" ); + else if ( tmp.find( """, pos ) == pos ) + tmp.replace( pos, 6, "\"" ); + } + return tmp; +} + +} // 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 @@ +/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+#include <qglobal.h>
+#include <qtopia/timeconversion.h>
+#include <qregexp.h>
+#include <stdlib.h>
+
+QString TimeConversion::toString( const QDate &d )
+{
+ QString r = QString::number( d.day() ) + "." +
+ QString::number( d.month() ) + "." +
+ QString::number( d.year() );
+ //qDebug("TimeConversion::toString %s", r.latin1());
+ return r;
+}
+
+QDate TimeConversion::fromString( const QString &datestr )
+{
+ int monthPos = datestr.find('.');
+ int yearPos = datestr.find('.', monthPos+1 );
+ if ( monthPos == -1 || yearPos == -1 ) {
+ qDebug("fromString didn't find . in str = %s; mpos = %d ypos = %d", datestr.latin1(), monthPos, yearPos );
+ return QDate();
+ }
+ int d = datestr.left( monthPos ).toInt();
+ int m = datestr.mid( monthPos+1, yearPos - monthPos - 1 ).toInt();
+ int y = datestr.mid( yearPos+1 ).toInt();
+ QDate date ( y,m,d );
+ //qDebug("TimeConversion::fromString ymd = %s => %d %d %d; mpos = %d ypos = %d", datestr.latin1(), y, m, d, monthPos, yearPos);
+ return date;
+}
+
+time_t TimeConversion::toUTC( const QDateTime& dt )
+{
+ time_t tmp;
+ struct tm *lt;
+
+#if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
+ _tzset();
+#else
+ tzset();
+#endif
+
+ // get a tm structure from the system to get the correct tz_name
+ tmp = time( 0 );
+ lt = localtime( &tmp );
+
+ lt->tm_sec = dt.time().second();
+ lt->tm_min = dt.time().minute();
+ lt->tm_hour = dt.time().hour();
+ lt->tm_mday = dt.date().day();
+ lt->tm_mon = dt.date().month() - 1; // 0-11 instead of 1-12
+ lt->tm_year = dt.date().year() - 1900; // year - 1900
+ //lt->tm_wday = dt.date().dayOfWeek(); ignored anyway
+ //lt->tm_yday = dt.date().dayOfYear(); ignored anyway
+ lt->tm_wday = -1;
+ lt->tm_yday = -1;
+ // tm_isdst negative -> mktime will find out about DST
+ lt->tm_isdst = -1;
+ // keep tm_zone and tm_gmtoff
+ tmp = mktime( lt );
+ return tmp;
+}
+
+QDateTime TimeConversion::fromUTC( time_t time )
+{
+ struct tm *lt;
+
+#if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
+ _tzset();
+#else
+ tzset();
+#endif
+ lt = localtime( &time );
+ QDateTime dt;
+ dt.setDate( QDate( lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday ) );
+ dt.setTime( QTime( lt->tm_hour, lt->tm_min, lt->tm_sec ) );
+ return dt;
+}
+
+
+int TimeConversion::secsTo( const QDateTime &from, const QDateTime &to )
+{
+ return toUTC( to ) - toUTC( from );
+}
+
+QCString TimeConversion::toISO8601( const QDate &d )
+{
+ time_t tmp = toUTC( d );
+ struct tm *utc = gmtime( &tmp );
+
+ QCString str;
+ str.sprintf("%04d%02d%02d", (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday );
+ return str;
+}
+
+QCString TimeConversion::toISO8601( const QDateTime &dt )
+{
+ time_t tmp = toUTC( dt );
+ struct tm *utc = gmtime( &tmp );
+
+ QCString str;
+ str.sprintf("%04d%02d%02dT%02d%02d%02dZ",
+ (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday,
+ utc->tm_hour, utc->tm_min, utc->tm_sec );
+ return str;
+}
+
+QDateTime TimeConversion::fromISO8601( const QCString &s )
+{
+
+#if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
+ _tzset();
+#else
+ tzset();
+#endif
+
+ struct tm *thetime = new tm;
+
+ QCString str = s.copy();
+ str.replace(QRegExp("-"), "" );
+ str.replace(QRegExp(":"), "" );
+ str.stripWhiteSpace();
+ str = str.lower();
+
+ int i = str.find( "t" );
+ QCString date;
+ QCString timestr;
+ if ( i != -1 ) {
+ date = str.left( i );
+ timestr = str.mid( i+1 );
+ } else {
+ date = str;
+ }
+
+// qDebug("--- parsing ISO time---");
+ thetime->tm_year = 100;
+ thetime->tm_mon = 0;
+ thetime->tm_mday = 0;
+ thetime->tm_hour = 0;
+ thetime->tm_min = 0;
+ thetime->tm_sec = 0;
+
+// qDebug("date = %s", date.data() );
+
+ switch( date.length() ) {
+ case 8:
+ thetime->tm_mday = date.right( 2 ).toInt();
+ case 6:
+ thetime->tm_mon = date.mid( 4, 2 ).toInt() - 1;
+ case 4:
+ thetime->tm_year = date.left( 4 ).toInt();
+ thetime->tm_year -= 1900;
+ break;
+ default:
+ break;
+ }
+
+ int tzoff = 0;
+ bool inLocalTime = FALSE;
+ if ( timestr.find( 'z' ) == (int)timestr.length() - 1 )
+ // UTC
+ timestr = timestr.left( timestr.length() -1 );
+ else {
+ int plus = timestr.find( "+" );
+ int minus = timestr.find( "-" );
+ if ( plus != -1 || minus != -1 ) {
+ // have a timezone offset
+ plus = (plus != -1) ? plus : minus;
+ QCString off = timestr.mid( plus );
+ timestr = timestr.left( plus );
+
+ int tzoffhour = 0;
+ int tzoffmin = 0;
+ switch( off.length() ) {
+ case 5:
+ tzoffmin = off.mid(3).toInt();
+ case 3:
+ tzoffhour = off.left(3).toInt();
+ default:
+ break;
+ }
+ tzoff = 60*tzoffhour + tzoffmin;
+ } else
+ inLocalTime = TRUE;
+ }
+
+ // get the time:
+ switch( timestr.length() ) {
+ case 6:
+ thetime->tm_sec = timestr.mid( 4 ).toInt();
+ case 4:
+ thetime->tm_min = timestr.mid( 2, 2 ).toInt();
+ case 2:
+ thetime->tm_hour = timestr.left( 2 ).toInt();
+ default:
+ break;
+ }
+
+ int tzloc = 0;
+ time_t tmp = time( 0 );
+ if ( !inLocalTime ) {
+ // have to get the offset between gmt and local time
+ struct tm *lt = localtime( &tmp );
+ tzloc = mktime( lt );
+ struct tm *ut = gmtime( &tmp );
+ tzloc -= mktime( ut );
+ }
+// qDebug("time: %d %d %d, tzloc=%d, tzoff=%d", thetime->tm_hour, thetime->tm_min, thetime->tm_sec,
+// tzloc, tzoff );
+
+ tmp = mktime( thetime );
+ tmp += 60*(-tzloc + tzoff);
+
+ delete thetime;
+
+ return fromUTC( tmp );
+}
+
|