osmo-sdr/utils/rum-ba/src/utils.c @ master
1 | a59a9825 | Christian Daniel | #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 | 4cd82c23 | Christian Daniel | #ifdef WINDOWS
|
9 | |||
10 | #include <windows.h>
|
||
11 | |||
12 | uint64_t getTickCount() |
||
13 | {
|
||
14 | return GetTickCount(); |
||
15 | }
|
||
16 | |||
17 | #else // WINDOWS
|
||
18 | |||
19 | a59a9825 | Christian Daniel | uint64_t getTickCount() |
20 | {
|
||
21 | 4cd82c23 | Christian Daniel | struct timespec t; |
22 | a59a9825 | Christian Daniel | |
23 | 4cd82c23 | Christian Daniel | 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 | a59a9825 | Christian Daniel | }
|
27 | |||
28 | 4cd82c23 | Christian Daniel | #endif
|
29 | |||
30 | a59a9825 | Christian Daniel | 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 | 4cd82c23 | Christian Daniel | #ifdef WINDOWS
|
43 | fprintf(stderr, "failed to allocate %u bytes of memory\n", (size_t)statbuf.st_size); |
||
44 | #else
|
||
45 | a59a9825 | Christian Daniel | fprintf(stderr, "failed to allocate %zu bytes of memory\n", (size_t)statbuf.st_size); |
46 | 4cd82c23 | Christian Daniel | #endif
|
47 | a59a9825 | Christian Daniel | 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 | }
|