Subversion Repositories oidplus

Rev

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