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

Annotation of /trunk/src/OEMisc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations)
Mon Mar 27 12:47:50 2006 UTC (19 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 3599 byte(s)
cleanups.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26