-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACconfig.cc
132 lines (96 loc) · 3.17 KB
/
ACconfig.cc
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
/*
-------------------------------------------------------------------------
OBJECT NAME: ACconfig.cc
FULL NAME:
DESCRIPTION:
COPYRIGHT: University Corporation for Atmospheric Research, 1995-2004
-------------------------------------------------------------------------
*/
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "ACconfig.h"
using namespace libraf;
const std::string ACconfig::NO_KEY = "** No key for this tail number **";
const std::string ACconfig::NO_TAILNUM = "** Non-existent tail number **";
const std::string ACconfig::NO_SERVER = "** No matching server **";
/* -------------------------------------------------------------------- */
ACconfig::ACconfig(const std::string& fileName)
{
char buffer[512], *p;
FILE *fp;
if ((fp = fopen(fileName.c_str(), "r")) == NULL)
{
std::cerr << "ACconfig: Can't open " << fileName << ".\n";
exit(1);
}
std::vector<std::string> _file;
/* Read file and strip out comments.
*/
while ( fgets(buffer, 512, fp) )
{
if (buffer[0] == COMMENT)
continue;
buffer[strlen(buffer)] = '\0'; /* Ditch newline */
if ( (p = strchr(buffer, COMMENT)) )
*p = '\0';
else
p = &buffer[strlen(buffer)];
while ( isspace(*(--p)) ) /* Strip trailing white-space */
*p = '\0';
if ( isspace(buffer[0]) ) /* Belongs to previous line */
_file.back().append(buffer);
else /* Fresh line */
_file.push_back(std::string(buffer));
}
fclose(fp);
for (size_t i = 0; i < _file.size(); ++i)
{
if (_file[i].compare(0, 5, "START") == 0)
{
int idx = _file[i].find_first_not_of(" \t", 5);
std::string thisTail = _file[i].substr(idx);
_tailNumList.push_back(thisTail);
for (++i; i < _file.size(); ++i)
{
if (_file[i].compare("END_AIRCRAFT") == 0)
break;
int end = _file[i].find_first_of(" \t");
std::string key = thisTail + "." + _file[i].substr(0, end);
int start = _file[i].find_first_not_of(" \t", end);
std::string value = _file[i].substr(start);
_attrs[key] = value;
}
}
}
} /* END ACCONFIG */
/* -------------------------------------------------------------------- */
std::string ACconfig::GetParameter(const std::string& tailNumber, const std::string& target) const
{
size_t i;
for (i = 0; i < _tailNumList.size(); ++i)
if (_tailNumList[i] == tailNumber)
break;
if (i == _tailNumList.size())
return(NO_TAILNUM);
std::string key = tailNumber + "." + target;
std::map<std::string, std::string>::const_iterator iter = _attrs.find(key);
return((iter == _attrs.end()) ? NO_KEY : iter->second);
} /* END GETPARAMETER */
/* -------------------------------------------------------------------- */
std::string ACconfig::LocateMyTailNumber(const std::string& serverName)
{
for (std::map<std::string,std::string>::iterator i = _attrs.begin(); i != _attrs.end(); ++i)
{
std::string key = i->first;
std::string value = i->second;
if (value == serverName)
{
return(key.substr(0, key.find_first_of('.')));
}
}
return(NO_SERVER);
} /* END LOCATEMYTAILNUMBER */
/* END ACCONFIG.CC */