Repository navigation
Expand file tree
/
Copy pathstringutil.h
More file actions
93 lines (80 loc) · 2.42 KB
/
Copy pathstringutil.h
File metadata and controls
93 lines (80 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright © 2019-2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <iostream>
#include <iomanip>
namespace vortex {
class ByteStream : public std::istream {
public:
ByteStream(const void *buf, std::size_t size) : buf_(buf), size_(size) {}
friend std::ostream& operator<<(std::ostream& os, const ByteStream& obj) {
auto oldflags = os.flags();
auto oldwidth = os.width();
auto oldfill = os.fill();
for (std::size_t i = 0, n = obj.size_; i < n; ++i) {
int byte = *((uint8_t*)obj.buf_ + (n - 1 - i));
os << std::hex << std::setw(2) << std::setfill('0') << byte;
}
os.fill(oldfill);
os.width(oldwidth);
os.flags(oldflags);
return os;
}
private:
const void *buf_;
std::size_t size_;
};
class IndentStream : public std::streambuf {
public:
explicit IndentStream(std::streambuf* dest, int indent = 4)
: dest_(dest)
, isBeginLine_(true)
, indent_(indent, ' ')
, owner_(nullptr)
{}
explicit IndentStream(std::ostream& dest, int indent = 4)
: dest_(dest.rdbuf())
, isBeginLine_(true)
, indent_(indent, ' ')
, owner_(&dest) {
owner_->rdbuf(this);
}
virtual ~IndentStream() {
if (owner_)
owner_->rdbuf(dest_);
}
protected:
virtual int overflow(int ch) {
if (isBeginLine_ && ch != '\n') {
dest_->sputn(indent_.data(), indent_.size());
}
isBeginLine_ = ch == '\n';
return dest_->sputc(ch);
}
private:
std::streambuf* dest_;
bool isBeginLine_;
std::string indent_;
std::ostream* owner_;
};
template <typename... Args>
std::string StrFormat(const std::string& fmt, Args... args) {
auto size = std::snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
if (size <= 0) {
throw std::runtime_error("Error during formatting.");
}
std::vector<char> buf(size);
std::snprintf(buf.data(), size, fmt.c_str(), args...);
return std::string(buf.data(), buf.data() + size - 1);
}
}