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