Subversion Repositories oidplus

Rev

Rev 739 | 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. procedure WriteOidFile(filename: string; oid: POid);
  32. procedure ReadOidFile(filename: string; oid: POid);
  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. procedure WriteOidFile(filename: string; oid: POid);
  98. var
  99.   f: Text;
  100.   i: integer;
  101.   lines: PStringList;
  102.   sTmp: string;
  103.   desc: string;
  104. begin
  105.   Assign(f, filename);
  106.   Rewrite(f);
  107.  
  108.   WriteLn(f, 'VERS' + WANT_VERS);
  109.  
  110.   WriteLn(f, 'SELF' + oid^.FileId + oid^.DotNotation);
  111.  
  112.   WriteLn(f, 'SUPR' + oid^.Parent);
  113.  
  114.   (* Sort sub IDs *)
  115.   ListBubbleSortSubIds(oid);
  116.  
  117.   for i := 0 to ListCount(oid^.SubIds)-1 do
  118.   begin
  119.     sTmp := ListGetElement(oid^.SubIds, i);
  120.     WriteLn(f, 'CHLD' + sTmp);
  121.   end;
  122.  
  123.   for i := 0 to ListCount(oid^.AsnIds)-1 do
  124.   begin
  125.     sTmp := ListGetElement(oid^.AsnIds, i);
  126.     WriteLn(f, 'ASN1' + sTmp);
  127.   end;
  128.  
  129.   desc := Trim(oid^.Description);
  130.   if desc <> '' then
  131.   begin
  132.     CreateList(lines);
  133.     SplitStrToList(desc, lines, #13#10);
  134.     for i := 0 to ListCount(lines)-1 do
  135.     begin
  136.       sTmp := ListGetElement(lines, i);
  137.       WriteLn(f, 'DESC' + sTmp);
  138.     end;
  139.     FreeList(lines);
  140.   end;
  141.  
  142.   Close(f);
  143. end;
  144.  
  145. procedure ReadOidFile(filename: string; oid: POid);
  146. var
  147.   f: Text;
  148.   line, cmd: string;
  149.   version: string;
  150. begin
  151.   ClearOidDef(oid);
  152.   version := '';
  153.  
  154.   Assign(f, filename);
  155.   Reset(f);
  156.   while not EOF(f) do
  157.   begin
  158.     ReadLn(f, line);
  159.     cmd := Copy(line,1,4);
  160.     Delete(line,1,4);
  161.  
  162.     if cmd = 'VERS' then
  163.     begin
  164.       version := line;
  165.     end;
  166.  
  167.     if cmd = 'SELF' then
  168.     begin
  169.       oid^.FileId := Copy(line,1,8);
  170.       Delete(line,1,8);
  171.       oid^.DotNotation := line;
  172.     end;
  173.  
  174.     if cmd = 'SUPR' then
  175.     begin
  176.       oid^.Parent := line;
  177.     end;
  178.  
  179.     if cmd = 'CHLD' then
  180.     begin
  181.       ListAppend(oid^.SubIds, line);
  182.     end;
  183.  
  184.     if cmd = 'ASN1' then
  185.     begin
  186.       ListAppend(oid^.ASNIds, line);
  187.     end;
  188.  
  189.     if cmd = 'DESC' then
  190.     begin
  191.       oid^.Description := oid^.Description + line + #13#10;
  192.     end;
  193.   end;
  194.  
  195.   (* Sort sub IDs *)
  196.   ListBubbleSortSubIds(oid);
  197.  
  198.   (* Remove last CRLF *)
  199.   oid^.Description := Copy(oid^.Description, 1, Length(oid^.Description)-Length(#13#10));
  200.  
  201.   (* Check if something is not correct *)
  202.   if (version <> WANT_VERS) or (oid^.FileId = '') then
  203.   begin
  204.     (* Invalidate everything *)
  205.     ClearOidDef(oid);
  206.   end;
  207.  
  208.   Close(f);
  209. end;
  210.  
  211. function FileIdPart(s: string): string;
  212. begin
  213.   FileIdPart := Copy(s,1,8);
  214. end;
  215.  
  216. function DotNotationPart(s: string): string;
  217. begin
  218.   Delete(s,1,8);
  219.   DotNotationPart := s;
  220. end;
  221.  
  222. end.
  223.