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 | bool closed() const override { return false; } |
44 | |
45 | Status Tell(int64_t* position) const override { |
46 | *position = pos_; |
47 | return Status::OK(); |
48 | } |
49 | |
50 | Status Write(const void* data, int64_t nbytes) override { |
51 | pos_ += nbytes; |
52 | std::cout.write(reinterpret_cast<const char*>(data), nbytes); |
53 | return Status::OK(); |
54 | } |
55 | |
56 | private: |
57 | int64_t pos_; |
58 | }; |
59 | |
60 | // Output stream that just writes to stderr. |
61 | class ARROW_EXPORT StderrStream : public OutputStream { |
62 | public: |
63 | StderrStream() : pos_(0) { set_mode(FileMode::WRITE); } |
64 | ~StderrStream() override {} |
65 | |
66 | Status Close() override { return Status::OK(); } |
67 | bool closed() const override { return false; } |
68 | |
69 | Status Tell(int64_t* position) const override { |
70 | *position = pos_; |
71 | return Status::OK(); |
72 | } |
73 | |
74 | Status Write(const void* data, int64_t nbytes) override { |
75 | pos_ += nbytes; |
76 | std::cerr.write(reinterpret_cast<const char*>(data), nbytes); |
77 | return Status::OK(); |
78 | } |
79 | |
80 | private: |
81 | int64_t pos_; |
82 | }; |
83 | |
84 | // Input stream that just reads from stdin. |
85 | class ARROW_EXPORT StdinStream : public InputStream { |
86 | public: |
87 | StdinStream() : pos_(0) { set_mode(FileMode::READ); } |
88 | ~StdinStream() override {} |
89 | |
90 | Status Close() override { return Status::OK(); } |
91 | bool closed() const override { return false; } |
92 | |
93 | Status Tell(int64_t* position) const override { |
94 | *position = pos_; |
95 | return Status::OK(); |
96 | } |
97 | |
98 | Status Read(int64_t nbytes, int64_t* bytes_read, void* out) override { |
99 | std::cin.read(reinterpret_cast<char*>(out), nbytes); |
100 | if (std::cin) { |
101 | *bytes_read = nbytes; |
102 | pos_ += nbytes; |
103 | } else { |
104 | *bytes_read = 0; |
105 | } |
106 | return Status::OK(); |
107 | } |
108 | |
109 | Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override { |
110 | std::shared_ptr<ResizableBuffer> buffer; |
111 | ARROW_RETURN_NOT_OK(AllocateResizableBuffer(nbytes, &buffer)); |
112 | int64_t bytes_read; |
113 | ARROW_RETURN_NOT_OK(Read(nbytes, &bytes_read, buffer->mutable_data())); |
114 | ARROW_RETURN_NOT_OK(buffer->Resize(bytes_read, false)); |
115 | buffer->ZeroPadding(); |
116 | *out = buffer; |
117 | return Status::OK(); |
118 | } |
119 | |
120 | private: |
121 | int64_t pos_; |
122 | }; |
123 | |
124 | } // namespace io |
125 | |
126 | namespace internal { |
127 | |
128 | #if defined(_MSC_VER) |
129 | // namespace fs = boost::filesystem; |
130 | // #define PlatformFilename fs::path |
131 | typedef ::boost::filesystem::path PlatformFilename; |
132 | |
133 | #else |
134 | |
135 | struct PlatformFilename { |
136 | PlatformFilename() {} |
137 | explicit PlatformFilename(const std::string& path) { utf8_path = path; } |
138 | |
139 | const char* c_str() const { return utf8_path.c_str(); } |
140 | |
141 | const std::string& string() const { return utf8_path; } |
142 | |
143 | size_t length() const { return utf8_path.size(); } |
144 | |
145 | std::string utf8_path; |
146 | }; |
147 | #endif |
148 | |
149 | ARROW_EXPORT |
150 | Status FileNameFromString(const std::string& file_name, PlatformFilename* out); |
151 | |
152 | ARROW_EXPORT |
153 | Status FileOpenReadable(const PlatformFilename& file_name, int* fd); |
154 | ARROW_EXPORT |
155 | Status FileOpenWritable(const PlatformFilename& file_name, bool write_only, bool truncate, |
156 | bool append, int* fd); |
157 | |
158 | ARROW_EXPORT |
159 | Status FileRead(int fd, uint8_t* buffer, const int64_t nbytes, int64_t* bytes_read); |
160 | ARROW_EXPORT |
161 | Status FileReadAt(int fd, uint8_t* buffer, int64_t position, int64_t nbytes, |
162 | int64_t* bytes_read); |
163 | ARROW_EXPORT |
164 | Status FileWrite(int fd, const uint8_t* buffer, const int64_t nbytes); |
165 | ARROW_EXPORT |
166 | Status FileTruncate(int fd, const int64_t size); |
167 | |
168 | ARROW_EXPORT |
169 | Status FileTell(int fd, int64_t* pos); |
170 | ARROW_EXPORT |
171 | Status FileSeek(int fd, int64_t pos); |
172 | ARROW_EXPORT |
173 | Status FileSeek(int fd, int64_t pos, int whence); |
174 | ARROW_EXPORT |
175 | Status FileGetSize(int fd, int64_t* size); |
176 | |
177 | ARROW_EXPORT |
178 | Status FileClose(int fd); |
179 | |
180 | ARROW_EXPORT |
181 | Status CreatePipe(int fd[2]); |
182 | |
183 | ARROW_EXPORT |
184 | Status MemoryMapRemap(void* addr, size_t old_size, size_t new_size, int fildes, |
185 | void** new_addr); |
186 | |
187 | ARROW_EXPORT |
188 | Status GetEnvVar(const char* name, std::string* out); |
189 | ARROW_EXPORT |
190 | Status GetEnvVar(const std::string& name, std::string* out); |
191 | ARROW_EXPORT |
192 | Status SetEnvVar(const char* name, const char* value); |
193 | ARROW_EXPORT |
194 | Status SetEnvVar(const std::string& name, const std::string& value); |
195 | ARROW_EXPORT |
196 | Status DelEnvVar(const char* name); |
197 | ARROW_EXPORT |
198 | Status DelEnvVar(const std::string& name); |
199 | |
200 | } // namespace internal |
201 | } // namespace arrow |
202 | |
203 | #endif // ARROW_UTIL_IO_UTIL_H |
204 | |