| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Dtf.Resources |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Text; |
| 8 | using System.Reflection; |
| 9 | using System.Collections; |
| 10 | using System.Collections.Generic; |
| 11 | using System.Globalization; |
| 12 | using System.Diagnostics.CodeAnalysis; |
| 13 | |
| 14 | /// <summary> |
| 15 | /// Represents a string table of a file version resource. |
| 16 | /// </summary> |
| 17 | [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] |
| 18 | public sealed class VersionStringTable : IDictionary<string, string> |
| 19 | { |
| 20 | private VersionResource parent; |
| 21 | private VersionInfo rawStringVersionInfo; |
| 22 | |
| 23 | internal VersionStringTable(VersionResource parent, VersionInfo rawStringVersionInfo) |
| 24 | { |
| 25 | this.parent = parent; |
| 26 | this.rawStringVersionInfo = rawStringVersionInfo; |
| 27 | } |
| 28 | |
| 29 | /// <summary> |
| 30 | /// Gets the locale (LCID) of the string table. |
| 31 | /// </summary> |
| 32 | public int Locale |
| 33 | { |
| 34 | get |
| 35 | { |
| 36 | return UInt16.Parse(rawStringVersionInfo.Key.Substring(0, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture); |
| 37 | } |
| 38 | set |
| 39 | { |
| 40 | rawStringVersionInfo.Key = ((ushort) value).ToString("x4", CultureInfo.InvariantCulture) + rawStringVersionInfo.Key.Substring(4, 4); |
| 41 | this.parent.dirty = true; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Gets or sets a string value. |
| 47 | /// </summary> |
| 48 | /// <param name="key">Name of the string.</param> |
| 49 | public string this[string key] |
| 50 | { |
| 51 | get |
| 52 | { |
| 53 | VersionInfo verValue = this.rawStringVersionInfo[key]; |
| 54 | if (verValue == null) |
| 55 | { |
| 56 | return null; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | return Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2); |
| 61 | } |
| 62 | } |
| 63 | set |
| 64 | { |
| 65 | if (value == null) |
| 66 | { |
| 67 | rawStringVersionInfo.Remove(key); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | VersionInfo verValue = rawStringVersionInfo[key]; |
| 72 | if (verValue == null) |
| 73 | { |
| 74 | verValue = new VersionInfo(key); |
| 75 | verValue.IsString = true; |
| 76 | rawStringVersionInfo.Add(verValue); |
| 77 | } |
| 78 | verValue.Data = new byte[Encoding.Unicode.GetByteCount(value) + 2]; |
| 79 | Encoding.Unicode.GetBytes(value, 0, value.Length, verValue.Data, 0); |
| 80 | } |
| 81 | this.parent.dirty = true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | bool ICollection<KeyValuePair<string, string>>.IsReadOnly |
| 86 | { |
| 87 | get |
| 88 | { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | bool IDictionary<string, string>.TryGetValue(string key, out string value) |
| 94 | { |
| 95 | value = this[key]; |
| 96 | return value != null; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item) |
| 101 | { |
| 102 | this[item.Key] = item.Value; |
| 103 | } |
| 104 | |
| 105 | bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item) |
| 106 | { |
| 107 | string value = this[item.Key]; |
| 108 | if (value == item.Value) |
| 109 | { |
| 110 | this[item.Key] = null; |
| 111 | return true; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item) |
| 120 | { |
| 121 | string value = this[item.Key]; |
| 122 | if (value == item.Value) |
| 123 | { |
| 124 | return true; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | bool IDictionary<string, string>.ContainsKey(string key) |
| 133 | { |
| 134 | return this[key] != null; |
| 135 | } |
| 136 | |
| 137 | void IDictionary<string, string>.Add(string key, string value) |
| 138 | { |
| 139 | this[key] = value; |
| 140 | } |
| 141 | |
| 142 | bool IDictionary<string, string>.Remove(string key) |
| 143 | { |
| 144 | if (this[key] != null) |
| 145 | { |
| 146 | this[key] = null; |
| 147 | return true; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /// <summary> |
| 156 | /// Removes all strings from the string table. |
| 157 | /// </summary> |
| 158 | public void Clear() |
| 159 | { |
| 160 | this.rawStringVersionInfo.Clear(); |
| 161 | } |
| 162 | |
| 163 | /// <summary> |
| 164 | /// Gets a collection of all the names of the strings in the table. |
| 165 | /// </summary> |
| 166 | public ICollection<string> Keys |
| 167 | { |
| 168 | get |
| 169 | { |
| 170 | List<string> keys = new List<string>(this.rawStringVersionInfo.Count); |
| 171 | foreach (VersionInfo verValue in this.rawStringVersionInfo) |
| 172 | { |
| 173 | keys.Add(verValue.Key); |
| 174 | } |
| 175 | return keys; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// <summary> |
| 180 | /// Gets a collection of all the values in the table. |
| 181 | /// </summary> |
| 182 | public ICollection<string> Values |
| 183 | { |
| 184 | get |
| 185 | { |
| 186 | List<string> values = new List<string>(this.rawStringVersionInfo.Count); |
| 187 | foreach (VersionInfo verValue in this.rawStringVersionInfo) |
| 188 | { |
| 189 | values.Add(Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2)); |
| 190 | } |
| 191 | return values; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /// <summary> |
| 196 | /// Gets the number of strings in the table. |
| 197 | /// </summary> |
| 198 | public int Count |
| 199 | { |
| 200 | get |
| 201 | { |
| 202 | return this.rawStringVersionInfo.Count; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void ICollection<KeyValuePair<string, string>>.CopyTo(KeyValuePair<string, string>[] array, int index) |
| 207 | { |
| 208 | foreach (VersionInfo verValue in this.rawStringVersionInfo) |
| 209 | { |
| 210 | array[index++] = new KeyValuePair<string, string>(verValue.Key, Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2)); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /// <summary> |
| 215 | /// Gets an enumeration over all strings in the table. |
| 216 | /// </summary> |
| 217 | /// <returns>Enumeration of string name and value pairs</returns> |
| 218 | public IEnumerator<KeyValuePair<string, string>> GetEnumerator() |
| 219 | { |
| 220 | foreach (VersionInfo verValue in this.rawStringVersionInfo) |
| 221 | { |
| 222 | yield return new KeyValuePair<string, string>(verValue.Key, Encoding.Unicode.GetString(verValue.Data, 0, verValue.Data.Length - 2)); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | IEnumerator IEnumerable.GetEnumerator() |
| 227 | { |
| 228 | return this.GetEnumerator(); |
| 229 | } |
| 230 | } |
| 231 | } |