GRASS GIS 7 Programmer's Manual  7.0.5(2016)-r00000
parse_ftcap.c
Go to the documentation of this file.
1 
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 
27 int font_exists(const char *name)
28 {
29  return access(name, R_OK) >= 0;
30 }
31 
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 
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 
124 void free_fontcap(struct GFONT_CAP *ftcap)
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
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int parse_fontcap_entry(struct GFONT_CAP *e, const char *str)
Parse fontcap entry.
Definition: parse_ftcap.c:41
#define NULL
Definition: ccmath.h:32
struct GFONT_CAP * ftcap
Definition: driver/init.c:27
void free_fontcap(struct GFONT_CAP *ftcap)
Free allocated GFONT_CAP structure.
Definition: parse_ftcap.c:124
int font_exists(const char *name)
Check if font exists.
Definition: parse_ftcap.c:27
Definition: path.h:16
#define file
const char * name
Definition: named_colr.c:7
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:41
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203