lib Library API Documentation

koscript_func.cc

00001 /* This file is part of the KDE project 00002 Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "koscript_func.h" 00021 #include "koscript_context.h" 00022 #include "koscript_value.h" 00023 #include "koscript_parsenode.h" 00024 #include "koscript_util.h" 00025 #include "koscript_method.h" 00026 00027 #include <kapplication.h> 00028 #include <dcopclient.h> 00029 #include <klocale.h> 00030 00031 #include <iostream> 00032 00033 // Imported from scanner.ll 00034 extern KLocale* s_koscript_locale; 00035 00036 using namespace std; 00037 00038 bool KSScriptFunction::call( KSContext& context ) 00039 { 00040 return m_node->eval( context ); 00041 } 00042 00043 static bool ksfunc_mid( KSContext& context ) 00044 { 00045 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00046 00047 uint len = 0xffffffff; 00048 if ( KSUtil::checkArgumentsCount( context, 3, "mid", false ) ) 00049 { 00050 if( KSUtil::checkType( context, args[2], KSValue::DoubleType, false ) ) 00051 len = (uint) args[2]->doubleValue(); 00052 else if( KSUtil::checkType( context, args[2], KSValue::IntType, true ) ) 00053 len = (uint) args[2]->intValue(); 00054 else 00055 return false; 00056 } 00057 else if ( !KSUtil::checkArgumentsCount( context, 2, "mid", true ) ) 00058 return false; 00059 00060 if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) ) 00061 return false; 00062 00063 if( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) ) 00064 return false; 00065 int pos = args[1]->intValue(); 00066 00067 QString tmp = args[0]->stringValue().mid( pos, len ); 00068 context.setValue( new KSValue(tmp)); 00069 return true; 00070 } 00071 00072 static bool ksfunc_time( KSContext& context ) 00073 { 00074 KSUtil::checkArgs( context, "s", "time", TRUE ); 00075 00076 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00077 00078 QTime t = s_koscript_locale->readTime( args[0]->stringValue() ); 00079 if ( !t.isValid() ) 00080 { 00081 QString tmp( i18n("Invalid time format: %1") ); 00082 context.setException( new KSException( "ParsingError", tmp.arg( args[0]->stringValue() ), -1 ) ); 00083 return false; 00084 } 00085 00086 context.setValue( new KSValue( t ) ); 00087 00088 return true; 00089 } 00090 00091 static bool ksfunc_date( KSContext& context ) 00092 { 00093 KSUtil::checkArgs( context, "s", "date", TRUE ); 00094 00095 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00096 00097 QDate t = s_koscript_locale->readDate( args[0]->stringValue() ); 00098 if ( !t.isValid() ) 00099 { 00100 QString tmp( i18n("Invalid date format: %1") ); 00101 context.setException( new KSException( "ParsingError", tmp.arg( args[0]->stringValue() ), -1 ) ); 00102 return false; 00103 } 00104 00105 context.setValue( new KSValue( t ) ); 00106 00107 return true; 00108 } 00109 00114 static bool ksfunc_length( KSContext& context ) 00115 { 00116 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00117 00118 if ( !KSUtil::checkArgumentsCount( context, 1, "length", true ) ) 00119 return false; 00120 00121 if ( KSUtil::checkType( context, args[0], KSValue::ListType, false ) ) 00122 { 00123 context.setValue( new KSValue( (KScript::Long) args[0]->listValue().count() ) ); 00124 } 00125 else if ( KSUtil::checkType( context, args[0], KSValue::MapType, false ) ) 00126 { 00127 context.setValue( new KSValue( (KScript::Long) args[0]->mapValue().count() ) ); 00128 } 00129 else if ( KSUtil::checkType( context, args[0], KSValue::StringType, false ) ) 00130 { 00131 context.setValue( new KSValue( (KScript::Long) args[0]->stringValue().length() ) ); 00132 } 00133 else 00134 { 00135 QString tmp( i18n("Cannot calculate length of a %1 value.") ); 00136 context.setException( new KSException( "CastingError", tmp.arg( args[0]->typeName() ), -1 ) ); 00137 return false; 00138 } 00139 00140 return true; 00141 } 00142 00143 static bool ksfunc_lower( KSContext& context ) 00144 { 00145 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00146 00147 if ( !KSUtil::checkArgs( context, args, "s", "lower", TRUE ) ) 00148 return FALSE; 00149 00150 context.setValue( new KSValue( args[0]->stringValue().lower() ) ); 00151 00152 return TRUE; 00153 } 00154 00155 static bool ksfunc_upper( KSContext& context ) 00156 { 00157 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00158 00159 if ( !KSUtil::checkArgs( context, args, "s", "lower", TRUE ) ) 00160 return FALSE; 00161 00162 context.setValue( new KSValue( args[0]->stringValue().upper() ) ); 00163 00164 return TRUE; 00165 } 00166 00167 static bool ksfunc_isEmpty( KSContext& context ) 00168 { 00169 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00170 00171 if ( !KSUtil::checkArgumentsCount( context, 1, "isEmpty", true ) ) 00172 return false; 00173 00174 if ( KSUtil::checkType( context, args[0], KSValue::ListType, false ) ) 00175 { 00176 context.setValue( new KSValue( args[0]->listValue().isEmpty() ) ); 00177 } 00178 else if ( KSUtil::checkType( context, args[0], KSValue::MapType, false ) ) 00179 { 00180 context.setValue( new KSValue( args[0]->mapValue().isEmpty() ) ); 00181 } 00182 else if ( KSUtil::checkType( context, args[0], KSValue::StringType, false ) ) 00183 { 00184 context.setValue( new KSValue( args[0]->stringValue().isEmpty() ) ); 00185 } 00186 else 00187 { 00188 QString tmp( i18n("Cannot determine emptiness of a %1 value.") ); 00189 context.setException( new KSException( "CastingError", tmp.arg( args[0]->typeName() ), -1 ) ); 00190 return false; 00191 } 00192 00193 return true; 00194 } 00195 00199 static bool ksfunc_toInt( KSContext& context ) 00200 { 00201 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00202 00203 if ( !KSUtil::checkArgumentsCount( context, 1, "toInt", true ) ) 00204 return false; 00205 00206 if ( KSUtil::checkType( context, args[0], KSValue::DoubleType, false ) ) 00207 { 00208 context.setValue( new KSValue( (KScript::Long)args[0]->doubleValue() ) ); 00209 return true; 00210 } 00211 else if ( KSUtil::checkType( context, args[0], KSValue::StringType, false ) ) 00212 { 00213 bool ok; 00214 KScript::Long l = args[0]->stringValue().toLong( &ok ); 00215 if ( ok ) 00216 { 00217 context.setValue( new KSValue( l ) ); 00218 return true; 00219 } 00220 } 00221 00222 QString tmp( i18n("Cannot calculate a numerical value from a %1 value.") ); 00223 context.setException( new KSException( "CastingError", tmp.arg( args[0]->typeName() ), -1 ) ); 00224 return false; 00225 } 00226 00230 static bool ksfunc_toFloat( KSContext& context ) 00231 { 00232 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00233 00234 if ( !KSUtil::checkArgumentsCount( context, 1, "toFloat", true ) ) 00235 return false; 00236 00237 if ( KSUtil::checkType( context, args[0], KSValue::IntType, false ) ) 00238 { 00239 context.setValue( new KSValue( (KScript::Double)args[0]->intValue() ) ); 00240 return true; 00241 } 00242 else if ( KSUtil::checkType( context, args[0], KSValue::StringType, false ) ) 00243 { 00244 bool ok; 00245 KScript::Double l = args[0]->stringValue().toDouble( &ok ); 00246 if ( ok ) 00247 { 00248 context.setValue( new KSValue( l ) ); 00249 return true; 00250 } 00251 } 00252 00253 QString tmp( i18n("Cannot calculate a floating point value from a %1 value.") ); 00254 context.setException( new KSException( "CastingError", tmp.arg( args[0]->typeName() ), -1 ) ); 00255 return false; 00256 } 00257 00264 static bool ksfunc_arg( KSContext& context ) 00265 { 00266 QValueList<KSValue::Ptr>& args = context.value()->listValue(); 00267 00268 if ( !KSUtil::checkArgumentsCount( context, 2, "arg", true ) ) 00269 return false; 00270 00271 if ( !KSUtil::checkType( context, args[0], KSValue::StringType, TRUE ) ) 00272 return FALSE; 00273 00274 QString str = args[0]->stringValue(); 00275 00276 if ( KSUtil::checkType( context, args[1], KSValue::StringType, FALSE ) ) 00277 context.setValue( new KSValue( str.arg( args[1]->stringValue() ) ) ); 00278 else if ( KSUtil::checkType( context, args[1], KSValue::IntType, FALSE ) ) 00279 context.setValue( new KSValue( str.arg( args[1]->intValue() ) ) ); 00280 else if ( KSUtil::checkType( context, args[1], KSValue::DoubleType, FALSE ) ) 00281 context.setValue( new KSValue( str.arg( args[1]->doubleValue() ) ) ); 00282 else if ( KSUtil::checkType( context, args[1], KSValue::CharType, FALSE ) ) 00283 context.setValue( new KSValue( str.arg( args[1]->charValue() ) ) ); 00284 else context.setValue( new KSValue( str.arg( args[1]->toString( context ) ) ) ); 00285 00286 return TRUE; 00287 } 00288 00289 static bool ksfunc_stringListSplit( KSContext &context ) 00290 { 00291 QValueList<KSValue::Ptr> &args = context.value()->listValue(); 00292 00293 if ( !KSUtil::checkArgumentsCount( context, 2, "arg", true ) ); 00294 00295 if ( !KSUtil::checkType( context, args[0], KSValue::StringType, TRUE ) ) 00296 return false; 00297 00298 QString sep = args[0]->stringValue(); 00299 QString str = args[1]->stringValue(); 00300 00301 QStringList strLst = QStringList::split( sep, str ); 00302 00303 KSValue *v = new KSValue( KSValue::ListType ); 00304 00305 QStringList::ConstIterator it = strLst.begin(); 00306 QStringList::ConstIterator end = strLst.end(); 00307 for (; it != end; ++it ) 00308 v->listValue().append( new KSValue( *it ) ); 00309 00310 context.setValue( v ); 00311 return true; 00312 } 00313 00314 static bool ksfunc_print( KSContext& context ) 00315 { 00316 // We know that the context always holds a list of parameters 00317 QValueList<KSValue::Ptr>::Iterator it = context.value()->listValue().begin(); 00318 QValueList<KSValue::Ptr>::Iterator end = context.value()->listValue().end(); 00319 00320 if ( it == end ) 00321 cout << endl; 00322 00323 for( ; it != end; ++it ) 00324 cout << (*it)->toString( context ).local8Bit(); 00325 00326 // context.value()->clear(); 00327 context.setValue( 0 ); 00328 00329 return true; 00330 } 00331 00332 static bool ksfunc_println( KSContext& context ) 00333 { 00334 // We know that the context always holds a list of parameters 00335 QValueList<KSValue::Ptr>::Iterator it = context.value()->listValue().begin(); 00336 QValueList<KSValue::Ptr>::Iterator end = context.value()->listValue().end(); 00337 00338 if ( it == end ) 00339 cout << endl; 00340 00341 for( ; it != end; ++it ) 00342 cout << (*it)->toString( context ).local8Bit() << endl; 00343 00344 // context.value()->clear(); 00345 context.setValue( 0 ); 00346 00347 return true; 00348 } 00349 00350 KSModule::Ptr ksCreateModule_KScript( KSInterpreter* interp ) 00351 { 00352 KSModule::Ptr module = new KSModule( interp, "koscript" ); 00353 00354 // ariya: for the time being, let's just disable KoScript built-in functions 00355 // because many have the same name as KSpread functions 00356 #if 0 00357 module->addObject( "time", new KSValue( new KSBuiltinFunction( module, "time", ksfunc_time ) ) ); 00358 module->addObject( "date", new KSValue( new KSBuiltinFunction( module, "date", ksfunc_date ) ) ); 00359 module->addObject( "print", new KSValue( new KSBuiltinFunction( module, "print", ksfunc_print ) ) ); 00360 module->addObject( "println", new KSValue( new KSBuiltinFunction( module, "println", ksfunc_println ) ) ); 00361 module->addObject( "length", new KSValue( new KSBuiltinFunction( module, "length", ksfunc_length ) ) ); 00362 module->addObject( "arg", new KSValue( new KSBuiltinFunction( module, "arg", ksfunc_arg ) ) ); 00363 module->addObject( "mid", new KSValue( new KSBuiltinFunction( module, "mid", ksfunc_mid ) ) ); 00364 module->addObject( "upper", new KSValue( new KSBuiltinFunction( module, "upper", ksfunc_upper ) ) ); 00365 module->addObject( "lower", new KSValue( new KSBuiltinFunction( module, "lower", ksfunc_lower ) ) ); 00366 module->addObject( "isEmpty", new KSValue( new KSBuiltinFunction( module, "isEmpty", ksfunc_isEmpty ) ) ); 00367 module->addObject( "toInt", new KSValue( new KSBuiltinFunction( module, "toInt", ksfunc_toInt ) ) ); 00368 module->addObject( "toFloat", new KSValue( new KSBuiltinFunction( module, "toFloat", ksfunc_toFloat ) ) ); 00369 module->addObject( "stringListSplit", new KSValue( new KSBuiltinFunction( module, "stringListSplit", ksfunc_stringListSplit ) ) ); 00370 #endif 00371 00372 return module; 00373 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 06:54:17 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003