Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / wvdotnet / wvhexdump.cs
blob823ba09ab8145f3dfcc87133e45479f57d8d88cf
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Text;
8 using System.Linq;
10 namespace Wv
12 public class WvDelayedString
14 Func<string> a;
16 public WvDelayedString(Func<string> a)
18 this.a = a;
21 public override string ToString()
23 return a();
27 public partial class wv
29 static string hexbyte(WvBytes b, int ofs)
31 if (ofs >= b.start && ofs < b.start+b.len)
32 return b.bytes[ofs].ToString("x2");
33 else
34 return " ";
37 static char printable(WvBytes b, int ofs)
39 if (ofs >= b.start && ofs < b.start+b.len)
41 byte n = b.bytes[ofs];
42 if (31 < n && n < 127)
43 return (char)n;
44 else
45 return '.';
47 else
48 return ' ';
51 public static string _hexdump(WvBytes b)
53 if (b.bytes == null)
54 return "(nil)";
56 var sb = new StringBuilder();
58 // This is overly complicated so that the body and header of
59 // the same buffer can be printed separately yet still show the
60 // proper alignment
62 int rowoffset = b.start & (~0xf);
64 // Note: it's important to set the right capacity when dealing
65 // with large quantities of data. Assume about 80 chars per line.
66 sb.EnsureCapacity((b.len / 16 + 2) * 80);
68 for (int i = rowoffset; i < b.len; i += 16)
70 sb.Append('[').Append(i.ToString("x4")).Append("]");
72 for (int j = 0; j < 16; j++)
74 if ((j % 4)==0)
75 sb.Append(' ');
76 sb.Append(hexbyte(b, i+j));
79 sb.Append(' ');
81 for (int j = 0; j < 16; j++)
83 if ((j % 4)==0)
84 sb.Append(' ');
85 sb.Append(printable(b, i+j));
88 sb.Append('\n');
91 return sb.ToString();
94 public static object hexdump(WvBytes b)
96 return new WvDelayedString(() => _hexdump(b));