Skip to content

Commit

Permalink
Zip organization, PiaFile write
Browse files Browse the repository at this point in the history
Added deflator zip methods and classes, and reorganized the compression
namespace to 'Zip'.
Started writing the PiaFile.Write methods and changed encoding to
Default for safety.
Began adding properties to the PlotConfiguration class.
  • Loading branch information
Allockse committed Mar 30, 2014
1 parent 140d910 commit f9dd758
Show file tree
Hide file tree
Showing 30 changed files with 5,544 additions and 56 deletions.
6 changes: 0 additions & 6 deletions PiaException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@

namespace PiaNO
{

#if !NETCF_1_0 && !NETCF_2_0
[Serializable]
#endif
public class PiaException : ApplicationException
{
#if !NETCF_1_0 && !NETCF_2_0

protected PiaException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
#endif

public PiaException()
{ }
Expand Down
43 changes: 33 additions & 10 deletions PiaFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using PiaNO.Compression.Streams;
using PiaNO.Zip.Compression;
using PiaNO.Zip.Streams;

namespace PiaNO
{
Expand Down Expand Up @@ -32,8 +33,10 @@ public void Read(string fileName)

try
{
var piaStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Read(piaStream);
using (var piaStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
Read(piaStream);
}
}
catch (Exception)
{
Expand All @@ -52,7 +55,7 @@ public void Read(Stream stream)

using (var zStream = new InflaterInputStream(stream))
{
var sr = new StreamReader(zStream);
var sr = new StreamReader(zStream, System.Text.Encoding.Default);
InnerData = sr.ReadToEnd();
}

Expand All @@ -67,20 +70,40 @@ public void Read(Stream stream)
{
throw;
}
finally
{
stream.Close();
stream.Dispose();
}
}

public void Write(string fileName)
{

using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
Write(fileStream);
}
}
public void Write(Stream stream)
{
try
{
var headerBytes = System.Text.Encoding.Default.GetBytes(Header.ToString());
stream.Write(headerBytes, 0, headerBytes.Length);

var piaBytes = System.Text.Encoding.Default.GetBytes(InnerData.ToString());
byte[] compressedBytes;
using (var ms = new MemoryStream())
{
var deflateStream = new DeflaterOutputStream(ms, new Deflater(Deflater.DEFAULT_COMPRESSION));
deflateStream.Write(piaBytes, 0, piaBytes.Length);
deflateStream.Flush();
deflateStream.Finish();

compressedBytes = ms.ToArray();
}

stream.Write(compressedBytes, 0, compressedBytes.Length);
}
catch (Exception)
{
throw;
}
}

private void _setOwnership(PiaNode node)
Expand Down
17 changes: 14 additions & 3 deletions PiaHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace PiaNO
public class PiaHeader
{
private const string PIA_HEADER_FORMAT = @"PIAFILEVERSION_{0},{1}VER{2},compress\r\npmzlibcodec\255\255\255\255\255\255\255\000\255\255\255\000";
private string _rawData;
private string _headerData;

public double PiaFileVersion { get; private set; }
public short TypeVersion { get; private set; }
public PiaType PiaType { get; private set; }

public PiaHeader(string headerString)
{
_rawData = headerString;
_headerData = headerString;

var firstLine = headerString.Split()[0];
var headerArray = headerString.Split(new char[] { ',', '_'});
Expand All @@ -31,7 +31,18 @@ public PiaHeader(string headerString)

public override string ToString()
{
return string.Format(PIA_HEADER_FORMAT, PiaFileVersion, PiaType, TypeVersion);
return _headerData;
//var fileversionString = string.Format("{0:0.0}", Math.Truncate(PiaFileVersion * 10) / 10);
//return string.Format(PIA_HEADER_FORMAT, fileversionString, PiaType, TypeVersion);
}

public byte[] ToByteArray()
{
var headerString = this.ToString();
var bytes = new byte[headerString.Length * sizeof(char)];
System.Buffer.BlockCopy(headerString.ToCharArray(), 0, bytes, 0, bytes.Length);

return bytes;
}
}
}
37 changes: 23 additions & 14 deletions PiaNO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Compression\Checksums\Adler32.cs" />
<Compile Include="Compression\Checksums\IChecksum.cs" />
<Compile Include="Compression\Deflater.cs" />
<Compile Include="Compression\DeflaterEngine.cs" />
<Compile Include="Compression\DeflaterHuffman.cs" />
<Compile Include="Compression\DeflaterPending.cs" />
<Compile Include="Compression\DeflatorConstants.cs" />
<Compile Include="Compression\Inflater.cs" />
<Compile Include="Compression\InflaterDynHeader.cs" />
<Compile Include="Compression\InflaterHuffmanTree.cs" />
<Compile Include="Compression\PendingBuffer.cs" />
<Compile Include="Compression\Streams\OutputWindow.cs" />
<Compile Include="Compression\Streams\StreamManipulator.cs" />
<Compile Include="Zip\Checksums\Adler32.cs" />
<Compile Include="Zip\Checksums\Crc32.cs" />
<Compile Include="Zip\Checksums\IChecksum.cs" />
<Compile Include="Zip\Compression\Deflater.cs" />
<Compile Include="Zip\Compression\DeflaterEngine.cs" />
<Compile Include="Zip\Compression\DeflaterHuffman.cs" />
<Compile Include="Zip\Compression\DeflaterPending.cs" />
<Compile Include="Zip\Compression\DeflatorConstants.cs" />
<Compile Include="Zip\Compression\Inflater.cs" />
<Compile Include="Zip\Compression\InflaterDynHeader.cs" />
<Compile Include="Zip\Compression\InflaterHuffmanTree.cs" />
<Compile Include="Zip\Compression\PendingBuffer.cs" />
<Compile Include="Zip\Streams\DeflatorOutputStream.cs" />
<Compile Include="Zip\Streams\OutputWindow.cs" />
<Compile Include="Zip\Streams\StreamManipulator.cs" />
<Compile Include="Zip\ZipConstants.cs" />
<Compile Include="Zip\ZipEntry.cs" />
<Compile Include="Zip\ZipException.cs" />
<Compile Include="Zip\ZipExtraData.cs" />
<Compile Include="Zip\ZipHelperStream.cs" />
<Compile Include="Zip\Encryption\PkzipClassic.cs" />
<Compile Include="Zip\Encryption\ZipAESTransform.cs" />
<Compile Include="PiaNode.cs" />
<Compile Include="PiaException.cs" />
<Compile Include="Plot\ColorDependentPlotStyleTable.cs" />
Expand All @@ -70,7 +79,7 @@
<Compile Include="Plot\PlotterModelParameters.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Serialization\PiaSerializer.cs" />
<Compile Include="Compression\Streams\InflaterInputStream.cs" />
<Compile Include="Zip\Streams\InflaterInputStream.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
17 changes: 15 additions & 2 deletions PiaNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

using PiaNO.Serialization;

Expand Down Expand Up @@ -87,11 +86,25 @@ protected virtual void Deserialize()
PiaSerializer.Deserialize(this);
}

protected virtual void Serialize(Stream stream)
{
PiaSerializer.Serialize(stream, this);
}

public override string ToString()
{
return this.NodeName;
}

public byte[] ToByteArray()
{
var headerString = this.ToString();
var bytes = new byte[headerString.Length * sizeof(char)];
System.Buffer.BlockCopy(headerString.ToCharArray(), 0, bytes, 0, bytes.Length);

return bytes;
}

#endregion

#region ICloneable
Expand Down
112 changes: 112 additions & 0 deletions Plot/PlotterConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,92 @@ namespace PiaNO.Plot
{
public class PlotterConfiguration : PiaFile
{
public string ModelPath
{
get { return _getMetaString("user_defined_model_pathname"); }
set { _setMetaString("user_defined_model_pathname", value); }
}

public string ModelBase
{
get { return _getMetaString("user_defined_model_basename"); }
set { _setMetaString("user_defined_model_basename", value); }
}

public string DriverPath
{
get { return _getMetaString("driver_pathname"); }
set { _setMetaString("driver_pathname", value); }
}

public string DriverVersion
{
get { return _getMetaString("driver_version"); }
set { _setMetaString("driver_version", value); }
}

public string DriverTagline
{
get { return _getMetaString("driver_tag_line"); }
set { _setMetaString("driver_tag_line", value); }
}

public int ToolkitVersion
{
get { return int.Parse(_getMetaString("toolkit_version")); }
set { _setMetaString("toolkit_version", value.ToString()); }
}

public int DriverType
{
get { return int.Parse(_getMetaString("driver_type")); }
set { _setMetaString("driver_type", value.ToString()); }
}

public string CanonicalFamily
{
get { return _getMetaString("canonical_family_name"); }
set { _setMetaString("canonical_family_name", value); }
}

public bool ShowCustomFirst
{
get { return bool.Parse(_getMetaString("show_custom_first")); }
set { _setMetaString("show_custom_first", value.ToString().ToUpper()); }
}

public bool TruetypeAsText
{
get { return bool.Parse(_getMetaString("truetype_as_text")); }
set { _setMetaString("truetype_as_text", value.ToString().ToUpper()); }
}

public string CanonicalModel
{
get { return _getMetaString("canonical_model_name"); }
set { _setMetaString("canonical_model_name", value); }
}

public string LocalizedFamily
{
get { return _getMetaString("localized_family_name"); }
set { _setMetaString("localized_family_name", value); }
}

public string LocalizedModel
{
get { return _getMetaString("localized_model_name"); }
set { _setMetaString("localized_model_name", value); }
}

public bool PlotToFile
{
get { return bool.Parse(_getMetaString("file_only")); }
set { _setMetaString("file_only", value.ToString().ToUpper()); }
}

//public Dictionary<string, object

// Meta
// Media
// > Size
Expand All @@ -25,5 +111,31 @@ public class PlotterConfiguration : PiaFile
// > 5
// > 6

private string _getMetaString(string name)
{
if (!HasChildNodes)
return null;

var metaNode = this["meta"];
if (metaNode == null)
return null;

if (!metaNode.Values.ContainsKey(name))
return null;

return metaNode.Values[name];
}
private void _setMetaString(string name, string value)
{
if (!HasChildNodes)
return;

var metaNode = this["meta"];
if (metaNode == null)
return;

metaNode.Values[name] = value;
}

}
}
10 changes: 8 additions & 2 deletions Serialization/PiaSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace PiaNO.Serialization
{
Expand Down Expand Up @@ -64,9 +65,14 @@ public static void Deserialize(PiaNode node)
}
}
}
public static Stream Serialize(PiaNode node)
public static void Serialize(Stream stream, PiaNode node)
{
throw new NotImplementedException();
if (stream == null)
throw new ArgumentNullException("Stream");

if (node == null)
throw new ArgumentNullException("Node");

}

public static KeyValuePair<string, string> _getValue(string valueString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

using System;

namespace PiaNO.Compression.Checksums
namespace PiaNO.Zip.Checksums
{

/// <summary>
Expand Down
Loading

0 comments on commit f9dd758

Please sign in to comment.