forked from freewilll/wcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
106 lines (87 loc) · 2.71 KB
/
utils.c
File metadata and controls
106 lines (87 loc) · 2.71 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
#include "wcc.h"
static struct timeval debug_log_start;
// Report an internal error and exit
void panic(char *format, ...) {
va_list ap;
va_start(ap, format);
fprintf(stderr, "Internal error: ");
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
// Strip the filename component from a path
char *base_path(char *path) {
int end = strlen(path) - 1;
char *result = wmalloc(strlen(path) + 1);
while (end >= 0 && path[end] != '/') end--;
if (end >= 0) result = memcpy(result, path, end + 1);
result[end + 1] = 0;
return result;
}
int wasprintf(char **ret, const char *format, ...) {
va_list ap1;
va_start(ap1, format);
va_list ap2;
va_copy(ap2, ap1);
int size = vsnprintf(NULL, 0, format, ap1);
va_end(ap1);
*ret = wmalloc(size + 1);
vsnprintf(*ret, size + 1, format, ap2);
va_end(ap2);
return size;
}
// Allocate data for a new string buffer, rounding initial_size up to a power of two.
StringBuffer *new_string_buffer(int initial_size) {
StringBuffer *sb = wmalloc(sizeof(StringBuffer));
sb->position = 0;
int allocated = 1;
while (allocated < initial_size * 2) allocated <<= 1;
sb->allocated = allocated;
sb->data = wmalloc(allocated);
return sb;
}
void free_string_buffer(StringBuffer *sb, int free_data) {
if (free_data) wfree(sb->data);
wfree(sb);
}
// Append a string to the string buffer
void append_to_string_buffer(StringBuffer *sb, char *str) {
int len = strlen(str);
int needed = sb->position + len + 1;
if (sb->allocated < needed) {
while (sb->allocated < needed) sb->allocated <<= 1;
sb->data = wrealloc(sb->data, sb->allocated);
}
sprintf(sb->data + sb->position, "%s", str);
sb->position += len;
}
// Null terminate the string in the string buffer
void terminate_string_buffer(StringBuffer *sb) {
sb->data[sb->position] = 0;
}
int set_debug_logging_start_time() {
gettimeofday(&debug_log_start, NULL);
}
int debug_log(char *format, ...) {
struct timeval end;
va_list ap;
va_start(ap, format);
gettimeofday(&end, NULL);
long secs_used=(end.tv_sec - debug_log_start.tv_sec); //avoid overflow by subtracting first
long microseconds = ((secs_used*1000000) + end.tv_usec) - (debug_log_start.tv_usec);
secs_used = microseconds / 1000000;
microseconds = (microseconds % 1000000);
microseconds /= 1000;
int hr=(int)(secs_used/3600);
int min=((int)(secs_used/60))%60;
int sec=(int)(secs_used%60);
printf("%02d:%02d:%02d.%03ld ", hr, min, sec, microseconds);
vprintf(format, ap);
printf("\n");
}