-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
92 lines (66 loc) · 1.5 KB
/
config.cpp
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
#include "config.hpp"
#include <fstream>
#include <regex.h>
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
namespace lyric_con {
map<string,string> config_info;
bool configured = false;
static void config_populate() {
ifstream in;
in.open("lyrics.config");
in.seekg(0, ios::end);
unsigned long length = in.tellg();
in.seekg(0, ios::beg);
char* buf = new char[length+1];
in.read(buf, length);
//cout << buf;
buf[length] = '\0';
in.close();
regex_t* re;
regmatch_t* mat;
char* pos = buf;
int status = 0;
do {
re = new regex_t;
mat = new regmatch_t;
regcomp(re, "[[:alnum:]]*=[^\n]*", REG_EXTENDED|REG_ICASE);
status = regexec(re, pos, 1, mat, 0);
if (status == 0) {
unsigned int so = mat->rm_so;
unsigned int eo = mat->rm_eo;
unsigned int off = 0;
char* sub_string = pos+so;
while (*sub_string != '=') {
sub_string++;
off++;
}
char* lhs = new char[off + 1];
char* rhs = new char[eo - so - off];
lhs[off] = '\0';
rhs[eo - so - off - 1] = '\0';
strncpy(lhs, pos + so, off);
strncpy(rhs, sub_string + 1, eo - so - off - 1);
string a = lhs;
string b = rhs;
delete[] lhs;
delete[] rhs;
config_info[a]=b;
pos = pos + eo;
}
regfree(re);
delete mat;
delete re;
} while(status == 0);
delete[] buf;
}
string return_config(string in) {
if (configured == false)
config_populate();
string out;
out = config_info[in];
return out;
}
}