ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/gr2lib/trunk/gr2lib/core/coreclasses/header/granny_header.cs
Revision: 106
Committed: Sat Jul 17 14:52:37 2010 UTC (13 years, 2 months ago) by william
File size: 6144 byte(s)
Log Message:
implement Granny2ExceptionWriter

File Contents

# User Rev Content
1 william 97 //#define HAVE_GRN_REFERENCE // when defined enabled GRN_REFERNCE in Header, else it is disabled
2     using System;
3 william 94 using System.Collections.Generic;
4     using System.Linq;
5     using System.Text;
6     using gr2lib.core.interfaces;
7     using gr2lib.core.helpers;
8     using System.Diagnostics;
9 william 106 using gr2lib.core.exceptions;
10 william 94
11     namespace gr2lib.core.coreclasses.header
12     {
13     public class granny_header : igranny_header
14     {
15     private const int GrannyGRNExtraTagCount = 4;
16     private const int GrannyGRNReservedUnusedCount = 3;
17    
18     public granny_header()
19     {
20     this.ExtraTags = new int[GrannyGRNExtraTagCount];
21     this.ReservedUnused = new int[GrannyGRNReservedUnusedCount];
22    
23     this.Version = 0;
24     this.TotalSize = 0;
25     this.CRC = 0;
26     this.SectionArrayOffset = 0;
27     this.SectionArrayCount = 0;
28 william 97 #if HAVE_GRN_REFERENCE
29 william 94 this.RootObjectTypeDefinition = new granny_reference();
30     this.RootObject = new granny_reference();
31 william 97 #endif
32 william 94 this.TypeTag = 0;
33     this.StringDatabaseCRC = 0;
34    
35     for (int i = 0; i < GrannyGRNExtraTagCount; i++) { this.ExtraTags[i] = 0; }
36     for (int i = 0; i < GrannyGRNReservedUnusedCount; i++) { this.ReservedUnused[i] = 0; }
37     }
38    
39     internal static granny_header ReadFromMemory(IntPtr pointer)
40     {
41     try
42     {
43     if (pointer == IntPtr.Zero) return null;
44     gr2lib.core.coretypes.native.granny_grn_file_header native = Helpers.ReadFromMemory<gr2lib.core.coretypes.native.granny_grn_file_header>(pointer);
45     granny_header managed = ReadFromNative(native);
46     managed.NativePointer = pointer;
47     return managed;
48     }
49 william 106 catch (Exception ex)
50 william 94 {
51     StackTrace st = new StackTrace(true);
52 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
53 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
54 william 102 #endif
55 william 94 return default(granny_header);
56     }
57     }
58     internal static granny_header ReadFromNative(gr2lib.core.coretypes.native.granny_grn_file_header native)
59     {
60     try
61     {
62     granny_header managed = new granny_header();
63    
64     managed.Version = native.Version;
65     managed.TotalSize = native.TotalSize;
66     managed.CRC = native.CRC;
67     managed.SectionArrayOffset = native.SectionArrayOffset;
68     managed.SectionArrayCount = native.SectionArrayCount;
69 william 97
70 william 94 // grn_reference
71    
72 william 97 #if HAVE_GRN_REFERENCE
73 william 94 if (native.RootObjectTypeDefinition != IntPtr.Zero)
74     {
75     //managed.RootObjectTypeDefinition = granny_reference.ReadFromMemory(native.RootObjectTypeDefinition);
76     managed.RootObjectTypeDefinition = new granny_reference();
77     }
78     if (native.RootObject != IntPtr.Zero)
79     {
80     //managed.RootObject = granny_reference.ReadFromMemory(native.RootObject);
81     managed.RootObject = new granny_reference();
82     }
83 william 97 #endif
84 william 94
85     //managed.TypeTag = native.TypeTag;
86     managed.ExtraTags = native.ExtraTags;
87     managed.StringDatabaseCRC = native.StringDatabaseCRC;
88     managed.ReservedUnused = native.ReservedUnused;
89     //process header
90     return managed;
91     }
92 william 106 catch (Exception ex)
93 william 94 {
94     StackTrace st = new StackTrace(true);
95 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
96 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
97 william 102 #endif
98 william 94 return default(granny_header);
99     }
100     }
101    
102     #region INativePointer Members
103     private IntPtr _NativePointer;
104     public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
105     #endregion
106    
107     #region igranny_header members
108    
109     private Int32 _Version;
110     private Int32 _TotalSize;
111     private Int32 _CRC;
112     private Int32 _SectionArrayOffset;
113     private Int32 _SectionArrayCount;
114 william 97 #if HAVE_GRN_REFERENCE
115 william 94 private granny_reference _RootObjectTypeDefinition;
116     private granny_reference _RootObject;
117 william 97 #endif
118 william 94 private Int32 _TypeTag;
119     private Int32[] _ExtraTags; //4
120     private Int32 _StringDatabaseCRC;
121     private Int32[] _ReservedUnused; //3
122    
123    
124     public Int32 Version { get { return _Version; } set { _Version = value; } }
125     public Int32 TotalSize { get { return _TotalSize; } set { _TotalSize = value; } }
126     public Int32 CRC { get { return _CRC; } set { _CRC = value; } }
127     public Int32 SectionArrayOffset { get { return _SectionArrayOffset; } set { _SectionArrayOffset = value; } }
128     public Int32 SectionArrayCount { get { return _SectionArrayCount; } set { _SectionArrayCount = value; } }
129 william 97 #if HAVE_GRN_REFERENCE
130 william 94 public granny_reference RootObjectTypeDefinition { get { return _RootObjectTypeDefinition; } set { _RootObjectTypeDefinition = value; } }
131     public granny_reference RootObject { get { return _RootObject; } set { _RootObject = value; } }
132 william 97 #endif
133 william 94 public Int32 TypeTag { get { return _TypeTag; } set { _TypeTag = value; } }
134     public Int32[] ExtraTags { get { return _ExtraTags; } set { if (value.Length > GrannyGRNExtraTagCount) throw new InvalidOperationException("The size of ExtraTags cannot be greater than " + GrannyGRNExtraTagCount); _ExtraTags = value; } } //4
135     public Int32 StringDatabaseCRC { get { return _StringDatabaseCRC; } set { _StringDatabaseCRC = value; } }
136     public Int32[] ReservedUnused { get { return _ReservedUnused; } set { if (value.Length > GrannyGRNReservedUnusedCount) throw new InvalidOperationException("The size of ReservedUnused cannot be greater than " + GrannyGRNReservedUnusedCount); _ReservedUnused = value; } } //3
137     #endregion
138     }
139     }