Skip to content

Commit a4a7b7a

Browse files
committed
1. add namespace Read_Txt_Tools
1 parent 0a1fce1 commit a4a7b7a

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ read_rho.o\
245245
read_atoms.o\
246246
read_cell_pseudopots.o\
247247
read_dm.o\
248+
read_txt_tools.o\
248249
write_pot.o\
249250
write_rho.o\
250251
write_rho_cube.o\

source/src_io/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_library(
2323
print_info.cpp
2424
read_dm.cpp
2525
read_rho.cpp
26+
read_txt_tools.cpp
2627
restart.cpp
2728
rwstream.cpp
2829
to_wannier90.cpp

source/src_io/read_txt_tools.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-08
4+
//=======================
5+
6+
#include "read_txt_tools.h"
7+
8+
#include <fstream>
9+
#include <sstream>
10+
#include <algorithm>
11+
12+
namespace Read_Txt_Tools
13+
{
14+
// {
15+
// INPUT_PARAMETERS: [1.0],
16+
// ecutwfc: [200, Ry]
17+
// }
18+
std::map<std::string, std::vector<std::string>> read_file_to_map(
19+
const std::string &file_name,
20+
const std::set<std::string> &comment_seps)
21+
{
22+
std::string str;
23+
std::ifstream ifs(file_name);
24+
std::map<std::string, std::vector<std::string>> m;
25+
while(ifs.good())
26+
{
27+
std::getline(ifs,str);
28+
const std::vector<std::string> vec = Read_Txt_Tools::split_whitespace(Read_Txt_Tools::ignore_comment(str, comment_seps));
29+
if(vec.size()>0)
30+
m[vec[0]] = std::vector<std::string>(vec.begin()+1, vec.end());
31+
}
32+
return m;
33+
}
34+
35+
36+
37+
// [
38+
// [INPUT_PARAMETERS, 1.0],
39+
// [ecutwfc, 200, Ry]
40+
// ]
41+
std::vector<std::vector<std::string>> read_file_to_vector(
42+
const std::string &file_name,
43+
const std::set<std::string> &comment_seps)
44+
{
45+
std::string str;
46+
std::ifstream ifs(file_name);
47+
std::vector<std::vector<std::string>> vec_all;
48+
while(ifs.good())
49+
{
50+
std::getline(ifs,str);
51+
const std::vector<std::string> vec = Read_Txt_Tools::split_whitespace(Read_Txt_Tools::ignore_comment(str, comment_seps));
52+
if(vec.size()>0)
53+
vec_all.push_back(vec);
54+
}
55+
return vec_all;
56+
}
57+
58+
59+
60+
// [ecutwfc, 200, Ry]
61+
std::vector<std::string> split_whitespace(const std::string &str)
62+
{
63+
std::stringstream ss;
64+
ss<<str;
65+
std::vector<std::string> vec;
66+
std::string s;
67+
while(ss>>s)
68+
vec.push_back(s);
69+
return vec;
70+
}
71+
72+
73+
74+
// str:
75+
// ecutwfc 200 Ry # XXX
76+
// return:
77+
// ecutwfc 200 Ry
78+
std::string ignore_comment(const std::string &str, const std::set<std::string> &comment_seps)
79+
{
80+
std::string str_new = str;
81+
for(const std::string &sep : comment_seps)
82+
str_new = str_new.substr(0, str_new.find(sep));
83+
return str_new;
84+
}
85+
86+
87+
88+
// return_prefix + content_new = content_old
89+
std::vector<std::vector<std::string>> cut_paragraph(
90+
std::vector<std::vector<std::string>> &content,
91+
const std::set<std::string> &labels)
92+
{
93+
std::vector<std::vector<std::string>>::iterator ptr=content.begin()+1;
94+
for(; ptr<content.end(); ++ptr)
95+
{
96+
if(std::find(labels.begin(), labels.end(), (*ptr)[0]) != labels.end())
97+
break;
98+
}
99+
const std::vector<std::vector<std::string>> prefix = std::vector<std::vector<std::string>>(content.begin(), ptr);
100+
content = std::vector<std::vector<std::string>>(ptr, content.end());
101+
return prefix;
102+
}
103+
}

source/src_io/read_txt_tools.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-08
4+
//=======================
5+
6+
#ifndef READ_TXT_TOOLS_H
7+
#define READ_TXT_TOOLS_H
8+
9+
#include <vector>
10+
#include <map>
11+
#include <set>
12+
#include <string>
13+
14+
namespace Read_Txt_Tools
15+
{
16+
// {
17+
// INPUT_PARAMETERS: [1.0],
18+
// ecutwfc: [200, Ry]
19+
// }
20+
std::map<std::string, std::vector<std::string>> read_file_to_map(
21+
const std::string &file_name,
22+
const std::set<std::string> &comment_seps);
23+
24+
25+
// [
26+
// [INPUT_PARAMETERS, 1.0],
27+
// [ecutwfc, 200, Ry]
28+
// ]
29+
std::vector<std::vector<std::string>> read_file_to_vector(
30+
const std::string &file_name,
31+
const std::set<std::string> &comment_seps);
32+
33+
34+
// [ecutwfc, 200, Ry]
35+
std::vector<std::string> split_whitespace(const std::string &str);
36+
37+
38+
// str:
39+
// ecutwfc 200 Ry # XXX
40+
// return:
41+
// ecutwfc 200 Ry
42+
std::string ignore_comment(const std::string &str, const std::set<std::string> &comment_seps);
43+
44+
45+
// return_prefix + content_new = content_old
46+
std::vector<std::vector<std::string>> cut_paragraph(
47+
std::vector<std::vector<std::string>> &content,
48+
const std::set<std::string> &labels);
49+
50+
51+
// data_v:
52+
// [
53+
// [INPUT_PARAMETERS, 1.0],
54+
// [ecutwfc, 200, Ry]
55+
// ]
56+
// return:
57+
// [INPUT_PARAMETERS, 1.0, ecutwfc, 200, Ry]
58+
template<typename T>
59+
std::vector<T> chain(const std::vector<std::vector<T>> &data_v)
60+
{
61+
std::vector<T> data_chain;
62+
for(const std::vector<T> & data_i : data_v)
63+
data_chain.insert(data_chain.end(), data_i.begin(), data_i.end());
64+
return data_chain;
65+
}
66+
}
67+
68+
#endif

0 commit comments

Comments
 (0)