-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline.cpp
359 lines (308 loc) · 8.36 KB
/
line.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
* Copyright (c) Microsoft Corporation. All Rights Reserved.
*/
/*
* line.cpp
*
* data type representing a string of ascii text along with a line number.
* a LINE can compare itself to another line, and maintain a link if the
* lines are similar. A line can also generate a hashcode for the line.
*
* Comparisons between lines take note of the global option flag
* ignore_blanks, defined elsewhere. If this is true, we ignore
* differences in spaces and tabs when comparing lines, and when
* generating hashcodes.
*
* Links and hashcodes are only generated once. to clear the link and
* force re-generation of the hashcode (eg after changing ignore_blanks)
* call line_reset.
*
* Lines can be allocated on a list. If a null list handle is passed, the
* line will be allocated using HeapAlloc().
*
*/
#include "precomp.h"
#include "sdkdiff.h" /* defines hHeap and ignore_blanks */
#include "list.h"
#include "line.h"
#define IS_BLANK(c) \
(((c) == ' ') || ((c) == '\t') || ((c) == '\r'))
/*
* a LINE handle is a pointer to a struct fileline, defined here
*/
struct fileline {
UINT flags; /* see below */
LPSTR text; /* null-terminated copy of line text */
DWORD hash; /* hashcode for line */
LINE link; /* handle for linked line */
UINT linenr; /* line number (any arbitrary value) */
LPWSTR pwzText; /* null-terminated original unicode text */
};
/* flag values (or-ed) */
#define LF_DISCARD 1 /* if true, alloced using HeapAlloc */
#define LF_HASHVALID 2 /* if true, hashcode need not be recalced */
/*
* create a new line. make a copy of the text.
*
* if the list is non-null, allocate on the list. if null, alloc using
* HeapAlloc.
*/
LINE
line_new(LPSTR text, int linelength, LPWSTR pwzText, int cwchText, UINT linenr, LIST list)
{
LINE line;
int cch = 0;
/* alloc a line. from the list if there is a list */
if (list) {
line = (LINE)List_NewLast(list, sizeof(struct fileline));
if (line == NULL) {
return(NULL);
}
line->flags = 0;
} else {
line = (LINE) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct fileline));
if (line == NULL) {
return(NULL);
}
line->flags = LF_DISCARD;
}
/* alloc space for the text. remember the null character */
/* also add cr/nl pair if absent for composite file */
cch = (text[linelength - 1] == '\n') ? 1 : 3;
line->text = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, linelength + cch);
if (line->text == NULL)
return NULL;
My_mbsncpy(line->text, text, linelength);
if (cch == 3) {
line->text[linelength++] = '\r';
line->text[linelength++] = '\n';
}
line->text[linelength] = '\0';
line->pwzText = 0;
if (pwzText)
{
/* alloc space for the unicode text. remember the null character */
/* also add cr/nl pair if absent for composite file */
cch = (pwzText[cwchText - 1] == '\n') ? 1 : 3;
line->pwzText = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (cwchText + cch) * sizeof(*pwzText));
if (line->pwzText == NULL)
return NULL;
wcsncpy(line->pwzText, pwzText, cwchText);
if (cch == 3) {
line->pwzText[cwchText++] = '\r';
line->pwzText[cwchText++] = '\n';
}
line->pwzText[cwchText] = '\0';
}
line->link = NULL;
line->linenr = linenr;
return(line);
}
/*
* delete a line. free up all associated memory and if the line
* was not alloc-ed from a list, free up the line struct itself
*/
void
line_delete(LINE line)
{
if (line == NULL) {
return;
}
/* free up text space */
HeapFree(GetProcessHeap(), NULL, line->text);
/* free up line itself only if not on list */
if (line->flags & LF_DISCARD) {
HeapFree(GetProcessHeap(), NULL, (LPSTR) line);
}
}
/*
* clear the link and force recalc of the hash code.
*/
void
line_reset(LINE line)
{
if (line == NULL) {
return;
}
line->link = NULL;
line->flags &= ~LF_HASHVALID;
}
/* return a pointer to the line text */
LPSTR
line_gettext(LINE line)
{
if (line == NULL) {
return(NULL);
}
return (line->text);
}
/* return a pointer to the line text */
LPWSTR
line_gettextW(LINE line)
{
if (line == NULL) {
return(NULL);
}
return (line->pwzText);
}
/* get the effective text length, ignoring blanks */
int line_gettextlen(LINE line)
{
int sum = 0;
LPSTR string = line->text;
while (*string != '\0') {
if (ignore_blanks) {
while (IS_BLANK(*string)) {
string++;
}
}
if(IsDBCSLeadByte((BYTE)*string)) {
++sum;
++string;
}
++sum;
++string;
}
return(sum);
}
/*
* line_gettabbedlength
*
* return length of line in characters, expanding tabs. useful
* for display-space calculations.
*/
int
line_gettabbedlength(LINE line, int tabstops)
{
int length;
LPSTR chp;
if (line == NULL) {
return(0);
}
for (length = 0, chp = line->text; *chp != '\0'; chp++) {
if (*chp == '\t') {
length = (length + tabstops) / tabstops * tabstops;
} else {
if (IsDBCSLeadByte(*chp)) {
chp++;
length++;
}
length++;
}
}
return(length);
}
/* return the hashcode for this line */
DWORD
line_gethashcode(LINE line)
{
if (line == NULL) {
return(0);
}
if (! (line->flags & LF_HASHVALID)) {
/* hashcode needs to be recalced */
line->hash = hash_string(line->text, ignore_blanks);
line->flags |= LF_HASHVALID;
}
return (line->hash);
}
/* return the handle for the line that is linked to this line (the
* result of a successful line_link() operation). This line is
* identical in text to the linked line (allowing for ignore_blanks).
*/
LINE
line_getlink(LINE line)
{
if (line == NULL) {
return(NULL);
}
return(line->link);
}
/* return the line number associated with this line */
UINT
line_getlinenr(LINE line)
{
if (line == NULL) {
return(0);
}
return(line->linenr);
}
/* compare two lines. return TRUE if they are the same. uses
* ignore_blanks to determine whether to ignore any
* spaces/tabs in the comparison.
*/
BOOL
line_compare(LINE line1, LINE line2)
{
LPSTR p1, p2;
if ((line1 == NULL) || (line2 == NULL)) {
/* null line handles do not compare */
return(FALSE);
}
/* check that the hashcodes match */
if (line_gethashcode(line1) != line_gethashcode(line2)) {
return(FALSE);
}
/* hashcodes match - are the lines really the same ? */
/* note that this is coupled to gutils\utils.c in definition of blank */
p1 = line_gettext(line1);
p2 = line_gettext(line2);
do {
if (ignore_blanks) {
while (IS_BLANK(*p1)) {
p1 = CharNext(p1);
}
while (IS_BLANK(*p2)) {
p2 = CharNext(p2);
}
}
if (IsDBCSLeadByte(*p1) && *(p1+1) != '\0'
&& IsDBCSLeadByte(*p2) && *(p2+1) != '\0') {
if (*p1 != *p2 || *(p1+1) != *(p2+1)) {
return(FALSE);
}
p1 += 2;
p2 += 2;
} else {
if (*p1 != *p2) {
return(FALSE);
}
p1++;
p2++;
}
} while ( (*p1 != '\0') && (*p2 != '\0'));
return(TRUE);
}
/*
* attempt to link two lines. return TRUE if succesful.
*
* this will fail if either line is NULL, or already linked, or if
* they differ.
*/
BOOL
line_link(LINE line1, LINE line2)
{
if ( (line1 == NULL) || (line2 == NULL)) {
return(FALSE);
}
if ( (line1->link != NULL) || (line2->link != NULL)) {
return(FALSE);
}
if (line_compare(line1, line2)) {
line1->link = line2;
line2->link = line1;
return(TRUE);
} else {
return(FALSE);
}
}
/* return TRUE iff line is blank. NULL => return FALSE */
BOOL line_isblank(LINE line)
{
return line!=NULL && utils_isblank(line->text);
}