-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRESTManager.cpp
More file actions
115 lines (98 loc) · 2.95 KB
/
RESTManager.cpp
File metadata and controls
115 lines (98 loc) · 2.95 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: RESTManager.cpp
* Author: tim
*
* Created on 18. Mai 2017, 11:45
*/
#include <vector>
#include <algorithm>
#include <list>
#include <unistd.h>
#include "RESTManager.h"
RESTManager::RESTManager() {
resources = NULL;
number = 0;
}
RESTManager::RESTManager(string resources[], unsigned int length) {
this->resources = resources;
number = length;
}
RESTManager::RESTManager(const RESTManager& orig) {
resources = orig.resources;
number = orig.number;
}
RESTManager::~RESTManager() {
}
void RESTManager::initStructure() const {
DIR* dir = opendir(RES_FOLDER_NAME.c_str());
if(dir == NULL) {
mkdir(RES_FOLDER_NAME.c_str(), 0770);
dir = opendir(RES_FOLDER_NAME.c_str());
}
while(dirent* file = readdir(dir)) {
if(strcmp(file->d_name, ".") && strcmp(file->d_name, "..") && strcmp(file->d_name, "config"))
remove(((string)(RES_FOLDER_NAME + "/" + string(file->d_name))).c_str());
}
ofstream out;
if(access((RES_FOLDER_NAME + "/" + CONFIG_NAME).c_str(), F_OK)) {
out.open((RES_FOLDER_NAME + "/" + CONFIG_NAME).c_str());
out << "ServerIp=127.0.0.1" << endl;
out << "ServerPort=27015" << endl;
out << "HttpServerIp=127.0.0.1" << endl;
out << "HttpServerPort=15000" << endl;
out << "Store1Ip=localhost" << endl;
out << "Store1Port=12000" << endl;
out << "ExternalStoreN=0" << endl;
out << "ExternalStores=127.0.0.1:12000,127.0.0.1:12000" << endl;
out << "BrokerIp=localhost";
out.close();
}
out.open(RES_FOLDER_NAME + "/" + INDEX_NAME);
for(int i = 0; i < number; i++)
out << resources[i] << endl;
out.close();
}
list<string> RESTManager::get(string res) const {
ifstream inf;
list<string> values;
do {
inf.open(RES_FOLDER_NAME + "/" + res);
while(inf.good() && !inf.eof()) {
string line = "";
inf >> line;
values.push_front(line);
}
//cout << "FAIL " << res << endl;
} while(inf.bad());
inf.close();
values.remove("");
return values;
}
void RESTManager::put(string res, string value, bool append) const {
ofstream outf;
if(append)
outf.open(RES_FOLDER_NAME + "/" + res, ios::out | ios::app);
else
outf.open(RES_FOLDER_NAME + "/" + res);
outf << value << endl;
outf.close();
}
string RESTManager::getConfig(string name) const {
ifstream in(RES_FOLDER_NAME + "/" + CONFIG_NAME);
string value = "";
while(in.good()) {
string line;
in >> line;
if(line.find(name) != string::npos) {
value = line.substr(line.find('=')+1);
break;
}
}
in.close();
return value;
}