option.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*!
  2. \file lib/manage/option.c
  3. \brief Manage Library - Define option for parser
  4. (C) 2001-2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <string.h>
  10. #include <grass/gis.h>
  11. #include <grass/glocale.h>
  12. #include "manage_local_proto.h"
  13. /*!
  14. \brief Define option for parser
  15. \param n element id
  16. \return pointer to Option structure
  17. \return NULL on error
  18. */
  19. struct Option* M_define_option(int n, const char *desc, int multiple)
  20. {
  21. char *str;
  22. struct Option *p;
  23. if (n >= nlist)
  24. return NULL;
  25. p = G_define_option();
  26. p->key = list[n].alias;
  27. p->type = TYPE_STRING;
  28. if (multiple)
  29. p->key_desc = "name";
  30. else
  31. p->key_desc = "from,to";
  32. p->required = NO;
  33. p->multiple = multiple;
  34. G_asprintf(&str, "old,%s,%s", list[n].mainelem, list[n].maindesc);
  35. p->gisprompt = str;
  36. G_asprintf(&str, _("%s to be %s"),
  37. list[n].text, desc);
  38. p->description = str;
  39. if (strcmp(p->key, "rast") == 0 || strcmp(p->key, "rast3d") == 0)
  40. p->guisection = _("Raster");
  41. else if (strcmp(p->key, "vect") == 0 || strcmp(p->key, "oldvect") == 0 ||
  42. strcmp(p->key, "asciivect") == 0 ||
  43. strcmp(p->key, "sites") == 0)
  44. p->guisection = _("Vector");
  45. else if (strcmp(p->key, "region") == 0 || strcmp(p->key, "region3d") == 0)
  46. p->guisection = _("Region");
  47. else if (strcmp(p->key, "group") == 0)
  48. p->guisection = _("Group");
  49. return p;
  50. }
  51. /*!
  52. \brief Get list of element types separated by comma
  53. String buffer is allocated by G_malloc().
  54. \param do_all TRUE to add "all" to the buffer
  55. \return pointer to allocated buffer with types
  56. */
  57. const char *M_get_options(int do_all)
  58. {
  59. int len, n;
  60. char *str;
  61. for (len = 0, n = 0; n < nlist; n++)
  62. len += strlen(list[n].alias) + 1;
  63. if (do_all)
  64. len += 4;
  65. str = G_malloc(len);
  66. for (n = 0; n < nlist; n++) {
  67. if (n) {
  68. strcat(str, ",");
  69. strcat(str, list[n].alias);
  70. }
  71. else
  72. strcpy(str, list[n].alias);
  73. }
  74. if (do_all)
  75. strcat(str, ",all");
  76. return str;
  77. }
  78. /*!
  79. \brief Get list of element desc separated by comma
  80. String buffer is allocated by G_malloc().
  81. \param do_all TRUE to add "all" to the buffer
  82. \return pointer to allocated buffer with desc
  83. */
  84. const char *M_get_option_desc(int do_all)
  85. {
  86. int len, n;
  87. char *str;
  88. const char *str_all = "all;all types";
  89. for (len = 0, n = 0; n < nlist; n++) {
  90. len += strlen(list[n].alias) + 1;
  91. len += strlen(list[n].text) + 1;
  92. }
  93. if (do_all)
  94. len += strlen(str_all);
  95. str = G_malloc(len);
  96. for (n = 0; n < nlist; n++) {
  97. if (n) {
  98. strcat(str, ";");
  99. strcat(str, list[n].alias);
  100. strcat(str, ";");
  101. strcat(str, list[n].text);
  102. }
  103. else {
  104. strcpy(str, list[n].alias);
  105. strcat(str, ";");
  106. strcat(str, list[n].text);
  107. }
  108. }
  109. if (do_all) {
  110. strcat(str, ";");
  111. strcat(str, str_all);
  112. }
  113. return str;
  114. }