[Applications.Common] Fix CultureInfoHelper class (#4268)
authorhjhun <36876573+hjhun@users.noreply.github.com>
Tue, 17 May 2022 02:26:07 +0000 (11:26 +0900)
committerGitHub <noreply@github.com>
Tue, 17 May 2022 02:26:07 +0000 (11:26 +0900)
* Fix CultureInfoHelper class

- Use iniparser

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
* Fix a wrong implementation about converting to string from IntPtr

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
* Remove unnecessary debugging logs

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/Tizen.Applications.Common/Interop/Interop.IniParser.cs [new file with mode: 0755]
src/Tizen.Applications.Common/Interop/Interop.Libraries.cs
src/Tizen.Applications.Common/Tizen.Applications/CultureInfoHelper.cs

diff --git a/src/Tizen.Applications.Common/Interop/Interop.IniParser.cs b/src/Tizen.Applications.Common/Interop/Interop.IniParser.cs
new file mode 100755 (executable)
index 0000000..b472bc2
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+    internal static partial class LibIniParser
+    {
+        [DllImport(Libraries.IniParser, EntryPoint = "iniparser_getstring", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern IntPtr GetString(IntPtr d, string key, IntPtr def);
+
+        [DllImport(Libraries.IniParser, EntryPoint = "iniparser_load", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern IntPtr Load(string iniName);
+
+        [DllImport(Libraries.IniParser, EntryPoint = "iniparser_freedict", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern void FreeDict(IntPtr d);
+    }
+}
\ No newline at end of file
index 000e034..9b1e33f 100755 (executable)
@@ -29,5 +29,6 @@ internal static partial class Interop
         public const string Application = "libcapi-appfw-application.so.0";
         public const string BaseUtilsi18n = "libbase-utils-i18n.so.0";
         public const string RpcPort = "librpc-port.so.1";
+        public const string IniParser = "libiniparser.so.1";
     }
 }
index 3b92796..3bd8e98 100755 (executable)
  */
 
 using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.IO;
-using System.Xml;
-using Tizen;
+using System.Runtime.InteropServices;
 
 namespace Tizen.Applications
 {
     internal static class CultureInfoHelper
     {
-        private const string LogTag = "Tizen.Applications";
-        private static bool _initialized = false;
-        private static readonly Dictionary<string, string> _cultureNames = new Dictionary<string, string>();
-        private static readonly object _lock = new object();
-        private const string _pathCultureInfoXml = "/usr/share/dotnet.tizen/framework/i18n/CultureInfo.xml";
+        private const string _pathCultureInfoIni = "/usr/share/dotnet.tizen/framework/i18n/CultureInfo.ini";
 
-        public static void Initialize()
-        {
-            if (File.Exists(_pathCultureInfoXml))
-            {
-                try
-                {
-                    ParseCultureInfoXml();
-                }
-                catch
-                {
-                    Log.Warn(LogTag, "Failed to parse CultureInfo.xml");
-                }
-            }
-
-            _initialized = true;
-        }
-
-        private static void ParseCultureInfoXml()
+        public static string GetCultureName(string locale)
         {
-            XmlDocument doc = new XmlDocument();
-            doc.Load(_pathCultureInfoXml);
-            XmlElement root = doc.DocumentElement;
-            foreach (XmlElement node in root.ChildNodes)
+            IntPtr dictionary = Interop.LibIniParser.Load(_pathCultureInfoIni);
+            if (dictionary == IntPtr.Zero)
             {
-                if (node.Name != "name" && node.FirstChild == null)
-                {
-                    continue;
-                }
-
-                string lang = node.GetAttribute("xml:lang");
-                string cultureName = node.FirstChild.Value;
-                if (!string.IsNullOrEmpty(lang) && !string.IsNullOrEmpty(cultureName))
-                {
-                    try
-                    {
-                        _cultureNames.Add(lang, cultureName);
-                    }
-                    catch (ArgumentException e)
-                    {
-                        Log.Error(LogTag, "ArgumentException: " + e.Message);
-                    }
-                }
+                return string.Empty;
             }
-        }
 
-        public static string GetCultureName(string locale)
-        {
-            lock (_lock)
+            string cultureName = string.Empty;
+            string key = "CultureInfo:" + locale.ToLowerInvariant();
+            IntPtr value = Interop.LibIniParser.GetString(dictionary, key, IntPtr.Zero);
+            if (value != IntPtr.Zero)
             {
-                if (!_initialized)
-                {
-                    Initialize();
-                }
-
-                if (_cultureNames.TryGetValue(locale.ToLowerInvariant(), out string cultureName))
-                {
-                    return cultureName;
-                }
+                cultureName = Marshal.PtrToStringAnsi(value);
             }
 
-            return string.Empty;
+            Interop.LibIniParser.FreeDict(dictionary);
+            return cultureName;
         }
     }
 }