-
Notifications
You must be signed in to change notification settings - Fork 50
/
utility.cpp
157 lines (146 loc) · 3.33 KB
/
utility.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
/* utility.cpp
* Richard Gibson, Jun 28, 2013
* Email: richard.g.gibson@gmail.com
*
* Home of implementations of useful functions used in multiple places in
* the project.
*
* Copyright (C) 2013 by Richard Gibson
*/
/* C / C++ / STL includes */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* Pure CFR includes */
#include "utility.hpp"
int strtoint64_units( const char *ptr, int64_t &retval )
{
int64_t number = 0;
long long int parseNumber = 0;
char unit = 'x';
int scanned = sscanf(ptr, "%lld%c", &parseNumber, &unit);
number = parseNumber;
if (scanned == 0) {
return 1;
} else if (scanned == 1) {
/* Only scanned a number. Return it. */
retval = number;
return 0;
} else if (scanned == 2) {
/* Scanned a number and a unit. */
switch(unit)
{
case 'k':
number *= 1000;
break;
case 'm':
number *= 1000000;
break;
case 'b':
number *= 1000000000;
break;
default:
return 2;
}
retval = number;
return 0;
} else {
return 2;
}
}
void int64tostr_units( int64_t val, char *ptr, int n )
{
char unit = ' ';
if( ( val >= 1000000000 ) && ( val % 1000000000 == 0 ) ) {
val /= 1000000000;
unit = 'b';
} else if( ( val >= 1000000 ) && ( val % 1000000 == 0 ) ) {
val /= 1000000;
unit = 'm';
} else if( ( val >= 1000 ) && ( val % 1000 == 0 ) ) {
val /= 1000;
unit = 'k';
}
if( unit == ' ' ) {
snprintf( ptr, n, "%jd", ( intmax_t ) val );
} else {
snprintf( ptr, n, "%jd%c", ( intmax_t ) val, unit);
}
}
int time_string_to_seconds( const char *str )
{
int units[ 4 ];
int numParsed = sscanf( str, "%d:%d:%d:%d",
&units[ 0 ],
&units[ 1 ],
&units[ 2 ],
&units[ 3 ] );
int total = 0;
if( numParsed == 4 ) {
total = units[ 0 ] * 86400
+ units[ 1 ] * 3600
+ units[ 2 ] * 60
+ units[ 3 ];
} else if( numParsed == 3 ) {
total = units[ 0 ] * 3600
+ units[ 1 ] * 60
+ units[ 2 ];
} else if( numParsed == 2 ) {
total = units[ 0 ] * 60
+ units[ 1 ];
} else if( numParsed == 1 ) {
total = units[ 0 ];
}
return total;
}
void time_seconds_to_string( int time, char *str, int strlen )
{
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
if( time > 86400 ) {
days = time / 86400;
time -= days * 86400;
}
if( time > 3600 ) {
hours = time / 3600;
time -= hours * 3600;
}
if( time > 60 ) {
minutes = time / 60;
time -= minutes * 60;
}
seconds = time;
if( days ) {
snprintf(str, strlen, "%d:%02d:%02d:%02d days", days, hours, minutes, seconds);
} else if( hours ) {
snprintf(str, strlen, "%d:%02d:%02d hours", hours, minutes, seconds);
} else if( minutes ) {
snprintf(str, strlen, "%d:%02d minutes", minutes, seconds);
} else if( seconds ) {
snprintf(str, strlen, "%d seconds", seconds);
} else {
snprintf(str, strlen, "0 seconds");
}
}
int get_next_token( char out[ PATH_LENGTH ], const char *str )
{
/* Skip whitespace */
int i = 0;
while( isspace( str[ i ] ) || ( str[ i ] == '=' ) ) {
++i;
}
/* Find next whitespace */
int t = i + 1;
while( !isspace( str[ t ] ) && ( str[ t ] != '\0' ) ) {
++t;
}
/* Copy the filename */
strncpy( out, &str[ i ], PATH_LENGTH );
if( t - i >= PATH_LENGTH ) {
return 1;
}
out[ t - i ] = '\0';
return 0;
}