Subversion Repositories oidplus

Rev

Rev 745 | Rev 748 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 745 Rev 747
1
unit OIDFILE;
1
unit OIDFILE;
2
 
2
 
3
(************************************************)
3
(************************************************)
4
(* OIDFILE.PAS                                  *)
4
(* OIDFILE.PAS                                  *)
5
(* Author:   Daniel Marschall                   *)
5
(* Author:   Daniel Marschall                   *)
6
(* Revision: 2022-02-15                         *)
6
(* Revision: 2022-02-15                         *)
7
(* License:  Apache 2.0                         *)
7
(* License:  Apache 2.0                         *)
8
(* This file contains:                          *)
8
(* This file contains:                          *)
9
(* - Functions to handle an OID ASCII format    *)
9
(* - Functions to handle an OID ASCII format    *)
10
(************************************************)
10
(************************************************)
11
 
11
 
12
interface
12
interface
13
 
13
 
14
uses
14
uses
15
  StrList;
15
  StrList;
16
 
16
 
17
type
17
type
18
  POID = ^TOID;
18
  POID = ^TOID;
19
  TOID = record
19
  TOID = record
20
    FileId: string;
20
    FileId: string;
21
    DotNotation: string;
21
    DotNotation: string;
22
    ASNIds: PStringList;
22
    ASNIds: PStringList;
23
    Description: string;
23
    Description: string;
24
    SubIds: PStringList; (* first 8 chars are FileId, followed by DotNotation *)
24
    SubIds: PStringList; (* first 8 chars are FileId, followed by DotNotation *)
25
    ParentFileId: string;
25
    ParentFileId: string;
26
    ParentDotNotation: string;
26
    ParentDotNotation: string;
27
  end;
27
  end;
28
 
28
 
29
procedure CreateOidDef(var oid: POid);
29
procedure CreateOidDef(var oid: POid);
30
procedure FreeOidDef(oid: POid);
30
procedure FreeOidDef(oid: POid);
31
procedure ClearOidDef(oid: POid);
31
procedure ClearOidDef(oid: POid);
32
function WriteOidFile(filename: string; oid: POid): boolean;
32
function WriteOidFile(filename: string; oid: POid): boolean;
33
function ReadOidFile(filename: string; oid: POid): boolean;
33
function ReadOidFile(filename: string; oid: POid): boolean;
34
 
34
 
35
(* For the strings in the list "SubIds": *)
35
(* For the strings in the list "SubIds": *)
36
function FileIdPart(s: string): string;
36
function FileIdPart(s: string): string;
37
function DotNotationPart(s: string): string;
37
function DotNotationPart(s: string): string;
38
 
38
 
39
implementation
39
implementation
40
 
40
 
41
uses
41
uses
42
  VtsFuncs, OidUtils;
42
  VtsFuncs, OidUtils;
43
 
43
 
44
const
44
const
45
  WANT_VERS = '2022';
45
  WANT_VERS = '2022';
46
 
46
 
47
procedure CreateOidDef(var oid: POid);
47
procedure CreateOidDef(var oid: POid);
48
begin
48
begin
49
  GetMem(oid, SizeOf(TOID));
49
  GetMem(oid, SizeOf(TOID));
50
  oid^.FileId := '';
50
  oid^.FileId := '';
51
  oid^.DotNotation := '';
51
  oid^.DotNotation := '';
52
  oid^.Description := '';
52
  oid^.Description := '';
53
  oid^.ParentFileId := '';
53
  oid^.ParentFileId := '';
54
  oid^.ParentDotNotation := '';
54
  oid^.ParentDotNotation := '';
55
  CreateList(oid^.ASNIds);
55
  CreateList(oid^.ASNIds);
56
  CreateList(oid^.SubIds);
56
  CreateList(oid^.SubIds);
57
end;
57
end;
58
 
58
 
59
procedure FreeOidDef(oid: POid);
59
procedure FreeOidDef(oid: POid);
60
begin
60
begin
61
  FreeList(oid^.ASNIds);
61
  FreeList(oid^.ASNIds);
62
  FreeList(oid^.SubIds);
62
  FreeList(oid^.SubIds);
63
  FreeMem(oid, SizeOf(TOID));
63
  FreeMem(oid, SizeOf(TOID));
64
end;
64
end;
65
 
65
 
66
procedure ClearOidDef(oid: POid);
66
procedure ClearOidDef(oid: POid);
67
begin
67
begin
68
  oid^.FileId := '';
68
  oid^.FileId := '';
69
  oid^.DotNotation := '';
69
  oid^.DotNotation := '';
70
  oid^.Description := '';
70
  oid^.Description := '';
71
  oid^.ParentFileId := '';
71
  oid^.ParentFileId := '';
72
  oid^.ParentDotNotation := '';
72
  oid^.ParentDotNotation := '';
73
  ListClear(oid^.ASNIds);
73
  ListClear(oid^.ASNIds);
74
  ListClear(oid^.SubIds);
74
  ListClear(oid^.SubIds);
75
end;
75
end;
76
 
76
 
77
procedure ListBubbleSortSubIds(oid: POid);
77
procedure ListBubbleSortSubIds(oid: POid);
78
var
78
var
79
  n, i: integer;
79
  n, i: integer;
80
  a, b: string;
80
  a, b: string;
81
  swapped: boolean;
81
  swapped: boolean;
82
begin
82
begin
83
  n := ListCount(oid^.SubIds);
83
  n := ListCount(oid^.SubIds);
84
  while n>1 do
84
  while n>1 do
85
  begin
85
  begin
86
    i := 0;
86
    i := 0;
87
    swapped := false;
87
    swapped := false;
88
    while i<n-1 do
88
    while i<n-1 do
89
    begin
89
    begin
90
      a := DotNotationPart(ListGetElement(oid^.SubIds, i));
90
      a := DotNotationPart(ListGetElement(oid^.SubIds, i));
91
      b := DotNotationPart(ListGetElement(oid^.SubIds, i+1));
91
      b := DotNotationPart(ListGetElement(oid^.SubIds, i+1));
92
      if CompareOID(a, b) > 0 then
92
      if CompareOID(a, b) > 0 then
93
      begin
93
      begin
94
        ListSwapElement(oid^.SubIds, i, i+1);
94
        ListSwapElement(oid^.SubIds, i, i+1);
95
        swapped := true;
95
        swapped := true;
96
      end;
96
      end;
97
      Inc(i);
97
      Inc(i);
98
    end;
98
    end;
99
    if not swapped then break;
99
    if not swapped then break;
100
    Dec(n);
100
    Dec(n);
101
  end;
101
  end;
102
end;
102
end;
103
 
103
 
104
function WriteOidFile(filename: string; oid: POid): boolean;
104
function WriteOidFile(filename: string; oid: POid): boolean;
105
var
105
var
106
  f: Text;
106
  f: Text;
107
  i: integer;
107
  i: integer;
108
  lines: PStringList;
108
  lines: PStringList;
109
  sTmp: string;
109
  sTmp: string;
110
  desc: string;
110
  desc: string;
111
begin
111
begin
112
  Assign(f, filename);
112
  Assign(f, filename);
113
 
113
 
114
  {$I-}
114
  {$I-}
115
  Rewrite(f);
115
  Rewrite(f);
116
  {$I+}
116
  {$I+}
117
  if IoResult <> 0 then
117
  if IoResult <> 0 then
118
  begin
118
  begin
119
    WriteOidFile := false;
119
    WriteOidFile := false;
120
    (* Must not call Close(f) if file was never opened *)
120
    (* Must not call Close(f) if file was never opened *)
121
    Exit;
121
    Exit;
122
  end;
122
  end;
123
 
123
 
124
  WriteLn(f, 'VERS' + WANT_VERS);
124
  WriteLn(f, 'VERS' + WANT_VERS);
125
 
125
 
126
  WriteLn(f, 'SELF' + oid^.FileId + oid^.DotNotation);
126
  WriteLn(f, 'SELF' + oid^.FileId + oid^.DotNotation);
127
 
127
 
128
  WriteLn(f, 'SUPR' + oid^.ParentFileId + oid^.ParentDotNotation);
128
  WriteLn(f, 'SUPR' + oid^.ParentFileId + oid^.ParentDotNotation);
129
 
129
 
130
  (* Sort sub IDs *)
130
  (* Sort sub IDs *)
131
  ListBubbleSortSubIds(oid);
131
  ListBubbleSortSubIds(oid);
132
 
132
 
133
  for i := 0 to ListCount(oid^.SubIds)-1 do
133
  for i := 0 to ListCount(oid^.SubIds)-1 do
134
  begin
134
  begin
135
    sTmp := ListGetElement(oid^.SubIds, i);
135
    sTmp := ListGetElement(oid^.SubIds, i);
136
    WriteLn(f, 'CHLD' + sTmp);
136
    WriteLn(f, 'CHLD' + sTmp);
137
  end;
137
  end;
138
 
138
 
139
  for i := 0 to ListCount(oid^.AsnIds)-1 do
139
  for i := 0 to ListCount(oid^.AsnIds)-1 do
140
  begin
140
  begin
141
    sTmp := ListGetElement(oid^.AsnIds, i);
141
    sTmp := ListGetElement(oid^.AsnIds, i);
142
    WriteLn(f, 'ASN1' + sTmp);
142
    WriteLn(f, 'ASN1' + sTmp);
143
  end;
143
  end;
144
 
144
 
145
  desc := Trim(oid^.Description);
145
  desc := Trim(oid^.Description);
146
  if desc <> '' then
146
  if desc <> '' then
147
  begin
147
  begin
148
    CreateList(lines);
148
    CreateList(lines);
149
    SplitStrToList(desc, lines, #13#10);
149
    SplitStrToList(desc, lines, #13#10);
150
    for i := 0 to ListCount(lines)-1 do
150
    for i := 0 to ListCount(lines)-1 do
151
    begin
151
    begin
152
      sTmp := ListGetElement(lines, i);
152
      sTmp := ListGetElement(lines, i);
153
      WriteLn(f, 'DESC' + sTmp);
153
      WriteLn(f, 'DESC' + sTmp);
154
    end;
154
    end;
155
    FreeList(lines);
155
    FreeList(lines);
156
  end;
156
  end;
157
 
157
 
158
  Close(f);
158
  Close(f);
159
 
159
 
160
  WriteOidFile := true;
160
  WriteOidFile := true;
161
end;
161
end;
162
 
162
 
163
function ReadOidFile(filename: string; oid: POid): boolean;
163
function ReadOidFile(filename: string; oid: POid): boolean;
164
var
164
var
165
  f: Text;
165
  f: Text;
166
  line, cmd: string;
166
  line, cmd: string;
167
  version: string;
167
  version: string;
168
begin
168
begin
169
  ClearOidDef(oid);
169
  ClearOidDef(oid);
170
  version := '';
170
  version := '';
171
 
171
 
172
  Assign(f, filename);
172
  Assign(f, filename);
173
  {$I-}
173
  {$I-}
174
  Reset(f);
174
  Reset(f);
175
  {$I+}
175
  {$I+}
176
  if IoResult <> 0 then
176
  if IoResult <> 0 then
177
  begin
177
  begin
178
    ReadOidFile := false;
178
    ReadOidFile := false;
179
    (* Must not call Close(f) if file was never opened *)
179
    (* Must not call Close(f) if file was never opened *)
180
    Exit;
180
    Exit;
181
  end;
181
  end;
182
 
182
 
183
  while not EOF(f) do
183
  while not EOF(f) do
184
  begin
184
  begin
185
    ReadLn(f, line);
185
    ReadLn(f, line);
186
    cmd := Copy(line,1,4);
186
    cmd := Copy(line,1,4);
187
    Delete(line,1,4);
187
    Delete(line,1,4);
188
 
188
 
189
    if cmd = 'VERS' then
189
    if cmd = 'VERS' then
190
    begin
190
    begin
191
      version := line;
191
      version := line;
192
    end;
192
    end;
193
 
193
 
194
    if cmd = 'SELF' then
194
    if cmd = 'SELF' then
195
    begin
195
    begin
196
      oid^.FileId := Copy(line,1,8);
196
      oid^.FileId := Copy(line,1,8);
197
      Delete(line,1,8);
197
      Delete(line,1,8);
198
      oid^.DotNotation := line;
198
      oid^.DotNotation := line;
199
    end;
199
    end;
200
 
200
 
201
    if cmd = 'SUPR' then
201
    if cmd = 'SUPR' then
202
    begin
202
    begin
203
      oid^.ParentFileId := FileIdPart(line);
203
      oid^.ParentFileId := FileIdPart(line);
204
      oid^.ParentDotNotation := DotNotationPart(line);
204
      oid^.ParentDotNotation := DotNotationPart(line);
205
    end;
205
    end;
206
 
206
 
207
    if cmd = 'CHLD' then
207
    if cmd = 'CHLD' then
208
    begin
208
    begin
209
      ListAppend(oid^.SubIds, line);
209
      ListAppend(oid^.SubIds, line);
210
    end;
210
    end;
211
 
211
 
212
    if cmd = 'ASN1' then
212
    if cmd = 'ASN1' then
213
    begin
213
    begin
214
      ListAppend(oid^.ASNIds, line);
214
      ListAppend(oid^.ASNIds, line);
215
    end;
215
    end;
216
 
216
 
217
    if cmd = 'DESC' then
217
    if cmd = 'DESC' then
218
    begin
218
    begin
219
      oid^.Description := oid^.Description + line + #13#10;
219
      oid^.Description := oid^.Description + line + #13#10;
220
    end;
220
    end;
221
  end;
221
  end;
222
 
222
 
223
  (* Sort sub IDs *)
223
  (* Sort sub IDs *)
224
  ListBubbleSortSubIds(oid);
224
  ListBubbleSortSubIds(oid);
225
 
225
 
226
  (* Remove last CRLF *)
226
  (* Remove last CRLF *)
227
  oid^.Description := Copy(oid^.Description, 1, Length(oid^.Description)-Length(#13#10));
227
  oid^.Description := Copy(oid^.Description, 1, Length(oid^.Description)-Length(#13#10));
228
 
228
 
229
  (* Check if something is not correct *)
229
  (* Check if everything is correct *)
230
  ReadOidFile := (version = WANT_VERS) and (oid^.FileId <> '');
230
  ReadOidFile := (version = WANT_VERS) and (oid^.FileId <> '');
231
 
231
 
232
  Close(f);
232
  Close(f);
233
end;
233
end;
234
 
234
 
235
function FileIdPart(s: string): string;
235
function FileIdPart(s: string): string;
236
begin
236
begin
237
  FileIdPart := Copy(s,1,8);
237
  FileIdPart := Copy(s,1,8);
238
end;
238
end;
239
 
239
 
240
function DotNotationPart(s: string): string;
240
function DotNotationPart(s: string): string;
241
begin
241
begin
242
  Delete(s,1,8);
242
  Delete(s,1,8);
243
  DotNotationPart := s;
243
  DotNotationPart := s;
244
end;
244
end;
245
 
245
 
246
end.
246
end.
247
 
247