GRASS Programmer's Manual  6.4.2(2012)
parse_ftcap.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <grass/gis.h>
00005 #include <grass/glocale.h>
00006 #include <grass/freetypecap.h>
00007 #include "driverlib.h"
00008 
00009 int font_exists(const char *name)
00010 {
00011     FILE *fp;
00012 
00013     fp = fopen(name, "r");
00014     if (!fp)
00015         return 0;
00016 
00017     fclose(fp);
00018     return 1;
00019 }
00020 
00021 struct GFONT_CAP *parse_freetypecap(void)
00022 {
00023     char *capfile, file[GPATH_MAX];
00024     char buf[GPATH_MAX];
00025     FILE *fp;
00026     int fonts_count = 0;
00027     struct GFONT_CAP *fonts = NULL;
00028 
00029     fp = NULL;
00030     if ((capfile = getenv("GRASS_FONT_CAP"))) {
00031         if ((fp = fopen(capfile, "r")) == NULL)
00032             G_warning(_("%s: Unable to read font definition file; use the default"),
00033                       capfile);
00034     }
00035     if (fp == NULL) {
00036         sprintf(file, "%s/etc/fontcap", G_gisbase());
00037         if ((fp = fopen(file, "r")) == NULL)
00038             G_warning(_("%s: No font definition file"), file);
00039     }
00040 
00041     if (fp != NULL) {
00042         while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
00043             char name[GNAME_MAX], longname[GNAME_MAX],
00044                 path[GPATH_MAX], encoding[128];
00045             int type, index;
00046             char *p;
00047 
00048             p = strchr(buf, '#');
00049             if (p)
00050                 *p = 0;
00051 
00052             if (sscanf(buf, "%[^|]|%[^|]|%d|%[^|]|%d|%[^|]|",
00053                        name, longname, &type, path, &index, encoding)
00054                 != 6)
00055                 continue;
00056 
00057             if (!font_exists(path))
00058                 continue;
00059 
00060             fonts = (struct GFONT_CAP *)G_realloc(fonts,
00061                                                   (fonts_count +
00062                                                    1) *
00063                                                   sizeof(struct GFONT_CAP));
00064 
00065             fonts[fonts_count].name = G_store(name);
00066             fonts[fonts_count].longname = G_store(longname);
00067             fonts[fonts_count].type = type;
00068             fonts[fonts_count].path = G_store(path);
00069             fonts[fonts_count].index = index;
00070             fonts[fonts_count].encoding = G_store(encoding);
00071 
00072             fonts_count++;
00073         }
00074         fclose(fp);
00075     }
00076 
00077     fonts = (struct GFONT_CAP *)G_realloc(fonts, (fonts_count + 1) *
00078                                           sizeof(struct GFONT_CAP));
00079     fonts[fonts_count].name = NULL;
00080     fonts[fonts_count].path = NULL;
00081 
00082     return fonts;
00083 }
00084 
00085 void free_freetypecap(struct GFONT_CAP *ftcap)
00086 {
00087     int i;
00088 
00089     if (ftcap == NULL)
00090         return;
00091 
00092     for (i = 0; ftcap[i].name; i++) {
00093         G_free(ftcap[i].name);
00094         G_free(ftcap[i].longname);
00095         G_free(ftcap[i].path);
00096         G_free(ftcap[i].encoding);
00097     }
00098 
00099     G_free(ftcap);
00100 
00101     return;
00102 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines