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
33namespace arrow {
34namespace io {
35
36// Output stream that just writes to stdout.
37class 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.
61class 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.
85class 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
126namespace internal {
127
128#if defined(_MSC_VER)
129// namespace fs = boost::filesystem;
130// #define PlatformFilename fs::path
131typedef ::boost::filesystem::path PlatformFilename;
132
133#else
134
135struct 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
149ARROW_EXPORT
150Status FileNameFromString(const std::string& file_name, PlatformFilename* out);
151
152ARROW_EXPORT
153Status FileOpenReadable(const PlatformFilename& file_name, int* fd);
154ARROW_EXPORT
155Status FileOpenWritable(const PlatformFilename& file_name, bool write_only, bool truncate,
156 bool append, int* fd);
157
158ARROW_EXPORT
159Status FileRead(int fd, uint8_t* buffer, const int64_t nbytes, int64_t* bytes_read);
160ARROW_EXPORT
161Status FileReadAt(int fd, uint8_t* buffer, int64_t position, int64_t nbytes,
162 int64_t* bytes_read);
163ARROW_EXPORT
164Status FileWrite(int fd, const uint8_t* buffer, const int64_t nbytes);
165ARROW_EXPORT
166Status FileTruncate(int fd, const int64_t size);
167
168ARROW_EXPORT
169Status FileTell(int fd, int64_t* pos);
170ARROW_EXPORT
171Status FileSeek(int fd, int64_t pos);
172ARROW_EXPORT
173Status FileSeek(int fd, int64_t pos, int whence);
174ARROW_EXPORT
175Status FileGetSize(int fd, int64_t* size);
176
177ARROW_EXPORT
178Status FileClose(int fd);
179
180ARROW_EXPORT
181Status CreatePipe(int fd[2]);
182
183ARROW_EXPORT
184Status MemoryMapRemap(void* addr, size_t old_size, size_t new_size, int fildes,
185 void** new_addr);
186
187ARROW_EXPORT
188Status GetEnvVar(const char* name, std::string* out);
189ARROW_EXPORT
190Status GetEnvVar(const std::string& name, std::string* out);
191ARROW_EXPORT
192Status SetEnvVar(const char* name, const char* value);
193ARROW_EXPORT
194Status SetEnvVar(const std::string& name, const std::string& value);
195ARROW_EXPORT
196Status DelEnvVar(const char* name);
197ARROW_EXPORT
198Status DelEnvVar(const std::string& name);
199
200} // namespace internal
201} // namespace arrow
202
203#endif // ARROW_UTIL_IO_UTIL_H
204