-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathparseuid.py
230 lines (209 loc) · 5.26 KB
/
parseuid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
Generate a C++ table for use as a UID dictionary.
"""
import sys
if len(sys.argv) != 2:
sys.stderr.write(
"""usage: python parseuid.py nemauids.txt > vtkDICOMUtilitiesUIDTable.cxx""")
sys.exit(1)
f = open(sys.argv[1], 'r')
lines = f.readlines()
f.close()
root = [0, 0, 0, 0, "", ""]
part = ""
l = 0
maxlevel = 0
while l < len(lines):
text = lines[l]
if text[0] == '#':
l += 1
continue
uid = lines[l].strip()
name = lines[l+1].strip()
utype = lines[l+2].strip()
if l+3 < len(lines):
cite = lines[l+3].strip()
else:
cite = "1.2.840.10008."
if uid[0:14] != "1.2.840.10008.":
sys.stderr.write("oops at line %d\n" % (l,))
l += 1
continue
if cite[0:14] != "1.2.840.10008." and cite[0] != '#':
l += 4
elif uid[0:18] == "1.2.840.10008.6.1.":
cite = name
name = utype
utype = "Context Group"
l += 3
else:
cite = utype
utype = "Frame of Reference"
l += 3
uid = uid[14:]
ret = " (Retired)"
if len(name) > 10 and name[-10:] == ret:
name = name[0:-10]
else:
ret = ""
k = name.find(": Default")
if k > 0:
name = name[0:k]
parts = [int(x) for x in uid.split('.')]
node = root
for level in range(len(parts)):
idx = parts[level]
if node[0] == 0:
node[1] = 1
node[2] = idx
node[0] = [ [0, 0, 0, 0, "", ""] ]
if node[2] > idx:
for i in range(idx, node[2]):
node[0].insert(0, [0, 0, 0, 0, "", ""])
node[1] += node[2] - idx
node[2] = idx
if node[2] + node[1] - 1 < idx:
for i in range(node[2] + node[1] - 1, idx):
node[0].append([0, 0, 0, 0, "", ""])
node[1] += idx - (node[2] + node[1] - 1)
node = node[0][idx - node[2]]
if level == len(parts)-1:
if cite[0:4] == "CID ":
node[3] = int(cite[4:])
else:
node[3] = 0
node[4] = " // 1.2.840.10008." + uid + ret
node[5] = name
if level > maxlevel:
maxlevel = level
f = sys.stdout
f.write("/*=========================================================================\n")
f.write("This is an automatically generated file. Include errata for any changes.\n")
f.write("=========================================================================*/\n")
f.write("\n#include \"vtkDICOMUtilitiesUIDTable.h\"\n")
f.write("\nnamespace {\n\n")
f.write("struct UIDTableEntry\n")
f.write("{\n")
f.write(" unsigned short Next;\n")
f.write(" unsigned short Size;\n")
f.write(" unsigned short First;\n")
f.write(" unsigned short CID;\n")
f.write(" const char *Name;\n")
f.write("};\n")
def printnode(node):
if node[0]:
child = node[0][0][6]
if node[3] or node[5]:
f.write("{ %d, %d, %d, %d,%s\n \"%s\" },\n" % tuple([child]+node[1:6]))
else:
f.write("{ %d, %d, %d, 0, nullptr },\n" % tuple([child]+node[1:3]))
else:
if node[3] or node[5]:
f.write("{ 0, %d, %d, %d,%s\n \"%s\" },\n" % tuple(node[1:6]))
else:
f.write("{ 0, %d, %d, 0, nullptr },\n" % tuple(node[1:3]))
def recursetrie(node, counter):
if node[0]:
for child in node[0]:
if counter:
counter[0] += 1
child.append(counter[0])
else:
printnode(child)
for child in node[0]:
recursetrie(child, counter)
f.write("\nconst UIDTableEntry UIDTable[] = {\n")
recursetrie(root, [0])
printnode(root)
recursetrie(root, None)
f.write("};\n")
getterfunc = \
"""
const UIDTableEntry *GetUIDTableEntry(const char *uid)
{
if (uid == nullptr)
{
return nullptr;
}
const char *prefix = "1.2.840.10008.";
while (*prefix != '\\0' && *uid != '\\0' && *prefix == *uid)
{
prefix++;
uid++;
}
if (*prefix != '\\0')
{
return nullptr;
}
const UIDTableEntry *table = UIDTable;
while (*uid != '\\0')
{
int i = -1;
if (*uid >= '0' && *uid <= '9')
{
if (uid[0] != '0' || uid[1] != '0')
{
i = 0;
do
{
i *= 10;
i += (*uid - '0');
uid++;
}
while (*uid >= '0' && *uid <= '9' && i < 214748364);
}
}
if (*uid == '.')
{
uid++;
if (*uid == '\\0')
{
table = nullptr;
break;
}
}
else if (*uid != '\\0')
{
table = nullptr;
break;
}
i -= table->First;
if (i < 0 || i >= static_cast<int>(table->Size))
{
table = nullptr;
break;
}
table = &UIDTable[table->Next + i];
}
return table;
}
"""
f.write(getterfunc)
f.write("} // anonymous namespace\n")
exportedcode = \
"""
const char *vtkDICOMUtilities::GetUIDName(const char *uid)
{
const char *result = "";
const UIDTableEntry *table = GetUIDTableEntry(uid);
if (table)
{
if (table->Name)
{
result = table->Name;
}
}
return result;
}
unsigned short vtkDICOMUtilities::GetCIDFromUID(const char *uid)
{
unsigned short result = 0;
const UIDTableEntry *table = GetUIDTableEntry(uid);
if (table)
{
result = table->CID;
}
return result;
}
"""
f.write(exportedcode)