Blame view

3rdparty/boost_1_81_0/libs/json/example/file.hpp 3.15 KB
73ef4ff3   Hu Chunming   提交三方库
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
  //
  // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  //
  // Distributed under the Boost Software License, Version 1.0. (See accompanying
  // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  //
  // Official repository: https://github.com/boostorg/json
  //
  
  #ifndef BOOST_JSON_EXAMPLE_FILE_HPP
  #define BOOST_JSON_EXAMPLE_FILE_HPP
  
  #include <boost/json/detail/config.hpp>
  #include <cstdio>
  #include <string>
  
  class file
  {
      FILE* f_ = nullptr;
      long size_ = 0;
  
      void
      fail(boost::json::error_code& ec)
      {
          ec.assign( errno, boost::system::generic_category() );
      }
  
  public:
      ~file()
      {
          if(f_)
              std::fclose(f_);
      }
  
      file() = default;
  
      file( file&& other ) noexcept
          : f_(other.f_)
      {
          other.f_ = nullptr;
      }
  
      file( char const* path, char const* mode )
      {
          open( path, mode );
      }
  
      file&
      operator=(file&& other) noexcept
      {
          close();
          f_ = other.f_;
          other.f_ = nullptr;
          return *this;
      }
  
      void
      close()
      {
          if(f_)
          {
              std::fclose(f_);
              f_ = nullptr;
              size_ = 0;
          }
      }
  
      void
      open(
          char const* path,
          char const* mode,
          boost::json::error_code& ec)
      {
          close();
  #if defined(_MSC_VER)
  # pragma warning( push )
  # pragma warning( disable : 4996 )
  #endif
          f_ = std::fopen( path, mode );
  #if defined(_MSC_VER)
  # pragma warning( pop )
  #endif
          if( ! f_ )
              return fail(ec);
          if( std::fseek( f_, 0, SEEK_END ) != 0)
              return fail(ec);
          size_ = std::ftell( f_ );
          if( size_ == -1 )
          {
              size_ = 0;
              return fail(ec);
          }
          if( std::fseek( f_, 0, SEEK_SET ) != 0)
              return fail(ec);
      }
  
      void
      open(
          char const* path,
          char const* mode)
      {
          boost::json::error_code ec;
          open(path, mode, ec);
          if(ec)
              throw boost::json::system_error(ec);
      }
  
      long
      size() const noexcept
      {
          return size_;
      }
  
      bool
      eof() const noexcept
      {
          return std::feof( f_ ) != 0;
      }
  
      std::size_t
      read( char* data, std::size_t size, boost::json::error_code& ec)
      {
          auto const nread = std::fread( data, 1, size, f_ );
          if( std::ferror(f_) )
              ec.assign( errno, boost::system::generic_category() );
          return nread;
      }
  
      std::size_t
      read( char* data, std::size_t size )
      {
          boost::json::error_code ec;
          auto const nread = read( data, size, ec );
          if(ec)
              throw boost::json::system_error(ec);
          return nread;
      }
  };
  
  inline
  std::string
  read_file( char const* path, boost::json::error_code& ec )
  {
      file f;
      f.open( path, "r", ec );
      if(ec)
          return {};
      std::string s;
      s.resize( f.size() );
      s.resize( f.read( &s[0], s.size(), ec) );
      if(ec)
          return {};
      return s;
  }
  
  inline
  std::string
  read_file( char const* path )
  {
      boost::json::error_code ec;
      auto s = read_file( path, ec);
      if(ec)
          throw boost::json::system_error(ec);
      return s;
  }
  
  #endif