GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
Loading...
Searching...
No Matches
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*/
27int 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*/
41int 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*/
73struct 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)
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*/
126void free_fontcap(struct GFONT_CAP *ftcap)
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}
void G_free(void *buf)
Free allocated memory.
Definition alloc.c:150
#define NULL
Definition ccmath.h:32
struct GFONT_CAP * ftcap
Definition driver/init.c:27
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition gis/error.c:203
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition gisbase.c:39
#define file
const char * name
Definition named_colr.c:6
struct GFONT_CAP * parse_fontcap(void)
Parse fontcaps.
Definition parse_ftcap.c:73
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.
int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
Parse fontcap entry.
Definition parse_ftcap.c:41
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
Definition path.h:15