-
Notifications
You must be signed in to change notification settings - Fork 19
/
PiaFile.cs
94 lines (77 loc) · 2.25 KB
/
PiaFile.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
//
// Copyright © 2014 Parrish Husband (parrish.husband@gmail.com)
// The MIT License (MIT) - See LICENSE.txt for further details.
//
using PiaNO.Serialization;
using System;
using System.IO;
namespace PiaNO
{
public abstract class PiaFile : PiaNode
{
#region Properties
public PiaHeader Header { get; internal set; }
public string FileName { get; set; }
#endregion
#region Constructors
protected internal PiaFile() : base() { }
protected PiaFile(string filePath) : base()
{
Read(filePath);
}
#endregion
#region Methods
public void Read()
{
if (string.IsNullOrEmpty(this.FileName))
throw new ArgumentNullException("FileName");
Read(this.FileName);
}
public void Read(string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
if (!File.Exists(fileName))
throw new FileNotFoundException("Plot file not found", fileName);
try
{
FileName = Path.GetFileName(fileName);
using (var inStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
PiaSerializer.Deserialize(inStream, this);
inStream.Close();
}
}
catch (Exception)
{
throw;
}
}
public void Write()
{
if (string.IsNullOrEmpty(this.FileName))
throw new ArgumentNullException("FileName");
Write(this.FileName);
}
public void Write(string fileName)
{
try
{
using (var outStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
PiaSerializer.Serialize(outStream, this);
outStream.Close();
}
}
catch (Exception)
{
throw;
}
}
public override string ToString()
{
return Path.GetFileName(FileName);
}
#endregion
}
}