read_list.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include "list.h"
  5. /*******************************************************************
  6. read the element list file
  7. format is:
  8. # ... comments
  9. main element:alias:description:menu text
  10. sub element:description
  11. sub element:description
  12. .
  13. .
  14. .
  15. ******************************************************************/
  16. int nlist;
  17. struct list *list;
  18. static int format_error(char *, int, char *);
  19. int read_list(int check_if_empty)
  20. {
  21. FILE *fd;
  22. char element_list[GPATH_MAX];
  23. char buf[1024];
  24. char elem[100];
  25. char alias[100];
  26. char desc[100];
  27. char text[100];
  28. int any;
  29. int line;
  30. char *env;
  31. nlist = 0;
  32. list = 0;
  33. any = 0;
  34. env = getenv("ELEMENT_LIST");
  35. if (env)
  36. strcpy(element_list, env);
  37. else
  38. sprintf(element_list, "%s/etc/element_list", G_gisbase());
  39. fd = fopen(element_list, "r");
  40. if (!fd)
  41. G_fatal_error("can't open database element list <%s>", element_list);
  42. line = 0;
  43. while (G_getl(buf, sizeof(buf), fd)) {
  44. line++;
  45. if (*buf == '#')
  46. continue;
  47. if (*buf == ' ' || *buf == '\t') { /* support element */
  48. *desc = 0;
  49. if (sscanf(buf, "%[^:]:%[^\n]", elem, desc) < 1)
  50. continue;
  51. if (*elem == '#')
  52. continue;
  53. if (nlist == 0)
  54. format_error(element_list, line, buf);
  55. G_strip(elem);
  56. G_strip(desc);
  57. add_element(elem, desc);
  58. }
  59. else { /* main element */
  60. if (sscanf
  61. (buf, "%[^:]:%[^:]:%[^:]:%[^\n]", elem, alias, desc,
  62. text) != 4)
  63. format_error(element_list, line, buf);
  64. G_strip(elem);
  65. G_strip(alias);
  66. G_strip(desc);
  67. G_strip(text);
  68. list =
  69. (struct list *)G_realloc(list, (nlist + 1) * sizeof(*list));
  70. list[nlist].mainelem = G_store(elem);
  71. list[nlist].alias = G_store(alias);
  72. list[nlist].maindesc = G_store(desc);
  73. list[nlist].text = G_store(text);
  74. list[nlist].nelem = 0;
  75. list[nlist].element = 0;
  76. list[nlist].desc = 0;
  77. list[nlist].status = 0;
  78. if (!check_if_empty || !empty(elem)) {
  79. list[nlist].status = 1;
  80. any = 1;
  81. }
  82. nlist++;
  83. add_element(elem, desc);
  84. }
  85. }
  86. fclose(fd);
  87. return any;
  88. }
  89. static int format_error(char *element_list, int line, char *buf)
  90. {
  91. G_fatal_error(_("Format error: <%s>\nLine: %d\n%s"), element_list, line,
  92. buf);
  93. return 1;
  94. }