// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Dtf.Resources { using System; using System.IO; using System.Text; using System.Reflection; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Diagnostics.CodeAnalysis; /// /// Represents a string table of a file version resource. /// [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] public sealed class VersionStringTable : IDictionary { private VersionResource parent; private VersionInfo rawStringVersionInfo; internal VersionStringTable(VersionResource parent, VersionInfo rawStringVersionInfo) { this.parent = parent; this.rawStringVersionInfo = rawStringVersionInfo; } /// /// Gets the locale (LCID) of the string table. /// public int Locale { get { return UInt16.Parse(rawStringVersionInfo.Key.Substring(0, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture); } set { rawStringVersionInfo.Key = ((ushort) value).ToString("x4", CultureInfo.InvariantCulture) + rawStringVersionInfo.Key.Substring(4, 4); this.parent.dirty = true; } } /// /// Gets or sets a string value. /// /// Name of the string. public string this[string key] { get { VersionInfo verValue = this.rawStringVersionInfo[key]; if (verValue == null) { return null; } else { return Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2); } } set { if (value == null) { rawStringVersionInfo.Remove(key); } else { VersionInfo verValue = rawStringVersionInfo[key]; if (verValue == null) { verValue = new VersionInfo(key); verValue.IsString = true; rawStringVersionInfo.Add(verValue); } verValue.Data = new byte[Encoding.Unicode.GetByteCount(value) + 2]; Encoding.Unicode.GetBytes(value, 0, value.Length, verValue.Data, 0); } this.parent.dirty = true; } } bool ICollection>.IsReadOnly { get { return false; } } bool IDictionary.TryGetValue(string key, out string value) { value = this[key]; return value != null; } void ICollection>.Add(KeyValuePair item) { this[item.Key] = item.Value; } bool ICollection>.Remove(KeyValuePair item) { string value = this[item.Key]; if (value == item.Value) { this[item.Key] = null; return true; } else { return false; } } bool ICollection>.Contains(KeyValuePair item) { string value = this[item.Key]; if (value == item.Value) { return true; } else { return false; } } bool IDictionary.ContainsKey(string key) { return this[key] != null; } void IDictionary.Add(string key, string value) { this[key] = value; } bool IDictionary.Remove(string key) { if (this[key] != null) { this[key] = null; return true; } else { return false; } } /// /// Removes all strings from the string table. /// public void Clear() { this.rawStringVersionInfo.Clear(); } /// /// Gets a collection of all the names of the strings in the table. /// public ICollection Keys { get { List keys = new List(this.rawStringVersionInfo.Count); foreach (VersionInfo verValue in this.rawStringVersionInfo) { keys.Add(verValue.Key); } return keys; } } /// /// Gets a collection of all the values in the table. /// public ICollection Values { get { List values = new List(this.rawStringVersionInfo.Count); foreach (VersionInfo verValue in this.rawStringVersionInfo) { values.Add(Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2)); } return values; } } /// /// Gets the number of strings in the table. /// public int Count { get { return this.rawStringVersionInfo.Count; } } void ICollection>.CopyTo(KeyValuePair[] array, int index) { foreach (VersionInfo verValue in this.rawStringVersionInfo) { array[index++] = new KeyValuePair(verValue.Key, Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2)); } } /// /// Gets an enumeration over all strings in the table. /// /// Enumeration of string name and value pairs public IEnumerator> GetEnumerator() { foreach (VersionInfo verValue in this.rawStringVersionInfo) { yield return new KeyValuePair(verValue.Key, Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2)); } } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } }