Subversion Repositories oidplus

Rev

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