GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
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 SPDX-FileCopyrightText: 2006-2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Glynn Clements <glynn gclements.plus.com> (original contributor)
10 \author Huidae Cho <grass4u gmail.com>
11*/
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17#include <grass/gis.h>
18#include <grass/glocale.h>
19#include <grass/fontcap.h>
20#include "driverlib.h"
21
22/*!
23 \brief Check if font exists
24*/
25int font_exists(const char *name)
26{
27 return access(name, R_OK) >= 0;
28}
29
30/*!
31 \brief Parse fontcap entry
32
33 \param e pointer to GFONT_CAP struct
34 \param str ?
35
36 \return 1 on success
37 \return 0 on failure
38*/
39int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
40{
41 char name[GNAME_MAX], longname[GNAME_MAX], path[GPATH_MAX], encoding[128];
42 int type, index;
43
44 if (sscanf(str, "%[^|]|%[^|]|%d|%[^|]|%d|%[^|]|", name, longname, &type,
45 path, &index, encoding) == 6) {
46 if (!font_exists(path))
47 return 0;
48 }
49 /* GFONT_DRIVER type fonts do not have path. */
50 else if (sscanf(str, "%[^|]|%[^|]|%d||%d|%[^|]|", name, longname, &type,
51 &index, encoding) == 5)
52 path[0] = '\0';
53 else
54 return 0;
55
56 e->name = G_store(name);
57 e->longname = G_store(longname);
58 e->type = type;
59 e->path = G_store(path);
60 e->index = index;
61 e->encoding = G_store(encoding);
62
63 return 1;
64}
65
66/*!
67 \brief Parse fontcaps
68
69 \return pointer to GFONT_CAP structure
70*/
72{
73 char *capfile, file[GPATH_MAX];
74 char buf[GPATH_MAX];
75 FILE *fp;
76 int fonts_count = 0;
77 struct GFONT_CAP *fonts = NULL;
78
79 fp = NULL;
80 if ((capfile = getenv("GRASS_FONT_CAP"))) {
81 if ((fp = fopen(capfile, "r")) == NULL)
83 _("%s: Unable to read font definition file; use the default"),
84 capfile);
85 }
86 if (fp == NULL) {
87 snprintf(file, sizeof(file), "%s/etc/fontcap", G_gisbase());
88 if ((fp = fopen(file, "r")) == NULL)
89 G_warning(_("%s: No font definition file"), file);
90 }
91
92 if (fp != NULL) {
93 while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
94 struct GFONT_CAP cap;
95 char *p;
96
97 p = strchr(buf, '#');
98 if (p)
99 *p = 0;
100
101 if (!parse_fontcap_entry(&cap, buf))
102 continue;
103
104 fonts =
105 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));
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);
134 G_free(ftcap[i].path);
136 }
137
138 G_free(ftcap);
139}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
char path[BUFSIZ]
Definition ami_stream.h:127
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
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:85
struct GFONT_CAP * ftcap
Definition driver/init.c:25
#define GPATH_MAX
Definition gis.h:196
#define GNAME_MAX
Definition gis.h:193
#define _(str)
Definition glocale.h:10
#define file
const char * name
Definition named_colr.c:6
struct GFONT_CAP * parse_fontcap(void)
Parse fontcaps.
Definition parse_ftcap.c:71
int font_exists(const char *name)
Check if font exists.
Definition parse_ftcap.c:25
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:39
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
#define access
Definition unistd.h:7
#define R_OK
Definition unistd.h:25