Subversion Repositories oidplus

Rev

Rev 739 | Rev 743 | 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
 
740 daniel-mar 70
procedure ListBubbleSortSubIds(oid: POid);
739 daniel-mar 71
var
72
  n, i: integer;
73
  a, b: string;
740 daniel-mar 74
  swapped: boolean;
739 daniel-mar 75
begin
76
  n := ListCount(oid^.SubIds);
77
  while n>1 do
78
  begin
79
    i := 0;
740 daniel-mar 80
    swapped := false;
739 daniel-mar 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);
740 daniel-mar 88
        swapped := true;
739 daniel-mar 89
      end;
90
      Inc(i);
91
    end;
740 daniel-mar 92
    if not swapped then break;
739 daniel-mar 93
    Dec(n);
94
  end;
95
end;
96
 
733 daniel-mar 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
 
740 daniel-mar 108
  WriteLn(f, 'VERS' + WANT_VERS);
733 daniel-mar 109
 
740 daniel-mar 110
  WriteLn(f, 'SELF' + oid^.FileId + oid^.DotNotation);
733 daniel-mar 111
 
740 daniel-mar 112
  WriteLn(f, 'SUPR' + oid^.Parent);
733 daniel-mar 113
 
739 daniel-mar 114
  (* Sort sub IDs *)
740 daniel-mar 115
  ListBubbleSortSubIds(oid);
116
 
733 daniel-mar 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
735 daniel-mar 132
    CreateList(lines);
733 daniel-mar 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
734 daniel-mar 151
  ClearOidDef(oid);
733 daniel-mar 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;
740 daniel-mar 194
 
739 daniel-mar 195
  (* Sort sub IDs *)
740 daniel-mar 196
  ListBubbleSortSubIds(oid);
733 daniel-mar 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 *)
734 daniel-mar 205
    ClearOidDef(oid);
733 daniel-mar 206
  end;
207
 
208
  Close(f);
209
end;
210
 
735 daniel-mar 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
 
733 daniel-mar 222
end.