//----------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. // //----------------------------------------------------------------------- namespace Microsoft.Isam.Esent.Interop { using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; /// /// The native version of the JET_UNICODEINDEX2 structure. /// [StructLayout(LayoutKind.Sequential)] [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "This should match the unmanaged API, which isn't capitalized.")] [SuppressMessage( "Microsoft.StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter", Justification = "This should match the unmanaged API, which isn't capitalized.")] internal struct NATIVE_UNICODEINDEX2 { /// /// The locale to be used when normalizing unicode data. /// public IntPtr szLocaleName; /// /// The flags for LCMapString. /// public uint dwMapFlags; } /// /// Customizes how Unicode data gets normalized when an index is created over a Unicode column. /// public sealed partial class JET_UNICODEINDEX : IContentEquatable, IDeepCloneable { /// /// Contains a limited mapping of LCIDs to locale names. /// private static readonly Dictionary LcidToLocales = new Dictionary(10); /// /// Initializes static members of the JET_UNICODEINDEX class. /// static JET_UNICODEINDEX() { // Some common LCIDs are listed at http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx. LcidToLocales.Add(127, string.Empty); LcidToLocales.Add(1033, "en-us"); LcidToLocales.Add(1046, "pt-br"); LcidToLocales.Add(3084, "fr-ca"); } /// /// Initializes a new instance of the class. /// public JET_UNICODEINDEX() { } /// /// Initializes a new instance of the class. /// /// The native object from which to read values. internal JET_UNICODEINDEX(ref NATIVE_UNICODEINDEX2 native) { this.szLocaleName = Marshal.PtrToStringUni(native.szLocaleName); this.dwMapFlags = native.dwMapFlags; } /// /// As a workaround for systems that do not have LCID support, we will convert a VERY limited number of /// LCIDs to locale names. /// /// A BCP-47 style locale name. public string GetEffectiveLocaleName() { if (this.szLocaleName != null) { return this.szLocaleName; } return LimitedLcidToLocaleNameMapping(this.lcid); } /// /// As a workaround for systems that do not have LCID support, we will convert a VERY limited number of /// LCIDs to locale names. /// /// A locale identifier. /// A BCP-47 style locale name. internal static string LimitedLcidToLocaleNameMapping(int lcid) { string locale; LcidToLocales.TryGetValue(lcid, out locale); return locale; } /// /// Gets the native version of this object. /// /// The native version of this object. internal NATIVE_UNICODEINDEX2 GetNativeUnicodeIndex2() { if (this.lcid != 0 && !LcidToLocales.ContainsKey(this.lcid)) { throw new ArgumentException("lcid was specified, but this version of the API does not accept LCIDs. Use a locale name or a different API."); } ////szLocaleName is converted at pinvoke time. var native = new NATIVE_UNICODEINDEX2 { dwMapFlags = this.dwMapFlags, }; return native; } } }