home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0007_READFILE.PAS.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  5.8 KB  |  180 lines

  1. {
  2.  Could somebody post some source code on how to read in a config File?  and
  3.  also have it ignore lines that start With the semicolon. Sorta like this
  4.  one:
  5.  
  6. Sure do, here is mine.  I have to include quite a couple of other Functions as
  7. they are used in the readcfg.  I included one 'block' as an example in which
  8. you read in a particular keyWord (named: 'keyWord') and find the parammeter
  9. which follows.  You can duplicate this block as many times as you like.
  10. Although it scans the whole File again, it's pretty fast as it does it in
  11. memory.
  12. }
  13. Function Trim(S : String) : String;
  14.   {Return a String With leading and trailing white space removed}
  15. Var
  16.   I : Word;
  17.   SLen : Byte Absolute S;
  18. begin
  19.   While (SLen > 0) and (S[SLen] <= ' ') do
  20.     Dec(SLen);
  21.   I := 1;
  22.   While (I <= SLen) and (S[I] <= ' ') do
  23.     Inc(I);
  24.   Dec(I);
  25.   if I > 0 then
  26.     Delete(S, 1, I);
  27.   Trim := S;
  28. end;
  29.  
  30.  
  31. {******************************************************}
  32. Function StrUpper(Str: String): String; Assembler;
  33.  Asm
  34.       jmp   @Start    { Jump over Table declared in the Code Segment }
  35.  
  36.   @Table:
  37.     { Characters from ASCII 0 --> ASCII 96 stay the same }
  38.   DB 00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21
  39.   DB 22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43
  40.   DB 44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65
  41.   DB 66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87
  42.   DB 88,89,90,91,92,93,94,95,96
  43.     { Characters from ASCII 97 "a" --> ASCII 122 "z" get translated }
  44.     { to Characters ASCII 65 "A" --> ASCII 90 "Z" }
  45.   DB 65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86
  46.   DB 87,88,89,90
  47.     { Characters from ASCII 123 --> ASCII 127 stay the same }
  48.   DB 123,124,125,126,127
  49.     { Characters from ASCII 128 --> ASCII 165 some changes
  50.      #129 --> #154, #130 --> #144, #132 --> #142, #134 --> #143
  51.       #135 --> #128, #145 --> #146, #148 --> #153, #164 --> #165}
  52.  
  53.   DB 128,154,144,131,142,133,143,128,136,137,138,139,140,141,142,143
  54.   DB 144,146,146,147,153,149,150,151,152,153,154,155,156,157,158,159
  55.   DB 160,161,162,163,165,165
  56.     { Characters from ASCII 166 --> ASCII 255 stay the same }
  57.   DB 166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181
  58.   DB 182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197
  59.   DB 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213
  60.   DB 214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229
  61.   DB 230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245
  62.   DB 246,247,248,249,250,251,252,253,254,255
  63.  
  64.   @Start:
  65.       push  DS                { Save Turbo's Data Segment address    }
  66.       lds   SI,Str            { DS:SI points to Str[0]               }
  67.       les   DI,@Result        { ES:DI points to StrUpper[0]          }
  68.       cld                     { Set direction to Forward             }
  69.       xor   CX,CX             { CX = 0                               }
  70.       mov   BX,ofFSET @Table  { BX = offset address of LookUpTable   }
  71.       lodsb                   { AL = Length(Str); SI -> Str[1]       }
  72.       mov   CL,AL             { CL = Length(Str)                     }
  73.       stosb                   { Move Length(Str) to Length(StrUpper) }
  74.       jcxz  @Exit             { Get out if Length(Str) is zero       }
  75.  
  76.   @GetNext:
  77.       lodsb                   { Load next Character into AL          }
  78.       segcs XLAT              { Translate Char using the LookupTable }
  79.                               { located in Code Segment at offset BX }
  80.       stosb                   { Save next translated Char in StrUpper}
  81.       loop  @GetNext          { Get next Character                   }
  82.  
  83.   @Exit:
  84.       pop   DS                { Restore Turbo's Data Segment address }
  85. end {StrUpper};
  86. {-----------------------------------------------------------------}
  87. Function MCS(element,line:String):Integer;
  88.  
  89. {Returns the position of an element in a line.
  90.  Returns zero if no match found.
  91.  Example: line:='abcdefg'
  92.  i:=MCS('bc',line) would make i=2
  93.  MCS is not Case sensitive}
  94.  
  95. begin
  96.   MCS:=pos(StrUpper(element),StrUpper(line));
  97. end;
  98.  
  99. Function getparameter(element,line:String;pos:Integer):String;
  100. {This Function is called With 'pos' already indexed after the command Word in
  101. a line.  It searches For the Word(s) after the command Word in the rest of
  102. the line, up to the end of the line or Until a ; is encountered}
  103.  
  104. Var
  105.   n,b,e,l:Byte;
  106.  
  107. begin
  108.    n:=pos+length(element);
  109.    {places n-index just after keyWord}
  110.  
  111.    While (line[n]=' ') do
  112.      inc(n); {increment line[n] over spaces}
  113.    b:=n; l:=length(line);
  114.    While (n<=l)  do
  115.    begin
  116.      if line[n]<>';' then
  117.      begin
  118.        inc(n);
  119.        e:=n;
  120.      end
  121.      else
  122.      begin
  123.        e:=n;
  124.        n:=l+1;
  125.      end;
  126.    end;
  127.    getparameter:=trim(copy(line,b,e-b));
  128.  
  129. end;
  130.  
  131. Procedure ReadCfg(name:String);  {'name' is Filename to read in}
  132. Type
  133.   Line     = String[80];
  134.   Lines    = Array[0..799] of Line;
  135.   LinesP   = ^Lines;
  136. Var
  137.   TextBuf  : LinesP;
  138.   TextFile : Text;
  139.   Index,Number:Integer;
  140.   buffer:Array[1..2048] of Char;
  141.   s:line;
  142.   s1:line;
  143.   n:Byte;
  144.   i:Integer;
  145. begin
  146.   assign( TextFile, name );
  147.   reset( TextFile );
  148.   SetTextBuf(TextFile,Buffer);
  149.   Index := 0;
  150.   new(TextBuf);
  151.  
  152.   While  not eof( TextFile)  do
  153.   {Read the Text File into heap memory}
  154.   begin
  155.     readln( TextFile,s);
  156.     if s[1]<>';' then if s<>'' then
  157.     begin
  158.       TextBuf^[Index]:=s;
  159.       inc( Index )
  160.     end;
  161.   end;
  162.   close( TextFile );
  163.  
  164. {********begin of  "find a keyWord" block***********}
  165.   Number := Index -1;
  166.   For Index := 0 to Number do
  167.   begin
  168.     s:=( TextBuf^[ Index ]);
  169.     n:=MCS('BoardNo',s);
  170.     if n > 0 then
  171.     begin
  172.       s1:=getparameter('KeyWord',s,n);
  173.       {do other things With found 'keyWord'}
  174.     end;
  175.   end;
  176. {end of "find a keyWord" block}
  177.  
  178.   dispose( TextBuf);  {release heap memory}
  179. end;
  180.