/[winpt]/trunk/Src/wptFileCBS.cpp
ViewVC logotype

Annotation of /trunk/Src/wptFileCBS.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations)
Wed Oct 12 10:04:26 2005 UTC (19 years, 4 months ago) by twoaday
File size: 3363 byte(s)
First testing phase finished.
Provide bug fixes for a lot of (minor) problems.

1 twoaday 24 /* wptFileCBS.cpp
2     * Copyright (C) 2001-2005 Timo Schulz
3     * Copyright (C) 2005 g10 Code GmbH
4     *
5     * This file is part of WinPT.
6     *
7     * WinPT is free software; you can redistribute it and/or
8     * modify it under the terms of the GNU General Public License
9     * as published by the Free Software Foundation; either version 2
10     * of the License, or (at your option) any later version.
11     *
12     * WinPT is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     * General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with WinPT; if not, write to the Free Software Foundation,
19     * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     */
21     #include <windows.h>
22     #include <stdio.h>
23     #include <malloc.h>
24     #include <errno.h>
25     #include <stdlib.h>
26 twoaday 25 #include <sys/stat.h>
27 twoaday 24
28 twoaday 25 #include "gpgme.h"
29 twoaday 24 #include "wptListView.h"
30     #include "wptGPG.h"
31     #include "wptFileManager.h"
32 twoaday 25 #include "wptErrors.h"
33     #include "wptTypes.h"
34 twoaday 24
35 twoaday 25 void progress_callback (void *opaque, const char *what, int type, int off, int max);
36    
37    
38 twoaday 24 /* Predefined read callback. */
39     static long
40     read_cb (void *handle, void *buffer, size_t size)
41     {
42     file_data_t cb = (file_data_t)handle;
43 twoaday 25 struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value;
44     int n = fread (buffer, 1, size, cb->handle);
45    
46     /* XXX: there is a sync problem with the progress dialog. */
47     if (pfx)
48     progress_callback (pfx, NULL, 0, cb->off, cb->size);
49     cb->off += n;
50     return n;
51 twoaday 24 }
52    
53    
54     /* Predefined write callback. */
55     static long
56     write_cb (void *handle, const void *buffer, size_t size)
57     {
58     file_data_t cb = (file_data_t)handle;
59 twoaday 25 int n = fwrite (buffer, 1, size, cb->handle);
60    
61     return n;
62 twoaday 24 }
63    
64    
65     /* Create a new data -> file association with a static callback.
66     @fname is the file which is associated to the object.
67     @r_cb is the context which holds all information.
68     @for_read is 1 if the file is opened for read only.
69     Return value: 0 on success. */
70     gpgme_error_t
71     gpg_file_data_new (const char *fname, int for_read, file_data_t *r_cb)
72    
73     {
74     gpgme_error_t err;
75     file_data_t cb;
76     FILE *f;
77    
78     f = fopen (fname, for_read?"rb" : "wb");
79     if (!f)
80     return gpgme_err_code_from_errno (errno);
81    
82     cb = (file_data_t)calloc (1, sizeof *cb);
83     if (!cb)
84     abort ();
85     cb->cbs.read = read_cb;
86     cb->cbs.write = write_cb;
87     cb->handle = f;
88 twoaday 25 if (for_read) {
89     struct stat st;
90     if (fstat (fileno (f), &st))
91     BUG (NULL);
92     cb->size = st.st_size;
93     cb->off = 0;
94     }
95 twoaday 24
96     err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb);
97     if (err) {
98     fclose (f);
99     free (cb);
100     return err;
101     }
102    
103     *r_cb = cb;
104     return err;
105     }
106    
107    
108 twoaday 25 /* Activate the progress callback for the given object @ctx. */
109     void
110     gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx)
111     {
112     ctx->cb_value = (void*)pfx;
113     }
114    
115    
116 twoaday 24 /* Release the context in @cb. Close all internal handles if possible. */
117     void
118     gpg_file_data_release (file_data_t cb)
119     {
120     if (!cb)
121     return;
122     if (cb->handle) {
123     FILE *f = (FILE *)cb->handle;
124     fclose (f);
125     }
126     if (cb->dat)
127     gpgme_data_release (cb->dat);
128     free (cb);
129     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26