args.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, struct cat_list **clist)
  11. {
  12. struct Option *input_opt, *output_opt, *format_opt, *dp_opt, *delim_opt,
  13. *field_opt, *column_opt, *where_opt, *cats_opt;
  14. struct Flag *old_flag, *header_flag, *region_flag;
  15. input_opt = G_define_standard_option(G_OPT_V_INPUT);
  16. field_opt = G_define_standard_option(G_OPT_V_FIELD);
  17. field_opt->guisection = _("Selection");
  18. output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  19. output_opt->label = _("Name for output ASCII file "
  20. "or ASCII vector name if '-o' is defined");
  21. output_opt->description = _("'-' for standard output");
  22. output_opt->required = NO;
  23. output_opt->answer = "-";
  24. column_opt = G_define_standard_option(G_OPT_DB_COLUMNS);
  25. column_opt->description = _("Name of attribute column(s) to be exported (point mode)");
  26. column_opt->guisection = _("Points");
  27. cats_opt = G_define_standard_option(G_OPT_V_CATS);
  28. cats_opt->guisection = _("Selection");
  29. where_opt = G_define_standard_option(G_OPT_DB_WHERE);
  30. where_opt->guisection = _("Selection");
  31. format_opt = G_define_option();
  32. format_opt->key = "format";
  33. format_opt->type = TYPE_STRING;
  34. format_opt->required = YES;
  35. format_opt->multiple = NO;
  36. format_opt->options = "point,standard,wkt";
  37. format_opt->answer = "point";
  38. format_opt->description = _("Output format");
  39. delim_opt = G_define_standard_option(G_OPT_F_SEP);
  40. delim_opt->description = _("Field separator (points mode)");
  41. delim_opt->guisection = _("Points");
  42. dp_opt = G_define_option();
  43. dp_opt->key = "dp";
  44. dp_opt->type = TYPE_INTEGER;
  45. dp_opt->required = NO;
  46. dp_opt->options = "0-32";
  47. dp_opt->answer = "8"; /* This value is taken from the lib settings in G_format_easting() */
  48. dp_opt->description =
  49. _("Number of significant digits (floating point only)");
  50. dp_opt->guisection = _("Points");
  51. old_flag = G_define_flag();
  52. old_flag->key = 'o';
  53. old_flag->description = _("Create old (version 4) ASCII file");
  54. header_flag = G_define_flag();
  55. header_flag->key = 'c';
  56. header_flag->description = _("Include column names in output (points mode)");
  57. header_flag->guisection = _("Points");
  58. region_flag = G_define_flag();
  59. region_flag->key = 'r';
  60. region_flag->description =
  61. _("Only export points falling within current 3D region (points mode)");
  62. region_flag->guisection = _("Points");
  63. if (G_parser(argc, argv))
  64. exit(EXIT_FAILURE);
  65. *input = G_store(input_opt->answer);
  66. *output = G_store(output_opt->answer);
  67. if (format_opt->answer[0] == 'p')
  68. *format = GV_ASCII_FORMAT_POINT;
  69. else if (format_opt->answer[0] == 's')
  70. *format = GV_ASCII_FORMAT_STD;
  71. else
  72. *format = GV_ASCII_FORMAT_WKT;
  73. if (sscanf(dp_opt->answer, "%d", dp) != 1)
  74. G_fatal_error(_("Failed to interpret 'dp' parameter as an integer"));
  75. /* the field separator */
  76. if (strcmp(delim_opt->answer, "\\t") == 0)
  77. *delim = G_store("\t");
  78. else if (strcmp(delim_opt->answer, "tab") == 0)
  79. *delim = G_store("\t");
  80. else if (strcmp(delim_opt->answer, "space") == 0)
  81. *delim = G_store(" ");
  82. else if (strcmp(delim_opt->answer, "comma") == 0)
  83. *delim = G_store(",");
  84. else
  85. *delim = G_store(delim_opt->answer);
  86. *field = G_store(field_opt->answer);
  87. *columns = NULL;
  88. if (column_opt->answer) {
  89. int i, nopt;
  90. nopt = 0;
  91. while(column_opt->answers[nopt++])
  92. ;
  93. *columns = (char **) G_malloc(nopt * sizeof(char *));
  94. for (i = 0; i < nopt - 1; i++)
  95. (*columns)[i] = G_store(column_opt->answers[i]);
  96. (*columns)[nopt - 1] = NULL;
  97. }
  98. *where = NULL;
  99. if (where_opt->answer) {
  100. *where = G_store(where_opt->answer);
  101. }
  102. *clist = NULL;
  103. if (cats_opt->answer) {
  104. int ret;
  105. *clist = Vect_new_cat_list();
  106. (*clist)->field = atoi(field_opt->answer);
  107. if ((*clist)->field < 1)
  108. G_fatal_error(_("Option <%s> must be > 0"), field_opt->key);
  109. ret = Vect_str_to_cat_list(cats_opt->answer, *clist);
  110. if (ret > 0)
  111. G_fatal_error(_("%d errors in cat option"), ret);
  112. }
  113. *region = region_flag->answer ? 1 : 0;
  114. *old_format = old_flag->answer ? 1 : 0;
  115. *header = header_flag->answer ? 1 : 0;
  116. }