Example #1
0
void Handle_aquery( VMachine *vm )
{
   Item* i_sql = vm->param(0);
   Item* i_params = vm->param(1);
   if ( i_sql == 0 || ! i_sql->isString()
      || i_params == 0 || ! i_params->isArray() )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
                                        .extra( "S,A" ) );
   }

   CoreObject *self = vm->self().asObject();
   DBIHandle *dbt = static_cast<DBIHandle *>( self->getUserData() );

   DBIRecordset* res = 0;
   res = dbt->query( *i_sql->asString(), &i_params->asArray()->items() );

   if( res != 0 )
   {
      Item* rset_item = vm->findWKI( "%Recordset" );
      fassert( rset_item != 0 );
      fassert( rset_item->isClass() );

      CoreObject* rset = rset_item->asClass()->createInstance();
      rset->setUserData( res );
      vm->retval( rset );
   }
}
Example #2
0
void Statement_aexec( VMachine *vm )
{
   Item* i_params = vm->param(0);
   if( i_params == 0 || ! i_params->isArray() )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
                                               .extra( "A" ) );
   }

   CoreObject *self = vm->self().asObject();
   DBIStatement *dbt = static_cast<DBIStatement *>( self->getUserData() );

   DBIRecordset* res;
   res = dbt->execute( &i_params->asArray()->items() );

   if( res != 0 )
   {
      Item* rset_item = vm->findWKI( "%Recordset" );
      fassert( rset_item != 0 );
      fassert( rset_item->isClass() );

      CoreObject* rset = rset_item->asClass()->createInstance();
      rset->setUserData( res );

      vm->retval( rset );
   }
   else
   {
      vm->retnil();
   }
}
Example #3
0
void DBIError_init( VMachine *vm )
{
   CoreObject *einst = vm->self().asObject();
   if( einst->getUserData() == 0 )
      einst->setUserData( new DBIError );

   ::Falcon::core::Error_init( vm );
}
Example #4
0
static void internal_stmt_open( VMachine* vm, DBIStatement* trans )
{
   Item *trclass = vm->findWKI( "%Statement" );
   fassert( trclass != 0 && trclass->isClass() );

   CoreObject *oth = trclass->asClass()->createInstance();
   oth->setUserData( trans );
   vm->retval( oth );
}
Example #5
0
FALCON_FUNC Dictionary_last( VMachine *vm )
{
   Item *itclass = vm->findWKI( "Iterator" );
   fassert( itclass != 0 );

   CoreObject *iterator = itclass->asClass()->createInstance();
   // we need to set the FalconData flag
   iterator->setUserData( new Iterator( &vm->self().asDict()->items(), true  ) );
   vm->retval( iterator );
}
Example #6
0
/*#
   @method GetCursor SDL
   @brief Gets currently active cursor.
   @return an instance of @a SDLCursor class or nil.

   This method returns the currently active cursor. It can be used to
   store this away and set it later.
*/
FALCON_FUNC sdl_GetCursor( ::Falcon::VMachine *vm )
{
   ::SDL_Cursor *cursor = ::SDL_GetCursor();
   if ( cursor == 0 )
      vm->retnil();
   else {
      Item *cls = vm->findWKI( "SDLCursor" );
      fassert( cls != 0 );
      CoreObject *obj = cls->asClass()->createInstance();
      obj->setUserData( new SDLCursorCarrier( cursor, false ) );
      vm->retval( obj );
   }
}
Example #7
0
void Handle_query( VMachine *vm )
{
   Item* i_sql = vm->param(0);

   if ( i_sql == 0 || ! i_sql->isString() )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
                                        .extra( "S, ..." ) );
   }

   CoreObject *self = vm->self().asObject();
   DBIHandle *dbt = static_cast<DBIHandle *>( self->getUserData() );
   

   DBIRecordset* res = 0;
   int32 pCount = vm->paramCount();
   if( pCount > 1 )
   {
      ItemArray params( pCount - 1 );      
      for( int32 i = 1; i < vm->paramCount(); i++)
      {
         params.append( *vm->param(i) );
      }
      
      // Query may throw.
      res = dbt->query( *i_sql->asString(), &params );
   }
   else 
   {
      res = dbt->query( *i_sql->asString() );
   }

   if( res != 0 )
   {
      Item* rset_item = vm->findWKI( "%Recordset" );
      fassert( rset_item != 0 );
      fassert( rset_item->isClass() );

      CoreObject* rset = rset_item->asClass()->createInstance();
      rset->setUserData( res );

      vm->retval( rset );
   }
}
Example #8
0
void Recordset_next( VMachine *vm )
{
   CoreObject *self = vm->self().asObject();
   DBIRecordset *dbr = static_cast<DBIRecordset *>( self->getUserData() );

   DBIRecordset *res = dbr->getNext();
   if( res != 0 )
   {
      Item* rset_item = vm->findWKI( "%Recordset" );
      fassert( rset_item != 0 );
      fassert( rset_item->isClass() );

      CoreObject* rset = rset_item->asClass()->createInstance();
      rset->setUserData( res );

      vm->retval( rset );
   }
   // else return nothing
}
Example #9
0
void Statement_execute( VMachine *vm )
{
   CoreObject *self = vm->self().asObject();
   DBIStatement *dbt = static_cast<DBIStatement *>( self->getUserData() );
   
   DBIRecordset* res;

   if( vm->paramCount() != 0 )
   {
      ItemArray params( vm->paramCount() );
      for( int32 i = 0; i < vm->paramCount(); i++)
      {
         params.append( *vm->param(i) );
      }
      res = dbt->execute( &params );
   }
   else {
      res = dbt->execute();
   }

   if( res != 0 )
   {
      Item* rset_item = vm->findWKI( "%Recordset" );
      fassert( rset_item != 0 );
      fassert( rset_item->isClass() );

      CoreObject* rset = rset_item->asClass()->createInstance();
      rset->setUserData( res );

      vm->retval( rset );
   }
   else
   {
      vm->retnil();
   }
}
Example #10
0
FALCON_FUNC  ConfParser_init( ::Falcon::VMachine *vm )
{
   CoreObject *self = vm->self().asObject();
   Item *i_fname = vm->param(0);
   Item *i_encoding = vm->param(1);

   if ( (i_fname != 0 && ! i_fname->isString()) || ( i_encoding != 0 && ! i_encoding->isString() ) )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).extra( "S, [S]" ) );
      return;
   }

   String fname;
   String encoding;

   if ( i_fname != 0 )
      fname = *i_fname->asString();

   if ( i_encoding != 0 )
      encoding = *i_encoding->asString();

   ConfigFile *cfile = new ConfigFile( fname, encoding );
   self->setUserData( cfile );
}
Example #11
0
/*#
   @method CreateCursor SDL
   @brief Gets currently active cursor.
   @param mbData MemBuf containing visual bit data.
   @param mbMask Membuf containing visibility mask data.
   @param width Width of the cursor.
   @param height Height of the cursor.
   @param Xspot X position of the cursor hotspot.
   @param Yspot Y position of the cursor hotspot.
   @raise SDLError if the cursor couldn't be created.

   See SDL_CreateCursor documentation. Method @a SDL.MakeCursor is
   probably simpler to use.
*/
FALCON_FUNC sdl_CreateCursor( ::Falcon::VMachine *vm )
{
   Item *i_data, *i_mask;
   Item *i_width, *i_height, *i_xspot, *i_yspot;

   if( vm->paramCount() < 6 ||
      ! (i_data = vm->param(0) )->isMemBuf() ||
      ! (i_mask = vm->param(1) )->isMemBuf() ||
      ! (i_width = vm->param(2) )->isOrdinal() ||
      ! (i_height = vm->param(3) )->isOrdinal() ||
      ! (i_xspot = vm->param(4) )->isOrdinal() ||
      ! (i_yspot = vm->param(5) )->isOrdinal()
      )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
         extra( "M,M,N,N,N,N" ) ) ;
      return;
   }

   MemBuf *data = i_data->asMemBuf();
   MemBuf *mask = i_mask->asMemBuf();
   // we are not interested in their word size.
   if( data->size() || data->size() != mask->size() )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Membuf must be of same size" ) ) ;
      return;
   }

   int width = (int) i_width->forceInteger();
   int height = (int) i_height->forceInteger();
   int xspot = (int) i_xspot->forceInteger();
   int yspot = (int) i_yspot->forceInteger();

   if( width < 8 || height < 1 || width % 8 != 0 )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Invalid sizes" ) ) ;
      return;
   }

   if( data->size() != (uint32)( width/8 * height ) )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Membuf doesn't match width and height" ) ) ;
      return;
   }

   if( xspot < 0  || xspot >= width || yspot < 0 || yspot >= height )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Hotspot outside cursor" ) ) ;
      return;
   }

   // ok, all fine
   ::SDL_Cursor *cursor = SDL_CreateCursor( (Uint8 *) data->data(), (Uint8 *) mask->data(),
      width, height, xspot, yspot );

   if ( cursor == 0 )
   {
      throw new SDLError( ErrorParam( FALCON_SDL_ERROR_BASE + 11, __LINE__ )
         .desc( "SDL Create Cursor" )
         .extra( SDL_GetError() ) ) ;
      return;
   }

   Item *cls = vm->findWKI( "SDLCursor" );
   fassert( cls != 0 );
   CoreObject *obj = cls->asClass()->createInstance();
   obj->setUserData( new SDLCursorCarrier( cursor, true ) );
   vm->retval( obj );
}
Example #12
0
/*#
   @method MakeCursor SDL
   @brief Builds an SDL cursor taking an array of strings as input.
   @param aImage a string image.
   @param hotX X coordinate of the hotspot.
   @param hotY Y coordinate of the hotspot.

   As it is generally quite hard to build a cursor writing directly
   its binary data, this helper method takes a set of strings in
   input and convers their visual representation to a valid cursor.

   The strings must have a lenght mutliple of 8, as each of its
   element will fill a bit in the final cursor.

   The hotspot coordinates must be equal to or smaller than the width and the
   height of the cursor. The width of the cursor is determined by the width
   of the strings, while its height is determined the size of the vector.

   The characters in the image are considered as follows:

      - "\@": white opaque pixel
      - ".": black opaque pixel
      - "X": reverse pixel (not always available)
      - " ": transparent pixel.

   In example, the following code generates a small cross with white borders, a reverse
   inner part and a small black shadow:
   @code
      strImage = [
         "         @XXX@          ",
         "         @XXX@          ",
         "         @XXX@          ",
         "         @X.X@          ",
         "  @@@@@@@@X.X@@@@@@@@   ",
         "  XXXXXXXXX.XXXXXXXXX.  ",
         "  XXXXXXXXX.XXXXXXXXX.  ",
         "  @@@@@@@@X.X@@@@@@@@.  ",
         "         @X.X@........  ",
         "         @XXX@.         ",
         "         @XXX@.         ",
         "         @XXX@.         ",
         "          ....          " ]

      SDL.MakeCursor( strImage, 12, 7 ).SetCursor()
   @endcode
*/
FALCON_FUNC sdl_MakeCursor( ::Falcon::VMachine *vm )
{
   Uint8 *data = 0, *mask = 0;
   Item *i_array, *i_xspot, *i_yspot;

   if ( vm->paramCount() < 3 ||
      ( ! (i_array = vm->param(0))->isArray() ) ||
      ( ! (i_xspot = vm->param(1))->isOrdinal() ) ||
      ( ! (i_yspot = vm->param(2))->isOrdinal() )
      )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
         extra( "A,N,N" ) ) ;
      return;
   }

   int xspot = i_xspot->forceInteger();
   int yspot = i_yspot->forceInteger();

   CoreArray *array = i_array->asArray();
   int height = (int) array->length();
   if ( height < 1 )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
         extra( "Array empty" ) ) ;
      return;
   }

   int width = -1;

   for ( int i = 0; i < height; i ++ )
   {
      Item &elem = array->at(i);
      if( ! elem.isString() )
      {
         throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
            extra( "Array contains non-strings" ) ) ;
         return;
      }

      String &row = *elem.asString();
      if( row.length() == 0 || row.length() % 8 != 0 )
      {
         throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
            extra( "Strings not modulo 8" ) ) ;
         if ( data != 0 )
         {
            memFree( data );
            memFree( mask );
         }
         return;
      }

      if( width == -1 )
      {
         // calculate first width
         width = row.length();
         data = (Uint8 *) memAlloc( width / 8 * height );
         mask = (Uint8 *) memAlloc( width / 8 * height );
         memset( data, 0, width / 8 * height );
         memset( mask, 0, width / 8 * height );
      }
      else if ( width != (int) row.length() )
      {
         throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
            extra( "Strings of different sizes" ) ) ;
         return;
      }

      // perform the parsing
      Uint8 *pdata = data + i * width/8;
      Uint8 *pmask = mask + i * width/8;

      for ( int j = 0; j < width; j ++ )
      {
         uint32 chr = row.getCharAt( j );

         switch( chr )
         {
            case ' ': // nothing to do 0/0
               break;

            case 'X': // inverse 1/0
               pdata[ j/8 ] |= 1 << (7 - (j % 8));
               break;

            case '@':  // white 0/1
               pmask[ j/8 ] |= 1 << (7 -(j % 8));
               break;

            case '.': // black 1/1
               pdata[ j/8 ] |= 1 << (7 -(j % 8));
               pmask[ j/8 ] |= 1 << (7 -(j % 8));
               break;

            default:
               // error
               memFree( data );
               memFree( mask );
               throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
                  extra( "Unrecognized char in string" ) );
               return;
         }
      }
   }

   ::SDL_Cursor *cursor = ::SDL_CreateCursor( data, mask, width, height, xspot, yspot );
   Item *cls = vm->findWKI( "SDLCursor" );
   fassert( cls != 0 );
   CoreObject *obj = cls->asClass()->createInstance();
   obj->setUserData( new SDLCursorCarrier( cursor, true ) );

   memFree( data );
   memFree( mask );

   vm->retval( obj );
}