-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.cpp
79 lines (64 loc) · 1.37 KB
/
source.cpp
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
// Copyright (c) Mathieu Malaterre
// SPDX-License-Identifier: BSD-3-Clause
#include "source.h"
#include <stdexcept>
#include <cassert>
#include <cstring>
namespace jlst {
source::source() : stream_(stdin)
{
}
source::source(std::string const& filename)
{
stream_ = std::fopen(filename.c_str(), "rb");
if (!stream_)
throw std::invalid_argument("bogus filename");
filename_ = filename;
}
source::~source()
{
if (!filename_.empty())
std::fclose(stream_);
}
int source::peek()
{
const int c = std::fgetc(stream_);
std::ungetc(c, stream_);
return c;
}
void source::rewind()
{
std::rewind(stream_);
}
size_t source::read(void* ptr, size_t n)
{
const size_t nr = std::fread(ptr, 1, n, stream_);
assert(n == nr);
return nr;
}
std::string source::getline()
{
char line[256];
std::fgets(line, sizeof(line), stream_);
size_t len = std::strlen(line);
if (len > 0 && line[len - 1] == '\n')
{
line[--len] = '\0';
}
return line;
}
size_t source::size()
{
std::fseek(stream_, 0, SEEK_END);
const size_t byte_count_file = std::ftell(stream_);
rewind();
return byte_count_file;
}
std::vector<uint8_t> source::read_bytes()
{
assert(std::ftell(stream_) == 0);
std::vector<uint8_t> buffer(size());
read(buffer.data(), buffer.size());
return buffer;
}
} // end namespace jlst