This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutil.cpp
185 lines (141 loc) · 2.79 KB
/
util.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
isDcc
(c) 1998 Andrew de Quincey
See README.TXT for copying/distribution/modification details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <stdarg.h>
#include <string.h>
#include "util.h"
void printIndent(int value)
{
int i;
for(i=0; i < (value*4); i++)
printf(" ");
}
char* filterLF(char* buffer)
{
int i=0;
while(buffer[i] != 0)
{
if ((buffer[i] == 0x0a) || (buffer[i] == 0x0d))
buffer[i] = ' ';
i++;
}
return(buffer);
}
uint get4Byte(int file)
{
uint tmp;
if (_read(file, &tmp, 4) != 4)
error("Read past EOF!\n");
return(tmp);
}
ushort get2Byte(int file)
{
ushort tmp;
if (_read(file, &tmp, 2) != 2)
error("Read past EOF!\n");
return(tmp);
}
uchar get1Byte(int file)
{
uchar tmp;
if (_read(file, &tmp, 1) != 1)
error("Read past EOF!\n");
return(tmp);
}
char* getString(int file, char* buffer, int length)
{
getLength(file, buffer, length);
buffer[length] =0;
return(buffer);
}
char* getLength(int file, char* buffer, int length)
{
if (_read(file, buffer, length) != length)
error("Read past EOF!\n");
return(buffer);
}
void error(char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
assert(false);
exit(1);
}
void warning(char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
char* escapeString(char* buffer)
{
char tmpBuf[4096];
int srcPos;
int destPos;
int c;
srcPos = 0;
destPos = 0;
while((c = buffer[srcPos++]) != 0)
switch(c)
{
case '\n':
tmpBuf[destPos++] = '\\';
tmpBuf[destPos++] = 'n';
break;
case '\r': // ignore
tmpBuf[destPos++] = '\\';
tmpBuf[destPos++] = 'r';
break;
case '\'':
tmpBuf[destPos++] = '\\';
tmpBuf[destPos++] = '\'';
break;
case '\"':
tmpBuf[destPos++] = '\\';
tmpBuf[destPos++] = '\"';
break;
case '\\':
tmpBuf[destPos++] = '\\';
tmpBuf[destPos++] = '\\';
break;
default:
if ((c < 32) || (c > 126))
{
sprintf(tmpBuf+destPos, "\\x%02x", c);
destPos+=4;
}
else
tmpBuf[destPos++] = c;
}
tmpBuf[destPos] = 0;
strcpy(buffer, tmpBuf);
return(buffer);
}
void* mallocX(long size)
{
void* buf;
buf = malloc(size);
if (buf == NULL)
error("Memory allocation error!\n");
memset(buf,0,size);
return(buf);
}
int readX(int fd, char* buffer, int len)
{
if (_read(fd, buffer, len) != len)
error("Read past EOF!\n");
return(len);
}
long tell(int fd)
{
return(lseek(fd, 0, 1));
}