ARB
readcfg.c
Go to the documentation of this file.
1 #include "readcfg.h"
2 
3 #include <string.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 
7 #define COMMENT ';'
8 #define MAXLEN 2048
9 
10 static cstr error_message; // Kann von decode gesetzt werden
11  // und wird dann als Fehlermeldung
12  // ausgegeben
13 
14 static void scanKeywords(struct S_cfgLine line[], int *lineanz, int *maxlen) {
15  // Anzahl und maximale Laenge der Keywords bestimmen
16  while (1) {
17  cstr keyword = line[*lineanz].keyword;
18  int keywordlen;
19 
20  if (!keyword) break;
21  keywordlen = strlen(keyword);
22  if (keywordlen>*maxlen) *maxlen = keywordlen;
23  (*lineanz)++;
24  }
25 }
26 static void cfgReadWarning(cstr fname, int lineno) {
27  warningf("Error in line %i of '%s'", lineno, fname);
28 }
29 int readCfg(cstr fname, struct S_cfgLine line[]) {
30  // liest und dekodiert CFG-Datei
31  FILE *in = fopen(fname, "r");
32  int ok = in!=NULL;
33 
34  if (!ok && errno==ENOENT) { // Datei nicht vorhanden
35  FILE *out = fopen(fname, "w");
36 
37  if (out) {
38  fprintf(out, ";\n; arb_treegen CFG-File '%s'\n;\n\n", fname);
39  fclose(out);
40 
41  warningf("'%s' has been generated.", fname);
42 
43  in = fopen(fname, "r");
44  ok = in!=NULL;
45  }
46  }
47 
48  if (ok) {
49  char linebuf[MAXLEN];
50  int readLines = 0, // Anzahl gelesener Zeilen
51  keywords = 0, // Anzahl keywords
52  maxKeywordlen = 0, // Laenge des laengsten keywords
53  lineno = 0, // fortlaufende Zeilennummer
54  *wordRead, // welche Worte wurden in welcher Zeile gelesen
55  x;
56 
57  scanKeywords(line, &keywords, &maxKeywordlen);
58 
59  wordRead = (int*)malloc(keywords*sizeof(int));
60  for (x = 0; x<keywords; x++) wordRead[x] = 0;
61 
62  // ---------------------------------
63  // CFG lesen und decodieren
64 
65  while (1) {
66  str firstWord;
67 
68  if (!fgets(linebuf, MAXLEN, in)) break;
69  lineno++;
70 
71  firstWord = strtok(linebuf, " \n");
72 
73  if (firstWord && firstWord[0]!=COMMENT && firstWord[0]!=0) {
74  int search;
75 
76  for (search = 0; search<keywords; search++) {
77  if (strcmp(line[search].keyword, firstWord)==0) {
78  str restDerZeile = strtok(NULL, "\n");
79  int decoded;
80 
81  error_message = NULL;
82  decoded = line[search].decode(restDerZeile, line[search].varPointer);
83 
84  if (!decoded) {
85  cfgReadWarning(fname, lineno);
87  else warningf("Can't interpret '%s'", line[search].keyword);
88  ok = 0;
89  }
90  else {
91  if (wordRead[search]) { // schonmal gelesen?
92  if (decoded!=2) { // mehrfache Verwendung erlaubt?
93  cfgReadWarning(fname, lineno);
94  warningf("Keyword '%s' duplicated (already specified in line %i)", firstWord, wordRead[search]);
95  ok = 0;
96  }
97  }
98  else { // nein, dann merken!
99  wordRead[search] = lineno;
100  readLines++;
101  }
102  }
103 
104  break;
105  }
106  }
107 
108  if (search==keywords) { // keyword nicht gefunden!
109  cfgReadWarning(fname, lineno);
110  warningf("Unknown Keyword '%s'", firstWord);
111  ok = 0;
112  }
113  }
114  }
115 
116  fclose(in);
117 
118  // ---------------------------------------------------
119  // Waren alle keywords im CFG-File vorhanden?
120 
121  if (ok && readLines<keywords) { // nein :-(
122  FILE *out = fopen(fname, "a");
123 
124  if (out) {
125  for (x = 0; x<keywords; x++) {
126  if (!wordRead[x]) {
127  fprintf(out, ";%s\n"
128  "\n"
129  "%-*s %s\n"
130  "\n",
131  line[x].description,
132  maxKeywordlen,
133  line[x].keyword,
134  line[x].defaultVal);
135  }
136  }
137 
138  fclose(out);
139  warningf("Missing keywords appended to '%s'.", fname);
140  }
141 
142  ok = 0;
143  }
144 
145  free(wordRead);
146  }
147 
148  return ok;
149 }
152 }
153 
154 
#define MAXLEN
Definition: readcfg.c:8
void warningf(int warning_num, const char *warning_messagef,...) __ATTR__FORMAT(2)
Definition: util.cxx:66
void warning(int warning_num, const char *warning_message)
Definition: util.cxx:61
decodeFunc decode
Definition: readcfg.h:26
cstr keyword
Definition: readcfg.h:24
#define COMMENT
Definition: readcfg.c:7
void message(char *errortext)
static void scanKeywords(struct S_cfgLine line[], int *lineanz, int *maxlen)
Definition: readcfg.c:14
int readCfg(cstr fname, struct S_cfgLine line[])
Definition: readcfg.c:29
static cstr error_message
Definition: readcfg.c:10
static void cfgReadWarning(cstr fname, int lineno)
Definition: readcfg.c:26
static int line
Definition: arb_a2ps.c:296
const char * cstr
Definition: defines.h:21
void setCfgError(cstr message)
Definition: readcfg.c:150