GnuCash  5.6-150-g038405b370+
qof-win32.cpp
1 /*
2  * qof-win32.c
3  *
4  * Copyright (C) 2007 Andreas Koehler <andi5.py@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, contact:
18  *
19  * Free Software Foundation Voice: +1-617-542-5942
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21  * Boston, MA 02110-1301, USA gnu@gnu.org
22  */
23 
24 #include <glib.h>
25 
26 #include <config.h>
27 
28 #include "gnc-date-p.h"
29 #include "strptime.h"
30 #include <windows.h>
31 #include <stdlib.h>
32 
33 static GHashTable *picture_to_format = NULL;
34 G_LOCK_DEFINE_STATIC(picture_to_format);
35 
36 gchar *
37 qof_time_format_from_utf8(const gchar *utf8_format)
38 {
39  wchar_t* utf16_format;
40  gchar* retval;
41  gsize count;
42 
43  utf16_format = reinterpret_cast<wchar_t*>(g_utf8_to_utf16(utf8_format, -1,
44  NULL, NULL, NULL));
45  if (!utf16_format)
46  return NULL;
47 
48  /* get number of resulting wide characters */
49  count = wcstombs(NULL, utf16_format, 0);
50  if (count <= 0)
51  return NULL;
52 
53  /* malloc and convert */
54  retval = static_cast<gchar*>(g_malloc((count + 1) * sizeof(gchar)));
55  count = wcstombs(retval, utf16_format, count + 1);
56  g_free(utf16_format);
57  if (count <= 0)
58  {
59  g_free(retval);
60  return NULL;
61  }
62 
63  return retval;
64 }
65 
66 gchar *
67 qof_formatted_time_to_utf8(const gchar *locale_string)
68 {
69  gunichar2 *utf16_string;
70  gchar *retval;
71  gsize count;
72 
73  /* get number of resulting wide characters */
74  count = mbstowcs(NULL, locale_string, 0);
75  if (count <= 0)
76  return NULL;
77 
78  /* malloc and convert */
79  utf16_string = static_cast<gunichar2*>(g_malloc((count + 1) * sizeof(gunichar2)));
80  count = mbstowcs(reinterpret_cast<wchar_t*>(utf16_string),
81  locale_string, count + 1);
82  if (count <= 0)
83  {
84  g_free(utf16_string);
85  return NULL;
86  }
87 
88  retval = g_utf16_to_utf8(utf16_string, -1, NULL, NULL, NULL);
89  g_free(utf16_string);
90 
91  return retval;
92 }
93 
94 const char *
95 qof_win32_get_time_format(QofWin32Picture picture)
96 {
97  gchar *locale_string, *format;
98  gchar *tmp1, *tmp2;
99 
100  switch (picture)
101  {
102  case QOF_WIN32_PICTURE_DATE:
103  locale_string = get_win32_locale_string(LOCALE_SSHORTDATE);
104  break;
105  case QOF_WIN32_PICTURE_TIME:
106  locale_string = get_win32_locale_string(LOCALE_STIMEFORMAT);
107  break;
108  case QOF_WIN32_PICTURE_DATETIME:
109  tmp1 = get_win32_locale_string(LOCALE_SSHORTDATE);
110  tmp2 = get_win32_locale_string(LOCALE_STIMEFORMAT);
111  locale_string = g_strconcat(tmp1, " ", tmp2, NULL);
112  g_free(tmp1);
113  g_free(tmp2);
114  break;
115  default:
116  g_assert_not_reached();
117  }
118 
119  G_LOCK(picture_to_format);
120  if (!picture_to_format)
121  picture_to_format = g_hash_table_new_full(g_str_hash, g_str_equal,
122  NULL, g_free);
123  format = static_cast<char*>(g_hash_table_lookup(picture_to_format, locale_string));
124  if (!format)
125  {
126  format = translate_win32_picture(locale_string);
127  g_hash_table_insert(picture_to_format, g_strdup(locale_string), format);
128  }
129  G_UNLOCK(picture_to_format);
130  g_free(locale_string);
131 
132  return format;
133 }