🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.2 KiB

  1. /*
  2. * stdio/physfs abstraction layer 2003-04-02
  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.
  9. */
  10. #ifndef _ABS_FILE_H
  11. #define _ABS_FILE_H
  12. /*
  13. PLEASE NOTE: This license applies to abs-file.h ONLY (to make it clear that
  14. you may embed this wrapper code within commercial software); PhysicsFS itself
  15. is (at the time of writing) released under a different license with
  16. additional restrictions.
  17. Copyright (C) 2002-2003 Adam D. Moss (the "Author"). All Rights Reserved.
  18. Permission is hereby granted, free of charge, to any person obtaining a copy
  19. of this software and associated documentation files (the "Software"), to deal
  20. in the Software without restriction, including without limitation the rights
  21. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. copies of the Software, and to permit persons to whom the Software is fur-
  23. nished to do so, subject to the following conditions:
  24. The above copyright notice and this permission notice shall be included in
  25. all copies or substantial portions of the Software.
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
  28. NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  30. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
  31. NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. Except as contained in this notice, the name of the Author of the
  33. Software shall not be used in advertising or otherwise to promote the sale,
  34. use or other dealings in this Software without prior written authorization
  35. from the Author.
  36. */
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. /*
  40. * API:
  41. *
  42. * Macro/function use like stdio equivalent...
  43. * -------------- ----------------------------
  44. * MY_FILETYPE FILE
  45. * MY_OPEN_FOR_READ fopen(..., "rb")
  46. * MY_READ fread(...)
  47. * MY_CLOSE fclose(...)
  48. * MY_GETC fgetc(...)
  49. * MY_GETS fgets(...)
  50. * MY_ATEOF feof(...)
  51. * MY_TELL ftell(...)
  52. * MY_SEEK fseek(..., SEEK_SET)
  53. * MY_REWIND rewind(...)
  54. * MY_SETBUFFER (not a standard for stdio, does nothing there)
  55. */
  56. /*
  57. * Important DEFINEs:
  58. * It is important to define these consistantly across the various
  59. * compilation modules of your program if you wish to exchange file
  60. * handles between them.
  61. *
  62. * USE_PHYSFS: Define USE_PHYSFS if PhysicsFS is being used; note that if
  63. * you do intend to use PhysicsFS then you will still need to initialize
  64. * PhysicsFS yourself and set up its search-paths.
  65. *
  66. * Optional DEFINEs:
  67. *
  68. * PHYSFS_DEFAULT_READ_BUFFER <bytes>: If set then abs-file.h sets the
  69. * PhysicsFS buffer size to this value whenever you open a file. You
  70. * may over-ride this on a per-filehandle basis by using the
  71. * MY_SETBUFFER() macro (which simply does nothing when not using
  72. * PhysicsFS). If you have not defined this value explicitly then
  73. * abs-file.h will default to the same default buffer size as used by
  74. * stdio if it can be determined, or 8192 bytes otherwise.
  75. */
  76. #ifndef PHYSFS_DEFAULT_READ_BUFFER
  77. #ifdef BUFSIZ
  78. #define PHYSFS_DEFAULT_READ_BUFFER BUFSIZ
  79. #else
  80. #define PHYSFS_DEFAULT_READ_BUFFER 8192
  81. #endif
  82. #endif
  83. #ifdef USE_PHYSFS
  84. #include <physfs.h>
  85. #define MY_FILETYPE PHYSFS_File
  86. #define MY_SETBUFFER(fp,size) PHYSFS_setBuffer(fp,size)
  87. #define MY_READ(p,s,n,fp) PHYSFS_read(fp,p,s,n)
  88. #if PHYSFS_DEFAULT_READ_BUFFER
  89. static MY_FILETYPE* MY_OPEN_FOR_READ(const char *const filename)
  90. {
  91. MY_FILETYPE *const file = PHYSFS_openRead(filename);
  92. if (file) {
  93. MY_SETBUFFER(file, PHYSFS_DEFAULT_READ_BUFFER);
  94. }
  95. return file;
  96. }
  97. #else
  98. #define MY_OPEN_FOR_READ(fn) PHYSFS_openRead(fn)
  99. #endif
  100. static int MY_GETC(MY_FILETYPE *const fp) {
  101. unsigned char c;
  102. /*if (PHYSFS_eof(fp)) {
  103. return EOF;
  104. }
  105. MY_READ(&c, 1, 1, fp);*/
  106. if (MY_READ(&c, 1, 1, fp) != 1) {
  107. return EOF;
  108. }
  109. return c;
  110. }
  111. static char * MY_GETS(char * const str, const int size,
  112. MY_FILETYPE *const fp) {
  113. int i = 0;
  114. int c;
  115. do {
  116. if (i == size-1) {
  117. break;
  118. }
  119. c = MY_GETC(fp);
  120. if (c == EOF) {
  121. break;
  122. }
  123. str[i++] = c;
  124. } while (c != '\0' &&
  125. c != -1 &&
  126. c != '\n');
  127. str[i] = '\0';
  128. if (i == 0) {
  129. return NULL;
  130. }
  131. return str;
  132. }
  133. #define MY_CLOSE(fp) PHYSFS_close(fp)
  134. #define MY_ATEOF(fp) PHYSFS_eof(fp)
  135. #define MY_TELL(fp) PHYSFS_tell(fp)
  136. #define MY_SEEK(fp,o) PHYSFS_seek(fp,o)
  137. #define MY_REWIND(fp) MY_SEEK(fp,0)
  138. #else
  139. #define MY_FILETYPE FILE
  140. #define MY_READ(p,s,n,fp) fread(p,s,n,fp)
  141. #define MY_OPEN_FOR_READ(n) fopen(n, "rb")
  142. #define MY_GETC(fp) fgetc(fp)
  143. #define MY_GETS(str,size,fp) fgets(str,size,fp)
  144. #define MY_CLOSE(fp) fclose(fp)
  145. #define MY_ATEOF(fp) feof(fp)
  146. #define MY_TELL(fp) ftell(fp)
  147. #define MY_SEEK(fp,o) fseek(fp,o, SEEK_SET)
  148. #define MY_REWIND(fp) rewind(fp)
  149. /*static void MY_SETBUFFER(const MY_FILETYPE *const file, const int num) { }*/
  150. #define MY_SETBUFFER(fp,size)
  151. #endif
  152. #endif