-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
227 lines (197 loc) · 3.52 KB
/
string.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "types.h"
#include "string.h"
#include "constants.h"
char *strippath(char *in)
// given a file containing a path name, returns a pointer
// to only the file name.
{
int i;
for(i=strlen(in)-1;i>=0;i--)
{
if (in[i]=='/' || in[i]=='\\')
{
return &in[i+1];
}
}
return in;
}
/*void splitext(char *filename, char *file, char *ext)
// split filename into 'file' and 'ext'
// 'autoexec.bat' becomes 'autoexec' and 'bat'
{
char *ptr;
strcpy(file, filename);
ptr = strrchr(file, '.');
if (ptr)
{
*ptr++ = 0;
if (ext) strcpy(ext, ptr);
}
else
{
if (ext) ext[0] = 0;
}
}*/
void validate_ext(char *V)
// V points to a file extension.
// Adds the dot to the extension if it's missing.
{
int i;
if (V[0] != '.')
{
for(i=strlen(V);i>=0;i--)
{
V[i+1] = V[i];
}
V[0] = '.';
}
}
char *RemoveQuotes(char *str)
// trims " " quotes from str, and returns a pointer to
// the quoteless string. The original string gets slightly mangled.
{
int l;
if (str[0]==34)
{
str++;
l = strlen(str)-1;
if (str[l]==34) str[l] = 0;
else
{
printf("RemoveQuotes: mismatched quotes:\n'%s'\n", str);
return NULL;
}
}
return str;
}
char *StringStartsWith(char *haystack, char *needle)
// determines if string haystack starts with string needle.
// if so, returns a pointer to the character after the last char scanned.
// else, returns NULL.
{
int i;
for(i=0;needle[i];i++)
{
if (haystack[i] != needle[i])
{
return NULL;
}
}
return &haystack[i];
}
// read data from a file until CR
void fgetline(FILE *fp, char *str, int maxlen)
{
int k;
str[0] = 0;
fgets(str, maxlen - 1, fp);
// trim the CR/LF that fgets returns
for(k=strlen(str)-1;k>=0;k--)
{
if (str[k] != 13 && str[k] != 10) break;
str[k] = 0;
}
}
uint LTrimAt(uchar *line, uchar atwhat, uint linelen)
{
int i;
for(i=0;i<linelen;i++)
{
if (line[i] == atwhat)
{
//strcpy(line, &line[i+1]);
int len = strlen(&line[i+1]);
memmove(line, &line[i+1], len+1);
return len;
}
}
return linelen;
}
int RTrim(char *line, int linelen)
// trim whitespace from the end of line line and return
// the new length of the line.
{
int i;
for(i=linelen-1;i>=0;i--)
{
if (!IS_WHITESPACE(line[i]))
{
line[++i] = 0;
return i;
}
}
// no whitespace was removed
return linelen;
}
uint RTrimAt(uchar *line, uchar ch, uint linelen)
{
int i;
for(i=linelen-1;i>=0;i--)
{
if (line[i]==ch) { line[i] = 0; return i; }
}
return linelen;
}
int fgeti(FILE *fp)
{
int lsb, msb;
msb = fgetc(fp);
lsb = fgetc(fp);
return (msb << 8) | lsb;
}
void fputi(uint word, FILE *fp)
{
fputc(word>>8, fp);
fputc(word&0xff, fp);
}
unsigned int fgetl(FILE *fp)
{
unsigned int temp1, temp2, temp3, temp4;
temp1 = fgetc(fp);
temp2 = fgetc(fp);
temp3 = fgetc(fp);
temp4 = fgetc(fp);
return (temp1<<24) | (temp2<<16) | (temp3<<8) | temp4;
}
void fputl(unsigned int word, FILE *fp)
{
unsigned int a,b,c,d;
a=b=c=d = word;
a &= 0xFF000000; a >>= 24;
b &= 0x00FF0000; b >>= 16;
c &= 0x0000FF00; c >>= 8;
d &= 0x000000FF;
fputc(a, fp);
fputc(b, fp);
fputc(c, fp);
fputc(d, fp);
}
// read a string from a file until a space is encountered
void freadstring(FILE *fp, char *buf, int max)
{
int i;
if (buf != NULL)
{
for(i=0;i<max;i++)
{
buf[i] = fgetc(fp);
if (!buf[i]) break;
}
}
else
{
do
{
i = fgetc(fp);
} while(i && i != -1);
}
}
// write a string to a file and null-terminate it
void fputstring(char *buf, FILE *fp)
{
if (buf[0]) fprintf(fp, "%s", buf);
fputc(0, fp);
}