-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSys.c
144 lines (116 loc) · 2.91 KB
/
Sys.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// MT, 2015oct26
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <ctype.h>
#include "Sys.h"
bool Sys_is_big_endian()
{
assert(sizeof(unsigned int)==4);
union
{
unsigned int uI;
unsigned char uC[4];
} u = { 0x01020304 };
return u.uC[0]==1;
}
/** Return monotonic clock time in milliseconds.
*
* - See: http://stackoverflow.com/questions/361363/how-to-measure-time-in-milliseconds-using-ansi-c
*/
uint64_t Sys_get_posix_clock_time_ms()
{
struct timespec ts;
if(clock_gettime(CLOCK_MONOTONIC, &ts)==0)
{
return (uint64_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
}
else
{
assert(false);
return 0;
}
}
char* Sys_create_time_str(bool const inDate, bool const inSeconds)
{
char* retVal = NULL;
time_t t = time(NULL);
struct tm const * const l = localtime(&t);
size_t const dateLen = 11,
timeLen = 5;
size_t len = timeLen+1, // 23:59 and '\0'
pos = 0;
if(inDate)
{
len += dateLen; // 2016/03/20-
}
if(inSeconds)
{
len += 3; // :59
}
size_t const byteLen = len*(sizeof *retVal);
retVal = malloc(byteLen);
assert(retVal!=NULL);
if(inDate)
{
snprintf(retVal+pos, byteLen-pos, "%d/%02d/%02d-", l->tm_year+1900, l->tm_mon+1, l->tm_mday);
pos += dateLen;
}
snprintf(retVal+pos, byteLen-pos, "%02d:%02d", l->tm_hour, l->tm_min);
pos += timeLen;
if(inSeconds)
{
snprintf(retVal+pos, byteLen-pos, ":%02d", l->tm_sec);
}
return retVal;
}
/** Original source: http://stackoverflow.com/questions/7831755/what-is-the-simplest-way-of-getting-user-input-in-c
*/
char* Sys_get_stdin()
{
size_t const granularity = 20;
size_t len = granularity,
i = 0;
char* retVal = malloc(len*(sizeof *retVal));
assert(retVal!=NULL);
while(true)
{
int const c = getchar();
// At terminating zero at end of string:
//
if(c=='\n')
{
retVal[i] = '\0';
break;
}
retVal[i] = c;
// Expand buffer, if full:
//
if(i==(len-1))
{
len = len+granularity;
retVal = realloc(retVal, len*(sizeof *retVal));
assert(retVal!=NULL);
}
++i;
}
return retVal;
}
/** Original source for variable arguments: http://stackoverflow.com/questions/1056411/how-to-pass-variable-number-of-arguments-to-printf-sprintf/1056424#1056424
*/
void Sys_log_line(bool const inDate, bool const inSeconds, char const * const inStr, ...)
{
va_list argPtr;
char * const timePtr = Sys_create_time_str(inDate, inSeconds);
printf("%s", timePtr);
printf(" - ");
assert(inStr!=NULL);
va_start(argPtr, inStr);
vprintf(inStr, argPtr);
va_end(argPtr);
printf("\n");
}