-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.cpp
46 lines (42 loc) · 1 KB
/
helpers.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
/* Copyright (c) 2021 Martin Profittlich. All rights reserved. */
/* The file LICENSE contains more information about licensing. */
#include <iostream>
#include <cstdio>
#include "helpers.h"
bool onlyDigits(std::string s)
{
bool result = s.size () > 0;
for (char c : s)
{
result &= (c >= '0') && (c <= '9');
}
return result;
}
std::vector<uint8_t> loadFile(std::string fn)
{
std::vector<uint8_t> result;
FILE * f = std::fopen (fn.c_str(), "rb");
if (f)
{
std::fseek(f, 0, SEEK_END);
size_t len = std::ftell(f);
std::fseek(f, 0, SEEK_SET);
result.resize (len);
std::fread(result.data(), len, 1, f);
std::fclose(f);
}
return result;
}
void writeFile(std::string fn, std::vector<uint8_t> data)
{
FILE * f = std::fopen (fn.c_str(), "wb");
if (f)
{
std::fwrite(data.data(), data.size(), 1, f);
std::fclose(f);
}
else
{
std::cerr << "Could not write " << fn << std::endl;
}
}