A
download JclSscanf.pas
Language: Delphi
LOC: 460
Project Info
XVCL
Server: SourceForge
Type: cvs
...rge\x\xvcl\xvcl\JCL\Source\
   _source_.dof
   DJCL50.dpk
   JCL.INC
   Jcl8087.pas
   JclAppInst.pas
   JclBase.pas
   JclCOM.pas
   JclComplex.pas
   JclCounter.pas
   JclDateTime.pas
   JclDebug.pas
   JclExcel.pas
   jclexpreval.pas
   JclFileUtils.pas
   JclGraphics.pas
   JclGraphUtils.pas
   JclHookExcept.pas
   JclIniFiles.pas
   JclLANMan.pas
   JclLocales.pas
   JclLogic.pas
   JclMapi.pas
   JclMath.pas
   JclMime.pas
   JclMiscel.pas
   JclMultimedia.pas
   JclNTFS.pas
   JclPeImage.pas
   JclPrint.pas
   JclRegistry.pas
   JclResources.pas
   JclRTF.pas
   JclRTTI.pas
   JclSecurity.pas
   JclShell.pas
   JclSscanf.pas
   JclStatistics.pas
   JclStrHashMap.pas
   JclStrings.pas
   JclSynch.pas
   JclSysInfo.pas
   JclSysUtils.pas
   JclUnicode.pas
   JclUnicode.rc
   JclUnitConv.pas
   JclWin32.pas
   LM.pas

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
{******************************************************************************}
{                                                                              }
{ Project JEDI Code Library (JCL)                                              }
{                                                                              }
{ The contents of this file are subject to the Mozilla Public License Version  }
{ 1.1 (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.mozilla.org/MPL/ }
{                                                                              }
{ Software distributed under the License is distributed on an "AS IS" basis,   }
{ WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for }
{ the specific language governing rights and limitations under the License.    }
{                                                                              }
{ The Original Code is JclSscanf.pas.                                          }
{                                                                              }
{ The Initial Developer of the Original Code is documented in the accompanying }
{ help file JCL.chm. Portions created by these individuals are Copyright (C)   }
{ of these individuals.                                                        }
{                                                                              }
{******************************************************************************}
{                                                                              }
{ Pascal implementation of the C/C++ sscanf (String Scan Format) function.     }
{                                                                              }
{ Unit owner:                                                                  }
{ Last modified: January 30, 2001                                              }
{                                                                              }
{******************************************************************************}

unit JclSscanf;

{$I JCL.INC}

{$WEAKPACKAGEUNIT ON}

interface

uses
  SysUtils,
  JclBase;

function Sscanf(const Data: string; const Format: string; const Args: array of const): Integer;

type
  EJclSscanfError = class (EJclError);

implementation

uses
  JclResources, JclStrings;

type
  TFieldKind = (ftChar, ftSmallInt, ftInteger, ftInt64, ftFloating, ftCurrency,
    ftString, ftHex, ftPointer, ftCount, ftUnsigned, ftLiteral, ftWhiteSpace);

//------------------------------------------------------------------------------

procedure CreateSet(Source: string; var ResultSet: TSysCharSet);
var
  At: Integer;
  Token: Char;
  EndToken: Char;
  LoopToken: Char;
  Negate: Boolean;

  function GetToken(IsLiteral: Boolean): Char;
  begin
    if At <= Length(Source) then
    begin
      Result := Source[At];
      Inc(At);
      if not IsLiteral then
      begin
        if Result = '\' then  // Literal character.
          Result := GetToken(True);
        // Provides support for embedded control characters with the standard
        // Pascal ^a format. Removed because ^ has a special meaning for scanf
        // sets. Kept for future reference.
        (*
        else
        if Result ='^' then
        begin
          Result := UpCase(GetToken(True));
          if not Result in [#64..#95] then
            raise EJclSscanfError.Create(RsSscanfBadSet + Source);
          Result := Char(Byte(Result)-64);
        end;
        *)
      end;
    end
    else
      raise EJclSscanfError.Create(RsSscanfBadSet + Source);
  end;

begin
  ResultSet := [];
  At := 1;
  Negate := (Copy(Source, 1, 1) = '^');
  if Negate then
  begin
    Delete(Source, 1, 1);
    ResultSet := [#0..#255];
  end;
  while At <= Length(Source) do
  begin
    Token := GetToken(False);
    EndToken := Token;
    if (Copy(Source, At, 1) = '-') and (At <> Length(Source)) then  // This is a range.
    begin
      Inc(At, 1);  // Go past dash.
      if At > Length(Source) then
        raise EJclSscanfError.Create(RsSscanfBadSet + Source);
      EndToken := GetToken(False);
    end;
    if EndToken < Token then  // Z-A is probably the result of a missing letter.
      raise EJclSscanfError.Create(RsSscanfBadSet + Source);
    if Negate then
      for LoopToken := Token to EndToken do
        Exclude(ResultSet, LoopToken)
    else
      for LoopToken := Token to EndToken do
        Include(ResultSet, LoopToken);
  end;
end;

//------------------------------------------------------------------------------

function GetToken(const Str: string; var At: Integer): Char;
begin
  if At <= Length(Str) then
  begin
    Result := Str[At];
    Inc(At);
  end
  else
  begin
    At := Length(Str) + 1;
    raise EJclSscanfError.CreateResRec(@RsSscanfBadFormat);
  end;
end;

//------------------------------------------------------------------------------

function PeekToken(const Str: string; const At: Integer): Char;
begin
  if At <= Length(Str) then
    Result := Str[At]
  else
    Result := #0;
end;

//------------------------------------------------------------------------------

function GetScanfToken(const Format: string; var At: Integer): string;
var
  Token: Char;
  TokenDone: Boolean;
  BuildingSet: Boolean;
begin
  Token := GetToken(Format, At);
  if Token = '%' then
  begin
    Result := Token;
    BuildingSet := False;
    TokenDone := False;
    repeat
      Token := GetToken(Format, At);
      Result := Result + Token;
      if (Token = '\') and BuildingSet then
      begin
        Token := GetToken(Format, At);
        Result := Result + Token;
      end
      else
      if Token = '[' then
        BuildingSet := True
      else
      if Token = ']' then
      begin
        BuildingSet := False;
        Token := PeekToken(Format, At);
        if Token in ['C', 'c'] then
        begin
          Token := GetToken(Format, At);
          Result := Result + Token;
        end;
        TokenDone := True;
      end
      else
      if (Token in ['*', '0'..'9']) or BuildingSet then
      begin
        // Data is accepted and added to the tag.
      end
      else
        TokenDone := True;
    until TokenDone;
  end
  else
  begin
    Result := Token;
    while Token in AnsiWhiteSpace do
    begin
      Token := PeekToken(Format, At);
      if Token in AnsiWhiteSpace then
        Token := GetToken(Format, At);
    end;
  end;
end;

//------------------------------------------------------------------------------

procedure InterpretScanToken(ScanToken: string; var FieldType: TFieldKind;
  var FieldWidth: Integer; var CharSet: TSysCharSet; var Stored: Boolean);
const
  NumberSet = ['0'..'9', '-'];
  HexSet = ['0'..'9','A'..'F', 'a'..'f', '$', 'x', 'X', '-'];
var
  TokenChar: Char;
  At: Integer;
  EndAt: Integer;
  Frag: string;
  Token: Char;
begin
  CharSet := [];
  if Copy(ScanToken, 1, 1) = '%' then
  begin
    Delete(ScanToken, 1, 1);
    TokenChar := ScanToken[Length(ScanToken)];
    if TokenChar <> ']' then
      Delete(ScanToken, Length(ScanToken), 1)
    else
      TokenChar := 's';
    At := Pos('[', ScanToken);
    if At <> 0 then
    begin
      EndAt := At;
      Token := GetToken(ScanToken, EndAt);
      while Token <> ']' do
      begin
        Token := GetToken(ScanToken, EndAt);
        if Token = '\' then
        begin
          Token := GetToken(ScanToken, EndAt);
          if Token = ']' then
            Token := #0; // Skip Literal ]'s.
        end;
      end;
      Dec(EndAt);
      Frag := Copy(ScanToken, At + 1, EndAt - At - 1);
      Delete(ScanToken, At, EndAt - At + 1);
      CreateSet(Frag, CharSet);
    end
    else
      CharSet := [];
    At := Pos('*', ScanToken);
    Stored := (At = 0);
    if not Stored then
      Delete(ScanToken, At, 1);
    if ScanToken <> '' then
    begin
      try
        FieldWidth := StrToInt(ScanToken);
      except
        raise EJclSscanfError.CreateResRec(@RsSscanfBadFormat);
      end;
    end
    else
      FieldWidth := -1;
    case TokenChar of
      'c':
        begin
          FieldType := ftChar;
          if FieldWidth = -1 then
            FieldWidth := 1;
        end;
      'd':
        begin
          FieldType := ftInteger;
          CharSet := ['0'..'9', '-'];
        end;
      'e', 'f', 'g':
        begin
          FieldType := ftFloating;
          CharSet := ['0'..'9', '.', '+', '-', 'E', 'e'];
        end;
      {$IFDEF SUPPORTS_INT64}
      'i':
        begin
          FieldType := ftInt64;
          CharSet := NumberSet;
        end;
      {$ENDIF SUPPORTS_INT64}
      'h':
        begin
          FieldType := ftSmallInt;
          CharSet := NumberSet;
        end;
      'm':
        begin
          FieldType := ftCurrency;
          CharSet := NumberSet;
        end;
      's':
        begin
           FieldType := ftString;
        end;
      'x':
        begin
          FieldType := ftHex;
          CharSet := HexSet;
        end;
      'p':
        begin
          FieldType := ftPointer; // All pointers are 32 bit.
          CharSet := HexSet;
        end;
      'u':
        begin
          FieldType := ftUnsigned;
          CharSet := ['0'..'9'];
        end;
      '%':
        begin
          FieldType := ftLiteral;
          if (FieldWidth <> -1) or (not Stored) or (CharSet <> []) then
            raise EJclSscanfError.CreateResRec(@RsSscanfBadFormat);
          Stored := False;
        end;
    else
      raise EJclSscanfError.CreateResRec(@RsSscanfBadFormat);
    end;
  end
  else
  begin
    if ScanToken[1] in AnsiWhiteSpace then
    begin
      FieldType := ftWhiteSpace;
      FieldWidth := -1;
      Stored := False;
      CharSet := [];
    end
    else
    begin
      FieldType := ftLiteral;
      FieldWidth := Length(ScanToken);
      Stored := False;
      CharSet := [];
    end;
  end;
end;

//------------------------------------------------------------------------------

function GetDataToken(const Data: string; var DataAt: Integer;
  var OutOfData: Boolean; const FieldType: TFieldKind;
  const FieldWidth: Integer; CharSet: TSysCharSet): string;
var
  Token: Char;
begin
  OutOfData := False;
  Result := '';
  if FieldType = ftChar then
  begin
    try
      GetToken(Data, DataAt);
    except
      OutOfData := True;
      Exit; // Hit End of string.
    end;
  end
  else
  begin
    Token := #32;
    while Token in AnsiWhiteSpace do
    begin
      try
        Token := GetToken(Data, DataAt);
      except
        OutOfData := True;
        Exit; // Hit end of string.
      end;
    end;
    if FieldType = ftWhiteSpace then
    begin
      Dec(DataAt);  // Unget the last token.
      Exit;
    end;
  end;

  if CharSet = [] then
  begin
    CharSet := [#0..#255];
    if FieldType <> ftChar then
      CharSet := CharSet-AnsiWhiteSpace;
  end;

  Dec(DataAt);  // Unget the last token.
  while (FieldWidth = -1) or (Length(Result) < FieldWidth) do // Check Length
  begin
    Token := PeekToken(Data, DataAt);
    if Token in CharSet then
    begin
      try
        Token := GetToken(Data, DataAt);
        Result := Result+Token;
      except
        Break;
      end;
    end
    else
      Break;
  end;
end;

//------------------------------------------------------------------------------

function CheckFieldData(Data: string; const FieldType: TFieldKind): Boolean;
begin
  try
    case FieldType of
//      ftChar:
//        begin
//        end;
      ftSmallInt:
        StrToInt(Data);
      ftInteger:
        StrToInt(Data);
      {$IFDEF SUPPORTS_INT64}
      ftInt64:
        StrToInt64(Data);
      {$ENDIF SUPPORTS_INT64}
      ftFloating:
        StrToFloat(Data);
      ftCurrency:
        StrToFloat(Data);
//      ftString:
//        begin
//        end;
      ftHex:
        begin
          if AnsiUpperCase(Copy(Data, 1, 2)) = '0X' then
            Delete(Data, 1, 2);
          if Copy(Data, 1, 1) <> '$' then
            Data := '$'+Data;
          StrToInt(Data);
        end;
      ftPointer:
        StrToInt(Data);
//      ftCount:
//        begin
//        end;
      ftUnsigned:
        StrToInt(Data);
    end;
    Result := True;
  except
    Result := False;
  end;
end;

//------------------------------------------------------------------------------

function StoreData(Data: string; const FieldType: TFieldKind;
  const FieldWidth: Integer; P: Pointer; VType: Integer): Boolean;
begin
  try
    case FieldType of
      ftChar:
        Move(Data[1], P^, FieldWidth);
      ftSmallInt:
        SmallInt(P^) := StrToInt(Data);
      ftInteger:
        Integer(P^) := StrToInt(Data);
      {$IFDEF SUPPORTS_INT64}
      ftInt64:
        Int64(P^) := StrToInt64(Data);
      {$ENDIF SUPPORTS_INT64}
      ftFloating:
        Extended(P^) := StrToFloat(Data);
      ftCurrency:
        Currency(P^) := StrToFloat(Data);
      ftString:
        begin
          if VType = vtString then
            ShortString(P^) := Data
          else
            string(P^) := Data;
        end;
      ftHex:
        begin
          if AnsiUpperCase(Copy(Data, 1, 2)) ='0X' then
            Delete(Data, 1, 2);
          if Copy(Data, 1, 1) <> '$' then
            Data := '$'+Data;
          Integer(P^) := StrToInt(Data);
        end;
      ftPointer:
        Integer(P^) := StrToInt(Data);
      ftCount:
        begin
        end;
      ftUnsigned:
        Cardinal(P^) := StrToInt(Data);
    end;
    Result := True;
  except
    Result := False;
  end;
end;

//------------------------------------------------------------------------------

function Sscanf(const Data: string; const Format: string;
  const Args: array of const): Integer;
var
  At: Integer;
  DataAt: Integer;
  Results: Integer;
  ScanToken: string;
  StringToken: string;
  FieldType: TFieldKind;
  FieldWidth: Integer;
  CharSet: TSysCharSet;
  Stored: Boolean;
  OutOfData: Boolean;
  P: Pointer;
begin
  At := 1;
  DataAt := 1;
  Results := 0;

  while At <= Length(Format) do
  begin
    try
      ScanToken := GetScanfToken(Format, At);
      InterpretScanToken(ScanToken, FieldType, FieldWidth, CharSet, Stored);
    except
      Break;
    end;
    if FieldType <> ftCount then
      StringToken := GetDataToken(Data, DataAt, OutOfData, FieldType, FieldWidth, CharSet);

    if FieldType = ftLiteral then
      if StringToken <> ScanToken then
        Break;

    if not OutOfData then
    begin
      if not CheckFieldData(StringToken, FieldType) then
        Break;
      if Stored then
      begin
        if (Results + Low(Args)) > High(Args) then
          raise EJclSscanfError.CreateResRec(@RsSscanfInsufficient);
        P := Args[Results + Low(Args)].VPointer;
        StoreData(StringToken, FieldType, FieldWidth, P, Args[Results + Low(Args)].VType);
        Inc(Results);
      end;
    end;
  end;
  Result := Results;
end;

end.

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us