konsolekalendar Library API Documentation

kalendarVariables.cpp

00001 /***************************************************************************
00002         kalendarVariables.cpp  -  description
00003            -------------------
00004     begin                : Sun Jan 6 2002
00005     copyright            : (C) 2002 by Tuukka Pasanen
00006     email                : illuusio@mailcity.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <qdatetime.h>
00019 #include <qstring.h>
00020 #include "kalendarVariables.h"
00021 
00022 #include <stdlib.h>
00023 #include <iostream>
00024 #include <stdio.h>
00025 
00026 KalendarVariables::KalendarVariables()
00027 {
00028   m_bIsDate = false;
00029   m_bIsStartDate = false;
00030   m_bIsEndDate = false;
00031   m_bNext = false;
00032   m_bVerbose = false;
00033 }
00034 
00035 KalendarVariables::~KalendarVariables()
00036 {
00037 }
00038 
00039 void KalendarVariables::setDate(QDate date)
00040 {
00041   m_bIsDate = true;
00042   m_date = date;
00043 }
00044   
00045 QDate KalendarVariables::getDate()
00046 {
00047   return m_date;
00048 }
00049   
00050 bool KalendarVariables::isDate()
00051 {
00052   return m_bIsDate;
00053 }
00054 
00055 void KalendarVariables::setStartDate(QDate start)
00056 {
00057   m_bIsStartDate = true;
00058   m_startDate = start;
00059 }
00060   
00061 QDate KalendarVariables::getStartDate()
00062 {
00063   return m_startDate;
00064 }
00065   
00066 bool KalendarVariables::isStartDate()
00067 {
00068   return m_bIsStartDate;
00069 }
00070 
00071 void KalendarVariables::setEndDate(QDate end)
00072 {
00073   m_bIsEndDate = true;
00074   m_endDate = end;
00075 }
00076 
00077 QDate KalendarVariables::getEndDate()
00078 {
00079   return m_endDate;
00080 }
00081 
00082 bool KalendarVariables::isEndDate()
00083 {
00084   return m_bIsEndDate;
00085 }
00086 
00087 void KalendarVariables::setNext(bool next)
00088 {
00089   m_bNext = next;
00090 }
00091 
00092 bool KalendarVariables::isNext()
00093 {
00094   return m_bNext;
00095 }
00096 
00097 void KalendarVariables::setVerbose(bool verbose)
00098 {
00099   m_bVerbose = verbose;
00100 }
00101 
00102 bool KalendarVariables::isVerbose()
00103 {
00104   return m_bVerbose;
00105 }
00106 
00107 void KalendarVariables::setCalendarFile(QString calendar)
00108 {
00109   m_calendar = calendar;
00110 }
00111 
00112 QString KalendarVariables::getCalendarFile()
00113 {
00114   return m_calendar;
00115 }
00116 
00117 bool KalendarVariables::isAll()
00118 {
00119   return m_bAll;
00120 }
00121      
00122 void KalendarVariables::setAll( bool all)
00123 {
00124   m_bAll = all;
00125 }
00126          
00127 bool KalendarVariables::getAll()
00128 {
00129   return m_bAll;
00130 }
00131 
00132 
00133 QDate KalendarVariables::parseDate(QString str)
00134 {  
00135   int strpos=0,   // actual position in string
00136   errpos=-1;    // position of first error in string, or -1 for "no error"
00137 
00138   bool lookNumber, done;
00139 
00140   int numbers[3]; // the three numbers making up a date. Order depends on inputmode
00141   int numstart;
00142   char separator = '\0';
00143   int actnum=0;   // the index of the next number
00144   int cursorpos = 0;
00145 
00146   int actsep=0,     // index of the next separator
00147   tottok=0,     // how many items/tokens have been parsed?
00148   seppos;       // position of last separator parsed
00149 
00150   int format = -1;
00151 
00152   /* For not having to call QString::length() frequently without knowing whether
00153    * that function starts to count it chars every time, we save that value
00154    */
00155 
00156   str_length=str.length();
00157 
00158   numbers[0]=numbers[1]=numbers[2]=-1;
00159 
00160   lookNumber=true;
00161   done=false;
00162 
00163   /* We parse the string until
00164    * - there is an error ( errpos!=-1 ), or
00165    * - we reach the end of the string ( strpos>=str.length() ), or
00166    * - we found everything that makes up a date
00167    */
00168 
00169   while( (errpos==-1) && (strpos < str_length) && (!done)) {
00170     if( lookNumber ) {
00171       // We are currently looking for a number
00172       if(( numbers[actnum]=findNumber(str,strpos,numstart) )==-1) {
00173         // but be reached the end of the string
00174         done=true;
00175       } else {
00176         /* if num==-2, this means that there was anything else.
00177          * this could mean
00178          * that the user deleted the number in-between some separators
00179          * and is just about to enter a new number
00180          */
00181         if(numbers[actnum]==-2) {
00182           numbers[actnum]=-1;
00183         }
00184 
00185         // since we found a number, we increase the counters
00186         actnum++;
00187         tottok++;
00188 
00189         /* if we found a total of three numbers, we're done.
00190          * if not, there should come a separator
00191          */
00192         if(actnum==3)
00193           done=true;
00194         else
00195           lookNumber=false;
00196       }
00197     } else {
00198       // We are currently looking for a separator
00199       switch(actsep) {
00200         case 0:
00201           // It's the first sep, so look what the user preferres
00202           separator = findSeparator(str,strpos,seppos);
00203           switch(separator) {
00204             case '.':
00205               // german format 'dd.mm.yyyy'
00206               format = 1;
00207               break;
00208             case '-':
00209               format = 2;
00210               break;
00211             case '/':
00212               // normal format 'mm/dd/yyyy' or 'mm-dd-yyyy'
00213               format = 3;
00214               break;
00215             default:
00216               // anything else we did not expect
00217               errpos=seppos;
00218           }
00219           break;
00220 
00221         case 1:
00222           // The second sep must be the same as the first (Not 1-1/2000)
00223           if(separator!=findSeparator(str,strpos,seppos))
00224             errpos=seppos;
00225           break;
00226 
00227       }
00228 
00229       // Increase all the counters
00230       actsep++;
00231       tottok++;
00232 
00233       lookNumber=true;
00234     };
00235   }
00236 
00237 
00238   /* We're through parsing.
00239    *
00240    * If there was no error, this could mean that
00241    * 1) the string ended before we found a complete date
00242    * 2) We found a complete date
00243    *
00244    * In the second case, there could be non-whitespace garbage at the end of the
00245    * string, which leads to an error.
00246    *
00247    * The test does nothing in the first case, since the string is already used up.
00248    */
00249   if(errpos==-1) {
00250     if(strpos<str_length) {
00251       skipWhiteSpace(str,strpos);
00252     
00253       if(strpos<str_length) {
00254         // There is garbage. ERROR!!!
00255         errpos=strpos;
00256       }
00257     }
00258   }
00259 
00260   // If there was an error, we can't do anymore.
00261   if(errpos!=-1) {
00262     cursorpos=errpos;
00263     return QDate::currentDate();
00264   };
00265 
00266   /* Now, we have anything the user gave us.
00267    *
00268    * So we can now check whether
00269    * - the user gave us enough
00270    * - he entered a real date
00271    * - he used a two-digit year which is not nice
00272    */
00273 
00274 
00275   // First, we sort the three numbers into day, month and year
00276   switch(format) {
00277     case 1:
00278       //ddescr.day=numbers[0];
00279       //ddescr.month=numbers[1];
00280       //ddescr.year=numbers[2];
00281 
00282       return QDate(numbers[2], numbers[1], numbers[0]);
00283 
00284     case 2:
00285     case 3:
00286        //ddescr.day=numbers[1];
00287        //ddescr.month=numbers[0];
00288        //ddescr.year=numbers[2];
00289        return QDate(numbers[2], numbers[1], numbers[0]);
00290 
00291     default:
00292       break;
00293   };
00294   return QDate::currentDate();
00295 }
00296 
00297 // res: Number, -1 reached end, -2 garbage
00298 int KalendarVariables::findNumber(const QString &str, int &pos, int &startpos)
00299 {
00300   skipWhiteSpace(str,pos);
00301 
00302   if(pos >= str_length)
00303     return -1;
00304 
00305   startpos=pos;
00306 
00307   while( (pos<str_length) && (str[pos]>='0') && (str[pos]<='9') )
00308     pos++;
00309 
00310   if(startpos==pos)
00311     return -2;
00312 
00313   return str.mid(startpos,pos-startpos).toInt();
00314 }
00315 
00316 
00317 // res: char, 0 reached end
00318 char KalendarVariables::findSeparator(const QString &str, int &pos, int &seppos)
00319 {
00320   skipWhiteSpace(str,pos);
00321 
00322   if(pos>=str_length) {
00323     seppos=-1;
00324     return 0;
00325   };
00326 
00327   seppos=pos;
00328   pos++;
00329   return str[seppos];
00330 }
00331 
00332 #define ISWHITESPACE(c) ( ((c)==' ') || ((c)=='\t') || ((c)=='\n') || ((c)=='\r') )
00333 void KalendarVariables::skipWhiteSpace(const QString &str, int &pos)
00334 {
00335   while( (pos<str_length) && ISWHITESPACE(str[pos]) )
00336     pos++;
00337 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:52 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001