-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cc
179 lines (157 loc) · 4.07 KB
/
config.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "config.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
// read membership.config file and parse data to idList and portList
// @param idList: List of nodeID
// @param portList: List of port corresponding to each id
void readMembershipConfig(vector<int> &idList, vector<int> &portList)
{
readFile("membership.conf",idList, portList);
}
// read memorymap.config file and parse data to byteList and nodeList
// @param byteList : memory address associated with nodeId
// @param id = Identifier
void readMemoryMapConfig(vector<int> &byteList, int id)
{
vector<int> nodeList, indexList, byteTemp;
readFile("memory_map.conf", byteTemp, nodeList);
// find indices in the nodeList that has this 'id'
for (unsigned int i=0; i < byteTemp.size(); ++i)
{
if (id == nodeList[i])
{
indexList.push_back(i);
}
}
// append all information that belongs to this 'id'
for (unsigned int i=0; i < indexList.size(); ++i)
{
byteList.push_back(byteTemp[indexList[i]]);
}
}
// find port
// @param: id nodeId to find the port for
// @param: nodeList: list of node
// @param: portList: list of port
// @return return port number if found, otherwise, -99 is returned.
int findPort(int id, vector<int> nodeList, vector<int> portList)
{
for (unsigned int i=0; i < nodeList.size(); ++i)
{
if (nodeList[i] == id)
{
return portList[i];
}
}
return -99;
}
// Read file and parse content
// @param filename
// @param keyList: a list of stored key (i.e. ID)
// @param valueList: a list of stored value corresponding to the key
void readFile(string filename, vector<int> &keyList, vector<int> &valueList)
{
ifstream file(filename.c_str());
int key, value;
if (file.is_open()){
while ( file.good() )
{
file >> key;
file >> value;
if( file.good() ){
keyList.push_back( key );
valueList.push_back( value );
}
}
file.close();
}
}
// read a file (with two integers per line) into a map
// keyed on the first integer of the line
map<int, int> readFileMap(string filename){
map<int, int> result;
ifstream file(filename.c_str());
int key, value;
if (file.is_open()){
while ( file.good() )
{
file >> key;
file >> value;
if( file.good() ){
result[key] = value;
}
}
file.close();
}
return result;
}
// Read command file
// @param filename : filename
// @param command: stored list of commands
void readCommandFile(char* filename, vector<string> &command)
{
ifstream file(filename);
string line;
if (file.is_open()){
while ( file.good() )
{
file >> line;
if( file.good() )
{
command.push_back(line);
}
}
file.close();
}
}
// Printing content from Vectors
// @param keyList: a list of stored key (i.e. ID)
// @param valueList: a list of stored value corresponding to the key
void printContent(vector<int> idList, vector<int> valueList)
{
for (unsigned int i=0; i < idList.size(); ++i)
{
printf("Id: %i : %i\n", idList[i], valueList[i]);
}
}
/**
* Spliting string into vector of string
* @param str : Full string
* @param delim : Delimiter (spliter)
* @param results: Vector result (pass by ref)
*/
void splitString(string str, string delim, vector<string> &results)
{
int cutAt;
while( (cutAt = str.find_first_of(delim)) != (int) str.npos )
{
if(cutAt > 0)
{
results.push_back(str.substr(0,cutAt));
}
str = str.substr(cutAt+1);
}
if(str.length() > 0)
{
results.push_back(str);
}
}
/**
* Interpret the string command
* @param str: string command
* @param commandResult: stored vector int
*/
void interpretCommand(string str, vector<int> &commandResult)
{
vector<string> results;
splitString(str, ":", results);
for (unsigned int i = 0; i < results.size(); ++i)
{
char* pch = (char*)malloc( sizeof( char ) *(results[i].length() +1) );
strcpy(pch, results[i].c_str() );
commandResult.push_back(atoi(pch));
free(pch);
}
}