]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 | #include "arrow/io/stdio.h" | |
19 | ||
20 | #include <iostream> | |
21 | ||
22 | #include "arrow/buffer.h" | |
23 | #include "arrow/result.h" | |
24 | ||
25 | namespace arrow { | |
26 | namespace io { | |
27 | ||
28 | // | |
29 | // StdoutStream implementation | |
30 | // | |
31 | ||
32 | StdoutStream::StdoutStream() : pos_(0) { set_mode(FileMode::WRITE); } | |
33 | ||
34 | Status StdoutStream::Close() { return Status::OK(); } | |
35 | ||
36 | bool StdoutStream::closed() const { return false; } | |
37 | ||
38 | Result<int64_t> StdoutStream::Tell() const { return pos_; } | |
39 | ||
40 | Status StdoutStream::Write(const void* data, int64_t nbytes) { | |
41 | pos_ += nbytes; | |
42 | std::cout.write(reinterpret_cast<const char*>(data), nbytes); | |
43 | return Status::OK(); | |
44 | } | |
45 | ||
46 | // | |
47 | // StderrStream implementation | |
48 | // | |
49 | ||
50 | StderrStream::StderrStream() : pos_(0) { set_mode(FileMode::WRITE); } | |
51 | ||
52 | Status StderrStream::Close() { return Status::OK(); } | |
53 | ||
54 | bool StderrStream::closed() const { return false; } | |
55 | ||
56 | Result<int64_t> StderrStream::Tell() const { return pos_; } | |
57 | ||
58 | Status StderrStream::Write(const void* data, int64_t nbytes) { | |
59 | pos_ += nbytes; | |
60 | std::cerr.write(reinterpret_cast<const char*>(data), nbytes); | |
61 | return Status::OK(); | |
62 | } | |
63 | ||
64 | // | |
65 | // StdinStream implementation | |
66 | // | |
67 | ||
68 | StdinStream::StdinStream() : pos_(0) { set_mode(FileMode::READ); } | |
69 | ||
70 | Status StdinStream::Close() { return Status::OK(); } | |
71 | ||
72 | bool StdinStream::closed() const { return false; } | |
73 | ||
74 | Result<int64_t> StdinStream::Tell() const { return pos_; } | |
75 | ||
76 | Result<int64_t> StdinStream::Read(int64_t nbytes, void* out) { | |
77 | std::cin.read(reinterpret_cast<char*>(out), nbytes); | |
78 | if (std::cin) { | |
79 | pos_ += nbytes; | |
80 | return nbytes; | |
81 | } else { | |
82 | return 0; | |
83 | } | |
84 | } | |
85 | ||
86 | Result<std::shared_ptr<Buffer>> StdinStream::Read(int64_t nbytes) { | |
87 | ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(nbytes)); | |
88 | ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, Read(nbytes, buffer->mutable_data())); | |
89 | ARROW_RETURN_NOT_OK(buffer->Resize(bytes_read, false)); | |
90 | buffer->ZeroPadding(); | |
91 | return std::move(buffer); | |
92 | } | |
93 | ||
94 | } // namespace io | |
95 | } // namespace arrow |