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 StdoutStream : public OutputStream {
38  public:
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 StderrStream : public OutputStream {
61  public:
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 StdinStream : public InputStream {
84  public:
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  auto buffer = std::make_shared<PoolBuffer>(NULLPTR);
108  RETURN_NOT_OK(buffer->Resize(nbytes));
109  int64_t bytes_read;
110  RETURN_NOT_OK(Read(nbytes, &bytes_read, buffer->mutable_data()));
111  RETURN_NOT_OK(buffer->Resize(bytes_read, false));
112  *out = buffer;
113  return Status::OK();
114  }
115 
116  private:
117  int64_t pos_;
118 };
119 
120 } // namespace io
121 
122 namespace internal {
123 
124 #if defined(_MSC_VER)
125 // namespace fs = boost::filesystem;
126 // #define PlatformFilename fs::path
127 typedef ::boost::filesystem::path PlatformFilename;
128 
129 #else
130 
131 struct PlatformFilename {
132  PlatformFilename() {}
133  explicit PlatformFilename(const std::string& path) { utf8_path = path; }
134 
135  const char* c_str() const { return utf8_path.c_str(); }
136 
137  const std::string& string() const { return utf8_path; }
138 
139  size_t length() const { return utf8_path.size(); }
140 
141  std::string utf8_path;
142 };
143 #endif
144 
145 Status FileNameFromString(const std::string& file_name, PlatformFilename* out);
146 
147 Status FileOpenReadable(const PlatformFilename& file_name, int* fd);
148 Status FileOpenWriteable(const PlatformFilename& file_name, bool write_only,
149  bool truncate, bool append, int* fd);
150 
151 Status FileRead(int fd, uint8_t* buffer, const int64_t nbytes, int64_t* bytes_read);
152 Status FileReadAt(int fd, uint8_t* buffer, int64_t position, int64_t nbytes,
153  int64_t* bytes_read);
154 Status FileWrite(int fd, const uint8_t* buffer, const int64_t nbytes);
155 Status FileTruncate(int fd, const int64_t size);
156 
157 Status FileTell(int fd, int64_t* pos);
158 Status FileSeek(int fd, int64_t pos);
159 Status FileSeek(int fd, int64_t pos, int whence);
160 Status FileGetSize(int fd, int64_t* size);
161 
162 Status FileClose(int fd);
163 
164 Status CreatePipe(int fd[2]);
165 
166 Status GetEnvVar(const char* name, std::string* out);
167 Status GetEnvVar(const std::string& name, std::string* out);
168 Status SetEnvVar(const char* name, const char* value);
169 Status SetEnvVar(const std::string& name, const std::string& value);
170 Status DelEnvVar(const char* name);
171 Status DelEnvVar(const std::string& name);
172 
173 } // namespace internal
174 } // namespace arrow
175 
176 #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
#define NULLPTR
Definition: macros.h:69
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:93
~StderrStream() override
Definition: io-util.h:63
Status Tell(int64_t *position) const override
Definition: io-util.h:67
Definition: io-util.h:83
#define RETURN_NOT_OK(s)
Definition: status.h:43
static Status OK()
Definition: status.h:106
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
void set_mode(FileMode::type mode)
Definition: interfaces.h:77
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
~StdoutStream() override
Definition: io-util.h:40
Status Close() override
Definition: io-util.h:65
Definition: interfaces.h:37