-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigReader.cpp
More file actions
149 lines (129 loc) · 2.67 KB
/
ConfigReader.cpp
File metadata and controls
149 lines (129 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <vector>
#include <time.h>
#include "main.h"
#include "ConfigReader.h"
const char* Field::GetType()
{
return "none";
}
void Field::wrongType()
{
printf("wrong type\n");
printf("The type is \"%s\"\n",this->GetType());
assert(false);
}
int Field::GetInt(){
wrongType();
return 0;
}
bool Field::GetBool(){
wrongType();
return false;
}
float Field::GetFloat(){
wrongType();
return 0.0f;
}
const char* Field::GetStr(){
wrongType();
return "";
}
FieldInt::FieldInt(int val):val(val){}
int FieldInt::GetInt()
{
return val;
}
FieldBool::FieldBool(bool val):val(val){}
bool FieldBool::GetBool()
{
return val;
}
FieldFloat::FieldFloat(float val):val(val){};
float FieldFloat::GetFloat()
{
return val;
}
FieldPixel::FieldPixel(pixel val):val(val){};
const pixel& FieldPixel::GetPixel()
{
return val;
}
FieldFPixel::FieldFPixel(fpixel val):val(val){}
const fpixel& FieldFPixel::GetFPixel()
{
return val;
}
Field3DVec::Field3DVec(_3DVec val):val(val){}
const _3DVec& Field3DVec::Get3DVec()
{
return val;
}
FieldStr::FieldStr(const char* val):val(val){}
const char* FieldStr::GetStr()
{
return val.c_str();
}
Config::Config()
{
config = new ConfigMap;
}
Config::~Config()
{
delete config;
}
void Config::LoadConfig(const char* filepath)
{
config->clear();
FILE * configFile;
printf("Loading configuration from %s file\n",filepath);
configFile = fopen(filepath,"r");
if (!configFile)
{
printf ("Missing %s file\n",filepath);
}
int lineNumber=1;
int res;
char s_type[255];
char s_name[255];
char s1[255];
char s2[255];
char s3[255];
char s4[255];
do
{
res = fscanf(configFile,"%s %s = %s, %s, %s, %s\n", s_type, s_name, s1, s2, s3, s4);
lineNumber++;
if (strcmp(s_type,"int")==0){
(*config)[std::string(s_name)] = new FieldInt(atoi(s1));
}
if (strcmp(s_type,"bool")==0){
(*config)[std::string(s_name)] = new FieldBool(atoi(s1));
}
if (strcmp(s_type,"float")==0){
(*config)[std::string(s_name)] = new FieldFloat(atof(s1));
}
if (strcmp(s_type,"3dvec")==0){
(*config)[std::string(s_name)] = new Field3DVec(_3DVec(atof(s1),atof(s2),atof(s3)));
}
if (strcmp(s_type,"fpixel")==0){
(*config)[std::string(s_name)] = new FieldFPixel(fpixel(atof(s1),atof(s2),atof(s3)));
}
if (strcmp(s_type,"pixel")==0){
(*config)[std::string(s_name)] = new FieldPixel(pixel(atoi(s1),atoi(s2),atoi(s3),atoi(s4)));
}
if (strcmp(s_type,"str")==0){
(*config)[std::string(s_name)] = new FieldStr(std::string(s1).c_str());
}
}while(res!=EOF);
fclose(configFile);
};
Field* Config::GetField(const char* fname)
{
return (*config)[fname];
}