Subversion Repositories oidplus

Rev

Rev 745 | Rev 748 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit OIDFILE;
  2.  
  3. (************************************************)
  4. (* OIDFILE.PAS                                  *)
  5. (* Author:   Daniel Marschall                   *)
  6. (* Revision: 2022-02-15                         *)
  7. (* License:  Apache 2.0                         *)
  8. (* This file contains:                          *)
  9. (* - Functions to handle an OID ASCII format    *)
  10. (************************************************)
  11.  
  12. interface
  13.  
  14. uses
  15.   StrList;
  16.  
  17. type
  18.   POID = ^TOID;
  19.   TOID = record
  20.     FileId: string;
  21.     DotNotation: string;
  22.     ASNIds: PStringList;
  23.     Description: string;
  24.     SubIds: PStringList; (* first 8 chars are FileId, followed by DotNotation *)
  25.     ParentFileId: string;
  26.     ParentDotNotation: string;
  27.   end;
  28.  
  29. procedure CreateOidDef(var oid: POid);
  30. procedure FreeOidDef(oid: POid);
  31. procedure ClearOidDef(oid: POid);
  32. function WriteOidFile(filename: string; oid: POid): boolean;
  33. function ReadOidFile(filename: string; oid: POid): boolean;
  34.  
  35. (* For the strings in the list "SubIds": *)
  36. function FileIdPart(s: string): string;
  37. function DotNotationPart(s: string): string;
  38.  
  39. implementation
  40.  
  41. uses
  42.   VtsFuncs, OidUtils;
  43.  
  44. const
  45.   WANT_VERS = '2022';
  46.  
  47. procedure CreateOidDef(var oid: POid);
  48. begin
  49.   GetMem(oid, SizeOf(TOID));
  50.   oid^.FileId := '';
  51.   oid^.DotNotation := '';
  52.   oid^.Description := '';
  53.   oid^.ParentFileId := '';
  54.   oid^.ParentDotNotation := '';
  55.   CreateList(oid^.ASNIds);
  56.   CreateList(oid^.SubIds);
  57. end;
  58.  
  59. procedure FreeOidDef(oid: POid);
  60. begin
  61.   FreeList(oid^.ASNIds);
  62.   FreeList(oid^.SubIds);
  63.   FreeMem(oid, SizeOf(TOID));
  64. end;
  65.  
  66. procedure ClearOidDef(oid: POid);
  67. begin
  68.   oid^.FileId := '';
  69.   oid^.DotNotation := '';
  70.   oid^.Description := '';
  71.   oid^.ParentFileId := '';
  72.   oid^.ParentDotNotation := '';
  73.   ListClear(oid^.ASNIds);
  74.   ListClear(oid^.SubIds);
  75. end;
  76.  
  77. procedure ListBubbleSortSubIds(oid: POid);
  78. var
  79.   n, i: integer;
  80.   a, b: string;
  81.   swapped: boolean;
  82. begin
  83.   n := ListCount(oid^.SubIds);
  84.   while n>1 do
  85.   begin
  86.     i := 0;
  87.     swapped := false;
  88.     while i<n-1 do
  89.     begin
  90.       a := DotNotationPart(ListGetElement(oid^.SubIds, i));
  91.       b := DotNotationPart(ListGetElement(oid^.SubIds, i+1));
  92.       if CompareOID(a, b) > 0 then
  93.       begin
  94.         ListSwapElement(oid^.SubIds, i, i+1);
  95.         swapped := true;
  96.       end;
  97.       Inc(i);
  98.     end;
  99.     if not swapped then break;
  100.     Dec(n);
  101.   end;
  102. end;
  103.  
  104. function WriteOidFile(filename: string; oid: POid): boolean;
  105. var
  106.   f: Text;
  107.   i: integer;
  108.   lines: PStringList;
  109.   sTmp: string;
  110.   desc: string;
  111. begin
  112.   Assign(f, filename);
  113.  
  114.   {$I-}
  115.   Rewrite(f);
  116.   {$I+}
  117.   if IoResult <> 0 then
  118.   begin
  119.     WriteOidFile := false;
  120.     (* Must not call Close(f) if file was never opened *)
  121.     Exit;
  122.   end;
  123.  
  124.   WriteLn(f, 'VERS' + WANT_VERS);
  125.  
  126.   WriteLn(f, 'SELF' + oid^.FileId + oid^.DotNotation);
  127.  
  128.   WriteLn(f, 'SUPR' + oid^.ParentFileId + oid^.ParentDotNotation);
  129.  
  130.   (* Sort sub IDs *)
  131.   ListBubbleSortSubIds(oid);
  132.  
  133.   for i := 0 to ListCount(oid^.SubIds)-1 do
  134.   begin
  135.     sTmp := ListGetElement(oid^.SubIds, i);
  136.     WriteLn(f, 'CHLD' + sTmp);
  137.   end;
  138.  
  139.   for i := 0 to ListCount(oid^.AsnIds)-1 do
  140.   begin
  141.     sTmp := ListGetElement(oid^.AsnIds, i);
  142.     WriteLn(f, 'ASN1' + sTmp);
  143.   end;
  144.  
  145.   desc := Trim(oid^.Description);
  146.   if desc <> '' then
  147.   begin
  148.     CreateList(lines);
  149.     SplitStrToList(desc, lines, #13#10);
  150.     for i := 0 to ListCount(lines)-1 do
  151.     begin
  152.       sTmp := ListGetElement(lines, i);
  153.       WriteLn(f, 'DESC' + sTmp);
  154.     end;
  155.     FreeList(lines);
  156.   end;
  157.  
  158.   Close(f);
  159.  
  160.   WriteOidFile := true;
  161. end;
  162.  
  163. function ReadOidFile(filename: string; oid: POid): boolean;
  164. var
  165.   f: Text;
  166.   line, cmd: string;
  167.   version: string;
  168. begin
  169.   ClearOidDef(oid);
  170.   version := '';
  171.  
  172.   Assign(f, filename);
  173.   {$I-}
  174.   Reset(f);
  175.   {$I+}
  176.   if IoResult <> 0 then
  177.   begin
  178.     ReadOidFile := false;
  179.     (* Must not call Close(f) if file was never opened *)
  180.     Exit;
  181.   end;
  182.  
  183.   while not EOF(f) do
  184.   begin
  185.     ReadLn(f, line);
  186.     cmd := Copy(line,1,4);
  187.     Delete(line,1,4);
  188.  
  189.     if cmd = 'VERS' then
  190.     begin
  191.       version := line;
  192.     end;
  193.  
  194.     if cmd = 'SELF' then
  195.     begin
  196.       oid^.FileId := Copy(line,1,8);
  197.       Delete(line,1,8);
  198.       oid^.DotNotation := line;
  199.     end;
  200.  
  201.     if cmd = 'SUPR' then
  202.     begin
  203.       oid^.ParentFileId := FileIdPart(line);
  204.       oid^.ParentDotNotation := DotNotationPart(line);
  205.     end;
  206.  
  207.     if cmd = 'CHLD' then
  208.     begin
  209.       ListAppend(oid^.SubIds, line);
  210.     end;
  211.  
  212.     if cmd = 'ASN1' then
  213.     begin
  214.       ListAppend(oid^.ASNIds, line);
  215.     end;
  216.  
  217.     if cmd = 'DESC' then
  218.     begin
  219.       oid^.Description := oid^.Description + line + #13#10;
  220.     end;
  221.   end;
  222.  
  223.   (* Sort sub IDs *)
  224.   ListBubbleSortSubIds(oid);
  225.  
  226.   (* Remove last CRLF *)
  227.   oid^.Description := Copy(oid^.Description, 1, Length(oid^.Description)-Length(#13#10));
  228.  
  229.   (* Check if everything is correct *)
  230.   ReadOidFile := (version = WANT_VERS) and (oid^.FileId <> '');
  231.  
  232.   Close(f);
  233. end;
  234.  
  235. function FileIdPart(s: string): string;
  236. begin
  237.   FileIdPart := Copy(s,1,8);
  238. end;
  239.  
  240. function DotNotationPart(s: string): string;
  241. begin
  242.   Delete(s,1,8);
  243.   DotNotationPart := s;
  244. end;
  245.  
  246. end.
  247.