parse_ftcap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*!
  2. \file lib/driver/parse_ftcap.c
  3. \brief Display Driver - fontcaps
  4. (C) 2006-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 Glynn Clements <glynn gclements.plus.com> (original contributor)
  8. \author Huidae Cho <grass4u gmail.com>
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. #include <grass/fontcap.h>
  17. #include "driverlib.h"
  18. /*!
  19. \brief Check if font exists
  20. */
  21. int font_exists(const char *name)
  22. {
  23. return access(name, R_OK) >= 0;
  24. }
  25. /*!
  26. \brief Parse fontcap entry
  27. \param e pointer to GFONT_CAP struct
  28. \param str ?
  29. \return 1 on success
  30. \return 0 on failure
  31. */
  32. int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
  33. {
  34. char name[GNAME_MAX], longname[GNAME_MAX], path[GPATH_MAX], encoding[128];
  35. int type, index;
  36. if (sscanf(str, "%[^|]|%[^|]|%d|%[^|]|%d|%[^|]|",
  37. name, longname, &type, path, &index, encoding) == 6) {
  38. if (!font_exists(path))
  39. return 0;
  40. }
  41. /* GFONT_DRIVER type fonts do not have path. */
  42. else if (sscanf(str, "%[^|]|%[^|]|%d||%d|%[^|]|",
  43. name, longname, &type, &index, encoding) == 5)
  44. path[0] = '\0';
  45. else
  46. return 0;
  47. e->name = G_store(name);
  48. e->longname = G_store(longname);
  49. e->type = type;
  50. e->path = G_store(path);
  51. e->index = index;
  52. e->encoding = G_store(encoding);
  53. return 1;
  54. }
  55. /*!
  56. \brief Parse fontcaps
  57. \return pointer to GFONT_CAP structure
  58. */
  59. struct GFONT_CAP *parse_fontcap(void)
  60. {
  61. char *capfile, file[GPATH_MAX];
  62. char buf[GPATH_MAX];
  63. FILE *fp;
  64. int fonts_count = 0;
  65. struct GFONT_CAP *fonts = NULL;
  66. fp = NULL;
  67. if ((capfile = getenv("GRASS_FONT_CAP"))) {
  68. if ((fp = fopen(capfile, "r")) == NULL)
  69. G_warning(_("%s: Unable to read font definition file; use the default"),
  70. capfile);
  71. }
  72. if (fp == NULL) {
  73. sprintf(file, "%s/etc/fontcap", G_gisbase());
  74. if ((fp = fopen(file, "r")) == NULL)
  75. G_warning(_("%s: No font definition file"), file);
  76. }
  77. if (fp != NULL) {
  78. while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
  79. struct GFONT_CAP cap;
  80. char *p;
  81. p = strchr(buf, '#');
  82. if (p)
  83. *p = 0;
  84. if (!parse_fontcap_entry(&cap, buf))
  85. continue;
  86. fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
  87. fonts[fonts_count++] = cap;
  88. }
  89. fclose(fp);
  90. }
  91. fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
  92. fonts[fonts_count].name = NULL;
  93. fonts[fonts_count].path = NULL;
  94. return fonts;
  95. }
  96. /*!
  97. \brief Free allocated GFONT_CAP structure
  98. \param ftcap pointer to GFONT_CAP to be freed
  99. */
  100. void free_fontcap(struct GFONT_CAP *ftcap)
  101. {
  102. int i;
  103. if (ftcap == NULL)
  104. return;
  105. for (i = 0; ftcap[i].name; i++) {
  106. G_free(ftcap[i].name);
  107. G_free(ftcap[i].longname);
  108. G_free(ftcap[i].path);
  109. G_free(ftcap[i].encoding);
  110. }
  111. G_free(ftcap);
  112. }