-
Notifications
You must be signed in to change notification settings - Fork 64
/
FormatSPF.cs
1486 lines (1304 loc) · 48.5 KB
/
FormatSPF.cs
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Name: FormatSPF.cs
// Description: Reads/writes ISO-10303-21 STEP Physical File (SPF).
// Author: Tim Chipman
// Origination: This is based on prior work of Constructivity donated to BuildingSmart at no charge.
// Copyright: (c) 2010 BuildingSmart International Ltd., (c) 2006-2010 Constructivity.com LLC.
// Note: This specific file has dual copyright such that both organizations maintain all rights to its use.
// License: http://www.buildingsmart-tech.org/legal
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using IfcDoc.Schema;
namespace IfcDoc.Format.SPF
{
public class FormatSPF : IDisposable
{
string m_url;
Stream m_stream;
Dictionary<string, Type> m_typemap;
Dictionary<FieldInfo, List<FieldInfo>> m_inversemap;
Dictionary<long, SEntity> m_instances;
ParseScope m_parsescope;
IList m_headertags;
/// <summary>
/// Encapsulates a STEP Physical File (ISO-10303-21)
/// </summary>
/// <param name="file">Required file path</param>
/// <param name="types">Required map of string identifiers and types</param>
/// <param name="instances">Optional map of instance identifiers and objects (specified if saving)</param>
public FormatSPF(
string file,
Dictionary<string, Type> typemap,
Dictionary<long, SEntity> instances)
{
m_url = file;
m_stream = new System.IO.FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
m_headertags = new List<object>();
m_typemap = typemap;
m_inversemap = new Dictionary<FieldInfo, List<FieldInfo>>();
m_instances = instances;
if (this.m_instances == null)
{
m_instances = new Dictionary<long, SEntity>();
}
// map fields for quick loading of inverse fields
foreach (Type t in typemap.Values)
{
FieldInfo[] fields = t.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (FieldInfo fTarget in fields)
{
DataLookupAttribute[] attrs = (DataLookupAttribute[])fTarget.GetCustomAttributes(typeof(DataLookupAttribute), false);
if (attrs.Length == 1)
{
Type typeElement;
if (fTarget.FieldType.IsGenericType)
{
// inverse set (most common)
typeElement = fTarget.FieldType.GetGenericArguments()[0];
}
else
{
// inverse scalar (more obscure)
typeElement = fTarget.FieldType;
}
FieldInfo fSource = SEntity.GetFieldByName(typeElement, attrs[0].Name); // dictionary requires reference uniqueness
if (fSource != null)
{
List<FieldInfo> listField = null;
if (!this.m_inversemap.TryGetValue(fSource, out listField))
{
listField = new List<FieldInfo>();
this.m_inversemap.Add(fSource, listField);
}
listField.Add(fTarget);
}
}
}
}
}
public void Dispose()
{
if (this.m_stream != null)
{
this.m_stream.Close();
this.m_stream = null;
}
}
public IList Headers
{
get
{
return m_headertags;
}
}
public Dictionary<string, Type> Types
{
get
{
return this.m_typemap;
}
}
public Dictionary<long, SEntity> Instances
{
get
{
return this.m_instances;
}
}
private string FormatIdentifier
{
get
{
return "ISO-10303-21";
}
}
/// <summary>
/// Reads file either parsing objects or fields
/// </summary>
/// <param name="stream">The stream to read</param>
/// <param name="bFields">If true, populates fields; if false, populates objects</param>
private void ReadFile()
{
m_stream.Position = 0;
System.IO.StreamReader reader = new System.IO.StreamReader(m_stream, Encoding.ASCII);
ParseSection parse = ParseSection.Unknown;
string commandline = ReadNext(reader);
while (commandline != null)
{
switch (parse)
{
case ParseSection.Unknown:
if (commandline.Equals(this.FormatIdentifier))
{
parse = ParseSection.IsoStep;
}
else
{
// invalid format
throw new NotSupportedException("Format is not " + this.FormatIdentifier);
}
break;
case ParseSection.IsoStep:
if (commandline.Equals("HEADER"))
{
parse = ParseSection.Header;
}
else if (commandline.Equals("DATA"))
{
parse = ParseSection.Data;
}
else if (commandline.Equals("END-" + this.FormatIdentifier))
{
parse = ParseSection.Unknown;
}
break;
case ParseSection.Header:
if (commandline.Equals("ENDSEC"))
{
if (this.m_parsescope == ParseScope.Header)
{
// got everything we need from header, so return
return;
}
parse = ParseSection.IsoStep;
}
else if(this.m_parsescope == ParseScope.Header)
{
// process header
object headertag = ParseConstructor(commandline);
if (headertag != null)
{
m_headertags.Add(headertag);
}
}
break;
case ParseSection.Data:
if (commandline.Equals("ENDSEC"))
{
parse = ParseSection.IsoStep;
}
else
{
// process data
ReadCommand(commandline);
}
break;
}
commandline = ReadNext(reader);
}
}
private enum ParseScope
{
None = 0,
Header = 1,
DataInstances = 2,
DataFields = 3,
}
private string ReadNext(StreamReader reader)
{
// reads the next expression
// skips whitespace, goes until ";" (ignores comments and string literals)
StringBuilder sb = new StringBuilder();
ParseCommand parse = ParseCommand.Open;
while (parse != ParseCommand.End)
{
int iChar = reader.Read();
if (iChar == -1)
{
// end of file
return null;
}
char ch = (char)iChar;
bool bAppend = true;
// case of "/**** " -> back in comment mode
if (parse == ParseCommand.CommentLeave && ch != '/')
{
parse = ParseCommand.Comment;
}
if (parse == ParseCommand.Comment)
{
bAppend = false;
}
switch (ch)
{
case ';': // done
if (parse == ParseCommand.Open)
{
return sb.ToString();
}
break;
case '/': // about to enter a command or about to leave a comment
if (parse == ParseCommand.Open)
{
parse = ParseCommand.CommentEnter;
bAppend = false;
}
else if (parse == ParseCommand.CommentLeave)
{
bAppend = false;
parse = ParseCommand.Open;
}
break;
case '*':
if (parse == ParseCommand.CommentEnter)
{
bAppend = false;
parse = ParseCommand.Comment;
}
else if (parse == ParseCommand.Comment)
{
parse = ParseCommand.CommentLeave;
}
break;
case ' ': // empty space
case '\r':
case '\n':
case '\t':
if (parse == ParseCommand.Open)
{
bAppend = false;
}
break;
case '\'':
if (parse == ParseCommand.Open)
{
parse = ParseCommand.String;
}
else if (parse == ParseCommand.String)
{
parse = ParseCommand.Open;
}
break;
}
if (bAppend)
{
sb.Append(ch);
}
}
return null; // end of file!
}
/// <summary>
/// Reads an ISO-STEP command and populates broker according to scope
/// </summary>
/// <param name="command">The processed STEP line to parse</param>
/// <param name="scope">If DataInstances, then reads instances. If DataFields, then reads fields.</param>
private void ReadCommand(string line)
{
if (line[0] != '#')
{
// invalid
throw new FormatException("Bad format: command must start with '#'");
}
int iIdTail = line.IndexOf('=');
if (iIdTail == -1)
{
throw new FormatException("Bad format: object identifier must be followed by '='");
}
string strId = line.Substring(1, iIdTail - 1);
long id;
if (!Int64.TryParse(strId, out id))
{
throw new FormatException("Bad format: object identifier must be 32-bit signed integer");
}
if (id == -1)
return; // buggy file that saved out a deleted element
string strConstructor = line.Substring(iIdTail + 1);
switch(m_parsescope)
{
case ParseScope.DataInstances:
{
Type t = ParseType(strConstructor);
if (t != null)
{
SEntity instance = (SEntity)this.CreateInstance(t);
instance.OID = id;
// set the object in position
this.m_instances.Add(id, instance);
}
else
{
throw new FormatException("Unrecognized type: " + strConstructor); }
}
break;
case ParseScope.DataFields:
{
object instance = this.m_instances[id];
LoadFields(instance, strConstructor);
}
break;
}
}
public virtual void Load()
{
// empty file: just return
if (this.m_stream.Length == 0)
return;
// read header and validate
m_parsescope = ParseScope.Header;
ReadFile();
// read instances
m_parsescope = ParseScope.DataInstances;
ReadFile();
// read fields
m_parsescope = ParseScope.DataFields;
ReadFile();
}
private enum ParseSection
{
Unknown = 0, // waiting for ISO-STEP directive
IsoStep = 1, // ISO-STEP; waiting for header or data
Header = 2, // header; receiving header elements until END_HEADER
Data = 3, // receiving data until end-data
}
public virtual void Save()
{
// reset stream
if (this.m_stream.CanSeek)
{
this.m_stream.SetLength(0);
}
// write file
StreamWriter writer = new StreamWriter(m_stream);
// write ISO file identifier
writer.Write(this.FormatIdentifier);
writer.WriteLine(";");
// HEADERS
writer.WriteLine("HEADER;");
foreach (object tag in m_headertags)
{
string strheader = FormatConstructor(tag);
writer.Write(strheader);
writer.WriteLine(";");
}
writer.WriteLine("ENDSEC;");
writer.WriteLine();
// DATA
writer.WriteLine("DATA;");
// save objects -- pass in typeof(object) to get all live objects (exclude commands and deleted objects)
foreach(SEntity entity in this.m_instances.Values)
{
string strConstructor = FormatConstructor(entity);
string strLine = "#" + entity.OID.ToString() + "= " + strConstructor + ";";
writer.WriteLine(strLine);
}
writer.WriteLine("ENDSEC;");
writer.WriteLine();
writer.WriteLine("END-" + this.FormatIdentifier + ";");
writer.Flush();
m_stream.Flush();
}
public enum ParseCommand
{
Open = 0, // normal parsing
End = 1, // end of
String = 2, // inside a string
Comment = 3, // inside a comment
CommentEnter = 4, // possibly entering a comment (/)
CommentLeave = 5, // possibly leaving a comment (*)
}
public string FormatFields(object o)
{
if (o == null)
return null;
StringBuilder sb = new StringBuilder();
Type t = o.GetType();
IList<FieldInfo> fields = SEntity.GetFieldsOrdered(t);
for (int iField = 0; iField < fields.Count; iField++)
{
if (iField != 0)
{
sb.Append(",");
}
System.Reflection.FieldInfo field = fields[iField];
if (t.GetProperty(field.Name) != null)
{
// special case if field is overridden
sb.Append("*");
}
else if (field.FieldType.IsInterface)
{
// may need to qualify constructor if not ID'd
object val = field.GetValue(o);
if (val is SEntity)
{
SEntity entity = (SEntity)val;
//System.Diagnostics.Debug.Assert(entity.OID != 0);
sb.Append("#" + entity.OID.ToString());
}
else if (val != null)
{
// must be value type
string strValue = FormatConstructor(val);
sb.Append(strValue);
}
else
{
sb.Append("$");
}
}
else
{
object val = field.GetValue(o);
string strValue = FormatValue(val);
sb.Append(strValue);
}
}
return sb.ToString();
}
protected string FormatConstructor(object o)
{
StringBuilder sb = new StringBuilder();
Type t = o.GetType();
string strType = t.Name.ToUpper();
sb.Append(strType);
sb.Append("(");
IList<FieldInfo> fields = SEntity.GetFieldsOrdered(t);
if (strType.Equals("IFCLABEL")) // hack until recompile
{
List<FieldInfo> custom = new List<FieldInfo>();
custom.Add(t.GetField("Value"));
fields = custom;
}
for (int iField = 0; iField < fields.Count; iField++)
{
if (iField != 0)
{
sb.Append(",");
}
System.Reflection.FieldInfo field = fields[iField];
if (t.GetProperty(field.Name) != null)
{
// special case if field is overridden
sb.Append("*");
}
else if (field.FieldType.IsInterface)
{
// may need to qualify constructor if not ID'd
object val = field.GetValue(o);
if (val is SEntity)
{
SEntity ent = (SEntity)val;
sb.Append("#" + ent.OID.ToString());
}
else if (val != null)
{
// value type
string strValue = FormatConstructor(val);
sb.Append(strValue);
}
else
{
sb.Append("$");
}
}
else
{
object val = field.GetValue(o);
string strValue = FormatValue(val);
sb.Append(strValue);
}
}
sb.Append(")");
return sb.ToString();
}
private string FormatValue(object o)
{
if (o == null)
{
return "$";
}
Type t = o.GetType();
if (t == typeof(Boolean))
{
bool bVal = (bool)o;
if (bVal)
{
return ".T.";
}
else
{
return ".F.";
}
}
else if (t == typeof(Double))
{
Double dval = (Double)o;
string strval = dval.ToString(CultureInfo.InvariantCulture);
if (!strval.Contains("."))
{
// must have decimal point per ISO-10303-21
int indexE = strval.IndexOf('E');
if (indexE > 0)
{
strval = strval.Insert(indexE, ".0000000"); // vex always pads seven zeros in such case
}
else
{
strval = strval + ".";
}
}
return strval;
}
else if (t == typeof(String))
{
return "'" + FormatString((string)o) + "'";
}
else if (t == typeof(DateTime))
{
DateTime date = (DateTime)o;
return "'" + FormatDateTime(date) + "'";
}
else if (t.IsEnum)
{
string strval = o.ToString();
if (strval != "_NONE_")
{
return "." + o.ToString() + ".";
}
else
{
return "$";
}
}
else if (t == typeof(Byte[]))
{
char[] s_hexchar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int modulo = 0; // we only support full bytes
Byte[] valuevector = (Byte[])o;
StringBuilder sb = new StringBuilder(valuevector.Length * 2 + 1);
sb.Append("\"");
byte b;
int start;
if (modulo >= 4)
{
// 4-7
sb.Append((modulo - 4).ToString());
b = valuevector[0]; // only lo nibble is valid
sb.Append(s_hexchar[b % 0x10]);
start = 1;
}
else
{
// 0-3
sb.Append(modulo.ToString());
start = 0;
}
for (int i = start; i < valuevector.Length; i++)
{
b = valuevector[i];
sb.Append(s_hexchar[b / 0x10]);
sb.Append(s_hexchar[b % 0x10]);
}
sb.Append("\"");
return sb.ToString();
}
else if (typeof(IList).IsAssignableFrom(t))
{
return FormatList((System.Collections.IList)o);
}
else if (t.IsValueType)
{
return o.ToString();
}
else if (o is SRecord)
{
SRecord record = (SRecord)o;
return "#" + record.OID.ToString();
}
else
{
return FormatConstructor(o);
}
}
private string FormatString(string value)
{
if (value == null)
return "";
// check if encoding is required
bool bRecode = false;
for (int i = 0; i < value.Length; i++)
{
Char ch = value[i];
if ((ch & 0xFF80) != 0 || ch == '\\' || ch == '\'' ||
ch == '\r' || ch == '\n' || ch == '\t')
{
bRecode = true;
break;
}
}
// return original if no recoding is required
if (!bRecode)
{
return value;
}
bool unicode = false; // flag for indicating whether to store as unicode
StringBuilder sb = new StringBuilder();
for (int i = 0; i < value.Length; i++)
{
Char ch = value[i];
// handle unicode
if (ch > 255)
{
// extended encoding
if (!unicode)
{
sb.Append(@"\X2\");
unicode = true;
}
sb.Append(String.Format("{0:X4}", (int)ch));
}
else if(unicode)
{
// end of unicode; terminate
sb.Append(@"\X0\");
unicode = false;
}
// then all other modes
if (ch == '\\')
{
// back-slash escaping
sb.Append(@"\\");
}
else if (ch == '\'')
{
// single-quote escaping
sb.Append(@"''"); // single-quote repeated
}
else if (ch >= 32 && ch < 126)
{
// direct encoding
sb.Append(ch);
}
else if (ch >= 128 + 32 && ch <= 128 + 126)
{
// shifted encoding
Char chMod = (Char)(ch & 0x007F);
sb.Append(@"\S\");
sb.Append(chMod);
}
else if (ch < 255)
{
// other character
sb.Append(@"\X\");
sb.Append(String.Format("{0:X2}", (int)ch));
}
}
if (unicode)
{
// end of unicode; terminate
sb.Append(@"\X0\");
unicode = false;
}
return sb.ToString();
}
private string FormatDateTime(DateTime value)
{
// '2006-07-28T15:17:15'
return value.ToString("yyyy-MM-ddTHH:mm:ss");
}
private string FormatList(System.Collections.IList list)
{
Type typeElement = list.GetType().GetGenericArguments()[0];
StringBuilder sb = new StringBuilder();
sb.Append("(");
for (int i = 0; i < list.Count; i++)
{
if (i != 0)
{
sb.Append(",");
}
object o = list[i];
// if value type for interface, must pre-qualify with constructor
if (typeElement.IsInterface && o != null && o.GetType().IsValueType)
{
string strConstructor = FormatConstructor(o);
sb.Append(strConstructor);
}
else
{
string strElement = FormatValue(o);
sb.Append(strElement);
}
}
sb.Append(")");
return sb.ToString();
}
private object ParseValue(Type type, string strval)
{
if (strval == "$" || strval == "*")
{
return null;
}
if (type.IsGenericType && type.IsValueType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
// special case for Nullable types
type = type.GetGenericArguments()[0];
}
// value types
Type typewrap = null;
while (type.IsValueType && !type.IsPrimitive)
{
FieldInfo fieldValue = type.GetField("Value");
if (fieldValue != null)
{
if (typewrap == null)
{
typewrap = type;
}
type = fieldValue.FieldType;
}
else
{
break;
}
}
object value = null;
if (typeof(Int32) == type)
{
value = Int32.Parse(strval, CultureInfo.InvariantCulture);
}
else if (typeof(Int64) == type)
{
value = Int64.Parse(strval, CultureInfo.InvariantCulture);
}
else if (typeof(Single) == type)
{
value = Single.Parse(strval, CultureInfo.InvariantCulture);
}
else if (typeof(Double) == type)
{
value = Double.Parse(strval, CultureInfo.InvariantCulture);
}
else if (typeof(Boolean) == type)
{
if (strval == ".T.")
{
value = true;
}
else
{
value = false;
}
}
else if (typeof(String) == type)
{
// backward compatibility for Visual Express aggregation change
if (strval[0] == '\'')
{
strval = strval.Substring(1, strval.Length - 2); // remove quotes
//strval = strval.Trim('\''); // buggy! if starts or ends with escaped quotes
value = ParseString(strval);
}
else
{
// could be int64
value = strval;
}
}
else if (typeof(DateTime) == type)
{
strval = strval.Trim('\'');
value = DateTime.Parse(strval);
}
else if (type != null && type.IsEnum)
{
strval = strval.Trim('.');
System.Reflection.FieldInfo enumfield = type.GetField(strval);
if (enumfield != null)
{
value = enumfield.GetValue(null);
}
}
else if (typeof(Byte[]) == type)
{
// assume surrounded by quotes
int len = (strval.Length - 3) / 2; // subtract surrounding quotes and modulus character
Byte[] valuevector = new byte[len];
int modulo = 0;
int offset;
if (strval.Length % 2 == 0)
{
modulo = Convert.ToInt32(strval[1]) + 4;
offset = 1;
char ch = strval[2];
valuevector[0] = (ch >= 'A' ? (byte)(ch - 'A' + 10) : (byte)ch);
}
else
{
modulo = Convert.ToInt32((strval[1] - '0')); // [0] is quote; [1] is modulo
offset = 0;
}
for (int i = offset; i < len; i++)
{
char hi = strval[i * 2 + 2 - offset];
char lo = strval[i * 2 + 3 - offset];
byte val = (byte)(
((hi >= 'A' ? +(int)(hi - 'A' + 10) : (int)(hi - '0')) << 4) +
((lo >= 'A' ? +(int)(lo - 'A' + 10) : (int)(lo - '0'))));
valuevector[i] = val;
}
value = valuevector;
}
else if (typeof(IList).IsAssignableFrom(type))
{
value = ParseList(type, strval);
}
else
{
value = ParseObject(strval);
}
if (typewrap != null)
{
object wrap = Activator.CreateInstance(typewrap);
FieldInfo fieldValue = typewrap.GetField("Value");
fieldValue.SetValue(wrap, value);
value = wrap;
}
return value;
}
private object ParseObject(string strval)
{
object value;
if (strval[0] == '#')
{
// reference to another object
int iIndex = Int32.Parse(strval.Substring(1));
if (!this.m_instances.ContainsKey(iIndex))
{
return null; // hack around buggy visual express files (such as IfcDateTimeResource from IFC2x4 Alpha)
}
value = this.m_instances[iIndex];
}
else
{
// inline
value = ParseConstructor(strval);
}