extras/physfs_rb/physfs/rb_physfs_file.c
changeset 654 c0ae01de361d
parent 514 f4e89d934fee
equal deleted inserted replaced
653:89d56fcb2b40 654:c0ae01de361d
    15 VALUE classPhysfsFile;
    15 VALUE classPhysfsFile;
    16 
    16 
    17 /*
    17 /*
    18  * construct new PhysicsFS::File object
    18  * construct new PhysicsFS::File object
    19  */
    19  */
    20 VALUE physfs_file_new (PHYSFS_file *file)
    20 VALUE physfs_file_new (PHYSFS_File *file)
    21 {
    21 {
    22     if (file == 0)
    22     if (file == 0)
    23         return Qnil;
    23         return Qnil;
    24 
    24 
    25     return Data_Wrap_Struct (classPhysfsFile, 0, 0, file);
    25     return Data_Wrap_Struct (classPhysfsFile, 0, 0, file);
    31  * Close the file. It's illegal to use the object after its closure.
    31  * Close the file. It's illegal to use the object after its closure.
    32  */
    32  */
    33 VALUE physfs_file_close (VALUE self)
    33 VALUE physfs_file_close (VALUE self)
    34 {
    34 {
    35     int result;
    35     int result;
    36     PHYSFS_file *file;
    36     PHYSFS_File *file;
    37     Data_Get_Struct (self, PHYSFS_file, file);
    37     Data_Get_Struct (self, PHYSFS_File, file);
    38 
    38 
    39     if (file == 0)
    39     if (file == 0)
    40 	return Qfalse;
    40 	return Qfalse;
    41 
    41 
    42     result = PHYSFS_close (file);
    42     result = PHYSFS_close (file);
    57 VALUE physfs_file_read (VALUE self, VALUE objSize, VALUE objCount)
    57 VALUE physfs_file_read (VALUE self, VALUE objSize, VALUE objCount)
    58 {
    58 {
    59     int objRead;
    59     int objRead;
    60     void *buffer;
    60     void *buffer;
    61     VALUE result;
    61     VALUE result;
    62     PHYSFS_file *file;
    62     PHYSFS_File *file;
    63 
    63 
    64     Data_Get_Struct (self, PHYSFS_file, file);
    64     Data_Get_Struct (self, PHYSFS_File, file);
    65     if (file == 0)
    65     if (file == 0)
    66 	return Qnil; //wasted file - no read possible
    66 	return Qnil; //wasted file - no read possible
    67 
    67 
    68     buffer  = malloc (FIX2UINT(objSize) * FIX2UINT(objCount));
    68     buffer  = malloc (FIX2UINT(objSize) * FIX2UINT(objCount));
    69     if (buffer == 0)
    69     if (buffer == 0)
    87  * return nil on failure or number of objects written.
    87  * return nil on failure or number of objects written.
    88  */
    88  */
    89 VALUE physfs_file_write (VALUE self, VALUE buf, VALUE objSize, VALUE objCount)
    89 VALUE physfs_file_write (VALUE self, VALUE buf, VALUE objSize, VALUE objCount)
    90 {
    90 {
    91     int result;
    91     int result;
    92     PHYSFS_file *file;
    92     PHYSFS_File *file;
    93 
    93 
    94     Data_Get_Struct (self, PHYSFS_file, file);
    94     Data_Get_Struct (self, PHYSFS_File, file);
    95     if (file == 0)
    95     if (file == 0)
    96 	return Qnil;
    96 	return Qnil;
    97 
    97 
    98     result = PHYSFS_write (file, STR2CSTR(buf), 
    98     result = PHYSFS_write (file, STR2CSTR(buf), 
    99                            FIX2UINT(objSize), FIX2UINT(objCount));
    99                            FIX2UINT(objSize), FIX2UINT(objCount));
   107  * PhysicsFS::File#eof? 
   107  * PhysicsFS::File#eof? 
   108  */
   108  */
   109 VALUE physfs_file_eof (VALUE self)
   109 VALUE physfs_file_eof (VALUE self)
   110 {
   110 {
   111     int result;
   111     int result;
   112     PHYSFS_file *file;
   112     PHYSFS_File *file;
   113 
   113 
   114     Data_Get_Struct (self, PHYSFS_file, file);
   114     Data_Get_Struct (self, PHYSFS_File, file);
   115     if (file == 0)
   115     if (file == 0)
   116 	return Qnil;
   116 	return Qnil;
   117 
   117 
   118     result = PHYSFS_eof (file);
   118     result = PHYSFS_eof (file);
   119 
   119 
   129  * tells current position in file
   129  * tells current position in file
   130  */
   130  */
   131 VALUE physfs_file_tell (VALUE self)
   131 VALUE physfs_file_tell (VALUE self)
   132 {
   132 {
   133     int result;
   133     int result;
   134     PHYSFS_file *file;
   134     PHYSFS_File *file;
   135 
   135 
   136     Data_Get_Struct (self, PHYSFS_file, file);
   136     Data_Get_Struct (self, PHYSFS_File, file);
   137     if (file == 0)
   137     if (file == 0)
   138 	return Qnil;
   138 	return Qnil;
   139 
   139 
   140     result = PHYSFS_tell (file);
   140     result = PHYSFS_tell (file);
   141 
   141 
   151  * seek to pos in file
   151  * seek to pos in file
   152  */
   152  */
   153 VALUE physfs_file_seek (VALUE self, VALUE pos)
   153 VALUE physfs_file_seek (VALUE self, VALUE pos)
   154 {
   154 {
   155     int result;
   155     int result;
   156     PHYSFS_file *file;
   156     PHYSFS_File *file;
   157 
   157 
   158     Data_Get_Struct (self, PHYSFS_file, file);
   158     Data_Get_Struct (self, PHYSFS_File, file);
   159     if (file == 0)
   159     if (file == 0)
   160 	return Qnil;
   160 	return Qnil;
   161 
   161 
   162     result = PHYSFS_seek (file, FIX2LONG(pos));
   162     result = PHYSFS_seek (file, FIX2LONG(pos));
   163 
   163 
   171  * PhysicsFS::File#length 
   171  * PhysicsFS::File#length 
   172  */
   172  */
   173 VALUE physfs_file_length (VALUE self)
   173 VALUE physfs_file_length (VALUE self)
   174 {
   174 {
   175     int result;
   175     int result;
   176     PHYSFS_file *file;
   176     PHYSFS_File *file;
   177 
   177 
   178     Data_Get_Struct (self, PHYSFS_file, file);
   178     Data_Get_Struct (self, PHYSFS_File, file);
   179     if (file == 0)
   179     if (file == 0)
   180 	return Qnil;
   180 	return Qnil;
   181 
   181 
   182     result = PHYSFS_fileLength (file);
   182     result = PHYSFS_fileLength (file);
   183 
   183 
   194  * File object becomes unusable after that - every operation
   194  * File object becomes unusable after that - every operation
   195  * should be done through new-born RWops object. 
   195  * should be done through new-born RWops object. 
   196  */
   196  */
   197 VALUE physfs_file_to_rwops (VALUE self)
   197 VALUE physfs_file_to_rwops (VALUE self)
   198 {
   198 {
   199     PHYSFS_file *file;
   199     PHYSFS_File *file;
   200     SDL_RWops   *rwops;
   200     SDL_RWops   *rwops;
   201 
   201 
   202     Data_Get_Struct (self, PHYSFS_file, file);
   202     Data_Get_Struct (self, PHYSFS_File, file);
   203     if (file == 0)
   203     if (file == 0)
   204 	return Qnil;
   204 	return Qnil;
   205 
   205 
   206     rwops = PHYSFSRWOPS_makeRWops (file);
   206     rwops = PHYSFSRWOPS_makeRWops (file);
   207     if (rwops == 0)
   207     if (rwops == 0)