open_misc.c 6.0 KB

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