main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. module->label =
  32. _("Internal GRASS utility for converting directory separator characters.");
  33. module->description =
  34. "Converts any directory separator characters in "
  35. "the input string to or from native host format, and writes the changed "
  36. "path to standard output. Useful in scripts for Windows compatibility.";
  37. tohost = G_define_flag();
  38. tohost->key = 'h';
  39. tohost->description =
  40. "Convert directory separators to native host format";
  41. tograss = G_define_flag();
  42. tograss->key = 'g';
  43. tograss->description =
  44. "Convert directory separators to GRASS internal format";
  45. path = G_define_option();
  46. path->key = "path";
  47. path->type = TYPE_STRING;
  48. path->required = NO;
  49. path->description =
  50. "Path to be converted (read from stdin if not specified)";
  51. if (G_parser(argc, argv))
  52. exit(EXIT_FAILURE);
  53. if (!tohost->answer && !tograss->answer)
  54. G_fatal_error("One of flags -%c or -%c must be specified!",
  55. tohost->key, tograss->key);
  56. if (tohost->answer && tograss->answer)
  57. G_fatal_error("Only one of flags -%c or -%c can be specified!",
  58. tohost->key, tograss->key);
  59. if (path->answer) {
  60. /* Take input from command-line option */
  61. char *pathstring = G_store(path->answer);
  62. if (tohost->answer)
  63. G_convert_dirseps_to_host(pathstring);
  64. if (tograss->answer)
  65. G_convert_dirseps_from_host(pathstring);
  66. puts(pathstring);
  67. }
  68. else {
  69. char inchar;
  70. while ((inchar = getc(stdin)) != EOF) {
  71. /* Read a character at a time from stdin until EOF
  72. * and copy to stdout after any conversion */
  73. if (tohost->answer) {
  74. if (inchar == GRASS_DIRSEP)
  75. inchar = HOST_DIRSEP;
  76. }
  77. if (tograss->answer) {
  78. if (inchar == HOST_DIRSEP)
  79. inchar = GRASS_DIRSEP;
  80. }
  81. putchar(inchar);
  82. }
  83. }
  84. fflush(stdout);
  85. exit(EXIT_SUCCESS);
  86. }