option.c 2.5 KB

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