Back to index

fet  5.18.0
timetable_defs.cpp
Go to the documentation of this file.
00001 /*
00002 File timetable_defs.cpp
00003 */
00004 
00005 /***************************************************************************
00006                           timetable_defs.cpp  -  description
00007                              -------------------
00008     begin                : Sat Mar 15 2003
00009     copyright            : (C) 2003 by Lalescu Liviu
00010     email                : Please see http://lalescu.ro/liviu/ for details about contacting Liviu Lalescu (in particular, you can find here the e-mail address)
00011  ***************************************************************************/
00012 
00013 /***************************************************************************
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  ***************************************************************************/
00021 
00022 #include "timetable_defs.h"
00023 
00024 #include <ctime>
00025 
00026 #include <QHash>
00027 
00028 #include <QLocale>
00029 
00030 bool checkForUpdates;
00031 
00032 QString internetVersion;
00033 
00037 const QString FET_VERSION="5.18.0";
00038 
00042 QString FET_LANGUAGE="en_US";
00043 
00050 QString OUTPUT_DIR;
00051 
00052 bool LANGUAGE_STYLE_RIGHT_TO_LEFT;
00053 
00054 QString LANGUAGE_FOR_HTML;
00055 
00059 int TIMETABLE_HTML_LEVEL;
00060 
00061 bool PRINT_NOT_AVAILABLE_TIME_SLOTS;
00062 
00063 bool PRINT_BREAK_TIME_SLOTS;
00064 
00065 bool PRINT_ACTIVITIES_WITH_SAME_STARTING_TIME;
00066 
00067 bool DIVIDE_HTML_TIMETABLES_WITH_TIME_AXIS_BY_DAYS;
00068 
00069 //this hashs are needed to get the IDs for html and css in timetableexport and statistics
00070 QHash<QString, QString> hashSubjectIDs;
00071 QHash<QString, QString> hashActivityTagIDs;
00072 QHash<QString, QString> hashStudentIDs;
00073 QHash<QString, QString> hashTeacherIDs;
00074 QHash<QString, QString> hashRoomIDs;
00075 QHash<QString, QString> hashDayIDs;
00076 
00080 const QString XML_PARSING_LOG_FILENAME="file_open.log";
00081 
00085 const QString PREDEFINED_DAYS_OF_THE_WEEK[]={"Monday", "Tuesday", "Wednesday",
00086        "Thursday", "Friday", "Saturday", "Sunday", "Monday2",
00087        "Tuesday2", "Wednesday2", "Thursday2", "Friday2", "Saturday2", "Sunday2",
00088        "Monday3", "Tuesday3", "Wednesday3",
00089        "Thursday3", "Friday3", "Saturday3", "Sunday3", "Monday4",
00090        "Tuesday4", "Wednesday4", "Thursday4", "Friday4", "Saturday4", "Sunday4"};
00091 
00095 const QString FILE_SEP="/";
00096 
00097 QString protect(const QString& str) //used for xml
00098 {
00099        QString p=str;
00100        p.replace("&", "&amp;");
00101        p.replace("\"", "&quot;");
00102        p.replace(">", "&gt;");
00103        p.replace("<", "&lt;");
00104        p.replace("'", "&apos;");
00105        return p;
00106 }
00107 
00108 QString protect2(const QString& str) //used for html
00109 {
00110        QString p=str;
00111        p.replace("&", "&amp;");
00112        p.replace("\"", "&quot;");
00113        p.replace(">", "&gt;");
00114        p.replace("<", "&lt;");
00115        //p.replace("'", "&apos;");
00116        return p;
00117 }
00118 
00119 QString protect2vert(const QString& str) //used for html
00120 {
00121        QString p=str;
00122        p.replace("&", "&amp;");
00123        p.replace("\"", "&quot;");
00124        p.replace(">", "&gt;");
00125        p.replace("<", "&lt;");
00126        //p.replace("'", "&apos;");
00127 
00128        QString returnstring;
00129        for(int i=0; i<p.size();i++){
00130               QString a=p.at(i);
00131               QString b="<br />";
00132               returnstring.append(a);
00133               returnstring.append(b);
00134        }
00135        return returnstring;
00136 }
00137 
00139 void weight_sscanf(const QString& str, const char* fmt, double* result)
00140 {
00141        assert(QString(fmt)==QString("%lf"));
00142 
00143        bool ok;
00144        double myres=customFETStrToDouble(str, &ok);
00145        if(!ok)
00146               (*result)=-2.5; //any value that does not belong to {>=0.0 and <=100.0} or {-1.0}
00147                                           //not -1.0 because of modify multiple constraints min days between activities,
00148                                           //-1 there represents any weight
00149                                           //potential bug found by Volker Dirr
00150        else
00151               (*result)=myres;
00152 }
00153 
00154 QString CustomFETString::number(int n)
00155 {
00156        return QString::number(n);
00157 }
00158 
00159 QString CustomFETString::number(double x)
00160 {
00161        QString tmp=QString::number(x, 'f', CUSTOM_DOUBLE_PRECISION);
00162        
00163        //remove trailing zeroes AFTER decimal points
00164        if(tmp.contains('.')){
00165               int n=tmp.length()-1;
00166               int del=0;
00167               while(tmp.at(n)=='0'){
00168                      n--;
00169                      del++;
00170               }
00171               if(tmp.at(n)=='.'){
00172                      n--;
00173                      del++;
00174               }
00175               tmp.chop(del);
00176        }
00177 
00178        return tmp;
00179 }
00180 
00181 double customFETStrToDouble(const QString& str, bool* ok)
00182 {
00183        QLocale c(QLocale::C);
00184 
00185        //tricks to convert numbers like 97.123456789 to 97.123457, to CUSTOM_DOUBLE_PRECISION (6) decimal digits after decimal point
00186        double tmpd=c.toDouble(str, ok);
00187        if(ok!=0)
00188               if((*ok)==false)
00189                      return tmpd;
00190        QString tmps=CustomFETString::number(tmpd);
00191        return c.toDouble(tmps, ok);
00192 }
00194 
00195 int XX;
00196 int YY;
00197 int ZZ;
00198 
00199 //random routines
00200 void initRandomKnuth()
00201 {
00202        assert(MM==2147483647);
00203        assert(AA==48271);
00204        assert(QQ==44488);
00205        assert(RR==3399);
00206        
00207        assert(MMM==2147483399);
00208        assert(MMM==MM-248);
00209        assert(AAA==40692);
00210        assert(QQQ==52774);
00211        assert(RRR==3791);
00212        
00213        //a few tests
00214        XX=123; YY=123;
00215        int tttt=randomKnuth1MM1();
00216        assert(XX==5937333);
00217        assert(YY==5005116);
00218        assert(tttt==932217);
00219 
00220        XX=4321; YY=54321;
00221        tttt=randomKnuth1MM1();
00222        assert(XX==208578991);
00223        assert(YY==62946733);
00224        assert(tttt==145632258);
00225 
00226        XX=87654321; YY=987654321;
00227        tttt=randomKnuth1MM1();
00228        assert(XX==618944401);
00229        assert(YY==1625301246);
00230        assert(tttt==1141126801);
00231 
00232        XX=1; YY=1;
00233        tttt=randomKnuth1MM1();
00234        assert(XX==48271);
00235        assert(YY==40692);
00236        assert(tttt==7579);
00237 
00238        XX=MM-1; YY=MMM-1;
00239        tttt=randomKnuth1MM1();
00240        assert(XX==2147435376);
00241        assert(YY==2147442707);
00242        assert(tttt==2147476315);
00243 
00244        XX=100; YY=1000;
00245        tttt=randomKnuth1MM1();
00246        assert(XX==4827100);
00247        assert(YY==40692000);
00248        assert(tttt==2111618746);
00250        
00251        //unsigned tt=unsigned(time(NULL));
00252        qint64 tt=qint64(time(NULL));
00253        
00254        //XX is the current time
00255        //XX = 1 + ( (unsigned(tt)) % (unsigned(MM-1)) );
00256        XX = 1 + int( tt%(qint64(MM-1)) );
00257        assert(XX>0);
00258        assert(XX<MM);
00259 
00260        //YY is the next random, after initializing YY with the current time
00261        //YY = 1 + ( (unsigned(tt)) % (unsigned(MMM-1)) );
00262        YY = 1 + int( tt%(qint64(MMM-1)) );
00263        assert(YY>0);
00264        assert(YY<MMM);
00265        YY=AAA*(YY%QQQ)-RRR*(YY/QQQ);
00266        if(YY<0)
00267               YY+=MMM;
00268        assert(YY>0);
00269        assert(YY<MMM);
00270        
00271        ZZ=XX-YY;
00272        if(ZZ<=0)
00273               ZZ+=MM-1; //-1 is not written in Knuth TAOCP vol. 2 third edition; I think it would be an improvement. (Later edit: yes, the author confirmed that).
00274        assert(ZZ>0);
00275        assert(ZZ<MM); //again, modified from Knuth TAOCP vol. 2 third edition, ZZ is strictly lower than MM (the author confirmed that, too).
00276 }
00277 
00278 int randomKnuth1MM1()
00279 {
00280        assert(XX>0);
00281        assert(XX<MM);
00282 
00283        XX=AA*(XX%QQ)-RR*(XX/QQ);
00284        if(XX<0)
00285               XX+=MM;
00286 
00287        assert(XX>0);
00288        assert(XX<MM);
00289 
00290        assert(YY>0);
00291        assert(YY<MMM);
00292 
00293        YY=AAA*(YY%QQQ)-RRR*(YY/QQQ);
00294        if(YY<0)
00295               YY+=MMM;
00296        
00297        assert(YY>0);
00298        assert(YY<MMM);
00299 
00300        ZZ=XX-YY;
00301        if(ZZ<=0)
00302               ZZ+=MM-1; //-1 is not written in Knuth TAOCP vol. 2 third edition; I think it would be an improvement. (Later edit: yes, the author confirmed that).
00303        assert(ZZ>0);
00304        assert(ZZ<MM); //again, modified from Knuth TAOCP vol. 2 third edition, ZZ is strictly lower than MM (the author confirmed that, too).
00305        
00306        return ZZ;
00307 }
00308 
00309 int randomKnuth(int k)
00310 {
00311        //like in Knuth TAOCP vol.2, reject some numbers (very few), so that the distribution is perfectly uniform
00312        for(;;){
00313               int U=randomKnuth1MM1();
00314               if( U <= k * ((MM-1)/k) )
00315                      return U%k;
00316        }
00317 }