/[gr2lib]/trunk/gr2lib/core/coreclasses/header/granny_file.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coreclasses/header/granny_file.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 97 - (show annotations) (download)
Sat Jul 17 05:53:44 2010 UTC (11 years, 4 months ago) by william
File size: 5576 byte(s)
Add:
#define HAVE_GRN_REFERENCE
when defined enables GRN_REFERENCE class, else disables it (the code does not work)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using gr2lib.core.interfaces;
6 using gr2lib.core.exceptions;
7 using gr2lib.core.apiversion;
8 using gr2lib.core.coreclasses.header;
9 using gr2lib.core.coretypes;
10 using gr2lib.core.helpers;
11 using gr2lib.core.coretypes.implementation;
12
13 namespace gr2lib.core.header
14 {
15 public class granny_file : igranny_file
16 {
17 //private const int GrannyGRNExtraTagCount = 4;
18 //private const int GrannyGRNMagixValueCount = 4;
19 //private const int GrannyGRNReservedUnusedCount = 3;
20 //private const int GrannyGRNReservedCount = 2;
21 public granny_file()
22 {
23 granny2apiloader _loader = new granny2apiloader();
24 this.IsByteReversed = false;
25 this.Header = new granny_header();
26 this.SourceMagicValue = new granny_magic();
27 this.SectionCount = 0;
28 this.Sections = IntPtr.Zero;
29 this.Marshalled = false;
30 this.IsUserMemory = false;
31 this.ConversionBuffer = IntPtr.Zero;
32
33 this.NativePointer = IntPtr.Zero;
34 this.NativeFilePointer = IntPtr.Zero;
35
36 this.GrannyFileInfo = new grnfileinfo();
37 }
38
39 private void Initialize()
40 {
41 }
42
43 public void Dispose()
44 {
45 if (NativeFilePointer == IntPtr.Zero) return;
46 coreapi.FreeFile(NativeFilePointer);
47 }
48
49 internal static void ReadFromMemory(string filename, ref granny_file grn_file)
50 {
51 //if (grn_file.NativePointer == IntPtr.Zero) return;
52 gr2lib.core.coretypes.native.granny_file native = Helpers.ReadFromMemory<gr2lib.core.coretypes.native.granny_file>(grn_file.NativePointer);
53 granny_file managed = new granny_file();
54 managed.NativePointer = grn_file.NativeFilePointer;
55
56 // process
57
58 managed.IsByteReversed = native.IsByteReversed;
59 managed.SectionCount = native.SectionCount;
60 managed.Sections = native.Sections;
61 managed.Marshalled = native.Marshalled;
62 managed.IsUserMemory = native.IsUserMemory;
63 managed.ConversionBuffer = native.ConversionBuffer;
64
65 if (native.Header != IntPtr.Zero)
66 {
67 managed.Header = granny_header.ReadFromMemory(native.Header);
68 managed.Header.TypeTag = coreapi.GrannyGetFileTypeTag(grn_file.NativePointer);
69 }
70 else
71 {
72 managed.Header = new granny_header();
73 }
74
75 if (native.SourceMagicValue != IntPtr.Zero)
76 {
77 managed.SourceMagicValue = granny_magic.ReadFromMemory(native.SourceMagicValue);
78 }
79 else
80 {
81 managed.SourceMagicValue = new granny_magic();
82 }
83
84 grn_file.NativeFilePointer = coreapi.GetFileInfo(grn_file.NativePointer);
85 managed.NativeFilePointer = grn_file.NativeFilePointer;
86 managed.GrannyFileInfo = grnfileinfo.ReadFromMemory(managed.NativeFilePointer);
87
88 grn_file = managed;
89 }
90
91
92
93 public static granny_file ReadFromFile(string filename)
94 {
95 granny_file grn_file = new granny_file();
96 grn_file.NativePointer = coreapi.GrannyReadEntireFile(filename);
97 if (grn_file.NativePointer == null || grn_file.NativePointer == IntPtr.Zero)
98 {
99 Console.WriteLine("Error: unable to load {0} as a Granny file.", filename);
100 }
101
102 ReadFromMemory(filename, ref grn_file);
103 coreapi.FreeFile(grn_file.NativePointer);
104 return grn_file;
105 }
106
107 #region INativePointer Members
108 private IntPtr _NativePointer;
109 public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
110 private IntPtr _NativeFilePointer;
111 public IntPtr NativeFilePointer { get { return _NativeFilePointer; } set { _NativeFilePointer = value; } }
112 #endregion
113
114 #region igranny2header members
115
116 private bool _IsByteReversed;
117 private granny_header _header;
118 private granny_magic _SourceMagicValue;
119 private int _SectionCount;
120 private IntPtr _Sections;
121 private bool _Marshalled;
122 private bool _IsUserMemory;
123 private IntPtr _ConversionBuffer;
124
125
126 private grnfileinfo _GrannyFileInfo;
127
128 public grnfileinfo GrannyFileInfo { get { return _GrannyFileInfo; } set { _GrannyFileInfo = value; } }
129 public bool IsByteReversed { get { return _IsByteReversed; } set { _IsByteReversed = value; } }
130 public granny_header Header { get { return _header; } set { _header = value; } }
131 public granny_magic SourceMagicValue { get { return _SourceMagicValue; } set { _SourceMagicValue = value; } }
132 public int SectionCount { get { return _SectionCount; } set { _SectionCount = value; } }
133 public IntPtr Sections { get { return _Sections; } set { _Sections = value; } }
134 public bool Marshalled { get { return _Marshalled; } set { _Marshalled = value; } }
135 public bool IsUserMemory { get { return _IsUserMemory; } set { _IsUserMemory = value; } }
136 public IntPtr ConversionBuffer { get { return _ConversionBuffer; } set { _ConversionBuffer = value; } }
137 #endregion
138 }
139 }

  ViewVC Help
Powered by ViewVC 1.1.22