1 #include <stdio.h>
2 
3 
4 #include "defines.h"
5 #include "configuration.h"
6 
7 
8 void ReadVariables(FILE *fileptr,
9 		   void AssignFunction(char *VariableName, char *value));
10 int GetWord(FILE *fileptr, char *Word, char EndOfWord, int MaxSize);
11 int IgnoreCommentsAndSpaces(FILE *fileptr, char *c);
12 void TerminateVariable(char *variable);
13 int GoToEndOfLine(FILE *fileptr);
14 
15 
ReadVariables(FILE * fileptr,void AssignFunction (char * VariableName,char * value))16 void ReadVariables(FILE *fileptr,
17 		   void AssignFunction(char *VariableName, char *value))
18 {
19   char VariableName[MAX_NAME_LENGTH], Value[MAX_NAME_LENGTH];
20 
21   while(GetWord(fileptr, VariableName, '=', MAX_NAME_LENGTH)) {
22     if (!GetWord(fileptr, Value, '\n', MAX_NAME_LENGTH)) {
23       return;
24     }
25     AssignFunction(VariableName, Value);
26   }
27   return;
28 }
29 
30 
GetWord(FILE * fileptr,char * Word,char EndOfWord,int MaxSize)31 int GetWord(FILE *fileptr, char *Word, char EndOfWord, int MaxSize)
32 {
33   char *c;
34   int i=1;
35 
36   c=Word;
37 
38   if (IgnoreCommentsAndSpaces(fileptr, c)) {
39     return(0);
40   }
41   c++;
42 
43   while((*c=fgetc(fileptr)) != EndOfWord) {
44     i++;
45     if (*c==EOF || i>MaxSize) {
46       return(0);
47     }
48     c++;
49   }
50   TerminateVariable(Word);
51 
52   return(1);
53 }
54 
55 
IgnoreCommentsAndSpaces(FILE * fileptr,char * c)56 int IgnoreCommentsAndSpaces(FILE *fileptr, char *c)
57 {
58   while((*c=fgetc(fileptr))!=EOF) {
59     // Check For Comment
60     if (*c=='/' || *c=='#' || *c==';') {
61       if (GoToEndOfLine(fileptr)) {
62 	return(1);
63       }
64     // Ignore White Spaces
65     } else if (*c != ' ' && *c != '\n' && *c != '\t') {
66       return(0);
67     }
68   }
69   return(1);
70 }
71 
72 
TerminateVariable(char * variable)73 void TerminateVariable(char *variable)
74 {
75   while(*variable!='=' && *variable!=' ' && *variable!='\t' && *variable!='\n'
76 	&& *variable!='/' && *variable!='#' && *variable!=';') {
77     variable++;
78   }
79   *variable='\0';
80 }
81 
82 
GoToEndOfLine(FILE * fileptr)83 int GoToEndOfLine(FILE *fileptr)
84 {
85   char c;
86 
87   while((c=fgetc(fileptr))!=EOF) {
88     if (c=='\n') {
89       return(0);
90     }
91   }
92 
93   return(1);
94 }
95