1 |
twoaday |
1 |
/* OEMisc.c - OE misc functions |
2 |
|
|
* Copyright (C) 2006 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of GPGOE. |
5 |
|
|
* |
6 |
|
|
* GPGOE is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU Lesser General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2.1 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* GPGOE 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 Lesser General Public License |
17 |
|
|
* along with GPGOE; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
#include <windows.h> |
25 |
|
|
#include <stdio.h> |
26 |
|
|
#include <stdarg.h> |
27 |
|
|
#include <stdlib.h> |
28 |
|
|
|
29 |
|
|
#include "gpgme.h" |
30 |
|
|
#include "GPGOE.h" |
31 |
|
|
|
32 |
|
|
|
33 |
twoaday |
12 |
char* |
34 |
|
|
native_to_utf8 (const char *string) |
35 |
|
|
{ |
36 |
|
|
wchar_t *result; |
37 |
|
|
char *native; |
38 |
|
|
int n; |
39 |
|
|
|
40 |
|
|
n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0); |
41 |
|
|
if (n < 0) |
42 |
|
|
return NULL; |
43 |
|
|
|
44 |
|
|
result = (wchar_t*)xcalloc (1, (n+1) * sizeof *result); |
45 |
|
|
|
46 |
|
|
n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n); |
47 |
|
|
if (n < 0) { |
48 |
|
|
free (result); |
49 |
|
|
return NULL; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL); |
53 |
|
|
if (n < 0) |
54 |
|
|
return NULL; |
55 |
|
|
|
56 |
|
|
native = (char*)xcalloc (1, n+1); |
57 |
|
|
|
58 |
|
|
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL); |
59 |
|
|
if (n < 0) { |
60 |
|
|
free (result); |
61 |
|
|
return NULL; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
free (result); |
65 |
|
|
return native; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
|
69 |
twoaday |
1 |
/* Convert the UTF8 string @string into the native charset. */ |
70 |
|
|
char* |
71 |
|
|
utf8_to_native (const char *string) |
72 |
|
|
{ |
73 |
|
|
wchar_t *result; |
74 |
|
|
char *native; |
75 |
|
|
int n; |
76 |
|
|
|
77 |
|
|
n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0); |
78 |
|
|
if (n < 0) |
79 |
|
|
return NULL; |
80 |
|
|
|
81 |
twoaday |
10 |
result = (wchar_t*)xcalloc (1, (n+1) * sizeof *result); |
82 |
twoaday |
1 |
|
83 |
|
|
n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n); |
84 |
|
|
if (n < 0) { |
85 |
|
|
free (result); |
86 |
|
|
return NULL; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL); |
90 |
|
|
if (n < 0) |
91 |
|
|
return NULL; |
92 |
|
|
|
93 |
|
|
native = (char*)malloc (n+1); |
94 |
|
|
if (!native) |
95 |
|
|
abort (); |
96 |
|
|
|
97 |
|
|
n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL); |
98 |
|
|
if (n < 0) { |
99 |
|
|
free (result); |
100 |
|
|
return NULL; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
free (result); |
104 |
|
|
return native; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
|
108 |
|
|
/* Return the clipboard contents as a string or NULL |
109 |
|
|
if the clipboard does not contain text. */ |
110 |
|
|
char* |
111 |
|
|
get_clip_text (HWND hwnd) |
112 |
|
|
{ |
113 |
|
|
HANDLE clipmem; |
114 |
|
|
char *cliptxt, *p; |
115 |
|
|
int len; |
116 |
|
|
|
117 |
|
|
if (OpenClipboard (hwnd) == FALSE) |
118 |
|
|
return NULL; |
119 |
|
|
clipmem = GetClipboardData (CF_TEXT); |
120 |
|
|
if (clipmem == NULL) { |
121 |
|
|
p = NULL; |
122 |
|
|
goto leave; |
123 |
|
|
} |
124 |
|
|
cliptxt = (char *) GlobalLock (clipmem); |
125 |
|
|
if (cliptxt == NULL) { |
126 |
|
|
p = NULL; |
127 |
|
|
goto leave; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
len = strlen (cliptxt); |
131 |
|
|
p = xcalloc (1, len+1); |
132 |
|
|
memcpy (p, cliptxt, len); |
133 |
|
|
p[len] = '\0'; |
134 |
|
|
GlobalUnlock (clipmem); |
135 |
|
|
|
136 |
|
|
leave: |
137 |
|
|
CloseClipboard (); |
138 |
|
|
return p; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
/* Set @text as the new clipboard content. */ |
143 |
|
|
int |
144 |
|
|
set_clip_text (HWND hwnd, const char *text, int nbytes) |
145 |
|
|
{ |
146 |
|
|
HANDLE clipmem; |
147 |
|
|
char *p; |
148 |
|
|
|
149 |
|
|
if (OpenClipboard (hwnd) == FALSE) |
150 |
|
|
return -1; |
151 |
|
|
EmptyClipboard (); |
152 |
|
|
|
153 |
|
|
clipmem = GlobalAlloc (GHND, nbytes + 1); |
154 |
|
|
if (clipmem == NULL) |
155 |
|
|
abort (); |
156 |
|
|
p = (char *) GlobalLock (clipmem); |
157 |
|
|
if (p == NULL) { |
158 |
twoaday |
12 |
CloseClipboard (); |
159 |
|
|
GlobalFree (clipmem); |
160 |
|
|
return -1; |
161 |
twoaday |
1 |
} |
162 |
|
|
memcpy (p, text, nbytes); |
163 |
|
|
p[nbytes] = '\0'; |
164 |
|
|
|
165 |
twoaday |
12 |
SetClipboardData (CF_TEXT, clipmem); |
166 |
twoaday |
1 |
GlobalUnlock (clipmem); |
167 |
twoaday |
12 |
CloseClipboard (); |
168 |
twoaday |
1 |
GlobalFree (clipmem); |
169 |
|
|
|
170 |
|
|
return 0; |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
|
174 |
|
|
/* Safe wrapper for calloc. */ |
175 |
|
|
void* |
176 |
|
|
xcalloc (size_t n, size_t m) |
177 |
|
|
{ |
178 |
|
|
void *p = calloc (n, m); |
179 |
|
|
if (!p) |
180 |
|
|
abort (); |
181 |
|
|
return p; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
|
185 |
|
|
/* Safe wrapper for strdup. */ |
186 |
|
|
char* |
187 |
|
|
xstrdup (const char *s) |
188 |
|
|
{ |
189 |
|
|
char *p = strdup (s); |
190 |
|
|
if (!p) |
191 |
|
|
abort (); |
192 |
|
|
return p; |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
|
196 |
twoaday |
12 |
void |
197 |
|
|
xfree (void *p) |
198 |
|
|
{ |
199 |
|
|
if (p != NULL) |
200 |
|
|
free (p); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
|
204 |
twoaday |
1 |
/* printf style message box. */ |
205 |
|
|
void |
206 |
|
|
show_error (HWND hwnd, const char *caption, UINT type, const char *fmt, ...) |
207 |
|
|
{ |
208 |
|
|
char buffer[1024]; |
209 |
|
|
va_list ptr; |
210 |
|
|
|
211 |
|
|
va_start (ptr, fmt); |
212 |
|
|
_vsnprintf (buffer, sizeof (buffer)-1, fmt, ptr); |
213 |
|
|
va_end (ptr); |
214 |
|
|
MessageBox (hwnd, buffer, caption, type); |
215 |
|
|
} |