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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
// file : tests/manifest-rewriter/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <cassert>
#ifndef __cpp_lib_modules_ts
#include <vector>
#include <string>
#include <cstdint> // uint64_t
#include <utility> // move()
#include <iostream>
#include <exception>
#endif
// Other includes.
#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.path;
import butl.optional;
import butl.fdstream;
import butl.manifest_parser;
import butl.manifest_rewriter;
#else
#include <libbutl/path.mxx>
#include <libbutl/optional.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/manifest-parser.mxx>
#include <libbutl/manifest-rewriter.mxx>
#endif
using namespace std;
namespace butl
{
using butl::optional;
using butl::nullopt;
// Value rewriting or insertion command.
//
struct edit_cmd
{
string name;
string value;
optional<string> after; // Rewrite an existing value if nullopt.
edit_cmd (string n, string v)
: name (move (n)), value (move (v)) {}
edit_cmd (string n, string v, string a)
: name (move (n)), value (move (v)), after (move (a)) {}
};
using edit_cmds = vector<edit_cmd>;
// Dump the manifest into the file, edit and return the resulting manifest.
//
// The file will stay in the filesystem for troubleshooting in case of an
// assertion failure and will be deleted otherwise.
//
static path temp_file (path::temp_path ("butl-manifest-rewriter"));
static string
edit (const char* manifest, const edit_cmds&);
int
main ()
{
auto_rmfile rm (temp_file);
assert (edit (":1\n# Comment\n# Comment\n a : b \n# Comment\n\nc:d\n",
{{"a", "xyz"}}) ==
":1\n# Comment\n# Comment\n a : xyz\n# Comment\n\nc:d\n");
assert (edit (":1\n\n a: b\n", {{"a", "xyz"}}) == ":1\n\n a: xyz\n");
assert (edit (":1\na: b", {{"a", "xyz"}}) == ":1\na: xyz");
assert (edit (":1\na:b\nc:d\ne:f",
{{"a", "xyz"}, edit_cmd {"x", "y", "c"}, {"e", "123"}}) ==
":1\na: xyz\nc:d\nx: y\ne: 123");
assert (edit (":1\na: b", {{"a", "xy\nz"}}) == ":1\na: \\\nxy\nz\n\\");
assert (edit (":1\n", {{"a", "b", ""}}) == ":1\na: b\n");
assert (edit (":1\n abc: b",
{{"abc", "xyz"}}) ==
":1\n abc: \\\nxyz\n\\");
// Test editing of manifests that contains CR characters.
//
assert (edit (":1\r\na: b\r\r\n", {{"a", "xyz"}}) == ":1\r\na: xyz\r\r\n");
assert (edit (":1\ra: b\r", {{"a", "xyz"}}) == ":1\ra: xyz\r");
assert (edit (":1\na: \\s", {{"a", "xyz"}}) == ":1\na: xyz");
assert (edit (":1\na: \\\nx\ny\nz\n\\\r", {{"a", "b"}}) == ":1\na: b\r");
return 0;
}
static string
edit (const char* manifest, const edit_cmds& cmds)
{
{
ofdstream os (temp_file);
os << manifest;
os.close ();
}
struct insertion
{
manifest_name_value value;
optional<manifest_name_value> pos; // Rewrite existing value if nullopt.
};
vector<insertion> insertions;
{
ifdstream is (temp_file);
manifest_parser p (is, temp_file.string ());
for (manifest_name_value nv; !(nv = p.next ()).empty (); )
{
for (const edit_cmd& c: cmds)
{
if (c.after)
{
if (nv.name == *c.after)
{
// Note: new value lines, columns and positions are all zero as
// are not used for an insertion.
//
insertions.push_back (
insertion {
manifest_name_value {c.name, c.value, 0, 0, 0, 0, 0, 0, 0},
move (nv)});
break;
}
}
else if (nv.name == c.name)
{
nv.value = c.value;
insertions.push_back (insertion {move (nv), nullopt /* pos */});
break;
}
}
}
}
{
manifest_rewriter rw (temp_file);
for (const auto& ins: reverse_iterate (insertions))
{
if (ins.pos)
rw.insert (*ins.pos, ins.value);
else
rw.replace (ins.value);
}
}
ifdstream is (temp_file);
return is.read_text ();
}
}
int
main ()
{
try
{
return butl::main ();
}
catch (const exception& e)
{
cerr << e << endl;
return 1;
}
}
|