parse_ftcap.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if (!font_exists(path))
  20. return 0;
  21. }
  22. /* GFONT_DRIVER type fonts do not have path. */
  23. else if (sscanf(str, "%[^|]|%[^|]|%d||%d|%[^|]|",
  24. name, longname, &type, &index, encoding) == 5)
  25. path[0] = '\0';
  26. else
  27. return 0;
  28. e->name = G_store(name);
  29. e->longname = G_store(longname);
  30. e->type = type;
  31. e->path = G_store(path);
  32. e->index = index;
  33. e->encoding = G_store(encoding);
  34. return 1;
  35. }
  36. struct GFONT_CAP *parse_fontcap(void)
  37. {
  38. char *capfile, file[GPATH_MAX];
  39. char buf[GPATH_MAX];
  40. FILE *fp;
  41. int fonts_count = 0;
  42. struct GFONT_CAP *fonts = NULL;
  43. fp = NULL;
  44. if ((capfile = getenv("GRASS_FONT_CAP"))) {
  45. if ((fp = fopen(capfile, "r")) == NULL)
  46. G_warning(_("%s: Unable to read font definition file; use the default"),
  47. capfile);
  48. }
  49. if (fp == NULL) {
  50. sprintf(file, "%s/etc/fontcap", G_gisbase());
  51. if ((fp = fopen(file, "r")) == NULL)
  52. G_warning(_("%s: No font definition file"), file);
  53. }
  54. if (fp != NULL) {
  55. while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
  56. struct GFONT_CAP cap;
  57. char *p;
  58. p = strchr(buf, '#');
  59. if (p)
  60. *p = 0;
  61. if (!parse_fontcap_entry(&cap, buf))
  62. continue;
  63. fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
  64. fonts[fonts_count++] = cap;
  65. }
  66. fclose(fp);
  67. }
  68. fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
  69. fonts[fonts_count].name = NULL;
  70. fonts[fonts_count].path = NULL;
  71. return fonts;
  72. }
  73. void free_fontcap(struct GFONT_CAP *ftcap)
  74. {
  75. int i;
  76. if (ftcap == NULL)
  77. return;
  78. for (i = 0; ftcap[i].name; i++) {
  79. G_free(ftcap[i].name);
  80. G_free(ftcap[i].longname);
  81. G_free(ftcap[i].path);
  82. G_free(ftcap[i].encoding);
  83. }
  84. G_free(ftcap);
  85. }