GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
parse_ftcap.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <grass/gis.h>
5 #include <grass/glocale.h>
6 #include <grass/freetypecap.h>
7 #include "driverlib.h"
8 
9 int font_exists(const char *name)
10 {
11  FILE *fp;
12 
13  fp = fopen(name, "r");
14  if (!fp)
15  return 0;
16 
17  fclose(fp);
18  return 1;
19 }
20 
21 struct GFONT_CAP *parse_freetypecap(void)
22 {
23  char *capfile, file[GPATH_MAX];
24  char buf[GPATH_MAX];
25  FILE *fp;
26  int fonts_count = 0;
27  struct GFONT_CAP *fonts = NULL;
28 
29  fp = NULL;
30  if ((capfile = getenv("GRASS_FONT_CAP"))) {
31  if ((fp = fopen(capfile, "r")) == NULL)
32  G_warning(_("%s: Unable to read font definition file; use the default"),
33  capfile);
34  }
35  if (fp == NULL) {
36  sprintf(file, "%s/etc/fontcap", G_gisbase());
37  if ((fp = fopen(file, "r")) == NULL)
38  G_warning(_("%s: No font definition file"), file);
39  }
40 
41  if (fp != NULL) {
42  while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
43  char name[GNAME_MAX], longname[GNAME_MAX],
44  path[GPATH_MAX], encoding[128];
45  int type, index;
46  char *p;
47 
48  p = strchr(buf, '#');
49  if (p)
50  *p = 0;
51 
52  if (sscanf(buf, "%[^|]|%[^|]|%d|%[^|]|%d|%[^|]|",
53  name, longname, &type, path, &index, encoding)
54  != 6)
55  continue;
56 
57  if (!font_exists(path))
58  continue;
59 
60  fonts = (struct GFONT_CAP *)G_realloc(fonts,
61  (fonts_count +
62  1) *
63  sizeof(struct GFONT_CAP));
64 
65  fonts[fonts_count].name = G_store(name);
66  fonts[fonts_count].longname = G_store(longname);
67  fonts[fonts_count].type = type;
68  fonts[fonts_count].path = G_store(path);
69  fonts[fonts_count].index = index;
70  fonts[fonts_count].encoding = G_store(encoding);
71 
72  fonts_count++;
73  }
74  fclose(fp);
75  }
76 
77  fonts = (struct GFONT_CAP *)G_realloc(fonts, (fonts_count + 1) *
78  sizeof(struct GFONT_CAP));
79  fonts[fonts_count].name = NULL;
80  fonts[fonts_count].path = NULL;
81 
82  return fonts;
83 }
84 
85 void free_freetypecap(struct GFONT_CAP *ftcap)
86 {
87  int i;
88 
89  if (ftcap == NULL)
90  return;
91 
92  for (i = 0; ftcap[i].name; i++) {
93  G_free(ftcap[i].name);
94  G_free(ftcap[i].longname);
95  G_free(ftcap[i].path);
96  G_free(ftcap[i].encoding);
97  }
98 
99  G_free(ftcap);
100 
101  return;
102 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
string name
Definition: render.py:1314
struct GFONT_CAP * ftcap
Definition: driver/init.c:28
char * getenv()
void free_freetypecap(struct GFONT_CAP *ftcap)
Definition: parse_ftcap.c:85
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
fclose(fd)
#define file
char * G_gisbase(void)
top level module directory
Definition: gisbase.c:42
struct GFONT_CAP * parse_freetypecap(void)
Definition: parse_ftcap.c:21
int font_exists(const char *name)
Definition: parse_ftcap.c:9