aboutsummaryrefslogtreecommitdiff
path: root/libbutl/manifest-serializer.cxx
blob: 05cdae5e76028498a19043465d2d3848311a9dc3 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// file      : libbutl/manifest-serializer.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbutl/manifest-serializer.hxx>

#include <ostream>

#include <libbutl/utf8.hxx>
#include <libbutl/utility.hxx>

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;
        }

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

        if (!v.empty ())
        {
          os_ << ' ';
          write_value (v, l + 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");

    string what;
    if (!utf8 (t, what, codepoint_types::graphic, U"\n\r\t"))
      throw serialization (name_, "invalid comment: " + what);

    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;
  }

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

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

    size_t r (0);
    pair<bool, bool> v;
    utf8_validator val (codepoint_types::graphic, U"\n\r\t");

    string what;
    for (char c: n)
    {
      v = val.validate (c, what);

      if (!v.first)
        throw serialization (name_, "invalid name: " + what);

      if (v.second) // Sequence last byte?
      {
        // Note: ASCII characters may not be a part of a multi-byte sequence.
        //
        switch (c)
        {
        case ' ':
        case '\t':
        case '\r':
        case '\n': throw serialization (name_, "name contains whitespace");
        case ':':  throw serialization (name_, "name contains ':'");
        default:   break;
        }

        ++r;
      }
    }

    // Make sure that the last UTF-8 sequence is complete.
    //
    if (!v.second)
      throw serialization (name_, "invalid name: incomplete UTF-8 sequence");

    os_ << n;
    return r;
  }

  void manifest_serializer::
  write_value (const char* s, size_t n, size_t cl)
  {
    utf8_validator val (codepoint_types::graphic, U"\n\r\t");

    char c ('\0');
    bool b (true); // Begin of UTF-8 byte sequence.

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

      pair<bool, bool> v (val.validate (c, what));

      if (!v.first)
        throw serialization (name_, "invalid value: " + what);

      // 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. We also delay until the beginning of a UTF-8
      // sequence.
      //
      if (pc != '\\' && b && !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;

      b = v.second;

      if (b)
        ++cl;
    }

    // Make sure that the last UTF-8 sequence is complete.
    //
    if (!b)
      throw serialization (name_, "invalid value: incomplete UTF-8 sequence");

    // 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) codepoints; 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.
  }
}