Merge "[2.2.1] Apply reviewed header file (Base to Collection)" into tizen_2.2
[platform/framework/native/appfw.git] / src / server / system / setting / providers / FSys_Icu.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FSys_Icu.cpp
19  * @brief               This is the implementation file for _Icu class.
20  */
21
22 #include <stdlib.h>
23 #include <memory.h>
24 #include <runtime_info.h>
25
26 #include <FBaseSysLog.h>
27 #include <FBase_NativeError.h>
28 #include "FSys_Icu.h"
29
30 namespace Tizen { namespace System
31 {
32
33 _Icu::_Icu()
34 {
35 }
36
37 _Icu::~_Icu()
38 {
39 }
40
41 char*
42 _Icu::GetIcuPatternN(const char* pLocale, UChar* pCustomSkeleton)
43 {
44         UErrorCode status = U_ZERO_ERROR;
45         UDateTimePatternGenerator* pGenerator = null;
46         UChar bestPattern[64] = {0,};
47
48         if(pLocale == null || pCustomSkeleton == null)
49         {
50                 return null;
51         }
52
53         char*   pBestPatternString = null;
54         int32_t bestPatternCapacity = 0;
55         int32_t bestPatternLength = 0;
56
57         pGenerator = udatpg_open(pLocale, &status);
58
59         if (U_FAILURE(status) == true)
60         {
61                 return null;
62         }
63
64         status = U_ZERO_ERROR;
65
66         bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
67         bestPatternLength = udatpg_getBestPattern(pGenerator, pCustomSkeleton, u_strlen(pCustomSkeleton), bestPattern, bestPatternCapacity, &status);
68         if (U_FAILURE(status) == true)
69         {
70                 return null;
71         }
72
73         status = U_ZERO_ERROR;
74         pBestPatternString = (char*)malloc(128);
75         if (pBestPatternString == null)
76         {
77                 return null;
78         }
79         memset(pBestPatternString, 0, 128);
80         u_austrncpy(pBestPatternString, bestPattern, 128);
81         udatpg_close(pGenerator);
82         SysLog(NID_SYS, "Format is [%s] \n", pBestPatternString);
83         return pBestPatternString;
84 }
85
86 char*
87 _Icu::GetDateTimeFormatN(const char* pIcuFormat)
88 {
89         char* pFormatString = null;
90         char* pDateTimeFormat = null;
91
92         if (pIcuFormat == null)
93         {
94                 return null;
95         }
96
97         UChar   customSkeleton[256] = {0, };
98         char* pRegionPtr;
99         if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
100         {
101                 u_uastrncpy(customSkeleton, pIcuFormat, sizeof(customSkeleton));
102                 pFormatString = GetIcuPatternN(pRegionPtr, customSkeleton);
103                 free(pRegionPtr);
104         }
105         return pFormatString;
106 }
107
108 } } // Tizen::System