/[gpgoe]/trunk/src/OEMisc.c
ViewVC logotype

Contents of /trunk/src/OEMisc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Fri Mar 24 13:36:54 2006 UTC (19 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 3623 byte(s)
Initial checkin of the GPGOE code.


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 /* Convert the UTF8 string @string into the native charset. */
34 char*
35 utf8_to_native (const char *string)
36 {
37 wchar_t *result;
38 char *native;
39 int n;
40
41 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
42 if (n < 0)
43 return NULL;
44
45 result = (wchar_t*)malloc ((n+1) * sizeof *result);
46 if (!result)
47 abort ();
48
49 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
50 if (n < 0) {
51 free (result);
52 return NULL;
53 }
54
55 n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL);
56 if (n < 0)
57 return NULL;
58
59 native = (char*)malloc (n+1);
60 if (!native)
61 abort ();
62
63 n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
64 if (n < 0) {
65 free (result);
66 return NULL;
67 }
68
69 free (result);
70 return native;
71 }
72
73
74 /* Return the clipboard contents as a string or NULL
75 if the clipboard does not contain text. */
76 char*
77 get_clip_text (HWND hwnd)
78 {
79 HANDLE clipmem;
80 char *cliptxt, *p;
81 int len;
82
83 if (OpenClipboard (hwnd) == FALSE)
84 return NULL;
85 clipmem = GetClipboardData (CF_TEXT);
86 if (clipmem == NULL) {
87 p = NULL;
88 goto leave;
89 }
90 cliptxt = (char *) GlobalLock (clipmem);
91 if (cliptxt == NULL) {
92 p = NULL;
93 goto leave;
94 }
95
96 len = strlen (cliptxt);
97 p = xcalloc (1, len+1);
98 memcpy (p, cliptxt, len);
99 p[len] = '\0';
100 GlobalUnlock (clipmem);
101
102 leave:
103 CloseClipboard ();
104 return p;
105 }
106
107
108 /* Set @text as the new clipboard content. */
109 int
110 set_clip_text (HWND hwnd, const char *text, int nbytes)
111 {
112 HANDLE clipmem;
113 int rc = 0;
114 char *p;
115
116 if (OpenClipboard (hwnd) == FALSE)
117 return -1;
118 EmptyClipboard ();
119
120 clipmem = GlobalAlloc (GHND, nbytes + 1);
121 if (clipmem == NULL)
122 abort ();
123 p = (char *) GlobalLock (clipmem);
124 if (p == NULL) {
125 rc = -1;
126 goto leave;
127 }
128 memcpy (p, text, nbytes);
129 p[nbytes] = '\0';
130
131 GlobalUnlock (clipmem);
132 SetClipboardData (CF_TEXT, clipmem);
133 GlobalFree (clipmem);
134
135 leave:
136 CloseClipboard ();
137 return 0;
138 }
139
140
141 /* Safe wrapper for calloc. */
142 void*
143 xcalloc (size_t n, size_t m)
144 {
145 void *p = calloc (n, m);
146 if (!p)
147 abort ();
148 return p;
149 }
150
151
152 /* Safe wrapper for strdup. */
153 char*
154 xstrdup (const char *s)
155 {
156 char *p = strdup (s);
157 if (!p)
158 abort ();
159 return p;
160 }
161
162
163 /* printf style message box. */
164 void
165 show_error (HWND hwnd, const char *caption, UINT type, const char *fmt, ...)
166 {
167 char buffer[1024];
168 va_list ptr;
169
170 va_start (ptr, fmt);
171 _vsnprintf (buffer, sizeof (buffer)-1, fmt, ptr);
172 va_end (ptr);
173 MessageBox (hwnd, buffer, caption, type);
174 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26