aboutsummaryrefslogtreecommitdiff
path: root/libbutl/manifest-rewriter.cxx
blob: e38d5f4b67f1e636907e6aaacd797db50b3341ee (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
// file      : libbutl/manifest-rewriter.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

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

#include <cassert>

// C includes.

#ifndef __cpp_lib_modules_ts
#include <string>
#include <cstdint> // uint64_t
#include <cstddef> // size_t
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.manifest_rewriter;

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

import butl.utility;             // utf8_length()
import butl.manifest_serializer;
#else
#include <libbutl/utility.mxx>
#include <libbutl/manifest-serializer.mxx>
#endif

using namespace std;

namespace butl
{
  manifest_rewriter::
  manifest_rewriter (path p, bool long_lines)
      : path_ (move (p)),
        long_lines_ (long_lines),
        fd_ (fdopen (path_,
                     fdopen_mode::in  |
                     fdopen_mode::out |
                     fdopen_mode::exclusive))
  {
  }

  // Seek the file descriptor to the specified logical position and truncate
  // the file. Return the file suffix (cached prior to truncating) starting
  // from the specified position.
  //
  static string
  truncate (auto_fd& fd, uint64_t pos, uint64_t suffix_pos)
  {
    string r;
    {
      // Temporary move the descriptor into the stream.
      //
      ifdstream is (move (fd));
      fdbuf& buf (static_cast<fdbuf&> (*is.rdbuf ()));

      // Read suffix.
      //
      buf.seekg (suffix_pos);
      r = is.read_text ();

      // Seek to the specified position and move the file descriptor back.
      //
      buf.seekg (pos);
      fd = is.release ();
    }

    // Truncate the file starting from the current position. Note that we need
    // to use the physical position rather than logical.
    //
    fdtruncate (fd.get (), fdseek (fd.get (), 0, fdseek_mode::cur));
    return r;
  }

  void manifest_rewriter::
  replace (const manifest_name_value& nv)
  {
    assert (nv.colon_pos != 0); // Sanity check.

    // Truncate right after the value colon.
    //
    string suffix (truncate (fd_, nv.colon_pos + 1, nv.end_pos));

    // Temporary move the descriptor into the stream.
    //
    ofdstream os (move (fd_));

    if (!nv.value.empty ())
    {
      os << ' ';

      manifest_serializer s (os, path_.string (), long_lines_);

      // Note that the name can be surrounded with the ASCII whitespace
      // characters and the start_pos refers to the first character in the
      // line.
      //
      // Also note that we assume the already serialized name to be a valid
      // UTF-8 byte string and so utf8_length() may not throw.
      //
      s.write_value (nv.value,
                     static_cast<size_t> (nv.colon_pos - nv.start_pos) -
                     (nv.name.size () - utf8_length (nv.name)) + 2);
    }

    os << suffix;

    // Move the file descriptor back.
    //
    fd_ = os.release (); // Note: flushes the buffer.
  }

  void manifest_rewriter::
  insert (const manifest_name_value& pos, const manifest_name_value& nv)
  {
    assert (pos.end_pos != 0); // Sanity check.

    // We could have just started writing over the suffix but the truncation
    // doesn't hurt.
    //
    string suffix (truncate (fd_, pos.end_pos, pos.end_pos));

    // Temporary move the descriptor into the stream.
    //
    ofdstream os (move (fd_));
    os << '\n';

    manifest_serializer s (os, path_.string (), long_lines_);
    size_t n (s.write_name (nv.name));

    os << ':';

    if (!nv.value.empty ())
    {
      os << ' ';

      // Note that the name can be surrounded with the ASCII whitespace
      // characters and the start_pos refers to the first character in the
      // line.
      //
      s.write_value (nv.value,
                     static_cast<size_t> (nv.colon_pos - nv.start_pos) -
                     (nv.name.size () - n) + 2);
    }

    os << suffix;

    // Move the file descriptor back.
    //
    fd_ = os.release (); // Note: flushes the buffer.
  }
}