/[msmoney_db_parser]/trunk/libmsmoneyfile/MSMoneyFile.cs
ViewVC logotype

Contents of /trunk/libmsmoneyfile/MSMoneyFile.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations) (download)
Fri Sep 18 13:17:49 2015 UTC (4 years ago) by william
File size: 4178 byte(s)


1 using System;
2 using System.IO;
3 using Enterprise.Logging;
4 using System.Text;
5 using System.Collections.Generic;
6
7
8 namespace libmsmoneyfile
9 {
10 public static class MSMoneyFile
11 {
12 const string MAGIC_MSMONEY_BACKUP_FILE = "MSCF";
13 const string MAGIC_MSMONEY_DATABASE = "MSISAM Database";
14 private static IMSMoneyFile OpenMSMoneyFile(FileInfo file) { return OpenMSMoneyFile (file.FullName); }
15 private static IMSMoneyFile OpenMSMoneyFile(string file)
16 {
17 _MSMoneyFile money_file = new _MSMoneyFile (file);
18 gLog.Info.WriteLine ("Opening MSMoney file: {0}", file);
19
20 if (!File.Exists (file)) {
21 gLog.Error.WriteLine ("Could not find MSMoney file: {0}", file);
22 throw new FileNotFoundException (string.Format("Could not find MSMoney file: {0}", file));
23 }
24
25 using (FileStream fs = new FileStream(file,FileMode.Open,FileAccess.Read, FileShare.Read))
26 {
27 using (BinaryReader br = new BinaryReader(fs)) {
28
29 byte[] raw_magic = br.ReadBytes (MAGIC_MSMONEY_BACKUP_FILE.Length);
30 string magic = Encoding.ASCII.GetString(raw_magic);
31
32 if (magic != MAGIC_MSMONEY_BACKUP_FILE) {
33 raw_magic = br.ReadBytes (MAGIC_MSMONEY_DATABASE.Length);
34 magic = Encoding.ASCII.GetString(raw_magic);
35 if (magic == MAGIC_MSMONEY_DATABASE) {
36 money_file.FileType = MSMONEY_FILE_TYPE.MSMONEY_DATABASE_FILE;
37 money_file.HeaderSize = -1;
38 money_file.HeaderOffset = br.BaseStream.Position;
39 money_file.DatabaseOffset = money_file.HeaderOffset;
40 gLog.Debug.WriteLine ("found magic: {0}", MAGIC_MSMONEY_DATABASE);
41 }
42 else {
43 gLog.Error.WriteLine ("File is not an MSMoney file or is corrupt: {0}", file);
44 throw new InvalidOperationException (string.Format("File is not an MSMoney file or is corrupt: {0}", file));
45 }
46 }
47 else if (magic == MAGIC_MSMONEY_BACKUP_FILE) {
48 money_file.FileType = MSMONEY_FILE_TYPE.MSMONEY_BACKUP_FILE;
49 gLog.Debug.WriteLine ("found magic: {0}", MAGIC_MSMONEY_BACKUP_FILE);
50 List<byte> backup_header = new List<byte> ();
51 raw_magic = br.ReadBytes (MAGIC_MSMONEY_DATABASE.Length);
52 magic = Encoding.ASCII.GetString(raw_magic);
53
54 while (magic != MAGIC_MSMONEY_DATABASE) {
55 br.BaseStream.Seek ((MAGIC_MSMONEY_DATABASE.Length) * -1, SeekOrigin.Current);
56 backup_header.Add (br.ReadByte ());
57 raw_magic = br.ReadBytes (MAGIC_MSMONEY_DATABASE.Length);
58 magic = Encoding.ASCII.GetString(raw_magic);
59 }
60 br.BaseStream.Seek (MAGIC_MSMONEY_DATABASE.Length * -1, SeekOrigin.Current);
61 raw_magic = br.ReadBytes (MAGIC_MSMONEY_DATABASE.Length);
62 magic = Encoding.ASCII.GetString(raw_magic);
63 if (magic == MAGIC_MSMONEY_DATABASE) {
64 gLog.Debug.WriteLine ("Found database magic '{0}' at offset: 0x{1:x8}", MAGIC_MSMONEY_DATABASE, br.BaseStream.Position - MAGIC_MSMONEY_DATABASE.Length);
65 }
66
67 // parse the backup_header ?
68 // using (MemoryStream ms = new MemoryStream(backup_header.ToArray())) {
69 // using (BinaryReader br2 = new BinaryReader(ms)) {
70 //
71 // uint mscf_header_unk1 = br2.ReadUInt32 ();
72 // uint mscf_header_unk2 = br2.ReadUInt32 ();
73 //
74 // br2.Close ();
75 // }
76 // }
77 money_file.HeaderSize = backup_header.Count;
78 money_file.HeaderOffset = MAGIC_MSMONEY_BACKUP_FILE.Length;
79 money_file.DatabaseOffset = br.BaseStream.Position;
80 }
81
82 // we should have an MSMoney Database file at this point
83
84 // parse the database header?
85
86 br.Close ();
87 }
88 }
89
90 return money_file;
91 }
92
93 public static IMSMoneyParser ParseMSMoneyFile(FileInfo file)
94 {
95 return ParseMSMoneyFile (file);
96 }
97 public static IMSMoneyParser ParseMSMoneyFile(string file)
98 {
99 var msmoney_file = OpenMSMoneyFile (file);
100 IMSMoneyParser parser = null;
101 switch (msmoney_file.FileType)
102 {
103 case MSMONEY_FILE_TYPE.MSMONEY_BACKUP_FILE:
104 parser = new MSCF_Parser (msmoney_file);
105 break;
106 case MSMONEY_FILE_TYPE.MSMONEY_DATABASE_FILE:
107 parser = new MSISAM_Parser (msmoney_file);
108 break;
109 default:
110 throw new InvalidOperationException (string.Format("unknown msmoney file: {0}", file));
111 }
112 return parser;
113 }
114 }
115 }
116

  ViewVC Help
Powered by ViewVC 1.1.22