Subversion Repositories oidplus

Rev

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