main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /****************************************************************************
  2. *
  3. * MODULE: g.dirseps
  4. * AUTHOR(S): Paul Kelly
  5. * PURPOSE: Copies input string to stdout, changing directory separator
  6. * characters as specified by flags.
  7. * Used for interoperability between Unix and Windows
  8. * pathnames.
  9. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. int main(int argc, char *argv[])
  21. {
  22. struct Flag *tohost, *tograss;
  23. struct Option *path;
  24. struct GModule *module;
  25. G_set_program_name(argv[0]);
  26. G_no_gisinit();
  27. G_set_gisrc_mode(G_GISRC_MODE_MEMORY);
  28. module = G_define_module();
  29. G_add_keyword(_("general"));
  30. G_add_keyword(_("map management"));
  31. G_add_keyword(_("scripts"));
  32. module->label =
  33. _("Internal GRASS utility for converting directory separator characters.");
  34. module->description =
  35. "Converts any directory separator characters in "
  36. "the input string to or from native host format, and writes the changed "
  37. "path to standard output. Useful in scripts for Windows compatibility.";
  38. tohost = G_define_flag();
  39. tohost->key = 'h';
  40. tohost->description =
  41. "Convert directory separators to native host format";
  42. tograss = G_define_flag();
  43. tograss->key = 'g';
  44. tograss->description =
  45. "Convert directory separators to GRASS internal format";
  46. path = G_define_option();
  47. path->key = "path";
  48. path->type = TYPE_STRING;
  49. path->required = NO;
  50. path->description =
  51. "Path to be converted (read from stdin if not specified)";
  52. if (G_parser(argc, argv))
  53. exit(EXIT_FAILURE);
  54. if (!tohost->answer && !tograss->answer)
  55. G_fatal_error("One of flags -%c or -%c must be specified!",
  56. tohost->key, tograss->key);
  57. if (tohost->answer && tograss->answer)
  58. G_fatal_error("Only one of flags -%c or -%c can be specified!",
  59. tohost->key, tograss->key);
  60. if (path->answer) {
  61. /* Take input from command-line option */
  62. char *pathstring = G_store(path->answer);
  63. if (tohost->answer)
  64. G_convert_dirseps_to_host(pathstring);
  65. if (tograss->answer)
  66. G_convert_dirseps_from_host(pathstring);
  67. puts(pathstring);
  68. }
  69. else {
  70. char inchar;
  71. while ((inchar = getc(stdin)) != EOF) {
  72. /* Read a character at a time from stdin until EOF
  73. * and copy to stdout after any conversion */
  74. if (tohost->answer) {
  75. if (inchar == GRASS_DIRSEP)
  76. inchar = HOST_DIRSEP;
  77. }
  78. if (tograss->answer) {
  79. if (inchar == HOST_DIRSEP)
  80. inchar = GRASS_DIRSEP;
  81. }
  82. putchar(inchar);
  83. }
  84. }
  85. fflush(stdout);
  86. exit(EXIT_SUCCESS);
  87. }