Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_writer.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f83911e6224834024643c561eb5975076710cbee (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
#pragma once

#include "coding/writer.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/write_to_sink.hpp"

#include "base/assert.hpp"
#include "base/macros.hpp"

#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>

// FileWriter, not thread safe.
class FileWriter : public Writer
{
  DISALLOW_COPY(FileWriter);

public:
  // Values actually match internal FileData::Op enum.
  enum Op
  {
    // Create an empty file for writing. If a file with the same name already exists
    // its content is erased and the file is treated as a new empty file.
    OP_WRITE_TRUNCATE = 1,

    // Open a file for update. The file is created if it does not exist.
    OP_WRITE_EXISTING = 2,

    // Append to a file. Writing operations append data at the end of the file.
    // The file is created if it does not exist.
    // Seek should not be called, if file is opened for append.
    OP_APPEND = 3
  };

  explicit FileWriter(std::string const & fileName,
                      Op operation = OP_WRITE_TRUNCATE);
  FileWriter(FileWriter && rhs) = default;

  ~FileWriter() override;

  // Writer overrides:
  void Seek(uint64_t pos) override;
  uint64_t Pos() const override;
  void Write(void const * p, size_t size) override;

  virtual uint64_t Size() const;
  virtual void Flush();

  std::string const & GetName() const;

  static void DeleteFileX(std::string const & fName);

protected:
  base::FileData & GetFileData();
  base::FileData const & GetFileData() const;

private:
  std::unique_ptr<base::FileData> m_pFileData;
};

class FilesContainerWriter : public FileWriter
{
public:
  FilesContainerWriter(std::string const & fileName, Op operation)
    : FileWriter(fileName, operation)
  {
  }

  void WritePaddingByEnd(size_t factor) { WritePadding(Size(), factor); }
  void WritePaddingByPos(size_t factor) { WritePadding(Pos(), factor); }

private:
  void WritePadding(uint64_t offset, uint64_t factor)
  {
    ASSERT_GREATER(factor, 1, ());
    uint64_t const padding = ((offset + factor - 1) / factor) * factor - offset;
    if (padding == 0)
      return;
    WriteZeroesToSink(*this, padding);
  }
};

class TruncatingFileWriter : public FilesContainerWriter
{
public:
  explicit TruncatingFileWriter(std::string const & fileName)
    : FilesContainerWriter(fileName, FileWriter::OP_WRITE_EXISTING)
  {
  }

  TruncatingFileWriter(TruncatingFileWriter && rhs) = default;

  // Writer overrides:
  ~TruncatingFileWriter() override
  {
    GetFileData().Flush();
    GetFileData().Truncate(Pos());
  }
};