kspread Library API Documentation

kspread_functions_information.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998-2002 The KSpread Team
00003                            www.koffice.org/kspread
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 // built-in information functions
00022 
00023 
00024 #include <config.h>
00025 #include <stdlib.h>
00026 #include <math.h>
00027 #include <float.h>
00028 #include <sys/utsname.h>
00029 
00030 #include <qdir.h>
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 
00034 #include <koscript_parser.h>
00035 #include <koscript_util.h>
00036 #include <koscript_func.h>
00037 #include <koscript_synext.h>
00038 
00039 #include "kspread_functions.h"
00040 #include "kspread_util.h"
00041 #include "kspread_doc.h"
00042 #include "kspread_sheet.h"
00043 #include "kspread_interpreter.h"
00044 #include "kspread_value.h"
00045 #include "valueparser.h"
00046 #include "valueconverter.h"
00047 
00048 // prototypes (sort alphabetically)
00049 //bool kspreadfunc_countblank( KSContext & context );
00050 bool kspreadfunc_filename( KSContext & context );
00051 bool kspreadfunc_info( KSContext& context );
00052 bool kspreadfunc_isblank( KSContext& context );
00053 bool kspreadfunc_isdate( KSContext& context );
00054 bool kspreadfunc_iseven( KSContext& context );
00055 bool kspreadfunc_islogical( KSContext& context );
00056 bool kspreadfunc_isnottext( KSContext& context );
00057 bool kspreadfunc_isnum( KSContext& context );
00058 bool kspreadfunc_isodd( KSContext& context );
00059 bool kspreadfunc_isref( KSContext& context );
00060 bool kspreadfunc_istext( KSContext& context );
00061 bool kspreadfunc_istime( KSContext& context );
00062 bool kspreadfunc_n( KSContext & context );
00063 bool kspreadfunc_type( KSContext & context );
00064 bool kspreadfunc_version( KSContext & context );
00065 
00066 // registers all information functions
00067 void KSpreadRegisterInformationFunctions()
00068 {
00069   KSpreadFunctionRepository* repo = KSpreadFunctionRepository::self();
00070 
00071   repo->registerFunction( "FILENAME",   kspreadfunc_filename );
00072   repo->registerFunction( "INFO",       kspreadfunc_info );
00073   repo->registerFunction( "ISBLANK",    kspreadfunc_isblank );
00074   repo->registerFunction( "ISDATE",     kspreadfunc_isdate );
00075   repo->registerFunction( "ISEVEN",     kspreadfunc_iseven );
00076   repo->registerFunction( "ISLOGICAL",  kspreadfunc_islogical );
00077   repo->registerFunction( "ISNONTEXT",  kspreadfunc_isnottext );
00078   repo->registerFunction( "ISNOTTEXT",  kspreadfunc_isnottext );
00079   repo->registerFunction( "ISNUM",      kspreadfunc_isnum );
00080   repo->registerFunction( "ISNUMBER",   kspreadfunc_isnum );
00081   repo->registerFunction( "ISODD",      kspreadfunc_isodd );
00082   repo->registerFunction( "ISREF",      kspreadfunc_isref );
00083   repo->registerFunction( "ISTEXT",     kspreadfunc_istext );
00084   repo->registerFunction( "ISTIME",     kspreadfunc_istime );
00085   repo->registerFunction( "N",          kspreadfunc_n );
00086   repo->registerFunction( "TYPE",       kspreadfunc_type );
00087 }
00088 
00089 // Function: INFO
00090 bool kspreadfunc_info( KSContext& context )
00091 {
00092   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00093 
00094   if ( !KSUtil::checkArgumentsCount( context, 1, "INFO", true ) )
00095     return false;
00096 
00097   if( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
00098     return false;
00099 
00100   QString type = args[0]->stringValue().lower();
00101 
00102   if ( type == "directory" )
00103   {
00104     context.setValue( new KSValue( QDir::currentDirPath() ) );
00105     return true;
00106   }
00107 
00108   else if ( type == "release" )
00109   {
00110     context.setValue( new KSValue( QString( VERSION ) ) );
00111     return true;
00112   }
00113 
00114   else if ( type == "numfile" )
00115   {
00116     context.setValue( new KSValue( (int)KSpreadDoc::documents().count() ) );
00117     return true;
00118   }
00119 
00120   else if ( type == "recalc" )
00121   {
00122     QString result;
00123     if ( ( (KSpreadInterpreter *) context.interpreter() )->document()->delayCalculation() )
00124       result = i18n( "Manual" );
00125     else
00126       result = i18n( "Automatic" );
00127     context.setValue( new KSValue( result ) );
00128     return true;
00129   }
00130 
00131   else if (type == "memavail")
00132   {
00133   }
00134 
00135   else if (type == "memused")
00136   {
00137   }
00138 
00139   else if (type == "origin")
00140   {
00141   }
00142 
00143   else if (type == "system")
00144   {
00145     struct utsname name;
00146     if( uname( &name ) >= 0 )
00147     {
00148        context.setValue( new KSValue( QString( name.sysname ) ) );
00149        return true;
00150     }
00151   }
00152 
00153   else if (type == "totmem")
00154   {
00155   }
00156 
00157   else if (type == "osversion")
00158   {
00159     struct utsname name;
00160     if( uname( &name ) >= 0 )
00161     {
00162        QString os = QString("%1 %2 (%3)").arg( name.sysname ).
00163          arg( name.release ).arg( name.machine );
00164        context.setValue( new KSValue( os ) );
00165        return true;
00166     }
00167   }
00168 
00169   return false;
00170 }
00171 
00172 // Function: ISBLANK
00173 bool kspreadfunc_isblank( KSContext& context )
00174 {
00175   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00176 
00177   if ( !KSUtil::checkArgumentsCount( context, 1, "ISBLANK", true ) )
00178     return false;
00179 
00180   bool result = false;
00181 
00182   if ( KSUtil::checkType( context, args[0], KSValue::Empty, false ) )
00183     result = true;
00184 
00185   // the following part is needed becase empty cell returns 0.0 instead
00186   // of KSValue::Empty. this is due to bug in many function implementation
00187   // until someone can fix this, leave it as it is (ariya, 16.05.2002)
00188   if ( KSUtil::checkType( context, args[0], KSValue::DoubleType, false ) )
00189     result = args[0]->doubleValue() == 0.0;
00190 
00191   if ( KSUtil::checkType( context, args[0], KSValue::StringType, false ) )
00192     result = args[0]->stringValue().isEmpty();
00193 
00194   context.setValue( new KSValue( result ) );
00195   return true;
00196 }
00197 
00198 // Function: ISLOGICAL
00199 bool kspreadfunc_islogical( KSContext& context )
00200 {
00201   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00202 
00203   if ( !KSUtil::checkArgumentsCount( context, 1, "ISLOGICAL", true ) )
00204     return false;
00205 
00206   bool logic = KSUtil::checkType( context, args[0], KSValue::BoolType, true );
00207   context.setValue( new KSValue(logic));
00208   return true;
00209 }
00210 
00211 // Function: ISTEXT
00212 bool kspreadfunc_istext( KSContext& context )
00213 {
00214   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00215 
00216   if ( !KSUtil::checkArgumentsCount( context, 1, "ISTEXT", true ) )
00217     return false;
00218 
00219   bool logic = KSUtil::checkType( context, args[0], KSValue::StringType, true );
00220   context.setValue( new KSValue(logic));
00221   return true;
00222 }
00223 
00224 // Function: ISREF
00225 bool kspreadfunc_isref( KSContext& context )
00226 {
00227   QValueList<KSValue::Ptr> & extra = context.extraData()->listValue();
00228 
00229   if ( !KSUtil::checkArgumentsCount( context, 1, "ISREF", true ) )
00230     return false;
00231 
00232   bool ref;
00233 
00234   if ( !KSUtil::checkType( context, extra[0], KSValue::StringType, true ) )
00235     ref = false;
00236   else
00237     ref = true;
00238 
00239   context.setValue( new KSValue( ref ) );
00240   return true;
00241 }
00242 
00243 // Function: ISNOTTEXT
00244 bool kspreadfunc_isnottext( KSContext& context )
00245 {
00246   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00247 
00248   if ( !KSUtil::checkArgumentsCount( context, 1, "ISNOTTEXT", true ) )
00249     return false;
00250 
00251   bool logic = !KSUtil::checkType( context, args[0], KSValue::StringType, true );
00252   context.setValue( new KSValue(logic));
00253   return true;
00254 }
00255 
00256 // Function: ISNUM
00257 bool kspreadfunc_isnum( KSContext& context )
00258 {
00259   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00260 
00261   if ( !KSUtil::checkArgumentsCount( context, 1, "ISNUM", true ) )
00262     return false;
00263 
00264   bool logic = KSUtil::checkType( context, args[0], KSValue::DoubleType, true );
00265   context.setValue( new KSValue(logic));
00266   return true;
00267 }
00268 
00269 // Function: ISTIME
00270 bool kspreadfunc_istime( KSContext& context )
00271 {
00272   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00273 
00274   if ( !KSUtil::checkArgumentsCount( context, 1, "ISTIME", true ) )
00275     return false;
00276   bool logic = false;
00277 
00278   if (!KSUtil::checkType( context, args[0], KSValue::TimeType, true ))
00279   {
00280       if ( KSUtil::checkType( context, args[0], KSValue::StringType, true ))
00281           KGlobal::locale()->readTime(args[0]->stringValue(), &logic);
00282       else
00283           return false;
00284   }
00285   else
00286       logic = true;
00287   context.setValue( new KSValue(logic));
00288   return true;
00289 }
00290 
00291 // Function: ISDATE
00292 bool kspreadfunc_isdate( KSContext& context )
00293 {
00294   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00295 
00296   if ( !KSUtil::checkArgumentsCount( context, 1, "ISDATE", true ) )
00297     return false;
00298 
00299   bool logic = false;
00300   if (!KSUtil::checkType( context, args[0], KSValue::DateType, true ))
00301   {
00302       if ( KSUtil::checkType( context, args[0], KSValue::StringType, true ))
00303           KGlobal::locale()->readDate(args[0]->stringValue(), &logic);
00304       else
00305           return false;
00306   }
00307   else
00308       logic = true;
00309 
00310   context.setValue( new KSValue(logic));
00311   return true;
00312 }
00313 
00314 // Function: ISODD
00315 bool kspreadfunc_isodd( KSContext& context )
00316 {
00317   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00318   if ( !KSUtil::checkArgumentsCount( context,1, "ISODD",true ) )
00319     return false;
00320   bool result=true;
00321   if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
00322         result= false;
00323   if(result)
00324         {
00325         //it's a integer => test if it's an odd integer
00326         if(args[0]->intValue() & 1)
00327                 result=true;
00328         else
00329                 result=false;
00330         }
00331 
00332   context.setValue( new KSValue(result));
00333 
00334   return true;
00335 }
00336 
00337 // Function: ISEVEN
00338 bool kspreadfunc_iseven( KSContext& context )
00339 {
00340   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00341   if ( !KSUtil::checkArgumentsCount( context,1, "ISEVEN",true ) )
00342     return false;
00343   bool result=true;
00344   if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
00345         result= false;
00346   if(result)
00347         {
00348         //it's a integer => test if it's an even integer
00349         if(args[0]->intValue() & 1)
00350                 result=false;
00351         else
00352                 result=true;
00353         }
00354 
00355   context.setValue( new KSValue(result));
00356 
00357   return true;
00358 }
00359 
00360 /*
00361 static bool kspreadfunc_countblank_helper( KSContext & context,
00362                                            QValueList<KSValue::Ptr> & args,
00363                                            int & result)
00364 {
00365   QValueList<KSValue::Ptr>::Iterator it  = args.begin();
00366   QValueList<KSValue::Ptr>::Iterator end = args.end();
00367 
00368   for( ; it != end; ++it )
00369   {
00370     if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
00371     {
00372 
00373       if ( !kspreadfunc_countblank_helper( context, (*it)->listValue(), result) )
00374         return false;
00375     }
00376     else
00377     if ( KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
00378     {
00379       ++result;
00380     }
00381     else
00382     if ( KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00383     {
00384       KScript::Double d = args[0]->doubleValue();
00385       if (d == 0.0)
00386         ++result;
00387     }
00388     else
00389     if ( KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
00390     {
00391       QString s = args[0]->stringValue();
00392       if ( s.isEmpty() || s.stripWhiteSpace().isEmpty() )
00393         ++result;
00394     }
00395   }
00396 
00397   return true;
00398 }
00399 
00400 bool kspreadfunc_countblank( KSContext & context )
00401 {
00402   int result = 0;
00403 
00404   bool b = kspreadfunc_countblank_helper( context, context.value()->listValue(), result);
00405 
00406   if ( b )
00407     context.setValue( new KSValue( result ) );
00408 
00409   return b;
00410 }
00411 */
00412 
00413 // Function: TYPE
00414 bool kspreadfunc_type( KSContext & context )
00415 {
00416   QValueList<KSValue::Ptr> & args = context.value()->listValue();
00417   QValueList<KSValue::Ptr> & extra = context.extraData()->listValue();
00418   kdDebug() << "Here " << endl;
00419 
00420   if ( !KSUtil::checkArgumentsCount( context, 1, "TYPE", true ) )
00421     return false;
00422 
00423   kdDebug() << "Here0 " << endl;
00424 
00425   if ( KSUtil::checkType( context, args[0], KSValue::StringType, false ) )
00426   {
00427     context.setValue( new KSValue( 1 ) );
00428     return true;
00429   }
00430 
00431   if ( KSUtil::checkType( context, args[0], KSValue::DoubleType, false )
00432        || KSUtil::checkType( context, args[0], KSValue::IntType, false )
00433        || KSUtil::checkType( context, args[0], KSValue::DateType, false )
00434        || KSUtil::checkType( context, args[0], KSValue::TimeType, false ) )
00435   {
00436     context.setValue( new KSValue( 2 ) );
00437     return true;
00438   }
00439   kdDebug() << "Here1 " << endl;
00440 
00441   if ( KSUtil::checkType( context, args[0], KSValue::BoolType, false ) )
00442   {
00443     context.setValue( new KSValue( 4 ) );
00444     return true;
00445   }
00446 
00447   kdDebug() << "Here2 " << endl;
00448 
00449   if ( KSUtil::checkType( context, args[0], KSValue::ListType, false ) )
00450   {
00451     context.setValue( new KSValue( 64 ) );
00452     return true;
00453   }
00454 
00455   kdDebug() << "Here3 " << endl;
00456 
00457   QString p( extra[0]->stringValue() );
00458   if ( !p.isEmpty() )
00459   {
00460     KSpreadMap *   map   = ((KSpreadInterpreter *) context.interpreter() )->document()->map();
00461     KSpreadSheet * sheet = ((KSpreadInterpreter *) context.interpreter() )->sheet();
00462 
00463     KSpreadPoint point( p, map, sheet );
00464     if ( point.isValid() )
00465     {
00466       KSpreadCell * cell = point.sheet->cellAt( point.pos.x(), point.pos.y() );
00467 
00468       if ( cell->hasError() )
00469       {
00470         context.setValue( new KSValue( 16 ) );
00471         return true;
00472       }
00473     }
00474   }
00475 
00476   context.setValue( new KSValue( 0 ) );
00477 
00478   return true;
00479 }
00480 
00481 bool kspreadfunc_filename( KSContext & context )
00482 {
00483   context.setValue( new KSValue( ( (KSpreadInterpreter *) context.interpreter() )->document()->url().prettyURL() ) );
00484 
00485   return true;
00486 }
00487 
00488 // Function: N
00489 bool kspreadfunc_n( KSContext & context )
00490 {
00491   //currently the same as the VALUE function
00492   
00493   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00494 
00495   if ( !KSUtil::checkArgumentsCount( context, 1, "N", true ) )
00496     return false;
00497 
00498   KSpreadValue val;
00499   
00500   if( KSUtil::checkType( context, args[0], KSValue::StringType, false ) )
00501     val.setValue (args[0]->stringValue());
00502   
00503   else if( KSUtil::checkType( context, args[0], KSValue::DoubleType, false ) )
00504     val.setValue (args[0]->doubleValue());
00505   
00506   else if( KSUtil::checkType( context, args[0], KSValue::TimeType, false ) )
00507     val.setValue (args[0]->timeValue());
00508   
00509   else if( KSUtil::checkType( context, args[0], KSValue::DateType, false ) )
00510     val.setValue (args[0]->dateValue());
00511   
00512   else if( KSUtil::checkType( context, args[0], KSValue::IntType, false ) )
00513     val.setValue (args[0]->intValue());
00514   
00515   else if( KSUtil::checkType( context, args[0], KSValue::BoolType, false ) )
00516     val.setValue (args[0]->boolValue());
00517     
00518   // temporary ugly hack, yay!
00519   KSpread::ValueParser *parser = new KSpread::ValueParser( KGlobal::locale() );
00520   KSpread::ValueConverter *converter = new KSpread::ValueConverter( parser );
00521   val = converter->asFloat (val);
00522   delete converter;
00523   delete parser;
00524   
00525   context.setValue( new KSValue( val.asFloat() ));
00526   return true;
00527 
00528 }
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:43:10 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003