file.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*!
  2. \file diglib/file.c
  3. \brief Vector library - file management (lower level functions)
  4. Lower level functions for reading/writing/manipulating vectors.
  5. Note: seems that the time is almost the same for both cases:
  6. - reading from file
  7. - load whole file to memory and read from memory
  8. (C) 2001-2009 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. \author Original author CERL, probably Dave Gerdes
  12. \author Update to GRASS 5.7 Radim Blazek
  13. */
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <grass/vector.h>
  20. #include <grass/glocale.h>
  21. /*!
  22. \brief Get struct gvfile position.
  23. \param file pointer to struct gvfile structure
  24. \return current file position
  25. */
  26. off_t dig_ftell(struct gvfile *file)
  27. {
  28. if (file->loaded) /* using memory */
  29. return (file->current - file->start);
  30. return (G_ftell(file->file));
  31. }
  32. /*!
  33. \brief Set struct gvfile position.
  34. Start positions:
  35. - SEEK_SET (start)
  36. - SEEK_CUR (current position)
  37. - SEEK_END (end)
  38. \param file pointer to struct gvfile structure
  39. \param offset offset position
  40. \param whence start position
  41. \return 0 OK
  42. \return -1 error
  43. */
  44. int dig_fseek(struct gvfile * file, off_t offset, int whence)
  45. {
  46. if (file->loaded) { /* using memory */
  47. switch (whence) {
  48. case SEEK_SET:
  49. file->current = file->start + offset;
  50. break;
  51. case SEEK_CUR:
  52. file->current += offset;
  53. break;
  54. case SEEK_END:
  55. file->current = file->start + file->size + offset;
  56. break;
  57. }
  58. return 0;
  59. }
  60. G_fseek(file->file, offset, whence);
  61. return 0;
  62. }
  63. /*!
  64. \brief Rewind file position.
  65. \param file pointer to gvfile structure
  66. */
  67. void dig_rewind(struct gvfile * file)
  68. {
  69. if (file->loaded) { /* using memory */
  70. file->current = file->start;
  71. }
  72. else {
  73. rewind(file->file);
  74. }
  75. }
  76. /*!
  77. \brief Flush struct gvfile.
  78. \param file pointer to struct gvfile structure
  79. \return 0
  80. */
  81. int dig_fflush(struct gvfile * file)
  82. {
  83. if (file->loaded) { /* using memory */
  84. return 0;
  85. }
  86. else {
  87. return (fflush(file->file));
  88. }
  89. }
  90. /*!
  91. \brief Read struct gvfile.
  92. \param[out] ptr data buffer
  93. \param size buffer size
  94. \param nmemb number of members
  95. \param file pointer to struct gvfile structure
  96. \return number of read members
  97. */
  98. size_t dig_fread(void *ptr, size_t size, size_t nmemb, struct gvfile *file)
  99. {
  100. long tot;
  101. size_t cnt;
  102. if (file->loaded) { /* using memory */
  103. if (file->current >= file->end) { /* EOF */
  104. return 0;
  105. }
  106. tot = size * nmemb;
  107. cnt = nmemb;
  108. if (file->current + tot > file->end) {
  109. tot = file->end - file->current;
  110. cnt = (int)tot / size;
  111. }
  112. memcpy(ptr, file->current, tot);
  113. file->current += tot;
  114. return (cnt);
  115. }
  116. return (fread(ptr, size, nmemb, file->file));
  117. }
  118. /*!
  119. \brief Write struct gvfile.
  120. \param ptr data buffer
  121. \param size buffer size
  122. \param nmemb number of members
  123. \param[out] file pointer to struct gvfile structure
  124. \return number of items written
  125. */
  126. size_t dig_fwrite(const void *ptr, size_t size, size_t nmemb, struct gvfile *file)
  127. {
  128. if (file->loaded) { /* using memory */
  129. G_fatal_error(_("Writing to file loaded to memory not supported"));
  130. }
  131. return fwrite(ptr, size, nmemb, file->file);
  132. }
  133. /*!
  134. \brief Initialize gvfile strcuture
  135. \param[in,out] file pointer to gvfile structure
  136. */
  137. void dig_file_init(struct gvfile *file)
  138. {
  139. G_zero(file, sizeof(struct gvfile));
  140. }
  141. /*!
  142. \brief Load opened struct gvfile to memory.
  143. Warning: position in file is set to the beginning.
  144. \param file pointer to struct gvfile structure
  145. \return 1 loaded
  146. \return 0 not loaded
  147. \return -1 error
  148. */
  149. /* unused, coor file is never loaded to memory. Remove ? MM 2010 */
  150. int dig_file_load(struct gvfile * file)
  151. {
  152. int ret, mode, load;
  153. const char *cmode;
  154. size_t size;
  155. struct stat sbuf;
  156. G_debug(2, "dig_file_load ()");
  157. if (file->file == NULL) {
  158. G_warning(_("Unable to load file to memory, file not open"));
  159. return -1;
  160. }
  161. /* Get mode */
  162. mode = GV_MEMORY_NEVER;
  163. cmode = G_getenv_nofatal("GV_MEMORY");
  164. if (cmode != NULL) {
  165. if (G_strcasecmp(cmode, "ALWAYS") == 0)
  166. mode = GV_MEMORY_ALWAYS;
  167. else if (G_strcasecmp(cmode, "NEVER") == 0)
  168. mode = GV_MEMORY_NEVER;
  169. else if (G_strcasecmp(cmode, "AUTO") == 0)
  170. mode = GV_MEMORY_AUTO;
  171. else
  172. G_warning(_("Vector memory mode not supported, using 'AUTO'"));
  173. }
  174. G_debug(2, " requested mode = %d", mode);
  175. fstat(fileno(file->file), &sbuf);
  176. size = sbuf.st_size;
  177. G_debug(2, " size = %lu", (long unsigned int) size);
  178. /* Decide if the file should be loaded */
  179. /* TODO: I don't know how to get size of free memory (portability) to decide if load or not for auto */
  180. if (mode == GV_MEMORY_AUTO)
  181. mode = GV_MEMORY_NEVER;
  182. if (mode == GV_MEMORY_ALWAYS)
  183. load = 1;
  184. else
  185. load = 0;
  186. if (load) {
  187. file->start = G_malloc(size);
  188. if (file->start == NULL)
  189. return -1;
  190. G_fseek(file->file, 0L, 0);
  191. ret = fread(file->start, size, 1, file->file); /* Better to read in smaller portions? */
  192. G_fseek(file->file, 0L, 0); /* reset to the beginning */
  193. if (ret <= 0) {
  194. G_free(file->start);
  195. return -1;
  196. }
  197. file->alloc = size;
  198. file->size = size;
  199. file->current = file->start;
  200. file->end = file->start + size;
  201. file->loaded = 1;
  202. G_debug(2, " file was loaded to the memory");
  203. return 1;
  204. }
  205. else {
  206. G_debug(2, " file was not loaded to the memory");
  207. }
  208. return 0;
  209. }
  210. /*!
  211. \brief Free struct gvfile.
  212. \param file pointer to struct gvfile structure
  213. */
  214. void dig_file_free(struct gvfile * file)
  215. {
  216. if (file->loaded) {
  217. G_free(file->start);
  218. file->loaded = 0;
  219. file->alloc = 0;
  220. }
  221. }