Subversion Repositories oidplus

Rev

Rev 735 | Rev 740 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
733 daniel-mar 1
unit OIDFILE;
2
 
3
(************************************************)
4
(* OIDFILE.PAS                                  *)
5
(* Author:   Daniel Marschall                   *)
734 daniel-mar 6
(* Revision: 2022-02-14                         *)
733 daniel-mar 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;
734 daniel-mar 24
    SubIds: PStringList; (* first 8 chars are FileId, followed by Dot-Notation *)
25
    Parent: string; (* First 8 chars are FileId, followed by Dot-Notation *)
733 daniel-mar 26
  end;
27
 
735 daniel-mar 28
procedure CreateOidDef(var oid: POid);
733 daniel-mar 29
procedure FreeOidDef(oid: POid);
734 daniel-mar 30
procedure ClearOidDef(oid: POid);
733 daniel-mar 31
procedure WriteOidFile(filename: string; oid: POid);
32
procedure ReadOidFile(filename: string; oid: POid);
33
 
735 daniel-mar 34
(* For the fields "SubIds" and "Parent" *)
35
function FileIdPart(s: string): string;
36
function DotNotationPart(s: string): string;
37
 
733 daniel-mar 38
implementation
39
 
40
uses
739 daniel-mar 41
  VtsFuncs, OidUtils;
733 daniel-mar 42
 
43
const
44
  WANT_VERS = '2022';
45
 
735 daniel-mar 46
procedure CreateOidDef(var oid: POid);
734 daniel-mar 47
begin
735 daniel-mar 48
  GetMem(oid, SizeOf(TOID));
734 daniel-mar 49
  oid^.FileId := '';
50
  oid^.DotNotation := '';
51
  oid^.Description := '';
52
  oid^.Parent := '';
735 daniel-mar 53
  CreateList(oid^.ASNIds);
54
  CreateList(oid^.SubIds);
734 daniel-mar 55
end;
56
 
733 daniel-mar 57
procedure FreeOidDef(oid: POid);
58
begin
59
  FreeList(oid^.ASNIds);
60
  FreeList(oid^.SubIds);
735 daniel-mar 61
  FreeMem(oid, SizeOf(TOID));
733 daniel-mar 62
end;
63
 
734 daniel-mar 64
procedure ClearOidDef(oid: POid);
65
begin
66
  FreeOidDef(oid);
735 daniel-mar 67
  CreateOidDef(oid);
734 daniel-mar 68
end;
69
 
739 daniel-mar 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
 
733 daniel-mar 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
 
739 daniel-mar 110
  (* Sort sub IDs *)
111
  BubbleSortSubIds(oid);
733 daniel-mar 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
735 daniel-mar 127
    CreateList(lines);
733 daniel-mar 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
734 daniel-mar 146
  ClearOidDef(oid);
733 daniel-mar 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;
739 daniel-mar 189
 
190
  (* Sort sub IDs *)
191
  BubbleSortSubIds(oid);
733 daniel-mar 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 *)
734 daniel-mar 200
    ClearOidDef(oid);
733 daniel-mar 201
  end;
202
 
203
  Close(f);
204
end;
205
 
735 daniel-mar 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
 
733 daniel-mar 217
end.