-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-merge.cpp
109 lines (89 loc) · 2.47 KB
/
json-merge.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
/*
This file is part of git-merge-drivers.
git-merge-drivers is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
git-merge-drivers is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this git-merge-drivers. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <fstream>
#include <json.hpp>
using json = nlohmann::json;
using namespace std;
void
merge(
istream& our_stream,
istream& older_stream,
istream& their_stream,
ostream& dst_stream)
{
auto our = json::parse(our_stream);
auto older = json::parse(older_stream);
auto their = json::parse(their_stream);
dst_stream << our.patch(json::diff(older, their)).dump(4);
}
void
usage(char *program)
{
cerr << "Usage: " << program << " <current> <older> <other>" << endl;
}
int main(
int argc,
char *argv[])
{
// Validate number of arguments or show usage.
if (argc < 4)
{
cerr << "Too few arguments" << endl;
usage(argv[0]);
exit(1);
}
// 3 input streams, 1 buffer and 1 output stream
ifstream our_stream;
ifstream older_stream;
ifstream their_stream;
ofstream output_stream;
ostringstream buffer_stream;
buffer_stream = ostringstream();
our_stream.open(argv[1]);
if (!our_stream.is_open())
{
cerr << "Cannot open current file " << argv[1] << endl;
exit(1);
}
older_stream.open(argv[2], fstream::in);
if (!older_stream.is_open())
{
cerr << "Cannot open older file " << argv[2] << endl;
exit(1);
}
their_stream.open(argv[3], fstream::in);
if (!their_stream.is_open())
{
cerr << "Cannot open other file " << argv[3] << endl;
exit(1);
}
// Parse and merge JSON streams
merge(our_stream, older_stream, their_stream, buffer_stream);
// Close input streams
our_stream.close();
older_stream.close();
their_stream.close();
// (Re)open our stream in output and dump buffer
output_stream.open(argv[1], ofstream::out);
if (!output_stream.is_open())
{
cerr << "Cannot open output file " << argv[1] << endl;
exit(1);
}
output_stream << buffer_stream.str();
// Close output stream
output_stream.close();
return 0;
}