Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
io-util.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_UTIL_IO_UTIL_H
19 #define ARROW_UTIL_IO_UTIL_H
20 
21 #include <iostream>
22 #include <memory>
23 #include <string>
24 
25 #include "arrow/buffer.h"
26 #include "arrow/io/interfaces.h"
27 #include "arrow/status.h"
28 
29 #if defined(_MSC_VER)
30 #include <boost/filesystem.hpp> // NOLINT
31 #endif
32 
33 namespace arrow {
34 namespace io {
35 
36 // Output stream that just writes to stdout.
37 class ARROW_EXPORT StdoutStream : public OutputStream {
38  public:
39  StdoutStream() : pos_(0) { set_mode(FileMode::WRITE); }
40  ~StdoutStream() override {}
41 
42  Status Close() override { return Status::OK(); }
43 
44  Status Tell(int64_t* position) const override {
45  *position = pos_;
46  return Status::OK();
47  }
48 
49  Status Write(const void* data, int64_t nbytes) override {
50  pos_ += nbytes;
51  std::cout.write(reinterpret_cast<const char*>(data), nbytes);
52  return Status::OK();
53  }
54 
55  private:
56  int64_t pos_;
57 };
58 
59 // Output stream that just writes to stderr.
60 class ARROW_EXPORT StderrStream : public OutputStream {
61  public:
62  StderrStream() : pos_(0) { set_mode(FileMode::WRITE); }
63  ~StderrStream() override {}
64 
65  Status Close() override { return Status::OK(); }
66 
67  Status Tell(int64_t* position) const override {
68  *position = pos_;
69  return Status::OK();
70  }
71 
72  Status Write(const void* data, int64_t nbytes) override {
73  pos_ += nbytes;
74  std::cerr.write(reinterpret_cast<const char*>(data), nbytes);
75  return Status::OK();
76  }
77 
78  private:
79  int64_t pos_;
80 };
81 
82 // Input stream that just reads from stdin.
83 class ARROW_EXPORT StdinStream : public InputStream {
84  public:
85  StdinStream() : pos_(0) { set_mode(FileMode::READ); }
86  ~StdinStream() override {}
87 
88  Status Close() override { return Status::OK(); }
89 
90  Status Tell(int64_t* position) const override {
91  *position = pos_;
92  return Status::OK();
93  }
94 
95  Status Read(int64_t nbytes, int64_t* bytes_read, void* out) override {
96  std::cin.read(reinterpret_cast<char*>(out), nbytes);
97  if (std::cin) {
98  *bytes_read = nbytes;
99  pos_ += nbytes;
100  } else {
101  *bytes_read = 0;
102  }
103  return Status::OK();
104  }
105 
106  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override {
107  std::shared_ptr<ResizableBuffer> buffer;
109  int64_t bytes_read;
110  ARROW_RETURN_NOT_OK(Read(nbytes, &bytes_read, buffer->mutable_data()));
111  ARROW_RETURN_NOT_OK(buffer->Resize(bytes_read, false));
112  buffer->ZeroPadding();
113  *out = buffer;
114  return Status::OK();
115  }
116 
117  private:
118  int64_t pos_;
119 };
120 
121 } // namespace io
122 
123 namespace internal {
124 
125 #if defined(_MSC_VER)
126 // namespace fs = boost::filesystem;
127 // #define PlatformFilename fs::path
128 typedef ::boost::filesystem::path PlatformFilename;
129 
130 #else
131 
132 struct PlatformFilename {
133  PlatformFilename() {}
134  explicit PlatformFilename(const std::string& path) { utf8_path = path; }
135 
136  const char* c_str() const { return utf8_path.c_str(); }
137 
138  const std::string& string() const { return utf8_path; }
139 
140  size_t length() const { return utf8_path.size(); }
141 
142  std::string utf8_path;
143 };
144 #endif
145 
146 ARROW_EXPORT
147 Status FileNameFromString(const std::string& file_name, PlatformFilename* out);
148 
149 ARROW_EXPORT
150 Status FileOpenReadable(const PlatformFilename& file_name, int* fd);
151 ARROW_EXPORT
152 Status FileOpenWritable(const PlatformFilename& file_name, bool write_only, bool truncate,
153  bool append, int* fd);
154 
155 ARROW_EXPORT
156 Status FileRead(int fd, uint8_t* buffer, const int64_t nbytes, int64_t* bytes_read);
157 ARROW_EXPORT
158 Status FileReadAt(int fd, uint8_t* buffer, int64_t position, int64_t nbytes,
159  int64_t* bytes_read);
160 ARROW_EXPORT
161 Status FileWrite(int fd, const uint8_t* buffer, const int64_t nbytes);
162 ARROW_EXPORT
163 Status FileTruncate(int fd, const int64_t size);
164 
165 ARROW_EXPORT
166 Status FileTell(int fd, int64_t* pos);
167 ARROW_EXPORT
168 Status FileSeek(int fd, int64_t pos);
169 ARROW_EXPORT
170 Status FileSeek(int fd, int64_t pos, int whence);
171 ARROW_EXPORT
172 Status FileGetSize(int fd, int64_t* size);
173 
174 ARROW_EXPORT
175 Status FileClose(int fd);
176 
177 ARROW_EXPORT
178 Status CreatePipe(int fd[2]);
179 
180 ARROW_EXPORT
181 Status MemoryMapRemap(void* addr, size_t old_size, size_t new_size, int fildes,
182  void** new_addr);
183 
184 ARROW_EXPORT
185 Status GetEnvVar(const char* name, std::string* out);
186 ARROW_EXPORT
187 Status GetEnvVar(const std::string& name, std::string* out);
188 ARROW_EXPORT
189 Status SetEnvVar(const char* name, const char* value);
190 ARROW_EXPORT
191 Status SetEnvVar(const std::string& name, const std::string& value);
192 ARROW_EXPORT
193 Status DelEnvVar(const char* name);
194 ARROW_EXPORT
195 Status DelEnvVar(const std::string& name);
196 
197 } // namespace internal
198 } // namespace arrow
199 
200 #endif // ARROW_UTIL_IO_UTIL_H
Status Read(int64_t nbytes, std::shared_ptr< Buffer > *out) override
Definition: io-util.h:106
Definition: interfaces.h:111
Definition: io-util.h:60
Definition: interfaces.h:116
Status Tell(int64_t *position) const override
Definition: io-util.h:44
Status Close() override
Definition: io-util.h:88
Status Write(const void *data, int64_t nbytes) override
Definition: io-util.h:49
StdinStream()
Definition: io-util.h:85
Definition: interfaces.h:37
Definition: status.h:95
~StderrStream() override
Definition: io-util.h:63
Status Tell(int64_t *position) const override
Definition: io-util.h:67
Definition: io-util.h:83
static Status OK()
Definition: status.h:124
Status Write(const void *data, int64_t nbytes) override
Definition: io-util.h:72
StderrStream()
Definition: io-util.h:62
Status Tell(int64_t *position) const override
Definition: io-util.h:90
StdoutStream()
Definition: io-util.h:39
Status Read(int64_t nbytes, int64_t *bytes_read, void *out) override
Definition: io-util.h:95
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
Status Close() override
Definition: io-util.h:42
~StdinStream() override
Definition: io-util.h:86
Definition: io-util.h:37
#define ARROW_RETURN_NOT_OK(s)
Definition: status.h:44
~StdoutStream() override
Definition: io-util.h:40
Status Close() override
Definition: io-util.h:65
Definition: interfaces.h:37
Status AllocateResizableBuffer(MemoryPool *pool, const int64_t size, std::shared_ptr< ResizableBuffer > *out)
Allocate a resizeable buffer from a memory pool, zero its padding.