option.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, "raster") == 0 || strcmp(p->key, "raster_3d") == 0)
  40. p->guisection = _("Raster");
  41. else if (strcmp(p->key, "vector") == 0)
  42. p->guisection = _("Vector");
  43. else if (strcmp(p->key, "region") == 0)
  44. p->guisection = _("Region");
  45. else if (strcmp(p->key, "group") == 0)
  46. p->guisection = _("Group");
  47. return p;
  48. }
  49. /*!
  50. \brief Get list of element types separated by comma
  51. String buffer is allocated by G_malloc().
  52. \param do_all TRUE to add "all" to the buffer
  53. \return pointer to allocated buffer with types
  54. */
  55. const char *M_get_options(int do_all)
  56. {
  57. int len, n;
  58. char *str;
  59. for (len = 0, n = 0; n < nlist; n++)
  60. len += strlen(list[n].alias) + 1;
  61. if (do_all)
  62. len += 4;
  63. str = G_malloc(len);
  64. for (n = 0; n < nlist; n++) {
  65. if (n) {
  66. strcat(str, ",");
  67. strcat(str, list[n].alias);
  68. }
  69. else
  70. strcpy(str, list[n].alias);
  71. }
  72. if (do_all)
  73. strcat(str, ",all");
  74. return str;
  75. }
  76. /*!
  77. \brief Get list of element desc separated by comma
  78. String buffer is allocated by G_malloc().
  79. \param do_all TRUE to add "all" to the buffer
  80. \return pointer to allocated buffer with desc
  81. */
  82. const char *M_get_option_desc(int do_all)
  83. {
  84. int len, n;
  85. char *str;
  86. const char *str_all = "all;all types";
  87. for (len = 0, n = 0; n < nlist; n++) {
  88. len += strlen(list[n].alias) + 1;
  89. len += strlen(list[n].text) + 1;
  90. }
  91. if (do_all)
  92. len += strlen(str_all) + 1;
  93. str = G_malloc(len);
  94. for (n = 0; n < nlist; n++) {
  95. if (n) {
  96. strcat(str, ";");
  97. strcat(str, list[n].alias);
  98. strcat(str, ";");
  99. strcat(str, list[n].text);
  100. }
  101. else {
  102. strcpy(str, list[n].alias);
  103. strcat(str, ";");
  104. strcat(str, list[n].text);
  105. }
  106. }
  107. if (do_all) {
  108. strcat(str, ";");
  109. strcat(str, str_all);
  110. }
  111. return str;
  112. }