home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / mp3osr05.zip / src / jctl.pas < prev    next >
Pascal/Delphi Source File  |  1999-12-21  |  6KB  |  279 lines

  1. {
  2.    .ctl files reader
  3.  
  4.    version 1.07
  5.  
  6.    Copyright (c) 1999 by Alexander Trunov [2:5069/10] [345:818/1]
  7.  
  8.         óδ ¼«ªÑΓÑ ßó«í«ñ¡« ¿ß»«½∞º«óáΓ∞, ¿º¼Ñ¡∩Γ∞ φë༫ñy½∞, ¡« ¼¡Ñ íyñÑΓ
  9.    «τÑ¡∞ »α¿∩Γ¡«, Ñß½¿ óδ ó¬½ετ¿ΓÑ ¼«Ñ ¿¼∩ ó ¬αÑñ¿Γßδ, á ΓᬪѠ»α¿Φ½ÑΓÑ
  10.    ¿ßσ«ñ¡¿¬ ¿ß»αáó½Ñ¡¡«⌐/¼«ñ¿Σ¿µ¿α«óá¡«⌐ óÑαß¿¿, Ñß½¿ Γᬫóá∩ íyñÑΓ,
  11.    ¬«¡Ñτ¡« ;)
  12.  
  13.    1.0  base realisation
  14.    1.01 fixed mistype - tab symbol was #8, now it's #9
  15.    1.02 fixed logical error - starting memory stream size was 1024
  16.    1.03 rewritten initialization part
  17.    1.04 added macros '@include'
  18.    1.05 fixed stupid memory leak. great help was provided by HeapTrc unit
  19.     from FreePascal package
  20.    1.06 added work with pools
  21.    1.07 fix for huge strings
  22.  
  23. }
  24.  
  25. unit jCtl;
  26.  
  27. interface
  28.  
  29. uses
  30.   Objects;
  31.  
  32. type
  33.  
  34.   PKeyRecord = ^TKeyRecord;
  35.   TKeyRecord = record
  36.     Key, Value: string;
  37.   end;
  38.  
  39.   PSCollection = ^TSCollection;
  40.   TSCollection = object(TCollection)
  41.     procedure FreeItem(P: Pointer); virtual;
  42.   end;
  43.  
  44.   PCtl = ^TCtl;
  45.   TCtl = object(TObject)
  46.  
  47.     coll: PSCollection;
  48.     wasExist: Boolean;
  49.  
  50.     constructor Init(CtlName: string);
  51.     destructor Done; virtual;
  52.  
  53.     function GetMString(Num: Longint; Param: string): string;
  54.         { »«½yτ¿Γ∞ »áαá¼ÑΓα ¡«¼Ñα Num }
  55.     function GetString(Param: string): string;
  56.     function GetMBoolean(Num: Longint; Param: string): Boolean;
  57.     function GetBoolean(Param: string): Boolean;
  58.     function GetMLongint(Num: Longint; Param: string): Longint;
  59.     function GetLongint(Param: string): Longint;
  60.     function GetPool(Param: string): PStringCollection;
  61.  
  62.     function ExistKey(Param: string): Boolean;
  63.  
  64.   private
  65.  
  66.     CtlFile: Text;
  67.     i: Longint;
  68.     kr: PKeyRecord;
  69.  
  70.     procedure IncludeFile(aFileName: string);
  71.  
  72.   end;
  73.  
  74. implementation
  75.  
  76. uses Wizard;
  77.  
  78. procedure TSCollection.FreeItem(P: Pointer);
  79. begin
  80.   Dispose(PKeyRecord(P));
  81. end;
  82.  
  83. procedure TCtl.IncludeFile(aFileName: string);
  84. var
  85.   S: string;
  86.   F: Text;
  87. begin
  88.   if not ExistFile(aFileName) then Exit;
  89.   Assign(F, aFileName);
  90.   Reset(F);
  91.  
  92.   while not EOF(F) do
  93.   begin
  94.     ReadLn(F, S);
  95.     s := Trim(s);
  96.     if ((Length(s) > 0) and (s[1] <> ';')) and (s <> '') then
  97.     begin
  98.       New(kr);
  99.       kr^.key := ExtractWord(1, s, [' ', #9]);
  100.       Delete(s, 1, Length(kr^.key));
  101.       kr^.value := Trim(ExtractWord(1, Trim(s), [';']));
  102.       if Copy(Trim(s), 1, 1) = ';' then kr^.Value := '';
  103.       if stLocase(kr^.Key) <> '@include' then
  104.       begin
  105.         coll^.Insert(kr);
  106.       end
  107.       else
  108.       begin
  109.         S := kr^.Value;
  110.         Dispose(kr);
  111.         if JustPathName(aFileName) = '' then
  112.           IncludeFile(S)
  113.         else
  114.           IncludeFile(JustPathname(aFileName) + '\' + S);
  115.       end;
  116.     end;
  117.   end;
  118.  
  119.   Close(F);
  120. end;
  121.  
  122. constructor TCtl.Init(CtlName: string);
  123. begin
  124.   inherited Init;
  125.  
  126.   coll := New(PSCollection, Init(10, 10));
  127.   Assign(CtlFile, CtlName);
  128. {$I-}
  129.   Reset(CtlFile);
  130.   if IOResult <> 0 then
  131.   begin
  132.     wasExist := False;
  133.     Exit
  134.   end
  135.   else
  136.     wasExist := True;
  137.   Close(CtlFile);
  138.   IncludeFile(CtlName);
  139. end;
  140.  
  141. function TCtl.GetMString(Num: Longint; Param: string): string;
  142. var
  143.   j: Longint;
  144. begin
  145.   GetMString := '';
  146.   i := 0; j := 0;
  147.   while (i <> num) and (j <= coll^.Count - 1) do
  148.   begin
  149.     if stLocase(PKeyRecord(coll^.Items^[j])^.Key) = stLocase(Param) then
  150.     begin
  151.       inc(i);
  152.     end;
  153.     inc(j);
  154.   end;
  155.  
  156.   if num = i then
  157.   begin
  158.     kr := PKeyRecord(coll^.Items^[j - 1]);
  159.     GetMString := kr^.value;
  160.   end
  161. end;
  162.  
  163. function TCtl.GetString(Param: string): string;
  164. begin
  165.   GetString := GetMString(1, Param);
  166. end;
  167.  
  168. function TCtl.GetMBoolean(Num: Longint; Param: string): Boolean;
  169.  
  170.   function Str2Bool(s: string): Boolean;
  171.   var
  172.     s1: string;
  173.   begin
  174.     s1 := stLocase(s);
  175.     if (s1 = 'yes') or (s1 = 'true') or (s1 = 'yeah') then
  176.       Str2Bool := True
  177.     else
  178.       Str2Bool := False;
  179.   end;
  180. begin
  181.   GetMBoolean := Str2Bool(GetMString(Num, Param));
  182. end;
  183.  
  184. function TCtl.GetBoolean(Param: string): Boolean;
  185. begin
  186.   GetBoolean := GetMBoolean(1, Param);
  187. end;
  188.  
  189. function TCtl.GetMLongint(Num: Longint; Param: string): Longint;
  190. var
  191.   Tmp: Longint;
  192. begin
  193.   Str2Longint(GetMString(Num, Param), Tmp);
  194.   GetMLongint := Tmp;
  195. end;
  196.  
  197. function TCtl.GetLongint(Param: string): Longint;
  198. begin
  199.   GetLongint := GetMLongint(1, Param);
  200. end;
  201.  
  202. function TCtl.ExistKey(Param: string): Boolean;
  203. begin
  204.   if GetString(Param) = '' then
  205.     ExistKey := False
  206.   else
  207.     ExistKey := True;
  208. end;
  209.  
  210. function TCtl.GetPool(Param: string): PStringCollection;
  211. var
  212.   poolStr, Command, S: string;
  213.   pool: PStringCollection;
  214.  
  215.   procedure AddFile(aFileName: string);
  216.   var
  217.     f: Text;
  218.   begin
  219.     if not ExistFile(aFileName) then Exit;
  220.     Assign(f, aFileName);
  221.     Reset(f);
  222.     while not EOF(f) do
  223.     begin
  224.       Readln(f, S);
  225.       pool^.AtInsert(pool^.Count, NewStr(S));
  226.     end;
  227.     Close(f);
  228.   end;
  229. begin
  230.   pool := New(PStringCollection, Init(5, 5));
  231.   if not ExistKey(Param) then
  232.   begin
  233.     GetPool := pool;
  234.     Exit;
  235.   end;
  236.   i := 1;
  237.   while GetMString(i, Param) <> '' do
  238.   begin
  239.     poolStr := GetMString(i, Param);
  240.     Command := stLocase(ExtractWord(1, poolStr, [' ', #9]));
  241.     if Command = 'kill' then pool^.FreeAll;
  242.     if Command = 'replace' then
  243.     begin
  244.       pool^.FreeAll;
  245.       Delete(poolStr, 1, 8);
  246.       pool^.AtInsert(pool^.Count, NewStr(Trim(poolStr)));
  247.     end;
  248.     if Command = 'add' then
  249.     begin
  250.       Delete(poolStr, 1, 4);
  251.       pool^.AtInsert(pool^.Count, NewStr(Trim(poolStr)));
  252.     end;
  253.     if Command = 'addfile' then
  254.     begin
  255.       Delete(poolStr, 1, 8);
  256.       AddFile(Trim(poolStr));
  257.     end;
  258.     if Command = 'replacefile' then
  259.     begin
  260.       Delete(poolStr, 1, 12);
  261.       pool^.FreeAll;
  262.       AddFile(Trim(poolStr));
  263.     end;
  264.  
  265.     inc(i);
  266.   end;
  267.  
  268.   GetPool := pool;
  269. end;
  270.  
  271. destructor TCtl.Done;
  272. begin
  273.   Dispose(coll, Done);
  274.   inherited Done;
  275. end;
  276.  
  277. end.
  278.  
  279.