Skip to content

Commit ece8fbe

Browse files
authored
refactor file util (#166)
1 parent a6ea2bf commit ece8fbe

File tree

6 files changed

+72
-62
lines changed

6 files changed

+72
-62
lines changed

src/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ loda: external/jute.h external/jute.cpp $(OBJS)
1616
du -sh loda
1717

1818
external/jute.h:
19-
mkdir -p external && curl -sS -o external/jute.h https://raw.githubusercontent.com/amir-s/jute/master/jute.h
19+
mkdir -p external && curl -sS -o external/jute.h https://raw.githubusercontent.com/ckrause/jute/master/jute.h
2020

2121
external/jute.cpp:
22-
mkdir -p external && curl -sS -o external/jute.cpp https://raw.githubusercontent.com/amir-s/jute/master/jute.cpp
22+
mkdir -p external && curl -sS -o external/jute.cpp https://raw.githubusercontent.com/ckrause/jute/master/jute.cpp
2323

2424
clean:
2525
rm -R -f $(OBJS) loda ../loda external

src/boinc.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,3 @@ void Boinc::run() {
8383
Miner miner(settings, &progress_monitor);
8484
miner.mine();
8585
}
86-
87-
std::map<std::string, std::string> Boinc::readXML(const std::string& path) {
88-
std::map<std::string, std::string> result;
89-
std::ifstream in(path);
90-
std::string line, key, value;
91-
while (std::getline(in, line)) {
92-
auto b = line.find('<');
93-
if (b == std::string::npos) {
94-
continue;
95-
}
96-
line = line.substr(b + 1);
97-
b = line.find('>');
98-
if (b == std::string::npos) {
99-
continue;
100-
}
101-
key = line.substr(0, b);
102-
line = line.substr(b + 1);
103-
b = line.find("</");
104-
if (b == std::string::npos) {
105-
continue;
106-
}
107-
value = line.substr(0, b);
108-
result[key] = value;
109-
Log::get().debug("read xml tag: " + key + "=" + value);
110-
}
111-
return result;
112-
}

src/config.cpp

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
11
#include "config.hpp"
22

33
#include "file.hpp"
4-
#include "jute.h"
54
#include "log.hpp"
65
#include "setup.hpp"
76

87
bool ConfigLoader::MAINTAINANCE_MODE = false;
98

10-
int64_t get_jint(jute::jValue &v, const std::string &key, int64_t def) {
11-
if (v[key].get_type() == jute::jType::JNUMBER) {
12-
return v[key].as_int();
13-
}
14-
return def;
15-
}
16-
17-
double get_jdouble(jute::jValue &v, const std::string &key, double def) {
18-
if (v[key].get_type() == jute::jType::JNUMBER) {
19-
return v[key].as_double();
20-
}
21-
return def;
22-
}
23-
24-
bool get_jbool(jute::jValue &v, const std::string &key, bool def) {
25-
if (v[key].get_type() == jute::jType::JBOOLEAN) {
26-
return v[key].as_bool();
27-
}
28-
return def;
29-
}
30-
319
std::string get_template(std::string t) {
3210
static const std::string h(
3311
"$LODA_HOME/programs/"); // TODO: use proper variable replacing
@@ -52,15 +30,15 @@ std::vector<Generator::Config> loadGeneratorConfigs(
5230
continue;
5331
}
5432
Generator::Config c;
55-
c.version = get_jint(g, "version", 1);
33+
c.version = getJInt(g, "version", 1);
5634
c.miner = miner;
57-
c.length = get_jint(g, "length", 20);
58-
c.max_constant = get_jint(g, "maxConstant", 4);
59-
c.max_index = get_jint(g, "maxIndex", 4);
60-
c.mutation_rate = get_jdouble(g, "mutationRate", 0.3);
61-
c.loops = get_jbool(g, "loops", true);
62-
c.calls = get_jbool(g, "calls", true);
63-
c.indirect_access = get_jbool(g, "indirectAccess", false);
35+
c.length = getJInt(g, "length", 20);
36+
c.max_constant = getJInt(g, "maxConstant", 4);
37+
c.max_index = getJInt(g, "maxIndex", 4);
38+
c.mutation_rate = getJDouble(g, "mutationRate", 0.3);
39+
c.loops = getJBool(g, "loops", true);
40+
c.calls = getJBool(g, "calls", true);
41+
c.indirect_access = getJBool(g, "indirectAccess", false);
6442
auto t = g["template"].get_type();
6543
switch (t) {
6644
case jute::jType::JSTRING: {
@@ -140,7 +118,7 @@ Miner::Config ConfigLoader::load(const Settings &settings) {
140118
}
141119

142120
// load matcher configs
143-
bool backoff = get_jbool(m, "backoff", true);
121+
bool backoff = getJBool(m, "backoff", true);
144122
auto matchers = m["matchers"];
145123
for (int j = 0; j < matchers.size(); j++) {
146124
Matcher::Config mc;

src/file.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifdef _WIN64
2222
#include <io.h>
2323
#include <psapi.h>
24+
#include <windows.h>
2425
#else
2526
#include <sys/file.h>
2627
#include <sys/stat.h>
@@ -295,6 +296,7 @@ int64_t getFileAgeInDays(const std::string &path) {
295296
return -1;
296297
}
297298

299+
// TODO: move this to process.hpp
298300
size_t getMemUsage() {
299301
size_t mem_usage = 0;
300302
#if __linux__
@@ -325,6 +327,54 @@ size_t getMemUsage() {
325327
return mem_usage;
326328
}
327329

330+
std::map<std::string, std::string> readXML(const std::string &path) {
331+
std::map<std::string, std::string> result;
332+
std::ifstream in(path);
333+
std::string line, key, value;
334+
while (std::getline(in, line)) {
335+
auto b = line.find('<');
336+
if (b == std::string::npos) {
337+
continue;
338+
}
339+
line = line.substr(b + 1);
340+
b = line.find('>');
341+
if (b == std::string::npos) {
342+
continue;
343+
}
344+
key = line.substr(0, b);
345+
line = line.substr(b + 1);
346+
b = line.find("</");
347+
if (b == std::string::npos) {
348+
continue;
349+
}
350+
value = line.substr(0, b);
351+
result[key] = value;
352+
Log::get().debug("read xml tag: " + key + "=" + value);
353+
}
354+
return result;
355+
}
356+
357+
int64_t getJInt(jute::jValue &v, const std::string &key, int64_t def) {
358+
if (v[key].get_type() == jute::jType::JNUMBER) {
359+
return v[key].as_int();
360+
}
361+
return def;
362+
}
363+
364+
double getJDouble(jute::jValue &v, const std::string &key, double def) {
365+
if (v[key].get_type() == jute::jType::JNUMBER) {
366+
return v[key].as_double();
367+
}
368+
return def;
369+
}
370+
371+
bool getJBool(jute::jValue &v, const std::string &key, bool def) {
372+
if (v[key].get_type() == jute::jType::JBOOLEAN) {
373+
return v[key].as_bool();
374+
}
375+
return def;
376+
}
377+
328378
FolderLock::FolderLock(std::string folder) {
329379
// obtain lock
330380
ensureTrailingFileSep(folder);

src/include/boinc.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class Boinc {
88

99
void run();
1010

11-
std::map<std::string, std::string> readXML(const std::string& path);
12-
1311
private:
1412
Settings settings;
1513
};

src/include/file.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#pragma once
22

33
#include <filesystem>
4+
#include <map>
45
#include <string>
56

7+
#include "jute.h"
8+
69
#ifdef _WIN64
710
static constexpr char FILE_SEP = '\\';
811
#else
@@ -50,6 +53,14 @@ int64_t getFileAgeInDays(const std::string &path);
5053

5154
size_t getMemUsage();
5255

56+
std::map<std::string, std::string> readXML(const std::string &path);
57+
58+
int64_t getJInt(jute::jValue &v, const std::string &key, int64_t def);
59+
60+
double getJDouble(jute::jValue &v, const std::string &key, double def);
61+
62+
bool getJBool(jute::jValue &v, const std::string &key, bool def);
63+
5364
class FolderLock {
5465
public:
5566
FolderLock(std::string folder);

0 commit comments

Comments
 (0)