Hopefully last changes for building with MSC.
[gpgme.git] / src / data-stream.c
1 /* data-stream.c - A stream based data object.
2    Copyright (C) 2002, 2004 g10 Code GmbH
3  
4    This file is part of GPGME.
5  
6    GPGME is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10    
11    GPGME is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15    
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #ifdef HAVE_SYS_TYPES_H
27 # include <sys/types.h>
28 #endif
29
30 #include "debug.h"
31 #include "data.h"
32
33 \f
34 static ssize_t
35 stream_read (gpgme_data_t dh, void *buffer, size_t size)
36 {
37   size_t amt = fread (buffer, 1, size, dh->data.stream);
38   if (amt > 0)
39     return amt;
40   return ferror (dh->data.stream) ? -1 : 0;
41 }
42
43
44 static ssize_t
45 stream_write (gpgme_data_t dh, const void *buffer, size_t size)
46 {
47   size_t amt = fwrite (buffer, 1, size, dh->data.stream);
48   if (amt > 0)
49     return amt;
50   return ferror (dh->data.stream) ? -1 : 0;
51 }
52
53
54 static off_t
55 stream_seek (gpgme_data_t dh, off_t offset, int whence)
56 {
57   int err;
58
59 #ifdef HAVE_FSEEKO
60   err = fseeko (dh->data.stream, offset, whence);
61 #else
62   /* FIXME: Check for overflow, or at least bail at compilation.  */
63   err = fseek (dh->data.stream, offset, whence);
64 #endif
65
66   if (err)
67     return -1;
68
69 #ifdef HAVE_FSEEKO
70   return ftello (dh->data.stream);
71 #else
72   return ftell (dh->data.stream);
73 #endif
74 }
75
76
77 static int
78 stream_get_fd (gpgme_data_t dh)
79 {
80   fflush (dh->data.stream);
81   return fileno (dh->data.stream);
82 }
83
84
85 static struct _gpgme_data_cbs stream_cbs =
86   {
87     stream_read,
88     stream_write,
89     stream_seek,
90     NULL,
91     stream_get_fd
92   };
93
94 \f
95 gpgme_error_t
96 gpgme_data_new_from_stream (gpgme_data_t *r_dh, FILE *stream)
97 {
98   gpgme_error_t err;
99   TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_stream", r_dh, "stream=%p",
100               stream);
101
102   err = _gpgme_data_new (r_dh, &stream_cbs);
103   if (err)
104     return TRACE_ERR (err);
105
106   (*r_dh)->data.stream = stream;
107   return TRACE_SUC1 ("dh=%p", *r_dh);
108 }