-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmplparser.c
978 lines (893 loc) · 25.5 KB
/
tmplparser.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/* Dialog template parsing and generation code
Also contains code for the dialog editor data model.
This is platform independent code.
Note on the data model: if a string variable has a limit of 255
characters, that limit is intentionally enforced! */
#include <stdio.h>
#include <string.h>
#include "xmalloc.h"
#include "tmplparser.h"
/* Microsoft Visual C++ memory leak detection. (This program has NO
memory leaks, but it is here just to be on the safe side.) */
#ifdef _DEBUG
#include <crtdbg.h>
#endif
/* MSVC >= 8.0 pragmas */
#if _MSC_VER >= 1400
#pragma warning (disable: 4996) /* Disable deprecate */
#pragma warning (disable: 4267) /* Disable size_t warnings */
#endif
/* Drawn controls are grouped by drawing style */
const unsigned numClasses = 8;
const unsigned numEachClass[] = {1, 4, 2, 4, 4, 2, 1, 1};
const unsigned maxPerClass = 4;
const char* drawClasses[8][4] = {
{"CONTROL"}, /* Custom controls */
{"AUTO3STATE", "STATE3", "AUTOCHECKBOX", "CHECKBOX"}, /* Check boxes */
{"AUTORADIOBUTTON", "RADIOBUTTON"}, /* Radio buttons */
{"EDITTEXT", "LISTBOX", "COMBOBOX", "ICON"}, /* Client boxes */
{"LTEXT", "CTEXT", "RTEXT", "PUSHBOX"}, /* Draw text only */
{"DEFPUSHBUTTON", "PUSHBUTTON"}, /* Push buttons */
{"GROUPBOX"}, /* Group box */
{"SCROLLBAR"}}; /* Scroll controls */
/* Dialog variables */
char* dlgHead;
POINT dlgPos;
long dlgWidth;
long dlgHeight;
BOOL dlgHasCaption;
char dlgCaption[256];
unsigned dlgPointSize;
char* dlgFontFam;
DlgItem_array dlgControls;
/* Parser variables */
unsigned curPos;
unsigned curLine;
char* errorDesc;
/* Translates a text buffer to Unix line endings in place. Returns
the new size of the data, which may have shrunken.
"dataSize" specifies the length of the string, not including the
null character. */
unsigned SetUnixNlChars(char* buffer, unsigned dataSize)
{
unsigned i, j;
for (i = 0, j = 0; i < dataSize; i++)
{
if (buffer[i] != '\r')
buffer[j] = buffer[i];
else
{
if (buffer[i+1] == '\n') /* CR+LF */
i++;
buffer[j] = '\n';
}
j++;
}
buffer[j] = '\0';
return j;
}
/* The caller of this function MUST free the returned memory.
"dataSize" specifies the length of the string, not including the
null character. */
heap_char GenWinNlChars(char* buffer, unsigned dataSize)
{
char* winBuff;
unsigned i, j;
winBuff = (char*)xmalloc(dataSize + 1);
for (i = 0, j = 0; i < dataSize; i++)
{
if (buffer[i] == '\n')
winBuff[j++] = '\r';
if (j >= dataSize)
winBuff = (char*)xrealloc(winBuff, j + 2);
winBuff[j++] = buffer[i];
if (j >= dataSize)
winBuff = (char*)xrealloc(winBuff, j + 2);
}
winBuff[j] = '\0';
return winBuff; /* This MUST be freed by the caller */
}
/* Translates escape codes in a string to characters. Returns the new
data size. This is not a fully featured escape code translator.
"dataSize" specifies the length of the string, not including the
null character. */
unsigned TransEscapeChars(char* buffer, unsigned dataSize)
{
unsigned curPos;
unsigned newDataSize;
curPos = 0;
newDataSize = dataSize;
while (curPos < newDataSize)
{
if (buffer[curPos] == '\\')
{
BOOL foundEscChar;
foundEscChar = TRUE;
curPos++;
if (curPos >= newDataSize)
break;
switch (buffer[curPos])
{
case '\\': break;
case 'a': buffer[curPos] = '\a'; break;
case 'b': buffer[curPos] = '\b'; break;
case 'f': buffer[curPos] = '\f'; break;
case 'n': buffer[curPos] = '\n'; break;
case 'r': buffer[curPos] = '\r'; break;
case 'v': buffer[curPos] = '\v'; break;
case 't': buffer[curPos] = '\t'; break;
case '\'': break;
case '\"': break;
case '?': break;
default:
foundEscChar = FALSE;
break;
}
if (foundEscChar == TRUE)
{
memmove(&buffer[curPos-1], &buffer[curPos],
newDataSize - curPos);
curPos--;
newDataSize--;
}
}
curPos++;
}
buffer[newDataSize] = '\0';
return newDataSize;
}
/* Reallocates the dynamically allocated memory pointed to by "buffer"
and writes a string with special characters replaced by escape
codes. Returns the new buffer pointer.
"dataSize" specifies the length of the string, not including the
null character. */
char* UntransEscChars(char* buffer, unsigned dataSize)
{
char* newBuffer;
unsigned curPos;
unsigned newDataSize;
newBuffer = buffer;
curPos = 0;
newDataSize = dataSize;
while (curPos < newDataSize)
{
BOOL foundEscChar;
char c;
foundEscChar = TRUE;
switch (newBuffer[curPos])
{
case '\\': c = '\\'; break;
case '\a': c = 'a'; break;
case '\b': c = 'b'; break;
case '\f': c = 'f'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\v': c = 'v'; break;
case '\t': c = 't'; break;
case '\'': c = '\''; break;
case '\"': c = '\"'; break;
default:
foundEscChar = FALSE;
break;
}
if (foundEscChar == TRUE)
{
newBuffer[curPos] = '\\';
curPos++;
newDataSize++;
newBuffer = (char*)xrealloc(newBuffer, newDataSize + 1);
memmove(&newBuffer[curPos+1], &newBuffer[curPos],
(newDataSize - 1) - curPos);
newBuffer[curPos] = c;
}
curPos++;
}
newBuffer[newDataSize] = '\0';
return newBuffer;
}
#define CHECK_SIZE_ERROR(errLabel) \
if (curPos >= dataSize) \
goto errLabel;
#define WHITESPACE (buffer[curPos] == ' ' || buffer[curPos] == '\t')
#define CHECK_CHAR(chcode) (buffer[curPos] == chcode)
#define SKIP_CHAR(chcode) \
if (!CHECK_CHAR(chcode)) \
goto headError; \
curPos++;
#define SAVE_STRING() \
lastString = (char*)xrealloc(lastString, curPos - lastPos + 1); \
strncpy(lastString, &buffer[lastPos], curPos - lastPos); \
lastString[curPos-lastPos] = '\0';
#define WS_AND_NL (WHITESPACE || buffer[curPos] == '\n')
#define SKIP_WHITESPACE() \
while (curPos < dataSize && WHITESPACE) \
curPos++;
#define CHECK_NO_NL_ERROR(label) \
if (CHECK_CHAR('\n')) \
goto label;
/* This function stores dynamically allocated memory. Therefore, you
must make sure that you free the relevent memory before calling
this function for recalculations.
"dataSize" specifies the length of the string, not including the
null character. */
BOOL ParseDlgHead(char* buffer, unsigned dataSize)
{
unsigned lastPos;
char* lastString;
BOOL foundHeadEnd;
/* Temporary variables */
unsigned i;
lastPos = 0;
foundHeadEnd = FALSE;
curPos = 0;
curLine = 1;
dlgHead = NULL;
dlgFontFam = NULL;
lastString = (char*)xmalloc(9);
/* Always skip comments (not done right now) */
/* Skip whitespace and newlines */
while (curPos < dataSize && WS_AND_NL)
{
if (CHECK_CHAR('\n'))
curLine++;
curPos++;
}
errorDesc = "Missing dialog template data.";
CHECK_SIZE_ERROR(headError);
CHECK_NO_NL_ERROR(headError);
/* Skip ID */
lastPos = curPos;
while (curPos < dataSize && !WS_AND_NL)
curPos++;
errorDesc = "Missing dialog ID.";
CHECK_SIZE_ERROR(headError);
CHECK_NO_NL_ERROR(headError);
errorDesc = "Missing space after dialog ID.";
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(headError);
CHECK_NO_NL_ERROR(headError);
/* Skip DIALOG or DIALOGEX */
lastPos = curPos;
while (curPos < dataSize && !WS_AND_NL)
curPos++;
errorDesc = "Missing dialog resource specifier.";
CHECK_SIZE_ERROR(headError);
CHECK_NO_NL_ERROR(headError);
errorDesc = "Missing space after DIALOG or DIALOGEX.";
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(headError);
CHECK_NO_NL_ERROR(headError);
/* Read x, y, w, h */
for (i = 0; i < 4; i++)
{
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR(',') && !WHITESPACE &&
!CHECK_CHAR('\n'))
curPos++;
if (i < 3)
errorDesc = "Missing dialog coordinates.";
else
errorDesc = "Missing dialog template data.";
CHECK_SIZE_ERROR(headError);
SAVE_STRING();
/* Data processing hook */
{
int numVal;
numVal = atoi(lastString);
switch (i)
{
case 0: dlgPos.x = numVal; break;
case 1: dlgPos.y = numVal; break;
case 2: dlgWidth = numVal; break;
case 3: dlgHeight = numVal; break;
}
}
if (i < 3)
curPos++; /* Skip the comma */
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(headError);
if (i < 3)
CHECK_NO_NL_ERROR(headError);
}
errorDesc = "Expected end of line.";
SKIP_CHAR('\n');
curLine++;
errorDesc = "Missing dialog template data.";
CHECK_SIZE_ERROR(headError);
/* Read only a CAPTION or a FONT statement */
/* Otherwise, skip until "BEGIN" or '{' */
dlgHasCaption = FALSE;
errorDesc = "Missing beginning marker of dialog control list.";
while (curPos < dataSize && foundHeadEnd == FALSE)
{
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(headError);
if (strncmp("CAPTION", &buffer[curPos], 7) == 0)
{
curPos += 7;
errorDesc = "Missing dialog caption string.";
CHECK_SIZE_ERROR(headError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(headError);
SKIP_CHAR('"');
CHECK_SIZE_ERROR(headError);
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('"'))
{
if (CHECK_CHAR('\\'))
{
/* Ignore escaped quotation marks */
curPos++;
CHECK_SIZE_ERROR(headError);
}
curPos++;
}
errorDesc = "Missing closing quote on dialog caption string.";
CHECK_SIZE_ERROR(headError);
/* Skip the quote after saving the string so not to include it */
SAVE_STRING();
SKIP_CHAR('"');
/* Data processing hook */
{
unsigned maxCpy;
maxCpy = strlen(lastString);
if (maxCpy > 255)
maxCpy = 255;
strncpy(dlgCaption, lastString, maxCpy);
dlgCaption[maxCpy] = '\0';
dlgHasCaption = TRUE;
}
/* Reset the error description */
errorDesc = "Missing beginning marker of dialog control list.";
}
else if (strncmp("FONT", &buffer[curPos], 4) == 0)
{
curPos += 4;
errorDesc = "Missing font point size.";
CHECK_SIZE_ERROR(headError);
SKIP_WHITESPACE();
/* Read the point size */
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR(','))
curPos++;
CHECK_SIZE_ERROR(headError);
SAVE_STRING();
/* Data processing hook */
dlgPointSize = atoi(lastString);
errorDesc = "Missing font face name.";
curPos++; /* Skip the comma */
CHECK_SIZE_ERROR(headError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(headError);
/* Read the font family */
CHECK_SIZE_ERROR(headError);
SKIP_CHAR('"');
CHECK_SIZE_ERROR(headError);
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('"'))
{
if (CHECK_CHAR('\\'))
{
/* Ignore escaped quotation marks */
curPos++;
CHECK_SIZE_ERROR(headError);
}
curPos++;
}
errorDesc = "Missing closing quote on font face string.";
CHECK_SIZE_ERROR(headError);
/* Skip the quote after saving the string so not to include it */
SAVE_STRING();
SKIP_CHAR('"');
/* Data processing hook */
dlgFontFam = (char*)xmalloc(strlen(lastString) + 1);
strcpy(dlgFontFam, lastString);
/* Reset the error description */
errorDesc = "Missing beginning marker of dialog control list.";
}
else if (CHECK_CHAR('{') ||
strncmp("BEGIN", &buffer[curPos], 5) == 0)
{
/* Quit at end of line */
foundHeadEnd = TRUE;
/* Read backwards so not to include the BEGIN line *
while (curPos > 0 && !CHECK_CHAR('\n'))
curPos--;
if (curPos == 0)
goto headError;
curPos++; /* Include the newline character *
headEndPos = curPos;
/* Continue to skip this line */
}
while (curPos < dataSize && !CHECK_CHAR('\n'))
curPos++;
CHECK_SIZE_ERROR(headError);
SKIP_CHAR('\n');
curLine++;
CHECK_SIZE_ERROR(headError);
}
goto noHeadError;
headError:
xfree(lastString);
return FALSE;
noHeadError:
/* Save the header */
lastPos = 0;
SAVE_STRING();
dlgHead = (char*)xmalloc(strlen(lastString) + 1);
strcpy(dlgHead, lastString);
xfree(lastString);
return TRUE;
}
/* If you call this function on a buffer that has a single control in
it and no dialog header, you will have to set "curPos" to zero
before calling this function.
This function also stores dynamically allocated memory. Therefore,
you must make sure that you free the relevent memory before calling
this function for recalculations.
"dataSize" specifies the length of the string, not including the
null character. */
BOOL ParseControl(char* buffer, unsigned dataSize, unsigned ctrlNum)
{
unsigned i, j;
BOOL foundType;
unsigned lastPos;
char* lastString;
DlgItem* pCtrl;
pCtrl = &dlgControls.d[ctrlNum];
pCtrl->id = NULL;
pCtrl->style = NULL;
pCtrl->exStyle = NULL;
foundType = FALSE;
lastString = (char*)xmalloc(9);
/* Assume "curPos" is directed to the beginning of the proper line */
SKIP_WHITESPACE();
errorDesc = "Missing control type statement.";
CHECK_SIZE_ERROR(ctrlError);
/* Check the type of control */
for (i = 0; i < numClasses; i++)
{
for (j = 0; j < numEachClass[i]; j++)
{
if (strncmp(drawClasses[i][j], &buffer[curPos],
strlen(drawClasses[i][j])) == 0)
{
/* We have a match */
pCtrl->rendClass = i;
pCtrl->rendType = j;
curPos += strlen(drawClasses[i][j]);
errorDesc = "Missing control parameters.";
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
foundType = TRUE;
break;
}
}
if (foundType == TRUE)
break;
}
if (foundType == FALSE)
{
errorDesc = "Unrecognized control type.";
goto ctrlError;
}
/* Read the caption */
/* Should SCROLLBAR really allow text? For now, no. */
/* Control types without text:
COMBOBOX, EDITTEXT, LISTBOX, SCROLLBAR */
if ((pCtrl->rendClass == 3 && /* Client boxes */
pCtrl->rendType != 3) || /* ICON */
pCtrl->rendClass == 7) /* Scrollbar */
goto readID;
if (pCtrl->rendClass == 3 && pCtrl->rendType == 3) /* ICON */
{
/* For ICON, "text" is actually the resource name and may not
be a string */
/* Don't require quotes on the text entry */
unsigned maxCpy;
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('\n') && !CHECK_CHAR(','))
curPos++;
errorDesc = "Missing control parameters after icon resource ID.";
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SAVE_STRING();
/* Data processing hook */
maxCpy = curPos - lastPos;
if (maxCpy > 255)
maxCpy = 255;
strncpy(pCtrl->text, lastString, maxCpy);
pCtrl->text[maxCpy] = '\0';
curPos++; /* Skip the comma */
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
goto readID;
}
errorDesc = "Missing quotes around caption text.";
if (!CHECK_CHAR('"'))
goto ctrlError;
curPos++; /* Skip the quote */
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('\n') && !CHECK_CHAR('"'))
{
if (CHECK_CHAR('\\'))
{
/* Ignore escaped quotation marks */
curPos++;
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
}
curPos++;
}
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
/* Skip the quote after saving the string so not to include it */
SAVE_STRING();
if (!CHECK_CHAR('"'))
goto ctrlError;
curPos++; /* Skip the quote */
errorDesc = "Missing control parameters after caption parameter.";
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
/* Data processing hook */
{
unsigned maxCpy;
maxCpy = TransEscapeChars(lastString, strlen(lastString));
if (maxCpy > 255)
maxCpy = 255;
strncpy(pCtrl->text, lastString, maxCpy);
pCtrl->text[maxCpy] = '\0';
}
curPos++; /* Skip the comma */
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
readID: /* Read the ID */
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('\n') && !CHECK_CHAR(','))
curPos++;
errorDesc = "Missing control parameters after ID parameter.";
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SAVE_STRING();
/* Data processing hook */
pCtrl->id = (char*)xmalloc(curPos - lastPos + 1);
strcpy(pCtrl->id, lastString);
curPos++; /* Skip the comma */
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
if (pCtrl->rendClass == 0) /* CONTROL */
{
/* Read the class */
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('\n') && !CHECK_CHAR(','))
curPos++;
errorDesc = "Missing control parameters after class parameter.";
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SAVE_STRING();
/* Data processing hook */
{
unsigned maxCpy;
maxCpy = curPos - lastPos;
if (maxCpy > 255)
maxCpy = 255;
strncpy(pCtrl->wndClass, lastString, maxCpy);
pCtrl->wndClass[maxCpy] = '\0';
}
curPos++; /* Skip the comma */
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
/* Read the style */
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('\n') && !CHECK_CHAR(','))
curPos++;
errorDesc = "Missing control parameters after style parameter.";
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SAVE_STRING();
/* Data processing hook */
pCtrl->style = (char*)xmalloc(curPos - lastPos + 1);
strcpy(pCtrl->style, lastString);
curPos++; /* Skip the comma */
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(ctrlError);
CHECK_NO_NL_ERROR(ctrlError);
}
/* Read x, y, w, h */
for (i = 0; i < 4; i++)
{
lastPos = curPos;
while (curPos < dataSize && buffer[curPos] != ',' && !WHITESPACE &&
!CHECK_CHAR('\n'))
curPos++;
if (i < 3)
errorDesc = "Missing control dimensions.";
else
errorDesc = "Missing newline character.";
CHECK_SIZE_ERROR(ctrlError);
if (i < 3) CHECK_NO_NL_ERROR(ctrlError);
SAVE_STRING();
/* Data processing hook */
{
int numVal;
numVal = atoi(lastString);
switch (i)
{
case 0: pCtrl->x = numVal; break;
case 1: pCtrl->y = numVal; break;
case 2: pCtrl->cx = numVal; break;
case 3: pCtrl->cy = numVal; break;
}
}
if (CHECK_CHAR(','))
curPos++;
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(ctrlError);
if (i < 3) CHECK_NO_NL_ERROR(ctrlError);
}
/* Read the style and extended style */
/* Just save the entire line ending */
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('\n'))
curPos++;
CHECK_SIZE_ERROR(ctrlError);
SAVE_STRING();
if (pCtrl->rendClass == 0)
{
/* Save in exStyle */
pCtrl->exStyle = (char*)xmalloc(curPos - lastPos + 1);
strcpy(pCtrl->exStyle, lastString);
}
else
{
/* Save in style */
pCtrl->style = (char*)xmalloc(curPos - lastPos + 1);
strcpy(pCtrl->style, lastString);
pCtrl->exStyle = NULL;
}
errorDesc = "Missing newline character.";
curPos++; /* Skip the newline character */
curLine++;
CHECK_SIZE_ERROR(ctrlError);
goto noCtrlError;
ctrlError:
xfree(lastString);
return FALSE;
noCtrlError:
xfree(lastString);
return TRUE;
}
/* All we really do for this function is parse and update the relevant
fields. */
void FmtDlgHeader()
{
char* buffer;
unsigned dataSize;
unsigned lastPos;
buffer = dlgHead;
dataSize = strlen(dlgHead);
curPos = 0;
lastPos = 0;
/* Skip whitespace and newlines */
while (curPos < dataSize && WS_AND_NL)
{
if (CHECK_CHAR('\n'))
curLine++;
curPos++;
}
CHECK_SIZE_ERROR(fmtHInternalErr);
if (CHECK_CHAR('\n'))
goto fmtHInternalErr;
/* Skip ID */
lastPos = curPos;
while (curPos < dataSize && !WS_AND_NL)
curPos++;
CHECK_SIZE_ERROR(fmtHInternalErr);
if (CHECK_CHAR('\n'))
goto fmtHInternalErr;
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(fmtHInternalErr);
if (CHECK_CHAR('\n'))
goto fmtHInternalErr;
/* Skip DIALOG or DIALOGEX */
lastPos = curPos;
while (curPos < dataSize && !WS_AND_NL)
curPos++;
CHECK_SIZE_ERROR(fmtHInternalErr);
if (CHECK_CHAR('\n'))
goto fmtHInternalErr;
SKIP_WHITESPACE();
CHECK_SIZE_ERROR(fmtHInternalErr);
if (CHECK_CHAR('\n'))
goto fmtHInternalErr;
/* Delete the old coordinates */
lastPos = curPos;
while (curPos < dataSize && !CHECK_CHAR('\n'))
curPos++;
CHECK_SIZE_ERROR(fmtHInternalErr);
/* (dataSize + 1) Move the null character too */
memmove(&dlgHead[lastPos], &dlgHead[curPos], (dataSize + 1) - curPos);
dataSize -= curPos - lastPos;
curPos = lastPos;
/* Write the updated ones */
{
char* outString;
/* Per programming convention, data (as opposed to single
variables) are always stored on the heap. */
/* Maximum buffer size assuming 32-bit integers */
outString = (char*)xmalloc(11 * 4 + 2 * 4 + 1);
sprintf(outString, "%li, %li, %li, %li",
dlgPos.x, dlgPos.y, dlgWidth, dlgHeight);
dlgHead = (char*)xrealloc(dlgHead, dataSize + strlen(outString) + 1);
memmove(&dlgHead[curPos+strlen(outString)], &dlgHead[curPos],
(strlen(dlgHead) + 1) - curPos); /* Move the null character too */
strncpy(&dlgHead[curPos], outString, strlen(outString));
dataSize += strlen(outString);
xfree(outString);
}
/* TODO: Update the font, need to store font family for this to work. */
fmtHInternalErr: ; /* This should never happen, but the label is here
in case it ever does. */
}
#define WRITE_DLG_VAR_TEXT(varname) \
line = (char*)xrealloc(line, lineLen + strlen(pCtrl->varname) + 1); \
strcpy(&line[lineLen], pCtrl->varname); \
lineLen += strlen(pCtrl->varname);
#define WRITE_COMMA() \
line = (char*)xrealloc(line, lineLen + 3); \
strcpy(&line[lineLen], ", "); \
lineLen += 2;
/* The caller of this function MUST xfree the returned memory. */
heap_char FmtControlText(unsigned ctrlNum)
{
char* line;
unsigned lineLen;
DlgItem* pCtrl;
/* Temporary variables */
unsigned i, j;
lineLen = 0;
pCtrl = &dlgControls.d[ctrlNum];
/* Write an initial tab */
line = (char*)xmalloc(2);
strcpy(&line[lineLen], "\t");
lineLen += 1;
/* Write the control name */
for (i = 0; i < numClasses; i++)
{
for (j = 0; j < numEachClass[i]; j++)
{
if (pCtrl->rendClass == i && pCtrl->rendType == j)
{
line = (char*)xrealloc(line, lineLen +
strlen(drawClasses[i][j]) + 1);
strcpy(&line[lineLen], drawClasses[i][j]);
lineLen += strlen(drawClasses[i][j]);
break;
}
}
}
/* Write a space */
line = (char*)xrealloc(line, lineLen + 2);
strcpy(&line[lineLen], " ");
lineLen += 1;
/* Write the caption */
/* Should SCROLLBAR really allow text? For now, no. */
/* Control types without text:
COMBOBOX, EDITTEXT, LISTBOX, SCROLLBAR */
if (pCtrl->rendClass == 3 && pCtrl->rendType == 3) /* ICON */
{
/* Don't write quotes for this part of the ICON control
statement, because it is a resource identifier. */
WRITE_DLG_VAR_TEXT(text);
WRITE_COMMA();
}
else if ((pCtrl->rendClass != 3 || /* Client boxes */
pCtrl->rendType == 3) && /* ICON */
pCtrl->rendClass != 7) /* Scrollbar */
{
char* untransLine;
unsigned oldLen;
unsigned untrLen;
/* Write a quote */
line = (char*)xrealloc(line, lineLen + 2);
strcpy(&line[lineLen], "\"");
lineLen += 1;
/* Translate special characters to escape codes */
oldLen = strlen(pCtrl->text);
untransLine = (char*)xmalloc(oldLen + 1);
strcpy(untransLine, pCtrl->text);
untransLine = UntransEscChars(untransLine, oldLen);
untrLen = strlen(untransLine);
line = (char*)xrealloc(line, lineLen + untrLen + 1);
strcpy(&line[lineLen], untransLine);
lineLen += untrLen;
xfree(untransLine);
/* WRITE_DLG_VAR_TEXT(text); */
/* Write a quote */
line = (char*)xrealloc(line, lineLen + 2);
strcpy(&line[lineLen], "\"");
lineLen += 1;
WRITE_COMMA();
}
/* Write the ID */
WRITE_DLG_VAR_TEXT(id);
WRITE_COMMA();
if (pCtrl->rendClass == 0) /* CONTROL */
{
/* Write the class */
WRITE_DLG_VAR_TEXT(wndClass);
WRITE_COMMA();
/* Write the style */
WRITE_DLG_VAR_TEXT(style);
WRITE_COMMA();
}
/* Write x, y, w, h */
{
char* outString;
/* Per programming convention, data (as opposed to single
variables) are always stored on the heap. */
/* Maximum buffer size assuming 32-bit integers */
outString = (char*)xmalloc(11 * 4 + 2 * 4 + 1);
sprintf(outString, "%i, %i, %i, %i",
pCtrl->x, pCtrl->y, pCtrl->cx, pCtrl->cy);
line = (char*)xrealloc(line, lineLen + strlen(outString) + 1);
strcpy(&line[lineLen], outString);
lineLen += strlen(outString);
xfree(outString);
}
/* Write the style and extended style */
if (pCtrl->rendClass == 0)
{
if (strlen(pCtrl->exStyle) != 0)
{
WRITE_COMMA();
/* Write exStyle */
WRITE_DLG_VAR_TEXT(exStyle);
}
}
else if (strlen(pCtrl->style) != 0)
{
WRITE_COMMA();
/* Just write style, it contains exStyle too */
WRITE_DLG_VAR_TEXT(style);
}
/* Write a newline character */
line = (char*)xrealloc(line, lineLen + 2);
strcpy(&line[lineLen], "\n");
lineLen += 1;
return line; /* This MUST be freed by the caller */
}
void FreeDlgData()
{
unsigned i;
for (i = 0; i < dlgControls.len; i++)
{
xfree(dlgControls.d[i].id);
xfree(dlgControls.d[i].style);
xfree(dlgControls.d[i].exStyle);
}
xfree(dlgControls.d);
dlgControls.d = NULL;
dlgControls.len = 0;
xfree(dlgFontFam);
dlgFontFam = NULL;
xfree(dlgHead);
dlgHead = NULL;
}