main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /****************************************************************************
  2. *
  3. * MODULE: g.filename
  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. * Hamish Bowman <hamish_b yahoo.com>,
  9. * Jan-Oliver Wagner <jan intevation.de>
  10. * PURPOSE: Prints GRASS data base file names
  11. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. int main(int argc, char *argv[])
  24. {
  25. char path[1024];
  26. const char *element;
  27. const char *mapset;
  28. const char *name;
  29. struct GModule *module;
  30. struct Option *opt1;
  31. struct Option *opt2;
  32. struct Option *opt3;
  33. struct Flag *flag_create;
  34. G_gisinit(argv[0]);
  35. module = G_define_module();
  36. G_add_keyword(_("general"));
  37. G_add_keyword(_("map management"));
  38. G_add_keyword(_("scripts"));
  39. module->description = _("Prints GRASS data base file names.");
  40. /* Define the different options */
  41. opt1 = G_define_option();
  42. opt1->key = "element";
  43. opt1->type = TYPE_STRING;
  44. opt1->required = YES;
  45. opt1->description = _("Name of an element");
  46. opt3 = G_define_option();
  47. opt3->key = "file";
  48. opt3->type = TYPE_STRING;
  49. opt3->required = YES;
  50. opt3->description = _("Name of a database file");
  51. opt2 = G_define_option();
  52. opt2->key = "mapset";
  53. opt2->type = TYPE_STRING;
  54. opt2->required = NO;
  55. opt2->description = _("Name of a mapset (default: current)");
  56. flag_create = G_define_flag();
  57. flag_create->key = 'c';
  58. flag_create->label = _("Create element directory in the current mapset");
  59. flag_create->description =
  60. _("If element directory for database file does not exist, create it");
  61. if (G_parser(argc, argv))
  62. exit(EXIT_FAILURE);
  63. element = opt1->answer;
  64. name = opt3->answer;
  65. if (opt2->answer)
  66. mapset = opt2->answer;
  67. else
  68. mapset = G_mapset();
  69. if (strcmp(mapset, ".") == 0 || strcmp(mapset, "") == 0)
  70. mapset = G_mapset();
  71. /* Create element directory if requested and in current mapset. */
  72. if (flag_create) {
  73. if (strcmp(mapset, G_mapset()) != 0) {
  74. G_fatal_error("Cannot create <%s> (flag -%c):"
  75. " <%s> is not the current mapset (%s)",
  76. element, flag_create->key, mapset, G_mapset());
  77. }
  78. G_make_mapset_object_group(element);
  79. }
  80. G_file_name(path, element, name, mapset);
  81. fprintf(stdout, "file='%s'\n", path);
  82. exit(EXIT_SUCCESS);
  83. }