extras/physfsrwops.c
changeset 1016 957c97389257
parent 809 116b8fe30371
child 1192 586f4cdee6af
equal deleted inserted replaced
1015:7bd933e66d8d 1016:957c97389257
    38         PHYSFS_sint64 current = PHYSFS_tell(handle);
    38         PHYSFS_sint64 current = PHYSFS_tell(handle);
    39         if (current == -1)
    39         if (current == -1)
    40         {
    40         {
    41             SDL_SetError("Can't find position in file: %s",
    41             SDL_SetError("Can't find position in file: %s",
    42                           PHYSFS_getLastError());
    42                           PHYSFS_getLastError());
    43             return(-1);
    43             return -1;
    44         } /* if */
    44         } /* if */
    45 
    45 
    46         pos = (int) current;
    46         pos = (int) current;
    47         if ( ((PHYSFS_sint64) pos) != current )
    47         if ( ((PHYSFS_sint64) pos) != current )
    48         {
    48         {
    49             SDL_SetError("Can't fit current file position in an int!");
    49             SDL_SetError("Can't fit current file position in an int!");
    50             return(-1);
    50             return -1;
    51         } /* if */
    51         } /* if */
    52 
    52 
    53         if (offset == 0)  /* this is a "tell" call. We're done. */
    53         if (offset == 0)  /* this is a "tell" call. We're done. */
    54             return(pos);
    54             return pos;
    55 
    55 
    56         pos += offset;
    56         pos += offset;
    57     } /* else if */
    57     } /* else if */
    58 
    58 
    59     else if (whence == SEEK_END)
    59     else if (whence == SEEK_END)
    60     {
    60     {
    61         PHYSFS_sint64 len = PHYSFS_fileLength(handle);
    61         PHYSFS_sint64 len = PHYSFS_fileLength(handle);
    62         if (len == -1)
    62         if (len == -1)
    63         {
    63         {
    64             SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
    64             SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
    65             return(-1);
    65             return -1;
    66         } /* if */
    66         } /* if */
    67 
    67 
    68         pos = (int) len;
    68         pos = (int) len;
    69         if ( ((PHYSFS_sint64) pos) != len )
    69         if ( ((PHYSFS_sint64) pos) != len )
    70         {
    70         {
    71             SDL_SetError("Can't fit end-of-file position in an int!");
    71             SDL_SetError("Can't fit end-of-file position in an int!");
    72             return(-1);
    72             return -1;
    73         } /* if */
    73         } /* if */
    74 
    74 
    75         pos += offset;
    75         pos += offset;
    76     } /* else if */
    76     } /* else if */
    77 
    77 
    78     else
    78     else
    79     {
    79     {
    80         SDL_SetError("Invalid 'whence' parameter.");
    80         SDL_SetError("Invalid 'whence' parameter.");
    81         return(-1);
    81         return -1;
    82     } /* else */
    82     } /* else */
    83 
    83 
    84     if ( pos < 0 )
    84     if ( pos < 0 )
    85     {
    85     {
    86         SDL_SetError("Attempt to seek past start of file.");
    86         SDL_SetError("Attempt to seek past start of file.");
    87         return(-1);
    87         return -1;
    88     } /* if */
    88     } /* if */
    89     
    89     
    90     if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
    90     if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
    91     {
    91     {
    92         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
    92         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
    93         return(-1);
    93         return -1;
    94     } /* if */
    94     } /* if */
    95 
    95 
    96     return(pos);
    96     return pos;
    97 } /* physfsrwops_seek */
    97 } /* physfsrwops_seek */
    98 
    98 
    99 
    99 
   100 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
   100 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
   101 {
   101 {
   105     {
   105     {
   106         if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
   106         if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
   107             SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   107             SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   108     } /* if */
   108     } /* if */
   109 
   109 
   110     return((int) rc);
   110     return ((int) rc);
   111 } /* physfsrwops_read */
   111 } /* physfsrwops_read */
   112 
   112 
   113 
   113 
   114 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
   114 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
   115 {
   115 {
   116     PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
   116     PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
   117     PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num);
   117     PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num);
   118     if (rc != num)
   118     if (rc != num)
   119         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   119         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   120 
   120 
   121     return((int) rc);
   121     return ((int) rc);
   122 } /* physfsrwops_write */
   122 } /* physfsrwops_write */
   123 
   123 
   124 
   124 
   125 static int physfsrwops_close(SDL_RWops *rw)
   125 static int physfsrwops_close(SDL_RWops *rw)
   126 {
   126 {
   127     PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
   127     PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
   128     if (!PHYSFS_close(handle))
   128     if (!PHYSFS_close(handle))
   129     {
   129     {
   130         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   130         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   131         return(-1);
   131         return -1;
   132     } /* if */
   132     } /* if */
   133 
   133 
   134     SDL_FreeRW(rw);
   134     SDL_FreeRW(rw);
   135     return(0);
   135     return 0;
   136 } /* physfsrwops_close */
   136 } /* physfsrwops_close */
   137 
   137 
   138 
   138 
   139 static SDL_RWops *create_rwops(PHYSFS_File *handle)
   139 static SDL_RWops *create_rwops(PHYSFS_File *handle)
   140 {
   140 {
   153             retval->close = physfsrwops_close;
   153             retval->close = physfsrwops_close;
   154             retval->hidden.unknown.data1 = handle;
   154             retval->hidden.unknown.data1 = handle;
   155         } /* if */
   155         } /* if */
   156     } /* else */
   156     } /* else */
   157 
   157 
   158     return(retval);
   158     return retval;
   159 } /* create_rwops */
   159 } /* create_rwops */
   160 
   160 
   161 
   161 
   162 SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
   162 SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
   163 {
   163 {
   165     if (handle == NULL)
   165     if (handle == NULL)
   166         SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
   166         SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
   167     else
   167     else
   168         retval = create_rwops(handle);
   168         retval = create_rwops(handle);
   169 
   169 
   170     return(retval);
   170     return retval;
   171 } /* PHYSFSRWOPS_makeRWops */
   171 } /* PHYSFSRWOPS_makeRWops */
   172 
   172 
   173 
   173 
   174 SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
   174 SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
   175 {
   175 {
   176     return(create_rwops(PHYSFS_openRead(fname)));
   176     return create_rwops(PHYSFS_openRead(fname));
   177 } /* PHYSFSRWOPS_openRead */
   177 } /* PHYSFSRWOPS_openRead */
   178 
   178 
   179 
   179 
   180 SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
   180 SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
   181 {
   181 {
   182     return(create_rwops(PHYSFS_openWrite(fname)));
   182     return create_rwops(PHYSFS_openWrite(fname));
   183 } /* PHYSFSRWOPS_openWrite */
   183 } /* PHYSFSRWOPS_openWrite */
   184 
   184 
   185 
   185 
   186 SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
   186 SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
   187 {
   187 {
   188     return(create_rwops(PHYSFS_openAppend(fname)));
   188     return create_rwops(PHYSFS_openAppend(fname));
   189 } /* PHYSFSRWOPS_openAppend */
   189 } /* PHYSFSRWOPS_openAppend */
   190 
   190 
   191 
   191 
   192 /* end of physfsrwops.c ... */
   192 /* end of physfsrwops.c ... */
   193 
   193