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

Diff of /trunk/Src/wptFileCBS.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.25  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26