GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
parse_ftcap.c
Go to the documentation of this file.
1 /*!
2  \file lib/driver/parse_ftcap.c
3 
4  \brief Display Driver - fontcaps
5 
6  (C) 2006-2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Glynn Clements <glynn gclements.plus.com> (original contributor)
12  \author Huidae Cho <grass4u gmail.com>
13 */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 #include <grass/fontcap.h>
22 #include "driverlib.h"
23 
24 /*!
25  \brief Check if font exists
26 */
27 int font_exists(const char *name)
28 {
29  return access(name, R_OK) >= 0;
30 }
31 
32 /*!
33  \brief Parse fontcap entry
34 
35  \param e pointer to GFONT_CAP struct
36  \param str ?
37 
38  \return 1 on success
39  \return 0 on failure
40 */
41 int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
42 {
43  char name[GNAME_MAX], longname[GNAME_MAX], path[GPATH_MAX], encoding[128];
44  int type, index;
45 
46  if (sscanf(str, "%[^|]|%[^|]|%d|%[^|]|%d|%[^|]|", name, longname, &type,
47  path, &index, encoding) == 6) {
48  if (!font_exists(path))
49  return 0;
50  }
51  /* GFONT_DRIVER type fonts do not have path. */
52  else if (sscanf(str, "%[^|]|%[^|]|%d||%d|%[^|]|", name, longname, &type,
53  &index, encoding) == 5)
54  path[0] = '\0';
55  else
56  return 0;
57 
58  e->name = G_store(name);
59  e->longname = G_store(longname);
60  e->type = type;
61  e->path = G_store(path);
62  e->index = index;
63  e->encoding = G_store(encoding);
64 
65  return 1;
66 }
67 
68 /*!
69  \brief Parse fontcaps
70 
71  \return pointer to GFONT_CAP structure
72 */
73 struct GFONT_CAP *parse_fontcap(void)
74 {
75  char *capfile, file[GPATH_MAX];
76  char buf[GPATH_MAX];
77  FILE *fp;
78  int fonts_count = 0;
79  struct GFONT_CAP *fonts = NULL;
80 
81  fp = NULL;
82  if ((capfile = getenv("GRASS_FONT_CAP"))) {
83  if ((fp = fopen(capfile, "r")) == NULL)
84  G_warning(
85  _("%s: Unable to read font definition file; use the default"),
86  capfile);
87  }
88  if (fp == NULL) {
89  sprintf(file, "%s/etc/fontcap", G_gisbase());
90  if ((fp = fopen(file, "r")) == NULL)
91  G_warning(_("%s: No font definition file"), file);
92  }
93 
94  if (fp != NULL) {
95  while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
96  struct GFONT_CAP cap;
97  char *p;
98 
99  p = strchr(buf, '#');
100  if (p)
101  *p = 0;
102 
103  if (!parse_fontcap_entry(&cap, buf))
104  continue;
105 
106  fonts =
107  G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
108  fonts[fonts_count++] = cap;
109  }
110 
111  fclose(fp);
112  }
113 
114  fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
115  fonts[fonts_count].name = NULL;
116  fonts[fonts_count].path = NULL;
117 
118  return fonts;
119 }
120 
121 /*!
122  \brief Free allocated GFONT_CAP structure
123 
124  \param ftcap pointer to GFONT_CAP to be freed
125 */
127 {
128  int i;
129 
130  if (ftcap == NULL)
131  return;
132 
133  for (i = 0; ftcap[i].name; i++) {
134  G_free(ftcap[i].name);
135  G_free(ftcap[i].longname);
136  G_free(ftcap[i].path);
137  G_free(ftcap[i].encoding);
138  }
139 
140  G_free(ftcap);
141 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
void G_warning(const char *,...) __attribute__((format(printf
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:39
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
#define R_OK
Definition: dirent.c:25
struct GFONT_CAP * ftcap
Definition: driver/init.c:27
#define GPATH_MAX
Definition: gis.h:194
#define GNAME_MAX
Definition: gis.h:191
#define _(str)
Definition: glocale.h:10
#define file
const char * name
Definition: named_colr.c:6
int font_exists(const char *name)
Check if font exists.
Definition: parse_ftcap.c:27
void free_fontcap(struct GFONT_CAP *ftcap)
Free allocated GFONT_CAP structure.
Definition: parse_ftcap.c:126
struct GFONT_CAP * parse_fontcap(void)
Parse fontcaps.
Definition: parse_ftcap.c:73
int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
Parse fontcap entry.
Definition: parse_ftcap.c:41
char * name
Definition: fontcap.h:6
char * path
Definition: fontcap.h:10
char * encoding
Definition: fontcap.h:16
int index
Definition: fontcap.h:12
int type
Definition: fontcap.h:14
char * longname
Definition: fontcap.h:8
Definition: path.h:15