src/file/SDL_rwops.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    42 #ifndef INVALID_SET_FILE_POINTER
    42 #ifndef INVALID_SET_FILE_POINTER
    43 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
    43 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
    44 #endif
    44 #endif
    45 
    45 
    46 static int SDLCALL
    46 static int SDLCALL
    47 win32_file_open (SDL_RWops * context, const char *filename, const char *mode)
    47 win32_file_open(SDL_RWops * context, const char *filename, const char *mode)
    48 {
    48 {
    49 #ifndef _WIN32_WCE
    49 #ifndef _WIN32_WCE
    50     UINT old_error_mode;
    50     UINT old_error_mode;
    51 #endif
    51 #endif
    52     HANDLE h;
    52     HANDLE h;
    64     /* "r+"= reading or writing, file must exist            */
    64     /* "r+"= reading or writing, file must exist            */
    65     /* "a" = writing, append file may not exist             */
    65     /* "a" = writing, append file may not exist             */
    66     /* "a+"= append + read, file may not exist              */
    66     /* "a+"= append + read, file may not exist              */
    67     /* "w+" = read, write, truncate. file may not exist    */
    67     /* "w+" = read, write, truncate. file may not exist    */
    68 
    68 
    69     must_exist = (SDL_strchr (mode, 'r') != NULL) ? OPEN_EXISTING : 0;
    69     must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
    70     truncate = (SDL_strchr (mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
    70     truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
    71     r_right = (SDL_strchr (mode, '+') != NULL
    71     r_right = (SDL_strchr(mode, '+') != NULL
    72                || must_exist) ? GENERIC_READ : 0;
    72                || must_exist) ? GENERIC_READ : 0;
    73     a_mode = (SDL_strchr (mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
    73     a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
    74     w_right = (a_mode || SDL_strchr (mode, '+')
    74     w_right = (a_mode || SDL_strchr(mode, '+')
    75                || truncate) ? GENERIC_WRITE : 0;
    75                || truncate) ? GENERIC_WRITE : 0;
    76 
    76 
    77     if (!r_right && !w_right)   /* inconsistent mode */
    77     if (!r_right && !w_right)   /* inconsistent mode */
    78         return -1;              /* failed (invalid call) */
    78         return -1;              /* failed (invalid call) */
    79 
    79 
    80 #ifdef _WIN32_WCE
    80 #ifdef _WIN32_WCE
    81     {
    81     {
    82         size_t size = SDL_strlen (filename) + 1;
    82         size_t size = SDL_strlen(filename) + 1;
    83         wchar_t *filenameW = SDL_stack_alloc (wchar_t, size);
    83         wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
    84 
    84 
    85         if (MultiByteToWideChar (CP_UTF8, 0, filename, -1, filenameW, size) ==
    85         if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) ==
    86             0) {
    86             0) {
    87             SDL_SetError ("Unable to convert filename to Unicode");
    87             SDL_SetError("Unable to convert filename to Unicode");
    88             SDL_stack_free (filenameW);
    88             SDL_stack_free(filenameW);
    89             return -1;
    89             return -1;
    90         }
    90         }
    91         h = CreateFile (filenameW, (w_right | r_right),
    91         h = CreateFile(filenameW, (w_right | r_right),
    92                         (w_right) ? 0 : FILE_SHARE_READ, NULL,
    92                        (w_right) ? 0 : FILE_SHARE_READ, NULL,
    93                         (must_exist | truncate | a_mode),
    93                        (must_exist | truncate | a_mode),
    94                         FILE_ATTRIBUTE_NORMAL, NULL);
    94                        FILE_ATTRIBUTE_NORMAL, NULL);
    95         SDL_stack_free (filenameW);
    95         SDL_stack_free(filenameW);
    96     }
    96     }
    97 #else
    97 #else
    98     /* Do not open a dialog box if failure */
    98     /* Do not open a dialog box if failure */
    99     old_error_mode =
    99     old_error_mode =
   100         SetErrorMode (SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
   100         SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
   101 
   101 
   102     h = CreateFile (filename, (w_right | r_right),
   102     h = CreateFile(filename, (w_right | r_right),
   103                     (w_right) ? 0 : FILE_SHARE_READ, NULL,
   103                    (w_right) ? 0 : FILE_SHARE_READ, NULL,
   104                     (must_exist | truncate | a_mode), FILE_ATTRIBUTE_NORMAL,
   104                    (must_exist | truncate | a_mode), FILE_ATTRIBUTE_NORMAL,
   105                     NULL);
   105                    NULL);
   106 
   106 
   107     /* restore old behaviour */
   107     /* restore old behaviour */
   108     SetErrorMode (old_error_mode);
   108     SetErrorMode(old_error_mode);
   109 #endif /* _WIN32_WCE */
   109 #endif /* _WIN32_WCE */
   110 
   110 
   111     if (h == INVALID_HANDLE_VALUE) {
   111     if (h == INVALID_HANDLE_VALUE) {
   112         SDL_SetError ("Couldn't open %s", filename);
   112         SDL_SetError("Couldn't open %s", filename);
   113         return -2;              /* failed (CreateFile) */
   113         return -2;              /* failed (CreateFile) */
   114     }
   114     }
   115     context->hidden.win32io.h = h;
   115     context->hidden.win32io.h = h;
   116     context->hidden.win32io.append = a_mode;
   116     context->hidden.win32io.append = a_mode;
   117 
   117 
   118     return 0;                   /* ok */
   118     return 0;                   /* ok */
   119 }
   119 }
   120 static int SDLCALL
   120 static int SDLCALL
   121 win32_file_seek (SDL_RWops * context, int offset, int whence)
   121 win32_file_seek(SDL_RWops * context, int offset, int whence)
   122 {
   122 {
   123     DWORD win32whence;
   123     DWORD win32whence;
   124     int file_pos;
   124     int file_pos;
   125 
   125 
   126     if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
   126     if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
   127         SDL_SetError ("win32_file_seek: invalid context/file not opened");
   127         SDL_SetError("win32_file_seek: invalid context/file not opened");
   128         return -1;
   128         return -1;
   129     }
   129     }
   130 
   130 
   131     switch (whence) {
   131     switch (whence) {
   132     case RW_SEEK_SET:
   132     case RW_SEEK_SET:
   137         break;
   137         break;
   138     case RW_SEEK_END:
   138     case RW_SEEK_END:
   139         win32whence = FILE_END;
   139         win32whence = FILE_END;
   140         break;
   140         break;
   141     default:
   141     default:
   142         SDL_SetError ("win32_file_seek: Unknown value for 'whence'");
   142         SDL_SetError("win32_file_seek: Unknown value for 'whence'");
   143         return -1;
   143         return -1;
   144     }
   144     }
   145 
   145 
   146     file_pos =
   146     file_pos =
   147         SetFilePointer (context->hidden.win32io.h, offset, NULL, win32whence);
   147         SetFilePointer(context->hidden.win32io.h, offset, NULL, win32whence);
   148 
   148 
   149     if (file_pos != INVALID_SET_FILE_POINTER)
   149     if (file_pos != INVALID_SET_FILE_POINTER)
   150         return file_pos;        /* success */
   150         return file_pos;        /* success */
   151 
   151 
   152     SDL_Error (SDL_EFSEEK);
   152     SDL_Error(SDL_EFSEEK);
   153     return -1;                  /* error */
   153     return -1;                  /* error */
   154 }
   154 }
   155 static int SDLCALL
   155 static int SDLCALL
   156 win32_file_read (SDL_RWops * context, void *ptr, int size, int maxnum)
   156 win32_file_read(SDL_RWops * context, void *ptr, int size, int maxnum)
   157 {
   157 {
   158 
   158 
   159     int total_bytes;
   159     int total_bytes;
   160     DWORD byte_read, nread;
   160     DWORD byte_read, nread;
   161 
   161 
   165         || total_bytes <= 0 || !size)
   165         || total_bytes <= 0 || !size)
   166         return 0;
   166         return 0;
   167 
   167 
   168     if (!ReadFile
   168     if (!ReadFile
   169         (context->hidden.win32io.h, ptr, total_bytes, &byte_read, NULL)) {
   169         (context->hidden.win32io.h, ptr, total_bytes, &byte_read, NULL)) {
   170         SDL_Error (SDL_EFREAD);
   170         SDL_Error(SDL_EFREAD);
   171         return 0;
   171         return 0;
   172     }
   172     }
   173     nread = byte_read / size;
   173     nread = byte_read / size;
   174     return nread;
   174     return nread;
   175 }
   175 }
   176 static int SDLCALL
   176 static int SDLCALL
   177 win32_file_write (SDL_RWops * context, const void *ptr, int size, int num)
   177 win32_file_write(SDL_RWops * context, const void *ptr, int size, int num)
   178 {
   178 {
   179 
   179 
   180     int total_bytes;
   180     int total_bytes;
   181     DWORD byte_written, nwritten;
   181     DWORD byte_written, nwritten;
   182 
   182 
   186         || total_bytes <= 0 || !size)
   186         || total_bytes <= 0 || !size)
   187         return 0;
   187         return 0;
   188 
   188 
   189     /* if in append mode, we must go to the EOF before write */
   189     /* if in append mode, we must go to the EOF before write */
   190     if (context->hidden.win32io.append) {
   190     if (context->hidden.win32io.append) {
   191         if (SetFilePointer (context->hidden.win32io.h, 0L, NULL, FILE_END)
   191         if (SetFilePointer(context->hidden.win32io.h, 0L, NULL, FILE_END)
   192             == INVALID_SET_FILE_POINTER) {
   192             == INVALID_SET_FILE_POINTER) {
   193             SDL_Error (SDL_EFWRITE);
   193             SDL_Error(SDL_EFWRITE);
   194             return 0;
   194             return 0;
   195         }
   195         }
   196     }
   196     }
   197 
   197 
   198     if (!WriteFile
   198     if (!WriteFile
   199         (context->hidden.win32io.h, ptr, total_bytes, &byte_written, NULL)) {
   199         (context->hidden.win32io.h, ptr, total_bytes, &byte_written, NULL)) {
   200         SDL_Error (SDL_EFWRITE);
   200         SDL_Error(SDL_EFWRITE);
   201         return 0;
   201         return 0;
   202     }
   202     }
   203 
   203 
   204     nwritten = byte_written / size;
   204     nwritten = byte_written / size;
   205     return nwritten;
   205     return nwritten;
   206 }
   206 }
   207 static int SDLCALL
   207 static int SDLCALL
   208 win32_file_close (SDL_RWops * context)
   208 win32_file_close(SDL_RWops * context)
   209 {
   209 {
   210 
   210 
   211     if (context) {
   211     if (context) {
   212         if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
   212         if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
   213             CloseHandle (context->hidden.win32io.h);
   213             CloseHandle(context->hidden.win32io.h);
   214             context->hidden.win32io.h = INVALID_HANDLE_VALUE;   /* to be sure */
   214             context->hidden.win32io.h = INVALID_HANDLE_VALUE;   /* to be sure */
   215         }
   215         }
   216         SDL_FreeRW (context);
   216         SDL_FreeRW(context);
   217     }
   217     }
   218     return (0);
   218     return (0);
   219 }
   219 }
   220 #endif /* __WIN32__ */
   220 #endif /* __WIN32__ */
   221 
   221 
   222 #ifdef HAVE_STDIO_H
   222 #ifdef HAVE_STDIO_H
   223 
   223 
   224 /* Functions to read/write stdio file pointers */
   224 /* Functions to read/write stdio file pointers */
   225 
   225 
   226 static int SDLCALL
   226 static int SDLCALL
   227 stdio_seek (SDL_RWops * context, int offset, int whence)
   227 stdio_seek(SDL_RWops * context, int offset, int whence)
   228 {
   228 {
   229     if (fseek (context->hidden.stdio.fp, offset, whence) == 0) {
   229     if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
   230         return (ftell (context->hidden.stdio.fp));
   230         return (ftell(context->hidden.stdio.fp));
   231     } else {
   231     } else {
   232         SDL_Error (SDL_EFSEEK);
   232         SDL_Error(SDL_EFSEEK);
   233         return (-1);
   233         return (-1);
   234     }
   234     }
   235 }
   235 }
   236 static int SDLCALL
   236 static int SDLCALL
   237 stdio_read (SDL_RWops * context, void *ptr, int size, int maxnum)
   237 stdio_read(SDL_RWops * context, void *ptr, int size, int maxnum)
   238 {
   238 {
   239     size_t nread;
   239     size_t nread;
   240 
   240 
   241     nread = fread (ptr, size, maxnum, context->hidden.stdio.fp);
   241     nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
   242     if (nread == 0 && ferror (context->hidden.stdio.fp)) {
   242     if (nread == 0 && ferror(context->hidden.stdio.fp)) {
   243         SDL_Error (SDL_EFREAD);
   243         SDL_Error(SDL_EFREAD);
   244     }
   244     }
   245     return (nread);
   245     return (nread);
   246 }
   246 }
   247 static int SDLCALL
   247 static int SDLCALL
   248 stdio_write (SDL_RWops * context, const void *ptr, int size, int num)
   248 stdio_write(SDL_RWops * context, const void *ptr, int size, int num)
   249 {
   249 {
   250     size_t nwrote;
   250     size_t nwrote;
   251 
   251 
   252     nwrote = fwrite (ptr, size, num, context->hidden.stdio.fp);
   252     nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   253     if (nwrote == 0 && ferror (context->hidden.stdio.fp)) {
   253     if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
   254         SDL_Error (SDL_EFWRITE);
   254         SDL_Error(SDL_EFWRITE);
   255     }
   255     }
   256     return (nwrote);
   256     return (nwrote);
   257 }
   257 }
   258 static int SDLCALL
   258 static int SDLCALL
   259 stdio_close (SDL_RWops * context)
   259 stdio_close(SDL_RWops * context)
   260 {
   260 {
   261     if (context) {
   261     if (context) {
   262         if (context->hidden.stdio.autoclose) {
   262         if (context->hidden.stdio.autoclose) {
   263             /* WARNING:  Check the return value here! */
   263             /* WARNING:  Check the return value here! */
   264             fclose (context->hidden.stdio.fp);
   264             fclose(context->hidden.stdio.fp);
   265         }
   265         }
   266         SDL_FreeRW (context);
   266         SDL_FreeRW(context);
   267     }
   267     }
   268     return (0);
   268     return (0);
   269 }
   269 }
   270 #endif /* !HAVE_STDIO_H */
   270 #endif /* !HAVE_STDIO_H */
   271 
   271 
   272 /* Functions to read/write memory pointers */
   272 /* Functions to read/write memory pointers */
   273 
   273 
   274 static int SDLCALL
   274 static int SDLCALL
   275 mem_seek (SDL_RWops * context, int offset, int whence)
   275 mem_seek(SDL_RWops * context, int offset, int whence)
   276 {
   276 {
   277     Uint8 *newpos;
   277     Uint8 *newpos;
   278 
   278 
   279     switch (whence) {
   279     switch (whence) {
   280     case RW_SEEK_SET:
   280     case RW_SEEK_SET:
   285         break;
   285         break;
   286     case RW_SEEK_END:
   286     case RW_SEEK_END:
   287         newpos = context->hidden.mem.stop + offset;
   287         newpos = context->hidden.mem.stop + offset;
   288         break;
   288         break;
   289     default:
   289     default:
   290         SDL_SetError ("Unknown value for 'whence'");
   290         SDL_SetError("Unknown value for 'whence'");
   291         return (-1);
   291         return (-1);
   292     }
   292     }
   293     if (newpos < context->hidden.mem.base) {
   293     if (newpos < context->hidden.mem.base) {
   294         newpos = context->hidden.mem.base;
   294         newpos = context->hidden.mem.base;
   295     }
   295     }
   298     }
   298     }
   299     context->hidden.mem.here = newpos;
   299     context->hidden.mem.here = newpos;
   300     return (context->hidden.mem.here - context->hidden.mem.base);
   300     return (context->hidden.mem.here - context->hidden.mem.base);
   301 }
   301 }
   302 static int SDLCALL
   302 static int SDLCALL
   303 mem_read (SDL_RWops * context, void *ptr, int size, int maxnum)
   303 mem_read(SDL_RWops * context, void *ptr, int size, int maxnum)
   304 {
   304 {
   305     size_t total_bytes;
   305     size_t total_bytes;
   306     size_t mem_available;
   306     size_t mem_available;
   307 
   307 
   308     total_bytes = (maxnum * size);
   308     total_bytes = (maxnum * size);
   314     mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   314     mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   315     if (total_bytes > mem_available) {
   315     if (total_bytes > mem_available) {
   316         total_bytes = mem_available;
   316         total_bytes = mem_available;
   317     }
   317     }
   318 
   318 
   319     SDL_memcpy (ptr, context->hidden.mem.here, total_bytes);
   319     SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   320     context->hidden.mem.here += total_bytes;
   320     context->hidden.mem.here += total_bytes;
   321 
   321 
   322     return (total_bytes / size);
   322     return (total_bytes / size);
   323 }
   323 }
   324 static int SDLCALL
   324 static int SDLCALL
   325 mem_write (SDL_RWops * context, const void *ptr, int size, int num)
   325 mem_write(SDL_RWops * context, const void *ptr, int size, int num)
   326 {
   326 {
   327     if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
   327     if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
   328         num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
   328         num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
   329     }
   329     }
   330     SDL_memcpy (context->hidden.mem.here, ptr, num * size);
   330     SDL_memcpy(context->hidden.mem.here, ptr, num * size);
   331     context->hidden.mem.here += num * size;
   331     context->hidden.mem.here += num * size;
   332     return (num);
   332     return (num);
   333 }
   333 }
   334 static int SDLCALL
   334 static int SDLCALL
   335 mem_writeconst (SDL_RWops * context, const void *ptr, int size, int num)
   335 mem_writeconst(SDL_RWops * context, const void *ptr, int size, int num)
   336 {
   336 {
   337     SDL_SetError ("Can't write to read-only memory");
   337     SDL_SetError("Can't write to read-only memory");
   338     return (-1);
   338     return (-1);
   339 }
   339 }
   340 static int SDLCALL
   340 static int SDLCALL
   341 mem_close (SDL_RWops * context)
   341 mem_close(SDL_RWops * context)
   342 {
   342 {
   343     if (context) {
   343     if (context) {
   344         SDL_FreeRW (context);
   344         SDL_FreeRW(context);
   345     }
   345     }
   346     return (0);
   346     return (0);
   347 }
   347 }
   348 
   348 
   349 
   349 
   353 /*
   353 /*
   354  * translate unix-style slash-separated filename to mac-style colon-separated
   354  * translate unix-style slash-separated filename to mac-style colon-separated
   355  * name; return malloced string
   355  * name; return malloced string
   356  */
   356  */
   357 static char *
   357 static char *
   358 unix_to_mac (const char *file)
   358 unix_to_mac(const char *file)
   359 {
   359 {
   360     int flen = SDL_strlen (file);
   360     int flen = SDL_strlen(file);
   361     char *path = SDL_malloc (flen + 2);
   361     char *path = SDL_malloc(flen + 2);
   362     const char *src = file;
   362     const char *src = file;
   363     char *dst = path;
   363     char *dst = path;
   364     if (*src == '/') {
   364     if (*src == '/') {
   365         /* really depends on filesystem layout, hope for the best */
   365         /* really depends on filesystem layout, hope for the best */
   366         src++;
   366         src++;
   368         /* Check if this is a MacOS path to begin with */
   368         /* Check if this is a MacOS path to begin with */
   369         if (*src != ':')
   369         if (*src != ':')
   370             *dst++ = ':';       /* relative paths begin with ':' */
   370             *dst++ = ':';       /* relative paths begin with ':' */
   371     }
   371     }
   372     while (src < file + flen) {
   372     while (src < file + flen) {
   373         const char *end = SDL_strchr (src, '/');
   373         const char *end = SDL_strchr(src, '/');
   374         int len;
   374         int len;
   375         if (!end)
   375         if (!end)
   376             end = file + flen;  /* last component */
   376             end = file + flen;  /* last component */
   377         len = end - src;
   377         len = end - src;
   378         if (len == 0 || (len == 1 && src[0] == '.')) {
   378         if (len == 0 || (len == 1 && src[0] == '.')) {
   379             /* remove repeated slashes and . */
   379             /* remove repeated slashes and . */
   380         } else {
   380         } else {
   381             if (len == 2 && src[0] == '.' && src[1] == '.') {
   381             if (len == 2 && src[0] == '.' && src[1] == '.') {
   382                 /* replace .. with the empty string */
   382                 /* replace .. with the empty string */
   383             } else {
   383             } else {
   384                 SDL_memcpy (dst, src, len);
   384                 SDL_memcpy(dst, src, len);
   385                 dst += len;
   385                 dst += len;
   386             }
   386             }
   387             if (end < file + flen)
   387             if (end < file + flen)
   388                 *dst++ = ':';
   388                 *dst++ = ':';
   389         }
   389         }
   393     return path;
   393     return path;
   394 }
   394 }
   395 #endif /* __MACOS__ */
   395 #endif /* __MACOS__ */
   396 
   396 
   397 SDL_RWops *
   397 SDL_RWops *
   398 SDL_RWFromFile (const char *file, const char *mode)
   398 SDL_RWFromFile(const char *file, const char *mode)
   399 {
   399 {
   400     SDL_RWops *rwops = NULL;
   400     SDL_RWops *rwops = NULL;
   401 #ifdef HAVE_STDIO_H
   401 #ifdef HAVE_STDIO_H
   402     FILE *fp = NULL;
   402     FILE *fp = NULL;
   403 #endif
   403 #endif
   404     if (!file || !*file || !mode || !*mode) {
   404     if (!file || !*file || !mode || !*mode) {
   405         SDL_SetError ("SDL_RWFromFile(): No file or no mode specified");
   405         SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   406         return NULL;
   406         return NULL;
   407     }
   407     }
   408 #if defined(__WIN32__)
   408 #if defined(__WIN32__)
   409     rwops = SDL_AllocRW ();
   409     rwops = SDL_AllocRW();
   410     if (!rwops)
   410     if (!rwops)
   411         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   411         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   412     rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   412     rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   413     if (win32_file_open (rwops, file, mode)) {
   413     if (win32_file_open(rwops, file, mode)) {
   414         SDL_FreeRW (rwops);
   414         SDL_FreeRW(rwops);
   415         return NULL;
   415         return NULL;
   416     }
   416     }
   417     rwops->seek = win32_file_seek;
   417     rwops->seek = win32_file_seek;
   418     rwops->read = win32_file_read;
   418     rwops->read = win32_file_read;
   419     rwops->write = win32_file_write;
   419     rwops->write = win32_file_write;
   421 
   421 
   422 #elif HAVE_STDIO_H
   422 #elif HAVE_STDIO_H
   423 
   423 
   424 #ifdef __MACOS__
   424 #ifdef __MACOS__
   425     {
   425     {
   426         char *mpath = unix_to_mac (file);
   426         char *mpath = unix_to_mac(file);
   427         fp = fopen (mpath, mode);
   427         fp = fopen(mpath, mode);
   428         SDL_free (mpath);
   428         SDL_free(mpath);
   429     }
   429     }
   430 #else
   430 #else
   431     fp = fopen (file, mode);
   431     fp = fopen(file, mode);
   432 #endif
   432 #endif
   433     if (fp == NULL) {
   433     if (fp == NULL) {
   434         SDL_SetError ("Couldn't open %s", file);
   434         SDL_SetError("Couldn't open %s", file);
   435     } else {
   435     } else {
   436         rwops = SDL_RWFromFP (fp, 1);
   436         rwops = SDL_RWFromFP(fp, 1);
   437     }
   437     }
   438 #else
   438 #else
   439     SDL_SetError ("SDL not compiled with stdio support");
   439     SDL_SetError("SDL not compiled with stdio support");
   440 #endif /* !HAVE_STDIO_H */
   440 #endif /* !HAVE_STDIO_H */
   441 
   441 
   442     return (rwops);
   442     return (rwops);
   443 }
   443 }
   444 
   444 
   445 #ifdef HAVE_STDIO_H
   445 #ifdef HAVE_STDIO_H
   446 SDL_RWops *
   446 SDL_RWops *
   447 SDL_RWFromFP (FILE * fp, int autoclose)
   447 SDL_RWFromFP(FILE * fp, int autoclose)
   448 {
   448 {
   449     SDL_RWops *rwops = NULL;
   449     SDL_RWops *rwops = NULL;
   450 
   450 
   451     rwops = SDL_AllocRW ();
   451     rwops = SDL_AllocRW();
   452     if (rwops != NULL) {
   452     if (rwops != NULL) {
   453         rwops->seek = stdio_seek;
   453         rwops->seek = stdio_seek;
   454         rwops->read = stdio_read;
   454         rwops->read = stdio_read;
   455         rwops->write = stdio_write;
   455         rwops->write = stdio_write;
   456         rwops->close = stdio_close;
   456         rwops->close = stdio_close;
   460     return (rwops);
   460     return (rwops);
   461 }
   461 }
   462 #endif /* HAVE_STDIO_H */
   462 #endif /* HAVE_STDIO_H */
   463 
   463 
   464 SDL_RWops *
   464 SDL_RWops *
   465 SDL_RWFromMem (void *mem, int size)
   465 SDL_RWFromMem(void *mem, int size)
   466 {
   466 {
   467     SDL_RWops *rwops;
   467     SDL_RWops *rwops;
   468 
   468 
   469     rwops = SDL_AllocRW ();
   469     rwops = SDL_AllocRW();
   470     if (rwops != NULL) {
   470     if (rwops != NULL) {
   471         rwops->seek = mem_seek;
   471         rwops->seek = mem_seek;
   472         rwops->read = mem_read;
   472         rwops->read = mem_read;
   473         rwops->write = mem_write;
   473         rwops->write = mem_write;
   474         rwops->close = mem_close;
   474         rwops->close = mem_close;
   478     }
   478     }
   479     return (rwops);
   479     return (rwops);
   480 }
   480 }
   481 
   481 
   482 SDL_RWops *
   482 SDL_RWops *
   483 SDL_RWFromConstMem (const void *mem, int size)
   483 SDL_RWFromConstMem(const void *mem, int size)
   484 {
   484 {
   485     SDL_RWops *rwops;
   485     SDL_RWops *rwops;
   486 
   486 
   487     rwops = SDL_AllocRW ();
   487     rwops = SDL_AllocRW();
   488     if (rwops != NULL) {
   488     if (rwops != NULL) {
   489         rwops->seek = mem_seek;
   489         rwops->seek = mem_seek;
   490         rwops->read = mem_read;
   490         rwops->read = mem_read;
   491         rwops->write = mem_writeconst;
   491         rwops->write = mem_writeconst;
   492         rwops->close = mem_close;
   492         rwops->close = mem_close;
   496     }
   496     }
   497     return (rwops);
   497     return (rwops);
   498 }
   498 }
   499 
   499 
   500 SDL_RWops *
   500 SDL_RWops *
   501 SDL_AllocRW (void)
   501 SDL_AllocRW(void)
   502 {
   502 {
   503     SDL_RWops *area;
   503     SDL_RWops *area;
   504 
   504 
   505     area = (SDL_RWops *) SDL_malloc (sizeof *area);
   505     area = (SDL_RWops *) SDL_malloc(sizeof *area);
   506     if (area == NULL) {
   506     if (area == NULL) {
   507         SDL_OutOfMemory ();
   507         SDL_OutOfMemory();
   508     }
   508     }
   509     return (area);
   509     return (area);
   510 }
   510 }
   511 
   511 
   512 void
   512 void
   513 SDL_FreeRW (SDL_RWops * area)
   513 SDL_FreeRW(SDL_RWops * area)
   514 {
   514 {
   515     SDL_free (area);
   515     SDL_free(area);
   516 }
   516 }
   517 
   517 
   518 /* Functions for dynamically reading and writing endian-specific values */
   518 /* Functions for dynamically reading and writing endian-specific values */
   519 
   519 
   520 Uint16
   520 Uint16
   521 SDL_ReadLE16 (SDL_RWops * src)
   521 SDL_ReadLE16(SDL_RWops * src)
   522 {
   522 {
   523     Uint16 value;
   523     Uint16 value;
   524 
   524 
   525     SDL_RWread (src, &value, (sizeof value), 1);
   525     SDL_RWread(src, &value, (sizeof value), 1);
   526     return (SDL_SwapLE16 (value));
   526     return (SDL_SwapLE16(value));
   527 }
   527 }
   528 
   528 
   529 Uint16
   529 Uint16
   530 SDL_ReadBE16 (SDL_RWops * src)
   530 SDL_ReadBE16(SDL_RWops * src)
   531 {
   531 {
   532     Uint16 value;
   532     Uint16 value;
   533 
   533 
   534     SDL_RWread (src, &value, (sizeof value), 1);
   534     SDL_RWread(src, &value, (sizeof value), 1);
   535     return (SDL_SwapBE16 (value));
   535     return (SDL_SwapBE16(value));
   536 }
   536 }
   537 
   537 
   538 Uint32
   538 Uint32
   539 SDL_ReadLE32 (SDL_RWops * src)
   539 SDL_ReadLE32(SDL_RWops * src)
   540 {
   540 {
   541     Uint32 value;
   541     Uint32 value;
   542 
   542 
   543     SDL_RWread (src, &value, (sizeof value), 1);
   543     SDL_RWread(src, &value, (sizeof value), 1);
   544     return (SDL_SwapLE32 (value));
   544     return (SDL_SwapLE32(value));
   545 }
   545 }
   546 
   546 
   547 Uint32
   547 Uint32
   548 SDL_ReadBE32 (SDL_RWops * src)
   548 SDL_ReadBE32(SDL_RWops * src)
   549 {
   549 {
   550     Uint32 value;
   550     Uint32 value;
   551 
   551 
   552     SDL_RWread (src, &value, (sizeof value), 1);
   552     SDL_RWread(src, &value, (sizeof value), 1);
   553     return (SDL_SwapBE32 (value));
   553     return (SDL_SwapBE32(value));
   554 }
   554 }
   555 
   555 
   556 Uint64
   556 Uint64
   557 SDL_ReadLE64 (SDL_RWops * src)
   557 SDL_ReadLE64(SDL_RWops * src)
   558 {
   558 {
   559     Uint64 value;
   559     Uint64 value;
   560 
   560 
   561     SDL_RWread (src, &value, (sizeof value), 1);
   561     SDL_RWread(src, &value, (sizeof value), 1);
   562     return (SDL_SwapLE64 (value));
   562     return (SDL_SwapLE64(value));
   563 }
   563 }
   564 
   564 
   565 Uint64
   565 Uint64
   566 SDL_ReadBE64 (SDL_RWops * src)
   566 SDL_ReadBE64(SDL_RWops * src)
   567 {
   567 {
   568     Uint64 value;
   568     Uint64 value;
   569 
   569 
   570     SDL_RWread (src, &value, (sizeof value), 1);
   570     SDL_RWread(src, &value, (sizeof value), 1);
   571     return (SDL_SwapBE64 (value));
   571     return (SDL_SwapBE64(value));
   572 }
   572 }
   573 
   573 
   574 int
   574 int
   575 SDL_WriteLE16 (SDL_RWops * dst, Uint16 value)
   575 SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
   576 {
   576 {
   577     value = SDL_SwapLE16 (value);
   577     value = SDL_SwapLE16(value);
   578     return (SDL_RWwrite (dst, &value, (sizeof value), 1));
   578     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   579 }
   579 }
   580 
   580 
   581 int
   581 int
   582 SDL_WriteBE16 (SDL_RWops * dst, Uint16 value)
   582 SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
   583 {
   583 {
   584     value = SDL_SwapBE16 (value);
   584     value = SDL_SwapBE16(value);
   585     return (SDL_RWwrite (dst, &value, (sizeof value), 1));
   585     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   586 }
   586 }
   587 
   587 
   588 int
   588 int
   589 SDL_WriteLE32 (SDL_RWops * dst, Uint32 value)
   589 SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
   590 {
   590 {
   591     value = SDL_SwapLE32 (value);
   591     value = SDL_SwapLE32(value);
   592     return (SDL_RWwrite (dst, &value, (sizeof value), 1));
   592     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   593 }
   593 }
   594 
   594 
   595 int
   595 int
   596 SDL_WriteBE32 (SDL_RWops * dst, Uint32 value)
   596 SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
   597 {
   597 {
   598     value = SDL_SwapBE32 (value);
   598     value = SDL_SwapBE32(value);
   599     return (SDL_RWwrite (dst, &value, (sizeof value), 1));
   599     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   600 }
   600 }
   601 
   601 
   602 int
   602 int
   603 SDL_WriteLE64 (SDL_RWops * dst, Uint64 value)
   603 SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
   604 {
   604 {
   605     value = SDL_SwapLE64 (value);
   605     value = SDL_SwapLE64(value);
   606     return (SDL_RWwrite (dst, &value, (sizeof value), 1));
   606     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   607 }
   607 }
   608 
   608 
   609 int
   609 int
   610 SDL_WriteBE64 (SDL_RWops * dst, Uint64 value)
   610 SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
   611 {
   611 {
   612     value = SDL_SwapBE64 (value);
   612     value = SDL_SwapBE64(value);
   613     return (SDL_RWwrite (dst, &value, (sizeof value), 1));
   613     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   614 }
   614 }
   615 
   615 
   616 /* vi: set ts=4 sw=4 expandtab: */
   616 /* vi: set ts=4 sw=4 expandtab: */