extras/physfsrwops.c
changeset 132 b53fa5093749
parent 123 53305c50c541
child 576 5da65f8e9a50
equal deleted inserted replaced
131:69b093825303 132:b53fa5093749
    32         pos = offset;
    32         pos = offset;
    33     } /* if */
    33     } /* if */
    34 
    34 
    35     else if (whence == SEEK_CUR)
    35     else if (whence == SEEK_CUR)
    36     {
    36     {
    37         int current = PHYSFS_tell(handle);
    37         PHYSFS_sint64 current = PHYSFS_tell(handle);
    38         if (current == -1)
    38         if (current == -1)
    39         {
    39         {
    40             SDL_SetError("Can't find position in file: %s",
    40             SDL_SetError("Can't find position in file: %s",
    41                           PHYSFS_getLastError());
    41                           PHYSFS_getLastError());
    42             return(-1);
    42             return(-1);
    43         } /* if */
    43         } /* if */
    44 
    44 
       
    45         pos = (int) current;
       
    46         if ( ((PHYSFS_sint64) pos) != current )
       
    47         {
       
    48             SDL_SetError("Can't fit current file position in an int!");
       
    49             return(-1);
       
    50         } /* if */
       
    51 
    45         if (offset == 0)  /* this is a "tell" call. We're done. */
    52         if (offset == 0)  /* this is a "tell" call. We're done. */
    46             return(offset);
    53             return(pos);
    47 
    54 
    48         pos = current + offset;
    55         pos += offset;
    49     } /* else if */
    56     } /* else if */
    50 
    57 
    51     else if (whence == SEEK_END)
    58     else if (whence == SEEK_END)
    52     {
    59     {
    53         int len = PHYSFS_fileLength(handle);
    60         PHYSFS_sint64 len = PHYSFS_fileLength(handle);
    54         if (len == -1)
    61         if (len == -1)
    55         {
    62         {
    56             SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
    63             SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
    57             return(-1);
    64             return(-1);
    58         } /* if */
    65         } /* if */
    59 
    66 
    60         pos = len + offset;
    67         pos = (int) len;
       
    68         if ( ((PHYSFS_sint64) pos) != len )
       
    69         {
       
    70             SDL_SetError("Can't fit end-of-file position in an int!");
       
    71             return(-1);
       
    72         } /* if */
       
    73 
       
    74         pos += offset;
    61     } /* else if */
    75     } /* else if */
    62 
    76 
    63     else
    77     else
    64     {
    78     {
    65         SDL_SetError("Invalid 'whence' parameter.");
    79         SDL_SetError("Invalid 'whence' parameter.");
    66         return(-1);
    80         return(-1);
    67     } /* else */
    81     } /* else */
    68 
    82 
    69     if (!PHYSFS_seek(handle, pos))
    83     if ( pos < 0 )
       
    84     {
       
    85         SDL_SetError("Attempt to seek past start of file.");
       
    86         return(-1);
       
    87     } /* if */
       
    88     
       
    89     if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
    70     {
    90     {
    71         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
    91         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
    72         return(-1);
    92         return(-1);
    73     } /* if */
    93     } /* if */
    74 
    94 
    77 
    97 
    78 
    98 
    79 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
    99 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
    80 {
   100 {
    81     PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
   101     PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
    82     int rc = PHYSFS_read(handle, ptr, size, maxnum);
   102     PHYSFS_sint64 rc = PHYSFS_read(handle, ptr, size, maxnum);
    83     if (rc != maxnum)
   103     if (rc != maxnum)
    84     {
   104     {
    85         if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
   105         if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
    86             SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   106             SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
    87     } /* if */
   107     } /* if */
    88 
   108 
    89     return(rc);
   109     return((int) rc);
    90 } /* physfsrwops_read */
   110 } /* physfsrwops_read */
    91 
   111 
    92 
   112 
    93 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
   113 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
    94 {
   114 {
    95     PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
   115     PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
    96     int rc = PHYSFS_write(handle, ptr, size, num);
   116     PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num);
    97     if (rc != num)
   117     if (rc != num)
    98         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
   118         SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
    99 
   119 
   100     return(rc);
   120     return((int) rc);
   101 } /* physfsrwops_write */
   121 } /* physfsrwops_write */
   102 
   122 
   103 
   123 
   104 static int physfsrwops_close(SDL_RWops *rw)
   124 static int physfsrwops_close(SDL_RWops *rw)
   105 {
   125 {