using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace Vestris.ResourceLib
{
///
/// This structure depicts the organization of data in a file-version resource. It typically contains a
/// list of language and code page identifier pairs that the version of the application or DLL supports.
/// http://msdn.microsoft.com/en-us/library/bb202818.aspx
///
public class VarTable : ResourceTableHeader
{
private Dictionary _languages = new Dictionary();
///
/// A dictionary of language and code page identifier pairs.
///
public Dictionary Languages
{
get
{
return _languages;
}
}
///
/// A new table of language and code page identifier pairs.
///
public VarTable()
{
}
///
/// A new table of language and code page identifier pairs.
///
/// Table key.
public VarTable(string key)
: base(key)
{
}
///
/// An existing table of language and code page identifier pairs.
///
/// Pointer to the beginning of the data.
internal VarTable(IntPtr lpRes)
{
Read(lpRes);
}
///
/// Read a table of language and code page identifier pairs.
///
/// Pointer to the beginning of the data.
///
internal override IntPtr Read(IntPtr lpRes)
{
_languages.Clear();
IntPtr pVar = base.Read(lpRes);
while (pVar.ToInt32() < (lpRes.ToInt32() + _header.wLength))
{
Kernel32.VAR_HEADER var = (Kernel32.VAR_HEADER) Marshal.PtrToStructure(
pVar, typeof(Kernel32.VAR_HEADER));
_languages.Add(var.wLanguageIDMS, var.wCodePageIBM);
pVar = new IntPtr(pVar.ToInt32() + Marshal.SizeOf(var));
}
return new IntPtr(lpRes.ToInt32() + _header.wLength);
}
///
/// Write the table of language and code page identifier pairs to a binary stream.
///
/// Binary stream.
/// Last unpadded position.
internal override void Write(BinaryWriter w)
{
long headerPos = w.BaseStream.Position;
base.Write(w);
Dictionary.Enumerator languagesEnum = _languages.GetEnumerator();
long valuePos = w.BaseStream.Position;
while (languagesEnum.MoveNext())
{
// id
w.Write((UInt16) languagesEnum.Current.Key);
// code page
w.Write((UInt16) languagesEnum.Current.Value);
}
ResourceUtil.WriteAt(w, w.BaseStream.Position - valuePos, headerPos + 2);
ResourceUtil.PadToDWORD(w);
ResourceUtil.WriteAt(w, w.BaseStream.Position - headerPos, headerPos);
}
///
/// Returns a code page identifier for a given language.
///
/// Language ID.
/// Code page identifier.
public UInt16 this[UInt16 key]
{
get
{
return _languages[key];
}
set
{
_languages[key] = value;
}
}
///
/// String representation of the var table.
///
/// Indent.
/// String representation of the var table.
public override string ToString(int indent)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("{0}BEGIN", new String(' ', indent)));
Dictionary.Enumerator languagesEnumerator = _languages.GetEnumerator();
while (languagesEnumerator.MoveNext())
{
sb.AppendLine(string.Format("{0}VALUE \"Translation\", 0x{1:x}, 0x{2:x}",
new String(' ', indent + 1), languagesEnumerator.Current.Key, languagesEnumerator.Current.Value));
}
sb.AppendLine(string.Format("{0}END", new String(' ', indent)));
return sb.ToString();
}
}
}