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.

265 lines
4.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**
  2. * @file wrappers.h
  3. * @brief File manipulation wrappers
  4. * @license GNU General Public License 3.0 - see LICENSE.txt
  5. */
  6. #ifndef WRAPPERS_H
  7. #define WRAPPERS_H
  8. #include "platform.h"
  9. #include <errno.h>
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. /**
  14. * <!-- Fopen() -->
  15. *
  16. * @brief TODO
  17. *
  18. * @param[in] filename const char*
  19. * @param[in] mode const char*
  20. *
  21. */
  22. static inline FILE * Fopen(const char *filename, const char *mode)
  23. {
  24. FILE * f;
  25. do {
  26. f = fopen(filename, mode);
  27. } while (f == NULL && errno == EINTR);
  28. return f;
  29. }
  30. /**
  31. * <!-- Fdopen() -->
  32. *
  33. * @brief TODO
  34. *
  35. * @param[in] fildes int
  36. * @param[in] mode const char*
  37. *
  38. */
  39. static inline FILE * Fdopen(int fildes, const char *mode)
  40. {
  41. FILE * f;
  42. do {
  43. f = fdopen(fildes, mode);
  44. } while (f == NULL && errno == EINTR);
  45. return f;
  46. }
  47. /**
  48. * <!-- Fgets() -->
  49. *
  50. * @brief TODO
  51. *
  52. * @param[in] str char*
  53. * @param[in] size int
  54. * @param[in] stream FILE*
  55. *
  56. */
  57. static inline char *Fgets(char * str, int size, FILE * stream)
  58. {
  59. char * s;
  60. do {
  61. s = fgets(str, size, stream);
  62. } while (s == NULL && errno == EINTR);
  63. return s;
  64. }
  65. /**
  66. * <!-- Fputs() -->
  67. *
  68. * @brief TODO
  69. *
  70. * @param[in] str const char*
  71. * @param[in] stream FILE*
  72. *
  73. */
  74. static inline int Fputs(const char *str, FILE * stream)
  75. {
  76. int r;
  77. do {
  78. r = fputs(str, stream);
  79. } while (r == EOF && errno == EINTR);
  80. return r;
  81. }
  82. /**
  83. * <!-- Fclose() -->
  84. *
  85. * @brief TODO
  86. *
  87. * @param[in] stream FILE*
  88. *
  89. */
  90. static inline int Fclose(FILE *stream)
  91. {
  92. int r;
  93. do {
  94. r = fclose(stream);
  95. } while (r == EOF && errno == EINTR);
  96. return r;
  97. }
  98. /**
  99. * <!-- Freopen() -->
  100. *
  101. * @brief TODO
  102. *
  103. * @param[in] filename const char*
  104. * @param[in] mode const char*
  105. * @param[in] stream FILE*
  106. *
  107. */
  108. static inline FILE * Freopen(
  109. const char *filename,
  110. const char *mode,
  111. FILE * stream
  112. )
  113. {
  114. FILE * f;
  115. do {
  116. f = freopen(filename, mode, stream);
  117. } while (f == NULL && errno == EINTR);
  118. return f;
  119. }
  120. static inline int Fprintf(FILE * stream, const char * format, ...)
  121. {
  122. int n;
  123. va_list arglist;
  124. va_start(arglist, format);
  125. do {
  126. n = vfprintf(stream, format, arglist);
  127. } while (n < 0 && errno == EINTR);
  128. va_end( arglist );
  129. return n;
  130. }
  131. /**
  132. * <!-- Fwrite() -->
  133. *
  134. * @brief TODO
  135. *
  136. * @param[in] ptr constvoid*
  137. * @param[in] size size_t
  138. * @param[in] nitems size_t
  139. * @param[in] stream FILE*
  140. *
  141. */
  142. static inline size_t Fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
  143. {
  144. size_t r = 0;
  145. do {
  146. clearerr(stream);
  147. size_t n = fwrite((char *) ptr + r, size, nitems, stream);
  148. nitems -= n;
  149. r += n * size;
  150. } while (nitems && ferror(stream) == EINTR);
  151. return r;
  152. }
  153. /**
  154. * <!-- Fread() -->
  155. *
  156. * @brief TODO
  157. *
  158. * @param[in] ptr void*
  159. * @param[in] size size_t
  160. * @param[in] nitems size_t
  161. * @param[in] stream FILE*
  162. *
  163. */
  164. static inline size_t Fread(void *ptr, size_t size, size_t nitems, FILE *stream)
  165. {
  166. size_t r = 0;
  167. do {
  168. clearerr(stream);
  169. size_t n = fread((char *) ptr + r, size, nitems, stream);
  170. nitems -= n;
  171. r += n * size;
  172. } while (!feof(stream) && nitems && ferror(stream) == EINTR);
  173. return r;
  174. }
  175. /**
  176. * <!-- Fflush() -->
  177. *
  178. * @brief TODO
  179. *
  180. * @param[in] stream FILE*
  181. *
  182. */
  183. static inline int Fflush(FILE *stream)
  184. {
  185. int r;
  186. do {
  187. r = fflush(stream);
  188. } while (r == -1 && errno == EINTR);
  189. return r;
  190. }
  191. /**
  192. * <!-- Mkstemp() -->
  193. *
  194. * @brief TODO
  195. *
  196. * @param[in] template char*
  197. *
  198. */
  199. static inline int Mkstemp(char *template)
  200. {
  201. int fd;
  202. do {
  203. fd = mkstemp(template);
  204. } while (fd == -1 && errno == EINTR);
  205. return fd;
  206. }
  207. /**
  208. * <!-- Close() -->
  209. *
  210. * @brief TODO
  211. *
  212. * @param[in] fildes int
  213. *
  214. */
  215. static inline int Close(int fildes)
  216. {
  217. int r;
  218. do {
  219. r = close(fildes);
  220. } while (r == -1 && errno == EINTR);
  221. return r;
  222. }
  223. #endif