open_misc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /****************************************************************************
  2. *
  3. * MODULE: gis library
  4. * AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
  5. * COPYRIGHT: (C) 2007 Glynn Clements and the GRASS Development Team
  6. *
  7. * NOTE: Based upon open.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. *****************************************************************************/
  20. #include <grass/config.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <grass/gis.h>
  26. #include <grass/glocale.h>
  27. static int G__open_misc(const char *dir,
  28. const char *element,
  29. const char *name, const char *mapset, int mode)
  30. {
  31. int fd;
  32. char path[GPATH_MAX];
  33. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  34. G__check_gisinit();
  35. /* READ */
  36. if (mode == 0) {
  37. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  38. if (*mapset && strcmp(xmapset, mapset) != 0) {
  39. G_warning(_("G__open_misc(read): mapset <%s> doesn't match xmapset <%s>"),
  40. mapset, xmapset);
  41. return -1;
  42. }
  43. name = xname;
  44. mapset = xmapset;
  45. }
  46. mapset = G_find_file2_misc(dir, element, name, mapset);
  47. if (!mapset)
  48. return -1;
  49. G_file_name_misc(path, dir, element, name, mapset);
  50. if ((fd = open(path, 0)) < 0)
  51. G_warning("G__open_misc(read): Unable to open '%s': %s",
  52. path, strerror(errno));
  53. return fd;
  54. }
  55. /* WRITE */
  56. if (mode == 1 || mode == 2) {
  57. mapset = G_mapset();
  58. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  59. if (strcmp(xmapset, mapset) != 0) {
  60. G_warning(_("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"),
  61. xmapset, mapset);
  62. return -1;
  63. }
  64. name = xname;
  65. }
  66. if (G_legal_filename(name) == -1)
  67. return -1;
  68. G_file_name_misc(path, dir, element, name, mapset);
  69. if (mode == 1 || access(path, 0) != 0) {
  70. G__make_mapset_element_misc(dir, name);
  71. close(creat(path, 0666));
  72. }
  73. if ((fd = open(path, mode)) < 0)
  74. G_warning("G__open_misc(write): Unable to open '%s': %s",
  75. path, strerror(errno));
  76. return fd;
  77. }
  78. return -1;
  79. }
  80. /*!
  81. * \brief open a new database file
  82. *
  83. * The database file <b>name</b> under the <b>element</b> in the
  84. * current mapset is created and opened for writing (but not reading).
  85. * The UNIX open( ) routine is used to open the file. If the file does not exist,
  86. * -1 is returned. Otherwise the file is positioned at the end of the file and
  87. * the file descriptor from the open( ) is returned.
  88. *
  89. * \param element
  90. * \param name
  91. * \return int
  92. */
  93. int G_open_new_misc(const char *dir, const char *element, const char *name)
  94. {
  95. return G__open_misc(dir, element, name, G_mapset(), 1);
  96. }
  97. /*!
  98. * \brief open a database file for reading
  99. *
  100. * The database file <b>name</b> under the
  101. * <b>element</b> in the specified <b>mapset</b> is opened for reading (but
  102. * not for writing).
  103. * The UNIX open( ) routine is used to open the file. If the file does not exist,
  104. * -1 is returned. Otherwise the file descriptor from the open( ) is returned.
  105. *
  106. * \param element
  107. * \param name
  108. * \param mapset
  109. * \return int
  110. */
  111. int G_open_old_misc(const char *dir, const char *element, const char *name,
  112. const char *mapset)
  113. {
  114. return G__open_misc(dir, element, name, mapset, 0);
  115. }
  116. /*!
  117. * \brief open a database file for update
  118. *
  119. * The database file <b>name</b> under the <b>element</b> in the
  120. * current mapset is opened for reading and writing.
  121. * The UNIX open( ) routine is used to open the file. If the file does not exist,
  122. * -1 is returned. Otherwise the file is positioned at the end of the file and
  123. * the file descriptor from the open( ) is returned.
  124. *
  125. * \param element
  126. * \param name
  127. * \return int
  128. */
  129. int G_open_update_misc(const char *dir, const char *element, const char *name)
  130. {
  131. int fd;
  132. fd = G__open_misc(dir, element, name, G_mapset(), 2);
  133. if (fd >= 0)
  134. lseek(fd, 0L, SEEK_END);
  135. return fd;
  136. }
  137. /*!
  138. * \brief open a new database file
  139. *
  140. * The database file <b>name</b> under the <b>element</b> in the
  141. * current mapset is created and opened for writing (but not reading).
  142. * The UNIX fopen( ) routine, with "w" write mode, is used to open the file. If
  143. * the file does not exist, the NULL pointer is returned. Otherwise the file is
  144. * positioned at the end of the file and the file descriptor from the fopen( ) is
  145. * returned.
  146. *
  147. * \param element
  148. * \param name
  149. * \return FILE *
  150. */
  151. FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name)
  152. {
  153. int fd;
  154. fd = G__open_misc(dir, element, name, G_mapset(), 1);
  155. if (fd < 0)
  156. return (FILE *) 0;
  157. return fdopen(fd, "w");
  158. }
  159. /*!
  160. * \brief open a database file for reading
  161. *
  162. * The database file <b>name</b> under the
  163. * <b>element</b> in the specified <b>mapset</b> is opened for reading (but
  164. * not for writing).
  165. * The UNIX fopen( ) routine, with "r" read mode, is used to open the file. If
  166. * the file does not exist, the NULL pointer is returned. Otherwise the file
  167. * descriptor from the fopen( ) is returned.
  168. *
  169. * \param element
  170. * \param name
  171. * \param mapset
  172. * \return FILE *
  173. */
  174. FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name,
  175. const char *mapset)
  176. {
  177. int fd;
  178. fd = G__open_misc(dir, element, name, mapset, 0);
  179. if (fd < 0)
  180. return (FILE *) 0;
  181. return fdopen(fd, "r");
  182. }
  183. FILE *G_fopen_append_misc(const char *dir, const char *element,
  184. const char *name)
  185. {
  186. int fd;
  187. fd = G__open_misc(dir, element, name, G_mapset(), 2);
  188. if (fd < 0)
  189. return (FILE *) 0;
  190. lseek(fd, 0L, SEEK_END);
  191. return fdopen(fd, "a");
  192. }
  193. FILE *G_fopen_modify_misc(const char *dir, const char *element,
  194. const char *name)
  195. {
  196. int fd;
  197. fd = G__open_misc(dir, element, name, G_mapset(), 2);
  198. if (fd < 0)
  199. return (FILE *) 0;
  200. lseek(fd, 0L, SEEK_END);
  201. return fdopen(fd, "r+");
  202. }