Initial revision
[swftools.git] / pdf2swf / xpdf / Params.cc
1 //========================================================================
2 //
3 // Params.cc
4 //
5 // Copyright 1996 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #include <stdlib.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include "gtypes.h"
14 #include "gmem.h"
15 #include "GString.h"
16 #include "gfile.h"
17 #include "Params.h"
18
19 char **fontPath = NULL;
20 static int fontPathLen, fontPathSize;
21
22 DevFontMapEntry *devFontMap = NULL;
23 static int devFontMapLen, devFontMapSize;
24
25 void initParams(char *configFile) {
26   GString *fileName;
27   FILE *f;
28   char buf[256];
29   char *p, *q;
30
31   // initialize font path and font map
32   fontPath = (char **)gmalloc((fontPathSize = 8) * sizeof(char *));
33   fontPath[fontPathLen = 0] = NULL;
34   devFontMap = (DevFontMapEntry *)gmalloc((devFontMapSize = 8) *
35                                           sizeof(DevFontMapEntry));
36   devFontMap[devFontMapLen = 0].pdfFont = NULL;
37
38   // read config file
39   fileName = appendToPath(getHomeDir(), configFile);
40   if ((f = fopen(fileName->getCString(), "r"))) {
41     while (fgets(buf, sizeof(buf)-1, f)) {
42       buf[sizeof(buf)-1] = '\0';
43       p = strtok(buf, " \t\n\r");
44       if (p && !strcmp(p, "fontpath")) {
45         if (fontPathLen+1 >= fontPathSize)
46           fontPath = (char **)
47               grealloc(fontPath, (fontPathSize += 8) * sizeof(char *));
48         p = strtok(NULL, " \t\n\r");
49         fontPath[fontPathLen++] = copyString(p);
50       } else if (p && !strcmp(p, "fontmap")) {
51         if (devFontMapLen+1 >= devFontMapSize)
52           devFontMap = (DevFontMapEntry *)
53               grealloc(devFontMap,
54                        (devFontMapSize += 8) * sizeof(DevFontMapEntry));
55         p = strtok(NULL, " \t\n\r");
56         devFontMap[devFontMapLen].pdfFont = copyString(p);
57         p = strtok(NULL, "\t\n\r");
58         while (*p == ' ')
59           ++p;
60         for (q = p + strlen(p) - 1; q >= p && *q == ' '; --q) ;
61         q[1] = '\0';
62         devFontMap[devFontMapLen++].devFont = copyString(p);
63       }
64     }
65     fclose(f);
66     fontPath[fontPathLen] = NULL;
67     devFontMap[devFontMapLen].pdfFont = NULL;
68   }
69   delete fileName;
70 }
71
72 void freeParams() {
73   int i;
74
75   if (fontPath) {
76     for (i = 0; i < fontPathLen; ++i)
77       gfree(fontPath[i]);
78     gfree(fontPath);
79   }
80   if (devFontMap) {
81     for (i = 0; i < devFontMapLen; ++i) {
82       gfree(devFontMap[i].pdfFont);
83       gfree(devFontMap[i].devFont);
84     }
85     gfree(devFontMap);
86   }
87 }