GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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|%[^|]|",
47  name, longname, &type, 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|%[^|]|",
53  name, longname, &type, &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(_("%s: Unable to read font definition file; use the default"),
85  capfile);
86  }
87  if (fp == NULL) {
88  sprintf(file, "%s/etc/fontcap", G_gisbase());
89  if ((fp = fopen(file, "r")) == NULL)
90  G_warning(_("%s: No font definition file"), file);
91  }
92 
93  if (fp != NULL) {
94  while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
95  struct GFONT_CAP cap;
96  char *p;
97 
98  p = strchr(buf, '#');
99  if (p)
100  *p = 0;
101 
102  if (!parse_fontcap_entry(&cap, buf))
103  continue;
104 
105  fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
106  fonts[fonts_count++] = cap;
107  }
108 
109  fclose(fp);
110  }
111 
112  fonts = G_realloc(fonts, (fonts_count + 1) * sizeof(struct GFONT_CAP));
113  fonts[fonts_count].name = NULL;
114  fonts[fonts_count].path = NULL;
115 
116  return fonts;
117 }
118 
119 /*!
120  \brief Free allocated GFONT_CAP structure
121 
122  \param ftcap pointer to GFONT_CAP to be freed
123 */
125 {
126  int i;
127 
128  if (ftcap == NULL)
129  return;
130 
131  for (i = 0; ftcap[i].name; i++) {
132  G_free(ftcap[i].name);
133  G_free(ftcap[i].longname);
134  G_free(ftcap[i].path);
135  G_free(ftcap[i].encoding);
136  }
137 
138  G_free(ftcap);
139 }
struct GFONT_CAP * parse_fontcap(void)
Parse fontcaps.
Definition: parse_ftcap.c:73
int type
Definition: fontcap.h:10
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
Parse fontcap entry.
Definition: parse_ftcap.c:41
#define R_OK
Definition: dirent.c:21
#define NULL
Definition: ccmath.h:32
char * path
Definition: fontcap.h:8
struct GFONT_CAP * ftcap
Definition: driver/init.c:27
char * encoding
Definition: fontcap.h:11
int type
Definition: gis.h:534
void free_fontcap(struct GFONT_CAP *ftcap)
Free allocated GFONT_CAP structure.
Definition: parse_ftcap.c:124
int index
Definition: fontcap.h:9
#define GPATH_MAX
Definition: gis.h:170
char * longname
Definition: fontcap.h:7
int font_exists(const char *name)
Check if font exists.
Definition: parse_ftcap.c:27
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:41
#define GNAME_MAX
Definition: gis.h:167
void G_warning(const char *,...) __attribute__((format(printf
Definition: path.h:16
#define file
#define G_realloc(p, n)
Definition: defs/gis.h:114
#define _(str)
Definition: glocale.h:10
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
const char * name
Definition: named_colr.c:7
char * getenv()
char * name
Definition: fontcap.h:6