GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
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
*/
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
snprintf(
file
,
sizeof
(
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
*/
126
void
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
}
G_free
void G_free(void *buf)
Free allocated memory.
Definition
alloc.c:147
NULL
#define NULL
Definition
ccmath.h:32
ftcap
struct GFONT_CAP * ftcap
Definition
driver/init.c:27
driverlib.h
G_warning
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition
gis/error.c:203
G_gisbase
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition
gisbase.c:39
file
#define file
name
const char * name
Definition
named_colr.c:6
parse_fontcap
struct GFONT_CAP * parse_fontcap(void)
Parse fontcaps.
Definition
parse_ftcap.c:73
font_exists
int font_exists(const char *name)
Check if font exists.
Definition
parse_ftcap.c:27
free_fontcap
void free_fontcap(struct GFONT_CAP *ftcap)
Free allocated GFONT_CAP structure.
Definition
parse_ftcap.c:126
parse_fontcap_entry
int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
Parse fontcap entry.
Definition
parse_ftcap.c:41
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition
strings.c:87
path
Definition
path.h:15
driver
parse_ftcap.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0