-
Notifications
You must be signed in to change notification settings - Fork 1
/
savehelper.cpp
151 lines (135 loc) · 4.08 KB
/
savehelper.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
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
#include "savehelper.h"
#include "PartArray.h"
SaveHelper::SaveHelper(std::string file, bool overwritePreviousData):
filename(file)
{
if (!overwritePreviousData){
f.open(file,ios_base::out);
if (f.fail())
fprintf(stderr,"saveHelper: file %s is unwriteable or not found\n", file.c_str());
std::string temp, section;
while (!f.eof()) {
std::getline(f,temp);
if (temp[0]=='[' && temp[temp.length()-1]==']'){
temp.erase(temp.begin()); temp.erase(temp.end()-1);
section = temp;
strList[section].clear();
} else{
if (!strList[section].empty())
strList[section].append("\n");
strList[section].append(temp);
}
}
f.close();
} else {
f.open(file, ios_base::out|ios_base::trunc);
if (f.fail())
fprintf(stderr,"saveHelper: file %s is unwritable or not found\n", file.c_str());
}
}
SaveHelper::~SaveHelper()
{
this->close();
}
void SaveHelper::go(std::string section)
{
this->section = section;
strList[section].clear();
}
void SaveHelper::close()
{
if (f.is_open()){
if (strList.find("header")!=strList.end() && strList.find("parts")!=strList.end()){
//пишем Header
f<<"[header]"<<std::endl;
f<<trim_copy(strList["header"]);
f<<std::endl;
//пишем parts
f<<std::endl<<"[parts]"<<std::endl;
f<<trim_copy(strList["parts"]);
f<<std::endl;
auto iter = strList.begin();
while (iter != strList.end()){
if (iter->first != "header" && iter->first != "parts"){
f<<std::endl<<"["<<iter->first<<"]"<<std::endl;
f<<trim_copy(iter->second);
f<<std::endl;
}
iter++;
}
}
f.close();
}
}
SaveHelper &SaveHelper::operator <<(const double num)
{
char buf[100];
snprintf(buf,100,"%.10e",num);
write( std::string(buf) ); //write down 10 digits after comma
return *this;
}
SaveHelper &SaveHelper::operator <<(const int num)
{
write(std::to_string(num));
return *this;
}
SaveHelper &SaveHelper::operator <<(const unsigned int num)
{
write(std::to_string(num));
return *this;
}
SaveHelper &SaveHelper::operator <<(const long int num)
{
write(std::to_string(num));
return *this;
}
SaveHelper &SaveHelper::operator <<(const std::string num)
{
write(num);
return *this;
}
SaveHelper &SaveHelper::operator <<(const bool num)
{
write(std::to_string((int)num));
return *this;
}
void SaveHelper::write(std::string str)
{
if (section.empty())
fprintf(stderr,"saveHelper: You can not write to the empty section\n");
else
strList[section].append(str).append("\t");
}
void SaveHelper::line(std::string str)
{
if (section.empty())
fprintf(stderr,"saveHelper: You can not write to the empty section\n");
else
strList[section].append(str).append("\n");
}
void SaveHelper::writeHeader(PartArray *sys)
{
go("header");
line("version=2");
line(std::string("dimensions=")+std::to_string(config::Instance()->dimensions()));
line(std::string("type=")+sys->type());
line(std::string("size=")+std::to_string(sys->count()));
line(std::string("emin=")+std::to_string(sys->eMin));
line(std::string("emax=")+std::to_string(sys->eMax));
line(std::string("state=")+sys->state.toString());
line(std::string("minstate=")+sys->minstate.toString());
line(std::string("maxstate=")+sys->maxstate.toString());
line(std::string("interactionrange=")+std::to_string(sys->interactionRange()));
line("sizescale=1");
line("magnetizationscale=1");
}
void SaveHelper::writeDumped(std::map<std::string, std::string> dumped)
{
auto iter = dumped.begin();
while (iter != dumped.end()){
if (strList.find(iter->first)==strList.end()){ //if not contains
strList[iter->first] = iter->second;
}
iter++;
}
}