Fix file not found exception
authorChangGyu Choi <uppletaste@gmail.com>
Thu, 25 May 2023 05:00:15 +0000 (14:00 +0900)
committerChangGyu Choi <changyu.choi@samsung.com>
Thu, 25 May 2023 05:33:46 +0000 (14:33 +0900)
When ini file does not exist, LibIniParser.Load() makes error logs.
This patch is to remove this case.
After applying this patch, we can expect performance improvement by
loading the dictionary only once for the first time.

Signed-off-by: ChangGyu Choi <uppletaste@gmail.com>
src/Tizen.Applications.Common/Tizen.Applications/CultureInfoHelper.cs

index 3bd8e98..fa42c77 100755 (executable)
  */
 
 using System;
+using System.IO;
 using System.Runtime.InteropServices;
 
 namespace Tizen.Applications
 {
+
     internal static class CultureInfoHelper
     {
-        private const string _pathCultureInfoIni = "/usr/share/dotnet.tizen/framework/i18n/CultureInfo.ini";
-
-        public static string GetCultureName(string locale)
+        private class CultureInfoManager
         {
-            IntPtr dictionary = Interop.LibIniParser.Load(_pathCultureInfoIni);
-            if (dictionary == IntPtr.Zero)
+            private static CultureInfoManager _inst;
+            private const string _pathCultureInfoIni = "/usr/share/dotnet.tizen/framework/i18n/CultureInfo.ini";
+            private static IntPtr _dictionary = IntPtr.Zero;
+
+            public static CultureInfoManager GetInst()
             {
-                return string.Empty;
+                return _inst;
             }
 
-            string cultureName = string.Empty;
-            string key = "CultureInfo:" + locale.ToLowerInvariant();
-            IntPtr value = Interop.LibIniParser.GetString(dictionary, key, IntPtr.Zero);
-            if (value != IntPtr.Zero)
+            private CultureInfoManager()
             {
-                cultureName = Marshal.PtrToStringAnsi(value);
+                if (File.Exists(_pathCultureInfoIni))
+                {
+                    _dictionary = Interop.LibIniParser.Load(_pathCultureInfoIni);
+                }
             }
 
-            Interop.LibIniParser.FreeDict(dictionary);
-            return cultureName;
+            ~CultureInfoManager()
+            {
+                if (_dictionary != IntPtr.Zero)
+                {
+                    Interop.LibIniParser.FreeDict(_dictionary);
+                }
+            }
+
+            public string GetCultureName(string locale)
+            {
+                if (_dictionary == IntPtr.Zero)
+                {
+                    return string.Empty;
+                }
+
+                string cultureName = string.Empty;
+                string key = "CultureInfo:" + locale.ToLowerInvariant();
+                IntPtr value = Interop.LibIniParser.GetString(_dictionary, key, IntPtr.Zero);
+                if (value != IntPtr.Zero)
+                {
+                    cultureName = Marshal.PtrToStringAnsi(value);
+                }
+
+                return cultureName;
+            }
+        }
+
+        public static string GetCultureName(string locale)
+        {
+            return CultureInfoManager.GetInst().GetCultureName(locale);
         }
     }
 }