main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * PURPOSE:
  10. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <grass/gis.h>
  21. #include <grass/glocale.h>
  22. int main(int argc, char *argv[])
  23. {
  24. char file[1024], name[GNAME_MAX], *mapset;
  25. char *search_mapset;
  26. struct GModule *module;
  27. struct Option *opt1;
  28. struct Option *opt2;
  29. struct Option *opt3;
  30. module = G_define_module();
  31. module->keywords = _("general");
  32. module->description =
  33. "Searches for GRASS data base files "
  34. "and sets variables for the shell.";
  35. G_gisinit(argv[0]);
  36. /* Define the different options */
  37. opt1 = G_define_option();
  38. opt1->key = "element";
  39. opt1->type = TYPE_STRING;
  40. opt1->required = YES;
  41. opt1->description = "Name of an element";
  42. opt2 = G_define_option();
  43. opt2->key = "mapset";
  44. opt2->type = TYPE_STRING;
  45. opt2->required = NO;
  46. opt2->description = "Name of a mapset";
  47. opt2->answer = "";
  48. opt3 = G_define_option();
  49. opt3->key = "file";
  50. opt3->type = TYPE_STRING;
  51. opt3->required = YES;
  52. opt3->description = "Name of an existing map";
  53. if (G_parser(argc, argv))
  54. exit(EXIT_FAILURE);
  55. search_mapset = opt2->answer;
  56. if (strcmp(".", search_mapset) == 0)
  57. search_mapset = G_mapset();
  58. if (opt2->answer && strlen(opt2->answer) > 0) {
  59. char **map_mapset = G_tokenize(opt3->answer, "@");
  60. if (G_number_of_tokens(map_mapset) > 1) {
  61. if (strcmp(map_mapset[1], opt2->answer))
  62. G_fatal_error(_("Parameter 'file' contains reference to <%s> mapset, but mapset parameter <%s> does not correspond"),
  63. map_mapset[1], opt2->answer);
  64. else
  65. strcpy(name, opt3->answer);
  66. }
  67. if (G_number_of_tokens(map_mapset) == 1)
  68. strcpy(name, opt3->answer);
  69. G_free_tokens(map_mapset);
  70. }
  71. else
  72. strcpy(name, opt3->answer);
  73. mapset = G_find_file2(opt1->answer, name, search_mapset);
  74. if (mapset) {
  75. fprintf(stdout, "name='%s'\n", name);
  76. fprintf(stdout, "mapset='%s'\n", mapset);
  77. fprintf(stdout, "fullname='%s'\n",
  78. G_fully_qualified_name(name, mapset));
  79. G__file_name(file, opt1->answer, name, mapset);
  80. fprintf(stdout, "file='%s'\n", file);
  81. }
  82. else {
  83. fprintf(stdout, "name=\n");
  84. fprintf(stdout, "mapset=\n");
  85. fprintf(stdout, "fullname=\n");
  86. fprintf(stdout, "file=\n");
  87. }
  88. exit(mapset == NULL);
  89. }