aboutsummaryrefslogtreecommitdiff
path: root/libbutl/manifest-serializer.cxx
blob: c17f4e91e58b48a4bc48fad649c174a7569ee134 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// file      : libbutl/manifest-serializer.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/manifest-serializer.mxx>
#endif

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <string>
#include <vector>
#include <cstddef>
#include <stdexcept>

#include <ostream>
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.manifest_serializer;

// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.manifest_types;
#endif

#endif

using namespace std;

namespace butl
{
  using serialization = manifest_serialization;

  void manifest_serializer::
  write_next (const string& n, const string& v)
  {
    switch (s_)
    {
    case start:
      {
        if (!n.empty ())
          throw serialization (name_, "format version pair expected");

        if (v.empty ())
        {
          // End of manifests.
          //
          os_.flush ();
          s_ = end;
          break;
        }

        if (v != "1")
          throw serialization (name_, "unsupported format version " + v);

        os_ << ':';

        if (v != version_)
        {
          os_ << ' ' << v;
          version_ = v;
        }

        os_ << endl;
        s_ = body;
        break;
      }
    case body:
      {
        if (n.empty ())
        {
          s_ = start;

          // Start new manifest if the end-of-manifest pair is omitted.
          //
          if (!v.empty ())
            return next (n, v);

          break;
        }

        write_name (n);
        os_ << ':';

        if (!v.empty ())
        {
          os_ << ' ';
          write_value (v, n.size () + 2);
        }

        os_ << endl;
        break;
      }
    case end:
      {
        throw serialization (name_, "serialization after eos");
      }
    }
  }

  void manifest_serializer::
  comment (const string& t)
  {
    if (s_ == end)
      throw serialization (name_, "serialization after eos");

    os_ << '#';

    if (!t.empty ())
      os_ << ' ' << t;

    os_ << endl;
  }

  string manifest_serializer::
  merge_comment (const string& value, const string& comment)
  {
    string r;
    for (char c: value)
    {
      // Escape ';' character.
      //
      if (c == ';')
        r += '\\';

      r += c;
    }

    // Add the comment.
    //
    if (!comment.empty ())
    {
      r += "; ";
      r += comment;
    }

    return r;
  }

  void manifest_serializer::
  write_name (const string& n)
  {
    if (n.empty ())
      throw serialization (name_, "empty name");

    if (n[0] == '#')
      throw serialization (name_, "name starts with '#'");

    for (char c: n)
    {
      switch (c)
      {
      case ' ':
      case '\t':
      case '\r':
      case '\n': throw serialization (name_, "name contains whitespace");
      case ':':  throw serialization (name_, "name contains ':'");
      default:   break;
      }
    }

    os_ << n;
  }

  void manifest_serializer::
  write_value (const char* s, size_t n, size_t cl)
  {
    char c ('\0');

    // The idea is to break on the 77th character (i.e., write it
    // on the next line) which means we have written 76 characters
    // on this line plus 2 for '\' and '\n', which gives us 78.
    //
    for (const char* e (s + n); s != e; s++, cl++)
    {
      char pc (c);
      c = *s;

      // Note that even the "hard" break (see below) is not that hard when it
      // comes to breaking the line right after the backslash. Doing so would
      // inject the redundant newline character, as the line-terminating
      // backslash would be escaped. So we delay breaking till the next
      // non-backslash character.
      //
      if (pc != '\\' && !long_lines_)
      {
        bool br (false); // Break the line.

        // If this is a whitespace, see if it's a good place to break the
        // line.
        //
        if (c == ' ' || c == '\t')
        {
          // Find the next whitespace (or the end) and see if it is a better
          // place.
          //
          for (const char* w (s + 1); ; w++)
          {
            if (w == e || *w == ' ' || *w == '\t')
            {
              // Is this whitespace past where we need to break? Also see
              // below the "hard" break case for why we use 78 at the end.
              //
              if (cl + static_cast<size_t> (w - s) > (w != e ? 77U : 78U))
              {
                // Only break if this whitespace is close enough to
                // the end of the line.
                //
                br = (cl > 57);
              }

              break;
            }
          }
        }

        // Do we have to do a "hard" break (i.e., without a whitespace)?
        // If there is just one character left, then instead of writing
        // '\' and then the character on the next line, we might as well
        // write it on this line.
        //
        if (cl >= (s + 1 != e ? 77U : 78U))
          br = true;

        if (br)
        {
          os_ << '\\' << endl;
          cl = 0;
        }
      }

      os_ << c;
    }

    // What comes next is always a newline. If the last character that
    // we have written is a backslash, escape it.
    //
    if (c == '\\')
      os_ << '\\';
  }

  void manifest_serializer::
  write_value (const string& v, size_t cl)
  {
    // Consider both \r and \n characters as line separators, and the
    // \r\n characters sequence as a single line separator.
    //
    auto nl = [&v] (size_t p = 0) {return v.find_first_of ("\r\n", p);};

    // Use the multi-line mode in any of the following cases:
    //
    // - column offset is too large (say greater than 39 (78/2) characters; we
    //   cannot start on the next line since that would start the multi-line
    //   mode)
    // - value contains newlines
    // - value contains leading/trailing whitespaces
    //
    // Should we use the multi-line mode for large column offsets if the long
    // lines flag is set? Probably yes, as this improves the manifest
    // readability, still allowing the user to easily copy the value which
    // seems to be the main reason for using the flag.
    //
    if (cl > 39 || nl () != string::npos ||
        v.front () == ' ' || v.front () == '\t' ||
        v.back () == ' ' || v.back () == '\t')
    {
      os_ << "\\" << endl; // Multi-line mode introductor.

      // Chunk the value into fragments separated by newlines.
      //
      for (size_t i (0), p (nl ()); ; p = nl (i))
      {
        if (p == string::npos)
        {
          // Last chunk.
          //
          write_value (v.c_str () + i, v.size () - i, 0);
          break;
        }

        write_value (v.c_str () + i, p - i, 0);
        os_ << endl;

        i = p + (v[p] == '\r' && v[p + 1] == '\n' ? 2 : 1);
      }

      os_ << endl << "\\"; // Multi-line mode terminator.
    }
    else
      write_value (v.c_str (), v.size (), cl);
  }

  // manifest_serialization
  //

  static inline string
  format (const string& n, const string& d)
  {
    string r;
    if (!n.empty ())
    {
      r += n;
      r += ": ";
    }
    r += "error: ";
    r += d;
    return r;
  }

  manifest_serialization::
  manifest_serialization (const string& n, const string& d)
      : runtime_error (format (n, d)), name (n), description (d)
  {
  }

  // serialize_manifest
  //
  void
  serialize_manifest (manifest_serializer& s,
                      const vector<manifest_name_value>& nvs,
                      bool eos)
  {
    s.next ("", "1"); // Start of manifest.

    for (const manifest_name_value& nv: nvs)
      s.next (nv.name, nv.value);

    s.next ("", ""); // End of manifest.

    if (eos)
      s.next ("", ""); // End of stream.
  }
}