FALCON_FUNC ConfParser_read( ::Falcon::VMachine *vm ) { CoreObject *self = vm->self().asObject(); ConfigFile *cfile = (ConfigFile *) self->getUserData(); Item *i_stream = vm->param(0); bool bRes; if( i_stream == 0 ) { vm->idle(); bRes = cfile->load(); vm->unidle(); } else { bool bValid = false; if ( i_stream->isObject() ) { CoreObject *streamObj = i_stream->asObject(); if ( streamObj->derivedFrom( "Stream" ) ) { Stream *base = (Stream *) streamObj->getUserData(); bRes = cfile->load( base ); bValid = true; } } if ( ! bValid ) { throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).extra( "Stream" ) ); return; } } if ( ! bRes ) { // is this an I/O or a parsing error? if ( cfile->fsError() != 0 ) { throw new IoError( ErrorParam( e_loaderror, __LINE__ ). sysError( cfile->fsError() ). extra( cfile->errorMessage() ) ); } else { String msg = cfile->errorMessage() + " at "; msg.writeNumber( (int64) cfile->errorLine() ); self->setProperty( "error", cfile->errorMessage() ); self->setProperty( "errorLine", (int64) cfile->errorLine() ); throw new ParseError( ErrorParam( FALCP_ERR_INVFORMAT, __LINE__ ) .desc( FAL_STR(cp_msg_invformat) ) .extra( msg ) ); } } }
/*# @method write ConfParser @brief Write the INI file. @optparam stream An optional output stream on which to write the configuration file. @raise IoError on write error. Writes the content of a modified or entirely generated configuration file on the given stream, that must be a valid Falcon stream opened for output. If a stream is not given, then the file name provided to the ConfParser constructor is opened for writing. In case the name has not been given in the constructor, the method raises an error. */ FALCON_FUNC ConfParser_write( ::Falcon::VMachine *vm ) { CoreObject *self = vm->self().asObject(); ConfigFile *cfile = (ConfigFile *) self->getUserData(); Item *i_stream = vm->param(0); bool bRes; if( i_stream == 0 ) { bRes = cfile->save(); } else { bool bValid = false; if ( i_stream->isObject() ) { CoreObject *streamObj = i_stream->asObject(); if ( streamObj->derivedFrom( "Stream" ) ) { Stream *base = (Stream *) streamObj->getUserData(); bRes = cfile->save( base ); bValid = true; } } if ( ! bValid ) { throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).extra( "Stream" ) ); return; } } if ( ! bRes ) { // is this a file error? if ( cfile->fsError() ) { throw new IoError( ErrorParam( e_file_output, __LINE__ ). sysError( cfile->fsError() ). extra( cfile->errorMessage() ) ); } else { // no -- it's a configuration file.d self->setProperty( "error", cfile->errorMessage() ); self->setProperty( "errorLine", (int64) cfile->errorLine() ); throw new ParseError( ErrorParam( FALCP_ERR_STORE, __LINE__ ). desc( FAL_STR(cp_msg_errstore) ).extra( cfile->errorMessage() ) ); } } }
FALCON_FUNC mth_dictFind( ::Falcon::VMachine *vm ) { Item *i_dict, *i_key; if( vm->self().isMethodic() ) { i_dict = &vm->self(); i_key = vm->param(0); } else { i_dict = vm->param(0); i_key = vm->param(1); } if( i_dict == 0 || ! i_dict->isDict() || i_key == 0 ) { throw new ParamError( ErrorParam( e_inv_params, __LINE__ ) .origin( e_orig_runtime ) .extra( vm->self().isMethodic() ? "X" : "D,X" ) ); } CoreDict *dict = i_dict->asDict(); Iterator iter( &dict->items() ); if ( ! dict->findIterator( *i_key, iter ) ) vm->retnil(); else { // find the iterator class, we'll need it Item *i_iclass = vm->findWKI( "Iterator" ); fassert( i_iclass != 0 ); CoreObject *ival = i_iclass->asClass()->createInstance( new Iterator( iter ) ); ival->setProperty( "_origin", *i_dict ); vm->retval( ival ); } }
FALCON_FUNC CmdlineParser_terminate( ::Falcon::VMachine *vm ) { CoreObject *self = vm->self().asObject(); self->setProperty( "_request", (int64) 2 ); }
/*# @method parse CmdlineParser @brief Starts command line parsing. @optparam args A specific string array that will be used as arguments to be parsed. @return true if the parsing is complete, false on error. Start the parsing process. If args parameter is not provided, the method gets the content of the @a args global vector defined in the Core module. Returns true if the parsing was complete, and false on error (for example, if some element in the array wasn't a string). */ FALCON_FUNC CmdlineParser_parse( ::Falcon::VMachine *vm ) { CoreObject *self = vm->self().asObject(); Item *i_params = vm->param( 0 ); if ( i_params == 0 ) { // get the parameters from the VM args object i_params = vm->findGlobalItem( "args" ); if ( i_params == 0 || ! i_params->isArray() ) { throw new CodeError( ErrorParam( e_undef_sym ).extra( "args" ).hard() ); } } else if ( ! i_params->isArray() ) { throw new ParamError( ErrorParam( e_inv_params ).extra( "( A )" ) ); } CoreArray *args = i_params->asArray(); // zero request. self->setProperty( "_request", Item((int64) 0) ); self->setProperty( "lastParsed", Item((int64) 0) ); // status. typedef enum { t_none, t_waitingValue, t_allFree } t_states; t_states state = t_none ; String currentOption; Item i_method; Item i_passMM; self->getProperty( "passMinusMinus", i_passMM ); bool passMM = i_passMM.isTrue(); Item _request; String subParam; uint32 i; for ( i = 0; i < args->length(); i++ ) { Item &i_opt = args->at( i ); if ( !i_opt.isString() ) { throw new ParamError( ErrorParam( e_param_type ). extra( vm->moduleString( rtl_cmdp_0 ) ) ); } String &opt = *i_opt.asString(); // if we were expecting a value, we MUST consider ANYTHING as it was a value. if ( state == t_waitingValue ) { self->getProperty( "onValue", i_method ); if ( i_method.methodize( self ) ) { vm->pushParam( new CoreString(currentOption) ); vm->pushParam( i_opt ); vm->callItemAtomic( i_method, 2 ); state = t_none; } else { vm->retval( false ); self->setProperty( "lastParsed", i ); return; } } else if( opt.length() == 0 || (opt.getCharAt( 0 ) != '-' || opt.length() == 1) || state == t_allFree ) { self->getProperty( "onFree", i_method ); if ( i_method.methodize( self ) ) { vm->pushParam( i_opt ); vm->callItemAtomic( i_method, 1 ); } else { vm->retval( false ); self->setProperty( "lastParsed", i ); return; } } else if ( opt == "--" && ! passMM ) { state = t_allFree; continue; // to skip return value. } else { // we have at least one '-', and length > 1 if ( opt.getCharAt( 1 ) == (uint32) '-' ) { self->getProperty( "onOption", i_method ); if ( i_method.methodize( self ) ) { if ( passMM && opt.size() == 2 ) vm->pushParam( i_opt ); else { //Minimal optimization; reuse the same string and memory subParam = opt.subString( 2 ); vm->pushParam( new CoreString( subParam ) ); } vm->callItemAtomic( i_method, 1 ); self->getProperty( "_request", _request ); // value requested? if ( _request.asInteger() == 1 ) { currentOption = subParam; } } else { vm->retval( false ); self->setProperty( "lastParsed", i ); return; } } else { // we have a switch set. for( uint32 chNum = 1; chNum < opt.length(); chNum++ ) { //Minimal optimization; reuse the same string and memory subParam.size( 0 ); subParam.append( opt.getCharAt( chNum ) ); if ( chNum < opt.length() -1 && opt.getCharAt( chNum +1 ) == (uint32) '-' ) { // switch turnoff. self->getProperty( "onSwitchOff", i_method ); if ( i_method.methodize( self ) ) { vm->pushParam( new CoreString(subParam) ); vm->callItemAtomic( i_method, 1 ); } else { vm->retval( false ); self->setProperty( "lastParsed", (int64) i ); return; } chNum++; } else { self->getProperty( "onOption", i_method ); if ( i_method.methodize( self ) ) { vm->pushParam( new CoreString(subParam) ); vm->callItemAtomic( i_method, 1 ); } else { vm->retval( false ); self->setProperty( "lastParsed", (int64) i ); return; } } self->getProperty( "_request", _request ); // value requested? if ( _request.asInteger() == 1 ) { currentOption = subParam; } } } self->getProperty( "_request", _request ); // value requested? if ( _request.asInteger() == 1 ) { state = t_waitingValue; self->setProperty( "_request", Item(0) ); } // or request to terminate? else if ( _request.asInteger() == 2 ) { self->setProperty( "_request", Item(0) ); vm->retval( true ); self->setProperty( "lastParsed", (int64) i ); return; } } } self->setProperty( "lastParsed", (int64) i ); vm->retval( true ); }