/[escript]/trunk/tools/trconfcmp.cc
ViewVC logotype

Contents of /trunk/tools/trconfcmp.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6556 - (show annotations)
Mon May 8 02:42:09 2017 UTC (23 months, 2 weeks ago) by jfenwick
File size: 4632 byte(s)
Add compare tool.

Trying to get trilinos to build? Want to work out what is the difference between your configuration file and one that "works"?

Compile up tools.trconfcmp.cc and give it the too files.
It will tell you which variables are exclusive to one or the other as well as listing variables with different types and values.

1
2
3 #include <map>
4 #include <string>
5 #include <fstream>
6 #include <iostream>
7 #include <set>
8 #include <algorithm>
9 #include <vector>
10 #include <cctype>
11
12 using namespace std;
13
14 class rec{
15 public:
16 string name;
17 string type;
18 string value;
19 bool operator==(const rec& r1) const {
20 return r1.name==this->name;
21 }
22 bool operator<(const rec& r1) const {
23 return r1.name<this->name;
24 }
25
26 friend ostream& operator<<(ostream& os, const rec& r);
27
28 };
29
30 ostream& operator<<(ostream& os, const rec& r) {
31 os << '[' << r.name << '|' << r.type << "](" << r.value << ")" << endl;
32 return os;
33 }
34
35
36
37 auto loadmap(ifstream& ifs) {
38 string line;
39 set<rec> res;
40 while(getline(ifs, line), ifs.good()) {
41 auto pos=line.find("-D");
42 if (pos==string::npos) {
43 continue;
44 }
45 while (pos+2<line.size() && (line[pos+2]==' ')) {
46 pos++;
47 }
48 pos+=2;
49 auto tpos=line.find(':', pos);
50 auto eqpos=line.find('=', pos);
51 auto end=line.find_last_of('\\');
52 if (eqpos==string::npos) {
53 continue;
54 }
55 if (end==string::npos) {
56 end=line.size()-1;
57 } else {
58 end-=1; // move back from the backslash
59 }
60 while (isspace(line[end])) {
61 end--;
62 }
63 rec r;
64 r.value=line.substr(eqpos+1, end-eqpos);
65 if (tpos!=string::npos) {
66 r.type=line.substr(tpos+1, eqpos-tpos-1);
67 } else {
68 tpos=eqpos; // type is missing so we pretend = and : coincide
69 }
70 r.name=line.substr(pos,tpos-pos);
71 res.insert(r);
72 //cout << r.name << ' ' << r.type << " " << r.value << endl;
73 }
74 return res;
75 }
76
77
78 int main(int argc, char** argv) {
79 if (argc!=3) {
80 cerr << argv[0] << " file1 file2\n";
81 return 1;
82 }
83 string line;
84 ifstream f1(argv[1]);
85 ifstream f2(argv[2]);
86 if (!f1 || !f2) {
87 cerr << "Can't open file\n";
88 return 2;
89 }
90 set<rec> r1=loadmap(f1);
91 set<rec> r2=loadmap(f2);
92
93 vector<rec> all;
94 vector<rec> only_left;
95 vector<rec> only_right;
96 vector<pair<rec,rec>> diff_types;
97 vector<pair<rec,rec>> diff_values;
98 all.reserve(r1.size()+r2.size());
99 set_union(r1.begin(), r1.end(), r2.begin(), r2.end(), inserter(all, all.begin()));
100
101 for (auto it=all.begin();it!=all.end();++it) {
102 const rec* lhs=0;
103 const rec* rhs=0;
104 for (auto il=r1.begin();il!=r1.end();++il) {
105 if (il->name==it->name) {
106 lhs=&(*il);
107 }
108 }
109 for (auto il=r2.begin();il!=r2.end();++il) {
110 if (il->name==it->name) {
111 rhs=&(*il);
112 }
113 }
114 if (rhs==0) {
115 only_left.push_back(*lhs);
116 } else if (lhs==0) {
117 only_right.push_back(*rhs);
118 } else if (lhs->value!=rhs->value) {
119 diff_values.push_back(pair<rec,rec>(*lhs, *rhs));
120 } else if (lhs->type!=rhs->type) {
121 diff_types.push_back(pair<rec,rec>(*lhs, *rhs));
122 } else {
123 }
124 }
125 if (!only_left.empty())
126 {
127 cout << "Only in LHS:\n";
128 for (auto it=only_left.begin();it!=only_left.end();++it) {
129 cout << " -D" << it->name;
130 if (!it->type.empty()) {
131 cout << ':' << it->type;
132 }
133 cout << '=' << it->value << " \\\n";
134 }
135 }
136 if (!only_right.empty())
137 {
138 cout << "Only in RHS:\n";
139 for (auto it=only_right.begin();it!=only_right.end();++it) {
140 cout << " -D" << it->name;
141 if (!it->type.empty()) {
142 cout << ':' << '{' << it->type << '}';
143 }
144 cout << '=' << it->value << " \\\n";
145 }
146 }
147 if (!diff_values.empty())
148 {
149 cout << "Different values:\n";
150 for (auto it=diff_values.begin();it!=diff_values.end();++it) {
151 cout << " " << it->first.name;
152 if (!it->first.type.empty()) {
153 cout << ':' << it->first.type;
154 } else if (!it->second.type.empty()) {
155 cout << ':' << it->second.type;
156 }
157 cout << " LHS=" << it->first.value << " RHS=" << it->second.value << endl;
158 }
159 }
160 if (!diff_types.empty()) {
161 cout << "Different types:\n";
162 for (auto it=diff_types.begin();it!=diff_types.end();++it) {
163 cout << " " << it->first.name;
164 cout << " LHS=" << it->first.type << " RHS=" << it->second.type << endl;
165 }
166 }
167 }

  ViewVC Help
Powered by ViewVC 1.1.26