-
Notifications
You must be signed in to change notification settings - Fork 2
/
Markup.c
272 lines (221 loc) · 6.01 KB
/
Markup.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "Markup.h"
const char *XMLGetTag(const char *Input, char **Namespace, char **TagType, char **TagData)
{
const char *ptr, *tptr;
char *wptr;
int len=0, InTag=FALSE, TagHasName=FALSE;
if (! StrValid(Input)) return(NULL);
ptr=Input;
//This ensures we are never dealing with nulls
if (! *TagType) *TagType=CopyStr(*TagType,"");
if (! *TagData) *TagData=CopyStr(*TagData,"");
if (*ptr=='<')
{
ptr++;
while (isspace(*ptr)) ptr++;
len=0;
InTag=TRUE;
TagHasName=TRUE;
//if we start with a slash tag, then add that to the tag name
if (*ptr=='/')
{
*TagType=AddCharToBuffer(*TagType,len,*ptr);
len++;
ptr++;
}
while (InTag)
{
switch (*ptr)
{
//These all cause us to end. NONE OF THEM REQUIRE ptr++
//ptr++ will happen when we read tag data
case '>':
case '\0':
case ' ':
case ' ':
case '\n':
InTag=FALSE;
break;
//If a namespace return value is supplied, break the name up into namespace and
//tag. Otherwise don't
case ':':
if (Namespace)
{
tptr=*TagType;
if (*tptr=='/')
{
tptr++;
len=1;
}
else len=0;
*Namespace=CopyStr(*Namespace,tptr);
}
else
{
*TagType=AddCharToBuffer(*TagType,len,*ptr);
len++;
}
ptr++;
break;
case '\r':
ptr++;
break;
default:
*TagType=AddCharToBuffer(*TagType,len,*ptr);
len++;
ptr++;
break;
}
}
}
StrTrunc(*TagType, len);
while (isspace(*ptr)) ptr++;
len=0;
InTag=TRUE;
while (InTag)
{
switch (*ptr)
{
//End of tag, skip '>' and fall through
case '>':
ptr++;
//Start of next tag or end of text
case '<':
case '\0':
InTag=FALSE;
break;
//Quotes!
case '\'':
case '\"':
//Somewhat ugly code. If we're dealing with an actual tag, then TagHasName
//will be set. This means we're dealing with data within a tag and quotes mean something.
//Otherwise we are dealing with text outside of tags and we just add the char to
//TagData and continue
if (TagHasName)
{
tptr=ptr;
while (*tptr != *ptr)
{
*TagData=AddCharToBuffer(*TagData,len,*ptr);
len++;
ptr++;
}
}
*TagData=AddCharToBuffer(*TagData,len,*ptr);
len++;
ptr++;
break;
default:
*TagData=AddCharToBuffer(*TagData,len,*ptr);
len++;
ptr++;
break;
}
}
//End of Parse TagData. Strip any '/'
wptr=*TagData;
if ((len > 0) && (wptr[len-1]=='/')) len--;
wptr[len]='\0';
StrLenCacheAdd(*TagData, len);
strlwr(*TagType);
while (isspace(*ptr)) ptr++;
return(ptr);
}
char *HTMLQuote(char *RetBuff, const char *Str)
{
char *RetStr=NULL, *Token=NULL;
const char *ptr;
int len;
RetStr=CopyStr(RetStr,"");
len=StrLen(Str);
for (ptr=Str; ptr < (Str+len); ptr++)
{
switch (*ptr)
{
case '&':
RetStr=CatStr(RetStr,"&");
break;
case '<':
RetStr=CatStr(RetStr,"<");
break;
case '>':
RetStr=CatStr(RetStr,">");
break;
default:
RetStr=AddCharToStr(RetStr,*ptr);
break;
}
}
DestroyString(Token);
return(RetStr);
}
char *HTMLUnQuote(char *RetStr, const char *Data)
{
char *Output=NULL, *Token=NULL;
const char *ptr;
int len=0;
Output=CopyStr(RetStr,Output);
ptr=Data;
while (ptr && (*ptr != '\0'))
{
if (*ptr=='&')
{
ptr++;
ptr=GetToken(ptr,";",&Token,0);
if (strncmp(Token,"#x", 2)==0)
{
Output=AddCharToBuffer(Output,len,strtol(Token+2,NULL,16));
len++;
}
else if (*Token=='#')
{
Output=AddCharToBuffer(Output,len,strtol(Token+1,NULL,10));
len++;
}
else if (strcmp(Token,"amp")==0)
{
Output=AddCharToBuffer(Output,len,'&');
len++;
}
else if (strcmp(Token,"lt")==0)
{
Output=AddCharToBuffer(Output,len,'<');
len++;
}
else if (strcmp(Token,"gt")==0)
{
Output=AddCharToBuffer(Output,len,'>');
len++;
}
else if (strcmp(Token,"quot")==0)
{
Output=AddCharToBuffer(Output,len,'"');
len++;
}
else if (strcmp(Token,"apos")==0)
{
Output=AddCharToBuffer(Output,len,'\'');
len++;
}
else if (strcmp(Token,"tilde")==0)
{
Output=AddCharToBuffer(Output,len,'~');
len++;
}
else if (strcmp(Token,"circ")==0)
{
Output=AddCharToBuffer(Output,len,'^');
len++;
}
}
else
{
Output=AddCharToBuffer(Output,len,*ptr);
len++;
ptr++;
}
}
StrLenCacheAdd(Output, len);
DestroyString(Token);
return(Output);
}