forked from qzhang62/vasp_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINCARModifier.cpp
More file actions
115 lines (103 loc) · 2.1 KB
/
INCARModifier.cpp
File metadata and controls
115 lines (103 loc) · 2.1 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
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <sstream>
using namespace std;
map<string,string> incar;
bool Parseline(string line,string& key, string& value){
int i;
for(i=0;i<line.length();i++){
if(line[i]=='#'){
line = line.substr(0,i);
break;
}
}
string firstPart,secondPart;
for(i=0;i<line.length();i++){
if(line[i]=='='){
firstPart = line.substr(0,i);
secondPart = line.substr(i+1, line.length()-i);
break;
}
}
if(i==line.length()){
return false;
}
istringstream firstPartIss(firstPart);
// istringstream secondPartIss(secondPart);
key = "";
value = "";
firstPartIss>>key;
// secondPartIss>>value;
value = secondPart;
if(key=="" || value=="" ){
return false;
}
//cout<<line<<"|"<<firstPart<<"|"<<secondPart<<"|"<<key<<"|"<<value<<"|"<<endl;
return true;
}
int ReadINCAR(char* filename){
fstream ifs(filename);
if(!ifs){
cout<<"Cannot open "<<filename<<endl;
return -1;
}
string line;
while(getline(ifs,line)){
string key,value;
bool result = Parseline(line,key,value);
if(result){
incar[key] = value;
}
}
return 0;
}
int WriteINCAR(){
for(map<string,string>::iterator itr = incar.begin(); itr!=incar.end(); ++itr ){
cout<<itr->first<<" = "<<itr->second<<endl;
}
return 0;
}
int TakingCommand(){
string command;
while(1){
cin>>command;
if(command=="done"){
return 0;
}
else if(command=="show"){
WriteINCAR();
}
else if(command=="get"){
string key;
cin>>key;
cout<<incar[key]<<endl;
}
else if(command=="set"){
string key,value;
cin>>key>>value;
incar[key] = value;
}
else if(command=="delete"){
string key;
cin>>key;
map<string,string>::iterator itr = incar.find(key);
if(itr!=incar.end()){
incar.erase(itr);
}
}
}
return 0;
}
int main(int argc,char* argv[]){
if(argc!=2){
cout<<"Usage : INCARModifier INCAR"<<endl;
return -1;
}
if( ReadINCAR(argv[1]) == -1){
return -1;
}
TakingCommand();
return 0;
}