parse_ftcap.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <grass/gis.h>
  6. #include <grass/glocale.h>
  7. #include <grass/fontcap.h>
  8. #include "driverlib.h"
  9. int font_exists(const char *name)
  10. {
  11. return access(name, R_OK) >= 0;
  12. }
  13. int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
  14. {
  15. char name[GNAME_MAX], longname[GNAME_MAX], path[GPATH_MAX], encoding[128];
  16. int type, index;
  17. if (sscanf(str, "%[^|]|%[^|]|%d|%[^|]|%d|%[^|]|",
  18. name, longname, &type, path, &index, encoding) != 6)
  19. return 0;
  20. if (!font_exists(path))
  21. return 0;
  22. e->name = G_store(name);
  23. e->longname = G_store(longname);
  24. e->type = type;
  25. e->path = G_store(path);
  26. e->index = index;
  27. e->encoding = G_store(encoding);
  28. return 1;
  29. }
  30. struct GFONT_CAP *parse_fontcap(void)
  31. {
  32. char *capfile, file[GPATH_MAX];
  33. char buf[GPATH_MAX];
  34. FILE *fp;
  35. int fonts_count = 0;
  36. struct GFONT_CAP *fonts = NULL;
  37. fp = NULL;
  38. if ((capfile = getenv("GRASS_FONT_CAP"))) {
  39. if ((fp = fopen(capfile, "r")) == NULL)
  40. G_warning(_("%s: Unable to read font definition file; use the default"),
  41. capfile);
  42. }
  43. if (fp == NULL) {
  44. sprintf(file, "%s/etc/fontcap", G_gisbase());
  45. if ((fp = fopen(file, "r")) == NULL)
  46. G_warning(_("%s: No font definition file"), file);
  47. }
  48. if (fp != NULL) {
  49. while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
  50. struct GFONT_CAP cap;
  51. char *p;
  52. p = strchr(buf, '#');
  53. if (p)
  54. *p = 0;
  55. if (!parse_fontcap_entry(&cap, buf))
  56. continue;
  57. fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
  58. fonts[fonts_count++] = cap;
  59. }
  60. fclose(fp);
  61. }
  62. fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
  63. fonts[fonts_count].name = NULL;
  64. fonts[fonts_count].path = NULL;
  65. return fonts;
  66. }
  67. void free_fontcap(struct GFONT_CAP *ftcap)
  68. {
  69. int i;
  70. if (ftcap == NULL)
  71. return;
  72. for (i = 0; ftcap[i].name; i++) {
  73. G_free(ftcap[i].name);
  74. G_free(ftcap[i].longname);
  75. G_free(ftcap[i].path);
  76. G_free(ftcap[i].encoding);
  77. }
  78. G_free(ftcap);
  79. }