OSDN Git Service

Fix bugs of confirmation of erasing host settings.
[ffftp/ffftp.git] / msgutil.c
1 
2 #include <windows.h>
3
4 /**
5  * アドレス固定のWide文字列とそれに対応するUTF-8文字列を格納する構造体
6  */
7 typedef struct {
8         const wchar_t *ws;
9         char *u8s;
10         size_t u8size;
11 } StrPair;
12
13 static BOOL g_initialized = FALSE;
14 static CRITICAL_SECTION g_msgUtilLLock;
15
16 // Wide文字列 - UTF-8文字列 対応Map
17 static StrPair *pStrMap = NULL;
18
19 //! 現在有効なStrPairの数
20 static int strMapCount = 0;
21
22 //! 確保済みのStrPairの領域数
23 static int strMapMaxCount = 0;
24
25 /**
26  * staticなWide文字列に対応するUTF-8バイナリ文字列領域を確保し、その先頭アドレスを返す
27  */
28 const char* const MessageUtil_GetUTF8StaticBinaryBlock(const wchar_t* const ws, size_t ws_area_length)
29 {
30         int i = 0;
31         int wcsCount = 0;
32         int newSize = 0;
33         const char *pResult = NULL;
34         if (!g_initialized)
35         {
36                 InitializeCriticalSection(&g_msgUtilLLock);
37                 g_initialized = TRUE;
38         }
39         EnterCriticalSection(&g_msgUtilLLock);
40         for (i = 0; i < strMapCount; i++)
41         {
42                 if (pStrMap[i].ws == ws)
43                 {
44                         pResult = pStrMap[i].u8s;
45                         break;
46                 }
47         }
48         if (pResult == NULL)
49         {
50                 if (strMapMaxCount < strMapCount + 1)
51                 {
52                         // 領域が足りなくなったので追加する
53                         if (strMapMaxCount == 0)
54                         {
55                                 strMapMaxCount = 100;
56                                 pStrMap = (StrPair*)malloc(sizeof(StrPair) * strMapMaxCount);
57                         }
58                         else
59                         {
60                                 strMapMaxCount += 100;
61                                 pStrMap = (StrPair*)realloc(pStrMap, sizeof(StrPair) * strMapMaxCount);
62                         }
63                 }
64                 newSize = WideCharToMultiByte(CP_UTF8, 0, ws, ws_area_length, 0, 0, NULL, NULL);
65                 if (newSize > 0)
66                 {
67                         int index = strMapCount;
68                         char *beginPos = 0;
69                         int postSize = 0;
70                         strMapCount++;
71                         pStrMap[index].ws = ws;
72                         pStrMap[index].u8size = newSize;
73                         beginPos = (char*)malloc(newSize);
74                         pStrMap[index].u8s = beginPos;
75                         postSize = WideCharToMultiByte(CP_UTF8, 0, ws, ws_area_length, beginPos, newSize, NULL, NULL);
76                         pResult = beginPos;
77                 }
78                 else
79                 {
80                         static char sEmpty[] = "";
81                         pResult = sEmpty;
82                 }
83         }
84         LeaveCriticalSection(&g_msgUtilLLock);
85         return pResult;
86 }
87
88 /**
89  * MessageUtil_GetUTF8StaticBinaryBlock() で確保した領域をすべて破棄する
90  */
91 void MessageUtil_FreeUTF8StaticBinaryBlocks()
92 {
93         int i = 0;
94         if (!g_initialized)
95         {
96                 InitializeCriticalSection(&g_msgUtilLLock);
97                 g_initialized = TRUE;
98         }
99         EnterCriticalSection(&g_msgUtilLLock);
100         for (i = 0; i < strMapCount; i++)
101         {
102                 free(pStrMap[i].u8s);
103         }
104         if (pStrMap)
105         {
106                 free(pStrMap);
107                 pStrMap = (StrPair*)NULL;
108         }
109         LeaveCriticalSection(&g_msgUtilLLock);
110 }