osmo-sdr/utils/rum-ba/src/utils.c @ master
1 |
#include <stdio.h>
|
---|---|
2 |
#include <errno.h>
|
3 |
#include <string.h>
|
4 |
#include <time.h>
|
5 |
#include <sys/stat.h>
|
6 |
#include "utils.h"
|
7 |
|
8 |
#ifdef WINDOWS
|
9 |
|
10 |
#include <windows.h>
|
11 |
|
12 |
uint64_t getTickCount() |
13 |
{
|
14 |
return GetTickCount(); |
15 |
}
|
16 |
|
17 |
#else // WINDOWS
|
18 |
|
19 |
uint64_t getTickCount() |
20 |
{
|
21 |
struct timespec t; |
22 |
|
23 |
if(clock_gettime(CLOCK_MONOTONIC, &t) != 0) |
24 |
return 0; |
25 |
return (((uint64_t)t.tv_sec) * 1000) + (((uint64_t)t.tv_nsec) / 1000000); |
26 |
}
|
27 |
|
28 |
#endif
|
29 |
|
30 |
void* loadFile(const char* filename, size_t* size) |
31 |
{
|
32 |
void* result = NULL; |
33 |
struct stat statbuf; |
34 |
FILE* f = NULL; |
35 |
|
36 |
if(stat(filename, &statbuf) < 0) { |
37 |
fprintf(stderr, "could not stat() file %s: %s\n", filename, strerror(errno)); |
38 |
goto failed; |
39 |
}
|
40 |
|
41 |
if((result = calloc(1, statbuf.st_size)) == NULL) { |
42 |
#ifdef WINDOWS
|
43 |
fprintf(stderr, "failed to allocate %u bytes of memory\n", (size_t)statbuf.st_size); |
44 |
#else
|
45 |
fprintf(stderr, "failed to allocate %zu bytes of memory\n", (size_t)statbuf.st_size); |
46 |
#endif
|
47 |
goto failed; |
48 |
}
|
49 |
if((f = fopen(filename, "rb")) == NULL) { |
50 |
fprintf(stderr, "failed to open %s: %s\n", filename, strerror(errno)); |
51 |
goto failed; |
52 |
}
|
53 |
if(fread(result, 1, statbuf.st_size, f) != statbuf.st_size) { |
54 |
fprintf(stderr, "could not read all bytes: %s\n", strerror(errno)); |
55 |
goto failed; |
56 |
}
|
57 |
|
58 |
fclose(f); |
59 |
*size = (size_t)statbuf.st_size; |
60 |
return result; |
61 |
|
62 |
failed:
|
63 |
if(f != NULL) |
64 |
fclose(f); |
65 |
return NULL; |
66 |
}
|