-
Notifications
You must be signed in to change notification settings - Fork 1
/
str.c
209 lines (158 loc) · 5.56 KB
/
str.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
/* =====[ STR.C ]=========================================================
Description: Convert stings.
Compiled: MS-VC.
Compiler opt: See makefile.
Revisions:
Revisions:
REV DATE BY DESCRIPTION
---- -------- ---------- --------------------------------------
0.00 mm/dd/95 Peter Glen Initial version.
======================================================================= */
/* -------- System includes: -------------------------------------------- */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
/* -------- Includes: --------------------------------------------------- */
#include "convert.h"
#include "hocdecl.h"
#include "str.h"
/* -------- Defines: ----------------------------------------------------- */
//define PG_DEBUG
#define MIN(a,b) ((a>b) ? a : b)
/* -------- Macros: ------------------------------------------------------ */
/* -------- Forward references for procedures: --------------------------- */
/* -------- Declarations: ------------------------------------------------ */
/* -------- Definitions: ------------------------------------------------- */
/* -------- Data: -------------------------------------------------------- */
/* -------- Strings: ----------------------------------------------------- */
/* -------- Implementation: ---------------------------------------------- */
/*
* int str_esc(char *str, char *out, int lim)
*
* Expand escape sequence like 'C'.
*
* Spec: esc char '\\' (backslash)
*
* special characters: a : alert
* b : backspace
* f : f
* r : return
* n : newline
* t : tab
* v : vertical tab
* \\ : backslash
* xnn : hex numbered character
* nnn : dec numbered character
*
* The dec number overflow is interpretes as 2 dec numbered character +
* the following character interpreted normally.
*
*/
unsigned int str_esc(char *str, char *out, int lim)
{
char *str2 = str;
int ret_val = 0;
*out = '\0';
if(!str)
return 0;
while(1)
{
*out = '\0';
if(!*str2)
break;
if(!lim)
{
ret_val = TRUE;
break;
}
else if(str2-str+1 >= lim)
{
fprintf(stderr, "String too large for buffer of %i chars; truncated\n", lim-1);
ret_val = FALSE;
*str2 = '\0';
break;
}
switch(*str2)
{
int tmp;
case '\\':
if(isdigit(*(str2+1)))
{
int len = 3;
tmp = dectoi(str2+1, len); // convert number
if(tmp > 255) // decimal overflow ?
len--;
tmp = dectoi(str2+1, len); // re-convert number
len = MIN(declen(str2+1), len);
*out = (char) tmp;
str2 += len;
}
else
{
switch(*(str2+1))
{
case 'x':
tmp = hextoi(str2+2, 2); // convert number
*out = (char) tmp;
str2 += MIN(hexlen(str2+2),2);
str2++;
break;
case 'a':
*out = 7;
str2++;
break;
case 'b':
*out = 8;
str2++;
break;
case 'f':
*out = 12;
str2++;
break;
case 'n':
*out = 10;
str2++;
break;
case 'r':
*out = 13;
str2++;
break;
case 't':
*out = 9;
str2++;
break;
case 'v':
*out = 11;
str2++;
break;
case '\\':
*out = '\\';
str2++;
break;
case '\'':
*out = '\'';
str2++;
break;
case '\"':
*out = '\"';
str2++;
break;
default:
*out = *str2;
str2++;
break;
}
}
break;
default:
*out = *str2;
break;
}
str2++; out++;
}
return ret_val;
}
/* EOF */