find.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**************************************************************
  2. * I_find_group (group)
  3. *
  4. * Find the a group in the current mapset
  5. **************************************************************/
  6. #include <grass/imagery.h>
  7. #include <grass/gis.h>
  8. /*!
  9. * \brief does group exist?
  10. *
  11. * Returns 1 if the
  12. * specified <b>group</b> exists in the current mapset; 0 otherwise.
  13. * Use I_find_group2 to search in all or a specific mapset.
  14. *
  15. * \param group
  16. * \return int 1 if group was found, 0 otherwise
  17. */
  18. int I_find_group(const char *group)
  19. {
  20. if (group == NULL || *group == 0)
  21. return 0;
  22. return G_find_file2("group", group, G_mapset()) != NULL;
  23. }
  24. /*!
  25. * \brief Does the group exists?
  26. *
  27. * Finds a group in specified mapset or any mapset if mapset is not set.
  28. * Internally uses G_find_file2().
  29. *
  30. * \param group
  31. * \param mapset
  32. * \return int 1 if group was found, 0 otherwise
  33. */
  34. int I_find_group2(const char *group, const char *mapset)
  35. {
  36. return G_find_file2("group", group, mapset) != NULL;
  37. }
  38. /*!
  39. * \brief Searches for a group file in the current mapset
  40. *
  41. * \param group
  42. * \param file
  43. * \return int 1 if file was found, 0 otherwise
  44. */
  45. int I_find_group_file(const char *group, const char *file)
  46. {
  47. if (!I_find_group(group))
  48. return 0;
  49. if (file == NULL || *file == 0)
  50. return 0;
  51. return G_find_file2_misc("group", file, group, G_mapset()) != NULL;
  52. }
  53. /*!
  54. * \brief Searches for a group file in the specified mapset
  55. *
  56. * \param group
  57. * \param file
  58. * \return int 1 if file was found, 0 otherwise
  59. */
  60. int I_find_group_file2(const char *group, const char *mapset,
  61. const char *file)
  62. {
  63. if (!I_find_group2(group, mapset))
  64. return 0;
  65. if (file == NULL || *file == 0)
  66. return 0;
  67. return G_find_file2_misc("group", file, group, mapset) != NULL;
  68. }
  69. /*!
  70. * \brief Searches for a subgroup in the current mapset
  71. *
  72. * \param group
  73. * \param subgroup
  74. * \return int 1 if subgroup was found, 0 otherwise
  75. */
  76. int I_find_subgroup(const char *group, const char *subgroup)
  77. {
  78. char element[GNAME_MAX];
  79. if (!I_find_group(group))
  80. return 0;
  81. if (subgroup == NULL || *subgroup == 0)
  82. return 0;
  83. sprintf(element, "subgroup%c%s", HOST_DIRSEP, subgroup);
  84. G_debug(5, "I_find_subgroup() element: %s", element);
  85. return G_find_file2_misc("group", element, group, G_mapset()) != NULL;
  86. }
  87. /*!
  88. * \brief Searches for a subgroup in specified mapset or any mapset if mapset is not set
  89. *
  90. * \param group
  91. * \param subgroup
  92. * \param mapset
  93. * \return int 1 if subrgoup was found, 0 otherwise
  94. */
  95. int I_find_subgroup2(const char *group, const char *subgroup,
  96. const char *mapset)
  97. {
  98. char element[GNAME_MAX];
  99. if (!I_find_group2(group, mapset))
  100. return 0;
  101. if (subgroup == NULL || *subgroup == 0)
  102. return 0;
  103. sprintf(element, "subgroup%c%s", HOST_DIRSEP, subgroup);
  104. G_debug(5, "I_find_subgroup2() element: %s", element);
  105. return G_find_file2_misc("group", element, group, mapset) != NULL;
  106. }
  107. /*!
  108. * \brief Searches for a subgroup file in the current mapset
  109. *
  110. * \param group
  111. * \param subgroup
  112. * \param file
  113. * \return int 1 if file was found, 0 otherwise
  114. */
  115. int I_find_subgroup_file(const char *group, const char *subgroup,
  116. const char *file)
  117. {
  118. char element[GNAME_MAX * 2];
  119. if (!I_find_group(group))
  120. return 0;
  121. if (subgroup == NULL || *subgroup == 0)
  122. return 0;
  123. if (file == NULL || *file == 0)
  124. return 0;
  125. sprintf(element, "subgroup%c%s%c%s", HOST_DIRSEP, subgroup, HOST_DIRSEP,
  126. file);
  127. G_debug(5, "I_find_subgroup_file() element: %s", element);
  128. return G_find_file2_misc("group", element, group, G_mapset()) != NULL;
  129. }
  130. /*!
  131. * \brief Searches for a subgroup file in the specified mapset
  132. *
  133. * \param group
  134. * \param subgroup
  135. * \param mapset
  136. * \param file
  137. * \return int 1 if file was found, 0 otherwise
  138. */
  139. int I_find_subgroup_file2(const char *group, const char *subgroup,
  140. const char *mapset, const char *file)
  141. {
  142. char element[GNAME_MAX * 2];
  143. if (!I_find_group2(group, mapset))
  144. return 0;
  145. if (subgroup == NULL || *subgroup == 0)
  146. return 0;
  147. if (file == NULL || *file == 0)
  148. return 0;
  149. sprintf(element, "subgroup%c%s%c%s", HOST_DIRSEP, subgroup, HOST_DIRSEP,
  150. file);
  151. G_debug(5, "I_find_subgroup_file2() element: %s", element);
  152. return G_find_file2_misc("group", element, group, mapset) != NULL;
  153. }
  154. /*!
  155. * \brief Find mapset containing signature file
  156. *
  157. * Looks for the signature <i>name</i> of type <i>type</i>
  158. * in the database. The <i>mapset</i> parameter can either be
  159. * the empty string "", which means search all the mapsets in
  160. * the users current mapset search path
  161. * (see \ref Mapset_Search_Path for more details about the search
  162. * path) or it can be a specific mapset name, which means look for the
  163. * signature only in this one mapset (for example, in the current
  164. * mapset). If found, the mapset where the signature lives is
  165. * returned. If not found, the NULL pointer is returned.
  166. *
  167. * Note: If the user specifies a fully qualified signature name which
  168. * exists, then I_find_signature() modifies <i>name</i> by removing
  169. * the "@<i>mapset</i>".
  170. * Use I_find_signature2 if altering passed in name is not desired.
  171. *
  172. * \param type I_SIGFILE_TYPE
  173. * \param name of signature
  174. * \param mapset set NULL to search in all mapsets
  175. * \return mapset or NULL
  176. */
  177. const char *I_find_signature(I_SIGFILE_TYPE type, char *name,
  178. const char *mapset)
  179. {
  180. char sdir[GNAME_MAX]; /* 'signatures/type\0' */
  181. G_debug(1, "I_find_signature(): type=%d name=%s mapset=%s", type, name,
  182. mapset);
  183. I_get_signatures_dir(sdir, type);
  184. /* We do not search for a specific file as file name is up to signature type */
  185. return G_find_file(sdir, name, mapset);
  186. }
  187. /*!
  188. * \brief Find mapset containing signature (look but don't touch)
  189. *
  190. * Looks for the signature <i>name</i> of type <i>type</i>
  191. * in the database. The <i>mapset</i> parameter can either be
  192. * the empty string "", which means search all the mapsets in
  193. * the users current mapset search path
  194. * (see \ref Mapset_Search_Path for more details about the search
  195. * path) or it can be a specific mapset name, which means look for the
  196. * signature only in this one mapset (for example, in the current
  197. * mapset). If found, the mapset where the signature lives is
  198. * returned. If not found, the NULL pointer is returned.
  199. *
  200. * Note: The passed name argument is not altered.
  201. * Use I_find_signature if stripping mapset part from the name is desired.
  202. *
  203. * \param type I_SIGFILE_TYPE
  204. * \param name of signature
  205. * \param mapset set NULL to search in all mapsets
  206. * \return mapset or NULL
  207. */
  208. const char *I_find_signature2(I_SIGFILE_TYPE type, const char *name,
  209. const char *mapset)
  210. {
  211. char sdir[GNAME_MAX]; /* 'signatures/type\0' */
  212. G_debug(1, "I_find_signature2(): type=%d name=%s mapset=%s", type, name,
  213. mapset);
  214. I_get_signatures_dir(sdir, type);
  215. return G_find_file2(sdir, name, mapset);
  216. }