rwfile.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Hey EMACS -*- linux-c -*- */
  2. /* $Id: rwfile.c 3227 2007-02-26 19:40:27Z roms $ */
  3. /* libtifiles - file format library, a part of the TiLP project
  4. * Copyright (C) 1999-2005 Romain Liévin
  5. * Copyright (C) 2007 Kevin Kofler
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. /*
  22. This unit contains some miscellaneous but useful functions.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdint.h>
  28. #include <sys/stat.h>
  29. #include "tifiles.h"
  30. #include "integers.h"
  31. #define tifiles_error(x...) (fprintf(stderr, x),fprintf(stderr, "\n"))
  32. /********************/
  33. /* Read/Write bytes */
  34. /********************/
  35. /*
  36. Read a block of 'n' bytes from a file
  37. - s [out]: a buffer for storing the data
  38. - f [in]: a file descriptor
  39. - [out]: -1 if error, 0 otherwise.
  40. */
  41. int fread_n_bytes(FILE * f, int n, uint8_t *s)
  42. {
  43. int i;
  44. if (s == NULL)
  45. for (i = 0; i < n; i++)
  46. fgetc(f);
  47. else
  48. if(fread(s, 1, n, f) < (size_t)n)
  49. return -1;
  50. return 0;
  51. }
  52. /*
  53. Write a string of 'n' chars max (NULL padded) to a file
  54. - s [in]: a string
  55. - f [in]: a file descriptor
  56. - [out]: -1 if error, 0 otherwise.
  57. */
  58. int fwrite_n_bytes(FILE * f, int n, const uint8_t *s)
  59. {
  60. if(fwrite(s, 1, n, f) < (size_t)n)
  61. return -1;
  62. return 0;
  63. }
  64. /**********************/
  65. /* Read/Write strings */
  66. /**********************/
  67. /*
  68. Read a string of 'n' chars max from a file
  69. - s [out]: a buffer for storing the string
  70. - f [in]: a file descriptor
  71. - [out]: -1 if error, 0 otherwise.
  72. */
  73. int fread_n_chars(FILE * f, int n, char *s)
  74. {
  75. int i;
  76. if(fread_n_bytes(f, n, (uint8_t *)s) < 0)
  77. return -1;
  78. if(s != NULL)
  79. {
  80. // set NULL terminator
  81. s[n] = '\0';
  82. // and set unused bytes to 0
  83. for(i = strlen(s); i < n; i++)
  84. s[i] = '\0';
  85. }
  86. return 0;
  87. }
  88. /*
  89. Write a string of 'n' chars max (NULL padded) to a file
  90. - s [in]: a string
  91. - f [in]: a file descriptor
  92. - [out]: -1 if error, 0 otherwise.
  93. */
  94. int fwrite_n_chars(FILE * f, int n, const char *s)
  95. {
  96. int i;
  97. int l = n;
  98. l = strlen(s);
  99. if (l > n)
  100. {
  101. tifiles_error("string passed in 'write_string8' is too long (>n chars).\n");
  102. abort();
  103. }
  104. for (i = 0; i < l; i++)
  105. if(fputc(s[i], f) == EOF)
  106. return -1;
  107. for (i = l; i < n; i++)
  108. if(fputc(0x00, f) == EOF)
  109. return -1;
  110. return 0;
  111. }
  112. /*
  113. Write a string of 'n' chars max (SPC padded) to a file
  114. - s [in]: a string
  115. - f [in]: a file descriptor
  116. - [out]: -1 if error, 0 otherwise.
  117. */
  118. int fwrite_n_chars2(FILE * f, int n, const char *s)
  119. {
  120. int i;
  121. int l = n;
  122. l = strlen(s);
  123. if (l > n)
  124. {
  125. tifiles_error("string passed in 'write_string8' is too long (>n chars).\n");
  126. abort();
  127. }
  128. for (i = 0; i < l; i++)
  129. if(fputc(s[i], f) == EOF)
  130. return -1;
  131. for (i = l; i < n; i++)
  132. if(fputc(0x20, f) == EOF)
  133. return -1;
  134. return 0;
  135. }
  136. int fread_8_chars(FILE * f, char *s)
  137. {
  138. return fread_n_chars(f, 8, s);
  139. }
  140. int fwrite_8_chars(FILE * f, const char *s)
  141. {
  142. return fwrite_n_chars(f, 8, s);
  143. }
  144. int fskip(FILE * f, int n)
  145. {
  146. return fseek(f, n, SEEK_CUR);
  147. }
  148. /***************************/
  149. /* Read byte/word/longword */
  150. /***************************/
  151. int fread_byte(FILE * f, uint8_t * data)
  152. {
  153. if (data != NULL)
  154. return (fread((void *) data, sizeof(uint8_t), 1, f) < 1) ? -1 : 0;
  155. else
  156. return fskip(f, 1);
  157. return 0;
  158. }
  159. int fread_word(FILE * f, uint16_t * data)
  160. {
  161. int ret = 0;
  162. if (data != NULL)
  163. {
  164. ret = (fread((void *) data, sizeof(uint16_t), 1, f) < 1) ? -1 : 0;
  165. *data = ReadHI2(*(HI2*)data);
  166. }
  167. else
  168. ret = fskip(f, 2);
  169. return ret;
  170. }
  171. int fread_long(FILE * f, uint32_t * data)
  172. {
  173. int ret = 0;
  174. if (data != NULL)
  175. {
  176. ret = (fread((void *) data, sizeof(uint32_t), 1, f) < 1) ? -1 : 0;
  177. *data = ReadHI4(*(HI4*)data);
  178. }
  179. else
  180. ret = fskip(f, 4);
  181. return ret;
  182. }
  183. /****************************/
  184. /* Write byte/word/longword */
  185. /****************************/
  186. int fwrite_byte(FILE * f, uint8_t data)
  187. {
  188. return (fwrite(&data, sizeof(uint8_t), 1, f) < 1) ? -1 : 0;
  189. }
  190. int fwrite_word(FILE * f, uint16_t data)
  191. {
  192. WriteHI2(*(HI2*)&data, data);
  193. return (fwrite(&data, sizeof(uint16_t), 1, f) < 1) ? -1 : 0;
  194. }
  195. int fwrite_long(FILE * f, uint32_t data)
  196. {
  197. WriteHI4(*(HI4*)&data, data);
  198. return (fwrite(&data, sizeof(uint32_t), 1, f) < 1) ? -1 : 0;
  199. }