read_list.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*!
  2. \file lib/manage/read_list.c
  3. \brief Manage Library - Read list of elements
  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 <stdlib.h>
  11. #include <unistd.h>
  12. #include <grass/gis.h>
  13. #include <grass/glocale.h>
  14. #include "manage_local_proto.h"
  15. int nlist;
  16. struct list *list;
  17. static void format_error(char *, int, char *);
  18. /*!
  19. \brief Read list of elements
  20. Format:
  21. \code
  22. # ... comments
  23. main element:alias:description:menu text
  24. sub element:description
  25. sub element:description
  26. .
  27. .
  28. .
  29. \endcode
  30. \param check_if_empty TRUE for check if element is empty
  31. \return 0
  32. \return 1
  33. */
  34. int M_read_list(int check_if_empty, int *num)
  35. {
  36. FILE *fd;
  37. char element_list[GPATH_MAX];
  38. char buf[1024];
  39. char elem[100];
  40. char alias[100];
  41. char desc[100];
  42. char text[100];
  43. int any;
  44. int line;
  45. char *env;
  46. nlist = 0;
  47. list = 0;
  48. any = 0;
  49. env = getenv("ELEMENT_LIST");
  50. if (env)
  51. strcpy(element_list, env);
  52. else
  53. sprintf(element_list, "%s/etc/element_list", G_gisbase());
  54. fd = fopen(element_list, "r");
  55. if (!fd)
  56. G_fatal_error(_("Unable to open data base element list '%s'"), element_list);
  57. line = 0;
  58. while (G_getl(buf, sizeof(buf), fd)) {
  59. line++;
  60. if (*buf == '#')
  61. continue;
  62. if (*buf == ' ' || *buf == '\t') { /* support element */
  63. *desc = 0;
  64. if (sscanf(buf, "%[^:]:%[^\n]", elem, desc) < 1)
  65. continue;
  66. if (*elem == '#')
  67. continue;
  68. if (nlist == 0)
  69. format_error(element_list, line, buf);
  70. G_strip(elem);
  71. G_strip(desc);
  72. M__add_element(elem, desc);
  73. }
  74. else { /* main element */
  75. if (sscanf
  76. (buf, "%[^:]:%[^:]:%[^:]:%[^\n]", elem, alias, desc,
  77. text) != 4)
  78. format_error(element_list, line, buf);
  79. G_strip(elem);
  80. G_strip(alias);
  81. G_strip(desc);
  82. G_strip(text);
  83. list =
  84. (struct list *)G_realloc(list, (nlist + 1) * sizeof(*list));
  85. list[nlist].mainelem = G_store(elem);
  86. list[nlist].alias = G_store(alias);
  87. list[nlist].maindesc = G_store(desc);
  88. list[nlist].text = G_store(text);
  89. list[nlist].nelem = 0;
  90. list[nlist].element = 0;
  91. list[nlist].desc = 0;
  92. list[nlist].status = 0;
  93. if (!check_if_empty || !M__empty(elem)) {
  94. list[nlist].status = 1;
  95. any = 1;
  96. }
  97. nlist++;
  98. M__add_element(elem, desc);
  99. }
  100. }
  101. if (num)
  102. *num = nlist;
  103. fclose(fd);
  104. return any;
  105. }
  106. void format_error(char *element_list, int line, char *buf)
  107. {
  108. G_fatal_error(_("Format error: file ('%s') line (%d) - %s"), element_list, line,
  109. buf);
  110. }