-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.cpp
75 lines (58 loc) · 1.49 KB
/
utils.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include "error.h"
void load_file(std::string filename_in, unsigned char **data, uint64_t *len)
{
const char *filename = filename_in.c_str();
FILE *fh = fopen(filename, "r");
if (!fh)
error_exit("Problem opening file %s", filename);
int fd = fileno(fh);
struct stat st;
if (fstat(fd, &st) == -1)
error_exit("Stat on file %s failed", filename);
*len = st.st_size;
*data = new unsigned char[*len + 1];
if (fread(*data, 1, *len, fh) != size_t(*len))
error_exit("Short read on %s", filename);
fclose(fh);
(*data)[*len] = 0x00;
}
std::string format(const char *fmt, ...)
{
char *buffer = NULL;
va_list ap;
va_start(ap, fmt);
(void)vasprintf(&buffer, fmt, ap);
va_end(ap);
std::string result = buffer;
free(buffer);
return result;
}
double get_ts()
{
struct timeval ts;
if (gettimeofday(&ts, NULL) == -1)
error_exit("gettimeofday failed");
return double(ts.tv_sec) + double(ts.tv_usec) / 1000000.0;
}
std::string to_bin_str(uint64_t value, uint8_t bits)
{
char buffer[65] = { 0 };
int out = 0;
for(int bit=bits-1; bit>=0; bit--)
{
if (value & (1 << bit))
buffer[out++] = '1';
else
buffer[out++] = '0';
}
return buffer;
}