extras/abs-file.h
changeset 485 53355a5a6096
child 512 5ed9ffa3ad8f
equal deleted inserted replaced
484:607915da2103 485:53355a5a6096
       
     1 /*
       
     2  * stdio/physfs abstraction layer 2002-08-29
       
     3  *
       
     4  * Adam D. Moss <adam@gimp.org> <aspirin@icculus.org>
       
     5  *
       
     6  * These wrapper macros and functions are designed to allow a program
       
     7  * to perform file I/O with identical semantics and syntax regardless
       
     8  * of whether PhysicsFS is being used or not.  Define USE_PHYSFS if
       
     9  * PhysicsFS is being usedl in this case you will still need to initialize
       
    10  * PhysicsFS yourself and set up its search-paths.
       
    11  */
       
    12 #ifndef _ABS_FILE_H
       
    13 #define _ABS_FILE_H
       
    14 /*
       
    15 PLEASE NOTE: This license applies to abs-file.h ONLY (to make it clear that
       
    16 you may embed this wrapper code within commercial software); PhysicsFS itself
       
    17 is (at the time of writing) released under a different license with
       
    18 additional restrictions.
       
    19 
       
    20 Copyright (C) 2002 Adam D. Moss (the "Author").  All Rights Reserved.
       
    21 
       
    22 Permission is hereby granted, free of charge, to any person obtaining a copy
       
    23 of this software and associated documentation files (the "Software"), to deal
       
    24 in the Software without restriction, including without limitation the rights
       
    25 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    26 copies of the Software, and to permit persons to whom the Software is fur-
       
    27 nished to do so, subject to the following conditions:
       
    28 
       
    29 The above copyright notice and this permission notice shall be included in
       
    30 all copies or substantial portions of the Software.
       
    31 
       
    32 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    33 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
       
    34 NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
       
    35 AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
       
    36 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
       
    37 NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    38 
       
    39 Except as contained in this notice, the name of the Author of the
       
    40 Software shall not be used in advertising or otherwise to promote the sale,
       
    41 use or other dealings in this Software without prior written authorization
       
    42 from the Author.
       
    43 */
       
    44 
       
    45 #include <stdlib.h>
       
    46 #include <stdio.h>
       
    47 
       
    48 #ifdef USE_PHYSFS
       
    49 
       
    50 #include <physfs.h>
       
    51 #define MY_FILETYPE PHYSFS_file
       
    52 #define MY_READ(p,s,n,fp) PHYSFS_read(fp,p,s,n)
       
    53 #define MY_OPEN_FOR_READ(fn) PHYSFS_openRead(fn)
       
    54 static int MY_GETC(MY_FILETYPE * fp) {
       
    55   unsigned char c;
       
    56   /*if (PHYSFS_eof(fp)) {
       
    57     return EOF;
       
    58   }
       
    59   MY_READ(&c, 1, 1, fp);*/
       
    60   if (MY_READ(&c, 1, 1, fp) != 1) {
       
    61     return EOF;
       
    62   }
       
    63   return c;
       
    64 }
       
    65 static char * MY_GETS(char * const str, int size, MY_FILETYPE * fp) {
       
    66   int i = 0;
       
    67   int c;
       
    68   do {
       
    69     if (i == size-1) {
       
    70       break;
       
    71     }
       
    72     c = MY_GETC(fp);
       
    73     if (c == EOF) {
       
    74       break;
       
    75     }
       
    76     str[i++] = c;
       
    77   } while (c != '\0' &&
       
    78 	   c != -1 &&
       
    79 	   c != '\n');
       
    80   str[i] = '\0';
       
    81   if (i == 0) {
       
    82     return NULL;
       
    83   }
       
    84   return str;
       
    85 }
       
    86 #define MY_CLOSE(fp) PHYSFS_close(fp)
       
    87 #define MY_ATEOF(fp) PHYSFS_eof(fp)
       
    88 #define MY_REWIND(fp) PHYSFS_seek(fp,0)
       
    89 
       
    90 #else
       
    91 
       
    92 #define MY_FILETYPE FILE
       
    93 #define MY_READ(p,s,n,fp) fread(p,s,n,fp)
       
    94 #define MY_OPEN_FOR_READ(n) fopen(n, "rb")
       
    95 #define MY_GETC(fp) fgetc(fp)
       
    96 #define MY_GETS(str,size,fp) fgets(str,size,fp)
       
    97 #define MY_CLOSE(fp) fclose(fp)
       
    98 #define MY_ATEOF(fp) feof(fp)
       
    99 #define MY_REWIND(fp) rewind(fp)
       
   100 
       
   101 #endif
       
   102 
       
   103 #endif