args.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/vector.h>
  5. #include <grass/glocale.h>
  6. #include "local_proto.h"
  7. void parse_args(int argc, char **argv,
  8. char **input, char**output, int *format, int *dp, char **delim,
  9. char **field, char ***columns, char **where, int *region,
  10. int *old_format, int *header, char **cats, int *type)
  11. {
  12. struct Option *input_opt, *output_opt, *format_opt, *dp_opt, *delim_opt,
  13. *field_opt, *column_opt, *where_opt, *cats_opt, *type_opt;
  14. struct Flag *old_flag, *header_flag, *region_flag;
  15. char *desc;
  16. input_opt = G_define_standard_option(G_OPT_V_INPUT);
  17. field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  18. field_opt->guisection = _("Selection");
  19. field_opt->answer = "1";
  20. type_opt = G_define_standard_option(G_OPT_V3_TYPE);
  21. type_opt->guisection = _("Selection");
  22. output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  23. output_opt->label = _("Name for output ASCII file "
  24. "or ASCII vector name if '-o' is defined");
  25. output_opt->description = _("If not given or '-' then standard output");
  26. output_opt->required = NO;
  27. output_opt->guisection = _("Output");
  28. column_opt = G_define_standard_option(G_OPT_DB_COLUMNS);
  29. column_opt->label = _("Name of attribute column(s) to be exported (point mode)");
  30. column_opt->description = _("\"*\" for all columns");
  31. column_opt->guisection = _("Points");
  32. cats_opt = G_define_standard_option(G_OPT_V_CATS);
  33. cats_opt->guisection = _("Selection");
  34. where_opt = G_define_standard_option(G_OPT_DB_WHERE);
  35. where_opt->guisection = _("Selection");
  36. format_opt = G_define_option();
  37. format_opt->key = "format";
  38. format_opt->type = TYPE_STRING;
  39. format_opt->required = YES;
  40. format_opt->multiple = NO;
  41. format_opt->options = "point,standard,wkt";
  42. format_opt->answer = "point";
  43. format_opt->description = _("Output format");
  44. desc = NULL;
  45. G_asprintf(&desc,
  46. "point;%s;standard;%s;wkt;%s",
  47. _("Simple point format (point per row)"),
  48. _("GRASS ASCII vector format"),
  49. _("OGC well-known text"));
  50. format_opt->descriptions = desc;
  51. delim_opt = G_define_standard_option(G_OPT_F_SEP);
  52. delim_opt->description = _("Field separator (points mode)");
  53. delim_opt->guisection = _("Points");
  54. dp_opt = G_define_option();
  55. dp_opt->key = "precision";
  56. dp_opt->type = TYPE_INTEGER;
  57. dp_opt->required = NO;
  58. dp_opt->options = "0-32";
  59. dp_opt->answer = "8"; /* This value is taken from the lib settings in G_format_easting() */
  60. dp_opt->description =
  61. _("Number of significant digits (floating point only)");
  62. dp_opt->guisection = _("Points");
  63. old_flag = G_define_flag();
  64. old_flag->key = 'o';
  65. old_flag->description = _("Create old (version 4) ASCII file");
  66. old_flag->guisection = _("Output");
  67. header_flag = G_define_flag();
  68. header_flag->key = 'c';
  69. header_flag->description = _("Include column names in output (points mode)");
  70. header_flag->guisection = _("Points");
  71. region_flag = G_define_flag();
  72. region_flag->key = 'r';
  73. region_flag->description =
  74. _("Only export points falling within current 3D region (points mode)");
  75. region_flag->guisection = _("Points");
  76. if (G_parser(argc, argv))
  77. exit(EXIT_FAILURE);
  78. *input = G_store(input_opt->answer);
  79. if (output_opt->answer)
  80. *output = G_store(output_opt->answer);
  81. else
  82. *output = G_store("-"); /* standard output */
  83. if (format_opt->answer[0] == 'p')
  84. *format = GV_ASCII_FORMAT_POINT;
  85. else if (format_opt->answer[0] == 's')
  86. *format = GV_ASCII_FORMAT_STD;
  87. else
  88. *format = GV_ASCII_FORMAT_WKT;
  89. if (sscanf(dp_opt->answer, "%d", dp) != 1)
  90. G_fatal_error(_("Failed to interpret 'dp' parameter as an integer"));
  91. /* the field separator */
  92. *delim = G_option_to_separator(delim_opt);
  93. *field = G_store(field_opt->answer);
  94. *type = Vect_option_to_types(type_opt);
  95. if (*type & GV_AREA) {
  96. *type |= GV_BOUNDARY;
  97. *type |= GV_CENTROID;
  98. }
  99. *columns = NULL;
  100. if (column_opt->answer) {
  101. int i, nopt;
  102. nopt = 0;
  103. while(column_opt->answers[nopt++])
  104. ;
  105. *columns = (char **) G_malloc(nopt * sizeof(char *));
  106. for (i = 0; i < nopt - 1; i++)
  107. (*columns)[i] = G_store(column_opt->answers[i]);
  108. (*columns)[nopt - 1] = NULL;
  109. }
  110. *where = NULL;
  111. if (where_opt->answer) {
  112. *where = G_store(where_opt->answer);
  113. }
  114. *cats = NULL;
  115. if (cats_opt->answer) {
  116. *cats = G_store(cats_opt->answer);
  117. }
  118. *region = region_flag->answer ? TRUE : FALSE;
  119. *old_format = old_flag->answer ? TRUE : FALSE;
  120. *header = header_flag->answer ? TRUE : FALSE;
  121. }