main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. *
  3. * MODULE: g.findfile
  4. * AUTHOR(S): Michael Shapiro CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Glynn Clements <glynn gclements.plus.com>,
  8. * Jan-Oliver Wagner <jan intevation.de>
  9. * Martin landa <landa.martin gmail.com>
  10. * PURPOSE: Searches for GRASS data base files
  11. * COPYRIGHT: (C) 1999-2008, 2011 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General
  14. * Public License (>=v2). Read the file COPYING that
  15. * comes with GRASS for details.
  16. *
  17. *****************************************************************************/
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include "local_proto.h"
  24. int main(int argc, char *argv[])
  25. {
  26. char file[GPATH_MAX], name[GNAME_MAX];
  27. const char *search_mapset, *mapset;
  28. struct GModule *module;
  29. struct Option *elem_opt;
  30. struct Option *mapset_opt;
  31. struct Option *file_opt;
  32. struct Flag *n_flag, *l_flag;
  33. module = G_define_module();
  34. G_add_keyword(_("general"));
  35. G_add_keyword(_("map management"));
  36. G_add_keyword(_("scripts"));
  37. module->description =
  38. _("Searches for GRASS data base files "
  39. "and sets variables for the shell.");
  40. G_gisinit(argv[0]);
  41. /* Define the different options */
  42. elem_opt = G_define_option();
  43. elem_opt->key = "element";
  44. elem_opt->type = TYPE_STRING;
  45. elem_opt->required = YES;
  46. elem_opt->description = _("Name of an element");
  47. file_opt = G_define_option();
  48. file_opt->key = "file";
  49. file_opt->type = TYPE_STRING;
  50. file_opt->required = YES;
  51. file_opt->description = _("Name of an existing map");
  52. mapset_opt = G_define_option();
  53. mapset_opt->key = "mapset";
  54. mapset_opt->type = TYPE_STRING;
  55. mapset_opt->required = NO;
  56. mapset_opt->label = _("Name of a mapset (default: search path)");
  57. mapset_opt->description = _("'.' for current mapset");
  58. n_flag = G_define_flag();
  59. n_flag->key = 'n';
  60. n_flag->description = _("Do not add quotes");
  61. l_flag = G_define_flag();
  62. l_flag->key = 'l';
  63. l_flag->description = _("List available elements and exit");
  64. l_flag->suppress_required = YES;
  65. if (G_parser(argc, argv))
  66. exit(EXIT_FAILURE);
  67. if (l_flag->answer) {
  68. list_elements();
  69. return EXIT_SUCCESS;
  70. }
  71. search_mapset = mapset_opt->answer;
  72. if (!search_mapset) {
  73. search_mapset = G_store("");
  74. }
  75. if (strcmp(".", search_mapset) == 0)
  76. search_mapset = G_mapset();
  77. if (mapset_opt->answer && strlen(mapset_opt->answer) > 0) {
  78. char **map_mapset = G_tokenize(file_opt->answer, "@");
  79. if (G_number_of_tokens(map_mapset) > 1) {
  80. if (strcmp(map_mapset[1], mapset_opt->answer))
  81. G_fatal_error(_("Parameter 'file' contains reference to <%s> mapset, "
  82. "but mapset parameter <%s> does not correspond"),
  83. map_mapset[1], mapset_opt->answer);
  84. else
  85. strcpy(name, file_opt->answer);
  86. }
  87. if (G_number_of_tokens(map_mapset) == 1)
  88. strcpy(name, file_opt->answer);
  89. G_free_tokens(map_mapset);
  90. }
  91. else
  92. strcpy(name, file_opt->answer);
  93. mapset = G_find_file2(elem_opt->answer, name, search_mapset);
  94. if (mapset) {
  95. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  96. const char *qchar = n_flag->answer ? "" : "'";
  97. const char *qual = G_fully_qualified_name(name, mapset);
  98. G_unqualified_name(name, mapset, xname, xmapset);
  99. G_file_name(file, elem_opt->answer, name, mapset);
  100. fprintf(stdout, "name=%s%s%s\n", qchar, xname, qchar);
  101. fprintf(stdout, "mapset=%s%s%s\n", qchar, xmapset, qchar);
  102. fprintf(stdout, "fullname=%s%s%s\n", qchar, qual, qchar);
  103. fprintf(stdout, "file=%s%s%s\n", qchar, file, qchar);
  104. return EXIT_SUCCESS;
  105. }
  106. else {
  107. fprintf(stdout, "name=\n");
  108. fprintf(stdout, "mapset=\n");
  109. fprintf(stdout, "fullname=\n");
  110. fprintf(stdout, "file=\n");
  111. }
  112. return EXIT_FAILURE;
  113. }