Skip to content

Commit

Permalink
[monkeydoc] Introduce support for versioned tree and gracefully handl…
Browse files Browse the repository at this point in the history
…e old trees.
  • Loading branch information
garuma committed Dec 20, 2012
1 parent a527653 commit ed413ba
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions mcs/tools/monkeydoc/MonkeyDoc/Tree.cs
Expand Up @@ -18,6 +18,7 @@ namespace MonkeyDoc
*/ */
public class Tree public class Tree
{ {
const long CurrentVersionNumber = 1;
public readonly HelpSource HelpSource; public readonly HelpSource HelpSource;


FileStream InputStream; FileStream InputStream;
Expand All @@ -31,6 +32,7 @@ public class Tree
/// </summary> /// </summary>
public Tree (HelpSource hs, string filename) public Tree (HelpSource hs, string filename)
{ {
HelpSource = hs;
Encoding utf8 = new UTF8Encoding (false, true); Encoding utf8 = new UTF8Encoding (false, true);


if (!File.Exists (filename)){ if (!File.Exists (filename)){
Expand All @@ -45,11 +47,15 @@ public Tree (HelpSource hs, string filename)
throw new Exception ("Invalid file format"); throw new Exception ("Invalid file format");


InputStream.Position = 4; InputStream.Position = 4;
// Try to read version information
if (InputReader.ReadInt32 () == -(int)'v')
VersionNumber = InputReader.ReadInt64 ();
else
InputStream.Position -= 4;

var position = InputReader.ReadInt32 (); var position = InputReader.ReadInt32 ();
rootNode = new Node (this, position); rootNode = new Node (this, position);
InflateNode (rootNode); InflateNode (rootNode);

HelpSource = hs;
} }


/// <summary> /// <summary>
Expand All @@ -73,14 +79,16 @@ public void Save (string file)
Encoding utf8 = new UTF8Encoding (false, true); Encoding utf8 = new UTF8Encoding (false, true);
using (FileStream output = File.OpenWrite (file)){ using (FileStream output = File.OpenWrite (file)){
// Skip over the pointer to the first node. // Skip over the pointer to the first node.
output.Position = 8; output.Position = 4 + 4 + 8 + 4;


using (BinaryWriter writer = new BinaryWriter (output, utf8)) { using (BinaryWriter writer = new BinaryWriter (output, utf8)) {
// Recursively dump // Recursively dump
rootNode.Serialize (output, writer); rootNode.Serialize (output, writer);


output.Position = 0; output.Position = 0;
writer.Write (new byte [] { (byte) 'M', (byte) 'o', (byte) 'H', (byte) 'P' }); writer.Write (new byte [] { (byte) 'M', (byte) 'o', (byte) 'H', (byte) 'P' });
writer.Write (-(int)'v');
writer.Write (CurrentVersionNumber);
writer.Write (rootNode.Address); writer.Write (rootNode.Address);
} }
} }
Expand All @@ -92,6 +100,11 @@ public void Save (string file)
} }
} }


public long VersionNumber {
get;
private set;
}

static bool GoodSig (byte [] sig) static bool GoodSig (byte [] sig)
{ {
if (sig.Length != 4) if (sig.Length != 4)
Expand Down

0 comments on commit ed413ba

Please sign in to comment.