/[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 48 by werner, Mon Oct 31 21:14:11 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 <stdio.h>
27    #include <malloc.h>
28  #include "gpgme.h"  #include <errno.h>
29  #include "wptListView.h"  #include <stdlib.h>
30  #include "wptGPG.h"  #include <sys/stat.h>
31  #include "wptFileManager.h"  
32  #include "wptErrors.h"  #include "gpgme.h"
33  #include "wptTypes.h"  #include "wptListView.h"
34    #include "wptGPG.h"
35  void progress_callback (void *opaque, const char *what, int type, int off, int max);  #include "wptFileManager.h"
36    #include "wptErrors.h"
37    #include "wptTypes.h"
38  /* Predefined read callback. */  
39  static long  void progress_callback (void *opaque, const char *what, int type, int off, int max);
40  read_cb (void *handle, void *buffer, size_t size)  
41  {  
42      file_data_t cb = (file_data_t)handle;  /* Predefined read callback. */
43      struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value;  static long
44      int n = fread (buffer, 1, size, cb->handle);  read_cb (void *handle, void *buffer, size_t size)
45    {
46      /* XXX: there is a sync problem with the progress dialog. */      file_data_t cb = (file_data_t)handle;
47      if (pfx)      struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value;
48          progress_callback (pfx, NULL, 0, cb->off, cb->size);      int n = fread (buffer, 1, size, cb->handle);
49      cb->off += n;  
50      return n;      /* XXX: there is a sync problem with the progress dialog. */
51  }      if (pfx)
52            progress_callback (pfx, NULL, 0, cb->off, cb->size);
53        cb->off += n;
54  /* Predefined write callback. */      return n;
55  static long  }
56  write_cb (void *handle, const void *buffer, size_t size)  
57  {  
58      file_data_t cb = (file_data_t)handle;  /* Predefined write callback. */
59      int n = fwrite (buffer, 1, size, cb->handle);  static long
60    write_cb (void *handle, const void *buffer, size_t size)
61      return n;  {
62  }      file_data_t cb = (file_data_t)handle;
63        int n = fwrite (buffer, 1, size, cb->handle);
64    
65  /* Create a new data -> file association with a static callback.      return n;
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. */  /* Create a new data -> file association with a static callback.
70  gpgme_error_t     @fname is the file which is associated to the object.
71  gpg_file_data_new (const char *fname, int for_read, file_data_t *r_cb)     @r_cb is the context which holds all information.
72                             @for_read is 1 if the file is opened for read only.
73  {     Return value: 0 on success. */
74      gpgme_error_t err;  gpgme_error_t
75      file_data_t cb;  gpg_file_data_new (const char *fname, int for_read, file_data_t *r_cb)
76      FILE *f;                        
77    {
78      f = fopen (fname, for_read?"rb" : "wb");      gpgme_error_t err;
79      if (!f)      file_data_t cb;
80          return gpgme_err_code_from_errno (errno);      FILE *f;
81    
82      cb = (file_data_t)calloc (1, sizeof *cb);      f = fopen (fname, for_read?"rb" : "wb");
83      if (!cb)      if (!f)
84          abort ();          return gpgme_err_code_from_errno (errno);
85      cb->cbs.read = read_cb;  
86      cb->cbs.write = write_cb;      cb = (file_data_t)calloc (1, sizeof *cb);
87      cb->handle = f;      if (!cb)
88      if (for_read) {          abort ();
89          struct stat st;      cb->cbs.read = read_cb;
90          if (fstat (fileno (f), &st))      cb->cbs.write = write_cb;
91              BUG (NULL);      cb->handle = f;
92          cb->size = st.st_size;      if (for_read) {
93          cb->off = 0;          struct stat st;
94      }          if (fstat (fileno (f), &st))
95                BUG (NULL);
96      err = gpgme_data_new_from_cbs  (&cb->dat, &cb->cbs, cb);          cb->size = st.st_size;
97      if (err) {          cb->off = 0;
98          fclose (f);      }
99          free (cb);  
100          return err;      err = gpgme_data_new_from_cbs  (&cb->dat, &cb->cbs, cb);
101      }      if (err) {
102            fclose (f);
103      *r_cb = cb;          free (cb);
104      return err;          return err;
105  }      }
106    
107        *r_cb = cb;
108  /* Activate the progress callback for the given object @ctx. */      return err;
109  void  }
110  gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx)  
111  {  
112      ctx->cb_value = (void*)pfx;  /* Activate the progress callback for the given object @ctx. */
113  }  void
114    gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx)
115    {
116  /* Release the context in @cb. Close all internal handles if possible. */      ctx->cb_value = (void*)pfx;
117  void  }
118  gpg_file_data_release (file_data_t cb)  
119  {  
120      if (!cb)  /* Release the context in @cb. Close all internal handles if possible. */
121          return;  void
122      if (cb->handle) {  gpg_file_data_release (file_data_t cb)
123          FILE *f = (FILE *)cb->handle;  {
124          fclose (f);      if (!cb)
125      }          return;
126      if (cb->dat)      if (cb->handle) {
127          gpgme_data_release (cb->dat);          FILE *f = (FILE *)cb->handle;
128      free (cb);          fclose (f);
129  }      }
130        if (cb->dat)
131            gpgme_data_release (cb->dat);
132        free (cb);
133    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26