open_misc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include "gis_local_proto.h"
  28. static int G__open_misc(const char *dir,
  29. const char *element,
  30. const char *name, const char *mapset, int mode)
  31. {
  32. int fd;
  33. char path[GPATH_MAX];
  34. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  35. G__check_gisinit();
  36. /* READ */
  37. if (mode == 0) {
  38. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  39. if (*mapset && strcmp(xmapset, mapset) != 0) {
  40. G_warning(_("G__open_misc(read): mapset <%s> doesn't match xmapset <%s>"),
  41. mapset, xmapset);
  42. return -1;
  43. }
  44. name = xname;
  45. mapset = xmapset;
  46. }
  47. mapset = G_find_file2_misc(dir, element, name, mapset);
  48. if (!mapset)
  49. return -1;
  50. G_file_name_misc(path, dir, element, name, mapset);
  51. if ((fd = open(path, 0)) < 0)
  52. G_warning("G__open_misc(read): Unable to open '%s': %s",
  53. path, strerror(errno));
  54. return fd;
  55. }
  56. /* WRITE */
  57. if (mode == 1 || mode == 2) {
  58. mapset = G_mapset();
  59. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  60. if (strcmp(xmapset, mapset) != 0) {
  61. G_warning(_("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"),
  62. xmapset, mapset);
  63. return -1;
  64. }
  65. name = xname;
  66. }
  67. if (G_legal_filename(name) == -1)
  68. return -1;
  69. G_file_name_misc(path, dir, element, name, mapset);
  70. if (mode == 1 || access(path, 0) != 0) {
  71. G__make_mapset_element_misc(dir, name);
  72. close(creat(path, 0666));
  73. }
  74. if ((fd = open(path, mode)) < 0)
  75. G_warning("G__open_misc(write): Unable to open '%s': %s",
  76. path, strerror(errno));
  77. return fd;
  78. }
  79. return -1;
  80. }
  81. /*!
  82. * \brief open a new database file
  83. *
  84. * The database file <b>name</b> under the <b>element</b> in the
  85. * current mapset is created and opened for writing (but not reading).
  86. * The UNIX open( ) routine is used to open the file. If the file does not exist,
  87. * -1 is returned. Otherwise the file is positioned at the end of the file and
  88. * the file descriptor from the open( ) is returned.
  89. *
  90. * \param element
  91. * \param name
  92. * \return int
  93. */
  94. int G_open_new_misc(const char *dir, const char *element, const char *name)
  95. {
  96. return G__open_misc(dir, element, name, G_mapset(), 1);
  97. }
  98. /*!
  99. * \brief open a database file for reading
  100. *
  101. * The database file <b>name</b> under the
  102. * <b>element</b> in the specified <b>mapset</b> is opened for reading (but
  103. * not for writing).
  104. * The UNIX open( ) routine is used to open the file. If the file does not exist,
  105. * -1 is returned. Otherwise the file descriptor from the open( ) is returned.
  106. *
  107. * \param element
  108. * \param name
  109. * \param mapset
  110. * \return int
  111. */
  112. int G_open_old_misc(const char *dir, const char *element, const char *name,
  113. const char *mapset)
  114. {
  115. return G__open_misc(dir, element, name, mapset, 0);
  116. }
  117. /*!
  118. * \brief open a database file for update
  119. *
  120. * The database file <b>name</b> under the <b>element</b> in the
  121. * current mapset is opened for reading and writing.
  122. * The UNIX open( ) routine is used to open the file. If the file does not exist,
  123. * -1 is returned. Otherwise the file is positioned at the end of the file and
  124. * the file descriptor from the open( ) is returned.
  125. *
  126. * \param element
  127. * \param name
  128. * \return int
  129. */
  130. int G_open_update_misc(const char *dir, const char *element, const char *name)
  131. {
  132. int fd;
  133. fd = G__open_misc(dir, element, name, G_mapset(), 2);
  134. if (fd >= 0)
  135. lseek(fd, 0L, SEEK_END);
  136. return fd;
  137. }
  138. /*!
  139. * \brief open a new database file
  140. *
  141. * The database file <b>name</b> under the <b>element</b> in the
  142. * current mapset is created and opened for writing (but not reading).
  143. * The UNIX fopen( ) routine, with "w" write mode, is used to open the file. If
  144. * the file does not exist, the NULL pointer is returned. Otherwise the file is
  145. * positioned at the end of the file and the file descriptor from the fopen( ) is
  146. * returned.
  147. *
  148. * \param element
  149. * \param name
  150. * \return FILE *
  151. */
  152. FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name)
  153. {
  154. int fd;
  155. fd = G__open_misc(dir, element, name, G_mapset(), 1);
  156. if (fd < 0)
  157. return (FILE *) 0;
  158. return fdopen(fd, "w");
  159. }
  160. /*!
  161. * \brief open a database file for reading
  162. *
  163. * The database file <b>name</b> under the
  164. * <b>element</b> in the specified <b>mapset</b> is opened for reading (but
  165. * not for writing).
  166. * The UNIX fopen( ) routine, with "r" read mode, is used to open the file. If
  167. * the file does not exist, the NULL pointer is returned. Otherwise the file
  168. * descriptor from the fopen( ) is returned.
  169. *
  170. * \param element
  171. * \param name
  172. * \param mapset
  173. * \return FILE *
  174. */
  175. FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name,
  176. const char *mapset)
  177. {
  178. int fd;
  179. fd = G__open_misc(dir, element, name, mapset, 0);
  180. if (fd < 0)
  181. return (FILE *) 0;
  182. return fdopen(fd, "r");
  183. }
  184. FILE *G_fopen_append_misc(const char *dir, const char *element,
  185. const char *name)
  186. {
  187. int fd;
  188. fd = G__open_misc(dir, element, name, G_mapset(), 2);
  189. if (fd < 0)
  190. return (FILE *) 0;
  191. lseek(fd, 0L, SEEK_END);
  192. return fdopen(fd, "a");
  193. }
  194. FILE *G_fopen_modify_misc(const char *dir, const char *element,
  195. const char *name)
  196. {
  197. int fd;
  198. fd = G__open_misc(dir, element, name, G_mapset(), 2);
  199. if (fd < 0)
  200. return (FILE *) 0;
  201. lseek(fd, 0L, SEEK_END);
  202. return fdopen(fd, "r+");
  203. }