-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCStringParser.cpp
More file actions
2034 lines (1830 loc) · 54.3 KB
/
Copy pathCStringParser.cpp
File metadata and controls
2034 lines (1830 loc) · 54.3 KB
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2002, 2024, 2026 Ivyware Pty Ltd, Khrustal & Mann
// MELBOURNE, VICTORIA, AUSTRALIA, 3000
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// CStringParser implementation
// NOTES: Home grown CString parser thats evolved over time
#include "StdAfx.h"
#include "Float.h"
#include "CStringParser.h"
#include "Msgexception.h"
void MessageThrow ( LPCTSTR lpszMessage, ... );
//
//
// Description: CLParser object
// NOTES: Use this object for command line
// parsing.
//
///////////////////////////////////////////////////////////////////////
// Constructors and destructor
//
// Description: Constructors
//
//
// Parameters: LPCTSTR lpszRawCommand
// Raw parser command
//
CStringParser::CStringParser ( LPCTSTR lpszRawCommand )
{
// Firstly
RenderParserSafe();
size_t iSize = wcslen(lpszRawCommand);
m_szRawCommand = new TCHAR [iSize+2];
wmemcpy ( m_szRawCommand, lpszRawCommand, iSize );
m_szRawCommand[iSize++] = 0;
m_szRawCommand[iSize++] = 0;
m_pClpEnv = new ClpEnv ( m_szRawCommand );
}
CStringParser::CStringParser ( )
{
// Firstly
RenderParserSafe ();
}
CStringParser::~CStringParser () noexcept
{
// Garbage collection
delete [] m_szRawCommand;
delete m_pClpEnv;
}
void
CStringParser::RenderParserSafe()
{
// Attributes
//m_szRawCommand = 0;
//m_pClpEnv = 0;
}
///////////////////////////////////////////////////////////////////////
// Overloaded operators
//
// Description: operator []
//
//
// Parameters: int nIndex
// Element to be retrieved
//
// Returns: char
//
TCHAR
CStringParser::operator [] ( int nIndex )
{
// Simply
return m_pClpEnv->m_pCompleted[nIndex];
}
///////////////////////////////////////////////////////////////////////
// Parsing
//
// Description: Decodes encapsulated command according to
// passed format
//
// Parameters: const char *pFormat
// Command decode specification
//
// ...
// Variable argument associated with above format
//
int
CStringParser::Scanf ( LPCTSTR pFormat, ... )
{
// Introduce the locals
va_list ap;
int *pInteger = nullptr; // Integer decode pointer
UINT *pUINT = nullptr; // HEX decode pointer
double *pDouble = nullptr; // Double decode pointer
int *pAscii = nullptr; // Ascii decode pointer
TCHAR *pString = nullptr; // String decode pointer
TCHAR *pChar = nullptr; // Char decode pointer
TCHAR *pCompleted = m_pClpEnv -> m_pCompleted;
int nMatched = 0;
// Initialise
va_start ( ap, pFormat );
// Loop until passed format exhausted
while ( *pFormat )
{
LPCTSTR lpszCommand=m_pClpEnv->m_pCommand;//DELETE-ME
LPCTSTR lpszCompleted=m_pClpEnv->m_pCompleted;//DELETE-ME
// Manage format control character
if ( *pFormat == '$' )
{
pFormat++; // Consume '$' character
// Enumerated data field
if ( *pFormat == _T('E') )
{
pFormat++; // Consume 'E' character
if ( *pFormat != '{' )
MessageThrow ( _T("Enumerated format must start with '$E{'") );
m_pClpEnv = m_pClpEnv -> Push ( m_pClpEnv -> m_pCommand
, 0
, m_pClpEnv -> m_pCommand );
pFormat++; // Consume '{' character
m_pClpEnv -> m_pFormat = (TCHAR *)pFormat;
m_pClpEnv -> m_eFCF = _T('E');
m_pClpEnv -> m_ePtr = va_arg ( ap, int * );
*(m_pClpEnv -> m_ePtr) = -1;
m_pClpEnv -> m_eSeqNum = 0;
m_pClpEnv = m_pClpEnv -> Push ( 0, 0, 0 );
continue;
}
// Jump sequence
if ( *pFormat == _T('J') )
{
pFormat++; // Consume 'F' character
if ( *pFormat != _T('{') )
EVERR->MODULE
->Message( _T("Jump sequence must start with '$J{'") )
->Throw ( );
pFormat++; // Consume '{' character
m_pClpEnv -> m_pFormat = (TCHAR *)pFormat;
m_pClpEnv = m_pClpEnv -> Push ( m_pClpEnv -> m_pCommand
, 0
, m_pClpEnv -> m_pCommand );
m_pClpEnv -> m_eFCF = _T('J');
m_pClpEnv -> m_ePtr = va_arg ( ap, int * );
*(m_pClpEnv -> m_ePtr) = -1;
m_pClpEnv -> m_eSeqNum = 0;
m_pClpEnv = m_pClpEnv -> Push ( 0, 0, 0 );
continue;
}
// Consumed command position update
// NOTES: Ignore when skip mode switched on
if ( *pFormat == L'C' )
{
pFormat++; // Consume 'C' character
if ( !m_pClpEnv -> m_bSkip )
pCompleted = m_pClpEnv -> m_pCommand;
// ppChar = va_arg ( ap, char ** );
// if ( !m_pClpEnv -> m_bSkip )
// *ppChar = m_pClpEnv -> m_pCommand;
lpszCommand=m_pClpEnv->m_pCommand;//DELETE-ME
lpszCompleted=m_pClpEnv->m_pCompleted;//DELETE-ME
continue;
}
// Abandon for NULL remaining command
if ( *pFormat == _T('N') )
{
pFormat++; // Consume 'N' character
if ( *(m_pClpEnv -> m_pCommand) )
continue; // Data still remains
while ( m_pClpEnv -> m_nLevel > 0 )
m_pClpEnv = m_pClpEnv -> Pop ( 0, 0, 0 );
break;
}
// Skip to current position
if ( *pFormat == _T('S') )
{
pFormat++; // Consume 'S' character
if ( !m_pClpEnv -> m_bSkip )
pCompleted = m_pClpEnv -> m_pCommand;
continue;
}
// Unknown logic control field
else
EVERR->MODULE
->Message( _T("Unknown format logic control field ($%c)") )
->Throw ( );
}
// Manage data control fields
// NOTES: These fields are NOT decoded if the clp_env->skip
// flag is set. However, the corresponding pointer
// in the variable argument list is always addressed
// to keep synchronisation.
// : The decode function is relied upon to move the
// format pointer forwards.
if ( *pFormat == '%' )
{
int xSize = 0;
pFormat++; // Consume '%' character
if ( isdigit(*pFormat) )
{
while ( isdigit(*pFormat) )
xSize = xSize*10 + (int)(*pFormat++ - '0');
}
else if ( *pFormat == '*' )
{
xSize = va_arg ( ap, int );
pFormat++; // Consume '*' character
}
// Manage INTEGER control field
if ( *pFormat == _T('i') )
{
pInteger = va_arg ( ap, int * );
GetInteger ( &pFormat, pInteger, xSize );
}
// Manage HEXADECIMAL control field
else if ( *pFormat == _T('x') )
{
pUINT = va_arg ( ap, UINT * );
GetHexadecimal ( &pFormat, pUINT );
}
// Manage FLOAT control field
else if ( *pFormat == _T('f') )
{
pDouble = va_arg ( ap, double * );
GetFloat ( &pFormat, pDouble );
}
// Manage FLOAT currency field
else if ( *pFormat == _T('F') )
{
pDouble = va_arg ( ap, double * );
GetFloatCurrency ( &pFormat, pDouble );
}
// Manage ASCII control field
else if ( *pFormat == _T('a') )
{
pAscii = va_arg ( ap, int * );
GetAscii ( &pFormat, pAscii );
}
// Manage STRING control field
else if ( *pFormat == _T('s') )
{
pString = va_arg ( ap, TCHAR * );
GetString ( &pFormat, pString, xSize );
}
// Manage tabbed STRING control field
else if ( *pFormat == L'T' )
{
pString = va_arg ( ap, TCHAR * );
GetStringTabbed ( &pFormat, pString, xSize );
}
// Manage CHARACTER control field
else if ( *pFormat == _T('c') )
{
pChar = va_arg ( ap, TCHAR * );
GetChar ( &pFormat, pChar );
}
// Unknown control field type
else
MessageThrow ( _T("Valid data control fields %%(i,x,f,s,a or c) ")
_T("not [%c]\n")
_T("ADVICE\t: Bug(SNHappen)")
, *pFormat);
if ( !m_pClpEnv -> m_bSkip )
nMatched++;
continue;
}
// Manage optional logic
if ( *pFormat == _T('[') )
{
pFormat++; // Consume '[' character
m_pClpEnv = m_pClpEnv -> Push ( 0
, 0
, m_pClpEnv -> m_pCommand );
m_pClpEnv -> m_bOptional = true; // Flag optional logic
continue;
}
if ( *pFormat == _T(']') )
{
pFormat++; // Consume ']' character
if ( !m_pClpEnv -> m_bOptional )
MessageThrow( _T("Decode format ] mismatch") );
m_pClpEnv = m_pClpEnv -> Pop ( 0
, 0
, m_pClpEnv -> m_pCommand );
continue;
}
// End of enumerated data field flags
if ( *pFormat == '|' ||
*pFormat == '}' )
{
if ( m_pClpEnv -> m_eFCF != _T('E') &&
m_pClpEnv -> m_eFCF != _T('J') )
MessageThrow ( _T("| and } only valid in $E and $F field types") );
// Matching field flagged by skip mode not set
if ( !m_pClpEnv -> m_bSkip )
{
lpszCommand=m_pClpEnv->m_pCommand;//DELETE-ME
lpszCompleted=m_pClpEnv->m_pCompleted;//DELETE-ME
m_pClpEnv = m_pClpEnv -> Pop ( 0
, m_pClpEnv -> m_pCommand
, m_pClpEnv -> m_pCheckpoint );
lpszCommand=m_pClpEnv->m_pCommand;//DELETE-ME
lpszCompleted=m_pClpEnv->m_pCompleted;//DELETE-ME
if ( m_pClpEnv -> m_eFCF == _T('E') )
{
if ( *(m_pClpEnv -> m_ePtr) >= 0 )
MessageThrow ( _T("Command sequence (%s) is not unique")
, m_pClpEnv -> m_pCommand );
*(m_pClpEnv -> m_ePtr) = m_pClpEnv -> m_eSeqNum;
}
if ( m_pClpEnv -> m_eFCF == _T('J') )
{
if ( *(m_pClpEnv -> m_ePtr) >= 0 )
MessageThrow ( _T("Jump sequence (%s) is not unique")
, m_pClpEnv -> m_pCommand );
*(m_pClpEnv -> m_ePtr) = m_pClpEnv -> m_eSeqNum;
}
nMatched++;
}
// Non-matching field flagged by skip mode being set
else
m_pClpEnv = m_pClpEnv -> Pop ( 0, 0, 0 );
// Manage start of new enumeration field
// NOTES: Move command pointer back to checkpoint position
if ( *pFormat == L'|' )
{
m_pClpEnv -> m_eSeqNum++; // This sequence number
m_pClpEnv = m_pClpEnv -> Push ( 0
, 0
, m_pClpEnv -> m_pCheckpoint );
pFormat++; // Consume '|' character
continue;
}
// Multi-field termination character
if ( *pFormat == _T('}') )
{
pFormat++; // Consume '}' character
// Handle enumerated fields
if ( m_pClpEnv -> m_eFCF == _T('E') )
{
if ( *(m_pClpEnv -> m_ePtr) < 0 &&
!m_pClpEnv -> m_bOptional )
MessageThrow ( _T("Cannot enumerate sequence (%.64s)")
, m_pClpEnv -> m_pCommand );
m_pClpEnv = m_pClpEnv -> Pop ( 0
, 0
, m_pClpEnv -> m_pCompleted );
continue;
}
// Handle jump fields
if ( m_pClpEnv -> m_eFCF == _T('J') )
{
if ( *(m_pClpEnv -> m_ePtr) < 0 &&
!m_pClpEnv -> m_bOptional &&
!*m_pClpEnv -> m_pCommand )
MessageThrow ( _T("Cannot jump to sequence (%.64s)")
, m_pClpEnv -> m_pCommand );
// Located
if ( *(m_pClpEnv->m_ePtr) >= 0 )
{
m_pClpEnv = m_pClpEnv -> Pop ( 0
, 0
, m_pClpEnv -> m_pCompleted );
continue; // Normal success
}
// Failure - Move character pointer forward and start again
// NOTES: Done by resetting pointers etc
pFormat = m_pClpEnv -> m_pFormat;
m_pClpEnv -> m_eSeqNum = 0;
m_pClpEnv -> m_pCommand++;
m_pClpEnv -> m_pCompleted = m_pClpEnv -> m_pCommand;
m_pClpEnv -> m_pCheckpoint = m_pClpEnv -> m_pCommand;
m_pClpEnv -> m_bSkip = false;
m_pClpEnv = m_pClpEnv->Push ( m_pClpEnv->m_pCommand/*initially 0 21/6/11*/
, m_pClpEnv->m_pCommand/*initially 0 20/6/11*/
, m_pClpEnv->m_pCommand/*Was 20/6/11 + 1*/ );
//m_pClpEnv = m_pClpEnv -> Pop ( 0
// , m_pClpEnv -> m_pCommand + 1
// , m_pClpEnv -> m_pCompleted );
continue;
}
lpszCommand=m_pClpEnv->m_pCommand;//DELETE-ME
lpszCompleted=m_pClpEnv->m_pCompleted;//DELETE-ME
ASSERT(0);
continue;
}
}
// Abandon scanning
// NOTES: Only applies when no enumerated alteratives
// matched.
// : Must be within $E{...|...|^} sequence
if ( *pFormat == _T('^') )
{
if ( m_pClpEnv -> m_eFCF == _T('E') )
{
if ( *(m_pClpEnv -> m_ePtr) < 0 )
{
while ( m_pClpEnv -> m_nLevel > 0 )
m_pClpEnv = m_pClpEnv -> Pop ( m_pClpEnv -> m_pCompleted
, m_pClpEnv -> m_pCompleted
, m_pClpEnv -> m_pCompleted );
break;
}
pFormat++;
m_pClpEnv -> m_bSkip = true; // Flag field for skipping
continue;
}
ASSERT(0);
}
// Pattern matching is the default
if ( *pFormat == '\\' )
pFormat++;
if ( !m_pClpEnv -> m_bSkip ) // Not if skip mode is on
{
if ( *pFormat == ' ' &&
( *(m_pClpEnv -> m_pCommand) == ' ' ||
*(m_pClpEnv -> m_pCommand) == '\t' ) )
{
while ( *(++pFormat) == ' ' );
while ( *(m_pClpEnv -> m_pCommand) == ' ' ||
*(m_pClpEnv -> m_pCommand) == '\t' )
++m_pClpEnv->m_pCommand;
continue;
}
if ( toupper( (int)*pFormat )
== toupper ( (int)*(m_pClpEnv->m_pCommand) ) )
{
pFormat++; // Consume character
m_pClpEnv -> m_pCommand++;
continue;
}
if ( m_pClpEnv -> m_bOptional ||
m_pClpEnv -> m_eFCF == _T('E') )
{
m_pClpEnv -> m_bSkip = 1; // Activate skip flag
pFormat++; // Consume character
continue;
}
if ( m_pClpEnv -> m_bOptional ||
m_pClpEnv -> m_eFCF == _T('J') )
{
m_pClpEnv -> m_bSkip = 1; // Activate skip flag
pFormat++; // Consume character
continue;
}
EVERR->MODULE
->Message( _T("Invalid command sequence (%-.12s)"),
m_pClpEnv -> m_pCompleted )
->Throw();
}
pFormat++; // Increment format position
}
// Tidy up
if ( m_pClpEnv -> m_nLevel != 0 )
MessageThrow ( _T("Command level(%i) did not return to zero"),
m_pClpEnv -> m_nLevel );
m_pClpEnv -> m_pCommand = pCompleted;
m_pClpEnv -> m_pCompleted = pCompleted;
m_pClpEnv -> m_pCheckpoint = pCompleted;
va_end ( ap );
return ( nMatched );
}
//
// Description: Reformats encapsulated command to remove
// leading, trailing and multiple space instances.
//
int
CStringParser::Reformat ( )
{
// Introduce locals
int i;
int iLength = 0;
TCHAR *pCommand= 0;
// Remove leading spaces
for ( i = 0; ( i < iLength &&
pCommand[i] == ' ' ); i++ );
if ( i > 0 )
wmemcpy ( pCommand, &pCommand[1], iLength -= i );
pCommand[iLength] = 0;
// Finally
return iLength;
}
//
// Description: Empties the contents of this object
//
//
void
CStringParser::Empty ( )
{
// Simply
while ( *m_pClpEnv->m_pCommand )
m_pClpEnv -> m_pCommand++;
m_pClpEnv -> m_pCompleted = m_pClpEnv -> m_pCommand;
}
//
// Description: Retrieves switch from command string
//
//
// Parameters: int nArg
// Argument from which switch is to be retrieved
//
// LPCTSTR lpszArgDelimiters
// Argument delimiters
//
// int nSwitch
// Switch in above argument
//
// LPCTSTR lpszSwitchdelimiters
// Switch delimiters
//
// Returns: CString
// Retrived switch
//
CString
CStringParser::Switch ( int nArg, LPCTSTR lpszArgDelimiters
, int nSwitch, LPCTSTR lpszSwitchDelimiters )
{
// Introduce locals
CString csArgument, csSwitch;
int nSwitchTag = 0;
bool bQuotes = false;
// Fetch argument
csArgument = Argument ( nArg, lpszArgDelimiters );
// Fetch switch
for ( int i = 0; ( i < csArgument.GetLength() &&
nSwitchTag <= nSwitch ); i++ )
{
TCHAR cNext = csArgument[i];
LPCTSTR lpszDels = lpszSwitchDelimiters;
// Manage opening quotations
if ( !bQuotes &&
cNext == '\"' )
bQuotes = true;
while ( *lpszDels &&
!bQuotes )
{
if ( cNext != *lpszDels++ )
continue;
nSwitchTag++;
break;
}
if ( nSwitch == nSwitchTag &&
cNext != '\"' )
csSwitch += cNext;
// Manage closing quotations
if ( bQuotes &&
cNext == '\"' )
bQuotes = true;
}
// Tidy up and
return csSwitch;
}
//
// Description: Retrieves argument from command string
// NOTES: The retrieved argument is not consumed
//
//
// Parameters: int nArg
// Argument to be retrieved
//
// LPCTSTR lpszArgDelimiters
// Argument delimiters
//
// Returns: CString
// Retrived argument
//
CString
CStringParser::Argument ( int nArg, LPCTSTR lpszArgDelimiters )
{
// Introduce locals
CString csArgument;
size_t iSizeCmds = lstrlen(m_szRawCommand);
int nArgTag = 0;
bool bArgument = false;
bool bQuotes = false;
// Fetch argument
for ( size_t i = 0; ( i < iSizeCmds &&
nArgTag <= nArg ); i++ )
{
TCHAR cNext = m_szRawCommand[i];
LPCTSTR lpszDels = lpszArgDelimiters;
// Manage opening quotations
if ( !bQuotes &&
cNext == '\"' )
bQuotes = true;
while ( *lpszDels )
{
if ( cNext != *lpszDels ||
bQuotes )
{
lpszDels++;
continue;
}
if ( bArgument )
nArgTag++;
bArgument = false;
break;
}
if ( *lpszDels == 0 )
{
if ( nArg == nArgTag )
csArgument += cNext;
bArgument = true;
}
// Manage closing quotations
if ( bQuotes &&
cNext == '\"' )
bQuotes = true;
}
// Tidy up and
return csArgument;
}
//
// Description: Subtitute a new parser command
//
//
// Parameters: LPCTSTR lpszRawCommand
// Raw parser command
//
CString
CStringParser::Substitute ( LPCTSTR lpszRawCommand )
{
// Introduce locals
CString csCurrent = m_szRawCommand;
// Firstly
delete [] m_szRawCommand;
delete m_pClpEnv;
RenderParserSafe();
// Reload
size_t iSize = wcslen(lpszRawCommand);
m_szRawCommand = new TCHAR [iSize+2];
wmemcpy ( m_szRawCommand, lpszRawCommand, iSize );
m_szRawCommand[iSize++] = 0;
m_szRawCommand[iSize++] = 0;
m_pClpEnv = new ClpEnv ( m_szRawCommand );
// Tidy up and
return csCurrent;
}
//
// Consumes single command character
//
// Returns: TCHAR
// Consumed character
TCHAR
CStringParser::Consume ( )
{
// Introduce locals
TCHAR nChar = m_pClpEnv->m_pCommand[0];
// Adjust
if ( nChar )
{
m_pClpEnv -> m_pCommand++;
m_pClpEnv -> m_pCompleted++;
}
// Tidy up and
return nChar;
}
//
// Jump over multiple command characters
BOOL
CStringParser::JumpOver ( LPCTSTR lpszSequence )
{
// Command characters loop
LPCTSTR lpszCommand = m_pClpEnv -> m_pCommand;
while ( *lpszCommand )
{
// Jump characters loop
LPCTSTR lpszJumpover = lpszSequence;
while ( *lpszJumpover )
{
if ( *lpszCommand != *lpszJumpover )
break;
lpszJumpover++;
lpszCommand++;
if ( *lpszJumpover )
continue;
m_pClpEnv -> m_pCommand = (TCHAR *)lpszCommand;
m_pClpEnv -> m_pCompleted = (TCHAR *)lpszCommand;
return true;
}
lpszCommand++;
}
// Tidy up and
return false;
}
///////////////////////////////////////////////////////////////////////
// Utilities
//
//
// Description: Decodes an integer field from the passed
// command line
// NOTES: Command line integer fields are assumed to
// be a string of digits commencing at the
// current decode position.
// : Integer fields are designated by the %i
// format sequence.
//
//
// Parameters: char **ppFormat
// Command line parsing format definition
//
// char *pInteger
// Decoded value
//
int
CStringParser::GetInteger ( const TCHAR **ppFormat, int *pInteger, int xSize )
{
// Introduce the locals
TCHAR *pCommand;
long iValue = INT_MAX;
// Observe current skip mode
if ( !m_pClpEnv -> m_bSkip )
{
pCommand = m_pClpEnv -> m_pCommand;
if ( xSize <= 0 )
iValue = wcstol ( pCommand, &m_pClpEnv->m_pCommand, 10 );
else
{
iValue = 0;
while ( xSize-- > 0 && isdigit(*m_pClpEnv->m_pCommand) )
{
iValue = iValue*10 + ( *m_pClpEnv->m_pCommand - _T('0') );
m_pClpEnv->m_pCommand++;
}
}
if ( m_pClpEnv->m_pCommand == pCommand )
{
if ( m_pClpEnv -> m_bOptional ||
m_pClpEnv -> m_eFCF == 'E' )
m_pClpEnv -> m_bSkip = true;
else
MessageThrow ( _T("Missing integer data field") );
}
else
*pInteger = iValue;
}
// Tidy up and
*ppFormat = *ppFormat + 1;
return iValue;
}
//
//
// Description: Decodes hexadecimal field from the passed
// command line
// NOTES: Command line hexadecimal fields are assumed
// to be a string of digits commencing at the
// current decode position.
// : Hexadecimal fields are designated by the %x
// format sequence.
//
//
// Parameters: char **ppFormat
// Command line parsing format definition
//
// char *pUINT
// Decoded value
//
UINT
CStringParser::GetHexadecimal ( const TCHAR **ppFormat, UINT *pUINT )
{
// Introduce the locals
TCHAR *pCommand;
ULONG uiValue = INT_MAX;
// Observe current skip mode
if ( !m_pClpEnv -> m_bSkip )
{
pCommand = m_pClpEnv -> m_pCommand;
uiValue = wcstoul ( pCommand, &m_pClpEnv->m_pCommand, 0 );
if ( m_pClpEnv->m_pCommand == pCommand )
{
if ( m_pClpEnv -> m_bOptional ||
m_pClpEnv -> m_eFCF == 'E' )
m_pClpEnv -> m_bSkip = true;
else
MessageThrow ( _T("Missing integer data field") );
}
else
*pUINT = uiValue;
}
// Tidy up and
*ppFormat = *ppFormat + 1;
return uiValue;
}
//
//
// Decodes a float field from the passed command line
// NOTES: Command line float fields are assumed to be a string of digits
// commencing at the current decode position.
// : Integer fields are designated by the %f format sequence.
//
//
// Parameters: char **ppFormat
// Command line parsing format definition
//
// double *pDouble
// Decoded value
//
double
CStringParser::GetFloat ( const TCHAR **ppFormat, double *pDouble )
{
// Introduce the locals
TCHAR *pCommand;
double dValue = DBL_MAX;
// Observe current skip mode
if ( !m_pClpEnv -> m_bSkip )
{
pCommand = m_pClpEnv -> m_pCommand;
// Quotes interceptions
if ( *pCommand == L'"' )
{
*m_pClpEnv->m_pCommand++;
dValue = GetFloat ( ppFormat, pDouble );
if ( *m_pClpEnv->m_pCommand != L'"')
MessageThrow ( L"Miss-matched quotes for float data field" );
*m_pClpEnv->m_pCommand++;
*pDouble = dValue;
return dValue;
}
dValue = wcstod ( pCommand, &m_pClpEnv->m_pCommand );
if ( m_pClpEnv->m_pCommand == pCommand )
{
if ( m_pClpEnv -> m_bOptional ||
m_pClpEnv -> m_eFCF == 'E' )
m_pClpEnv -> m_bSkip = true;
else
MessageThrow ( L"Missing float data field" );
}
else
*pDouble = dValue;
}
// Tidy up and
*ppFormat = *ppFormat + 1;
return dValue;
}
//
//
// Description: Decodes a float field from the passed
// command line
// NOTES: Command line float fields are assumed to
// be a string of digits commencing at the
// current decode position.
// : Integer fields are designated by the %f
// format sequence.
//
//
// Parameters: char **ppFormat
// Command line parsing format definition
//
// double *pDouble
// Decoded value
//
double
_tcstodcomma ( const TCHAR *nptr, TCHAR **endptr )
{
double dValue = 0.0;
int nPoM = 1;
const TCHAR *nptr_work = nptr;
*endptr = (TCHAR *)nptr;
int nQuoted = 0;
while ( *nptr_work == _T(' ') ) nptr_work++;
if ( *nptr_work == _T('\"') ) { nQuoted = 1; nptr_work++; }
if ( *nptr_work == _T('-') ) { nPoM = -1; nptr_work++; }
else if ( *nptr_work == _T('+') ) { nptr_work++; }
TOP:while ( isdigit(*nptr_work) )
dValue = dValue * 10.0 + double ( (int)*nptr_work++ - (int)_T('0') );
if ( *nptr_work == _T(',') )
{
nptr_work++;
if ( isdigit(*(nptr_work+0)) &&
isdigit(*(nptr_work+1)) &&
isdigit(*(nptr_work+2)) &&
!isdigit(*(nptr_work+3)) )
goto TOP;
return 0.0;
}
if ( *nptr_work == _T('.') )
{
double dFactor = 0.1;
nptr_work++;
while ( isdigit(*nptr_work) )
{
dValue = dValue + double ( (int)*nptr_work++ - (int)_T('0') ) * dFactor;
dFactor /= 10;
}
if ( nQuoted &&
( *nptr_work == _T('.') ||
*nptr_work == _T(',') ) )
return 0.0;
}
if ( nQuoted )