FreeTDS API
stream.h
1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 2013 Frediano Ziglio
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 #ifndef _tdsguard_a5iqP6Ed7kDGP9L1OvOL7W_
21 #define _tdsguard_a5iqP6Ed7kDGP9L1OvOL7W_
22 
23 #ifndef _tdsguard_hfOrWb5znoUCWdBPoNQvqN_
24 #error Include tds.h first
25 #endif
26 
27 #include <stdio.h> /* FILE * */
28 #include <freetds/tds/iconv.h> /* TDS_ICONV_DIRECTION */
29 #include <freetds/pushvis.h>
30 
31  /* Support 64-bit file seek if possible */
32 #ifdef HAVE_FSEEKO
33 typedef off_t offset_type;
34 #elif defined(_WIN32) || defined(_WIN64)
35 /* win32 version */
36 typedef __int64 offset_type;
37 
38 #if defined(HAVE__FSEEKI64) && defined(HAVE__FTELLI64)
39 #define fseeko(f,o,w) _fseeki64((f),o,w)
40 #define ftello(f) _ftelli64((f))
41 #else
42 #define fseeko(f,o,w) (_lseeki64(fileno(f),o,w) == -1 ? -1 : 0)
43 #define ftello(f) _telli64(fileno(f))
44 #endif
45 #else
46 /* use old version */
47 #define fseeko(f,o,w) fseek(f,o,w)
48 #define ftello(f) ftell(f)
49 typedef long offset_type;
50 #endif
51 
52 
54 typedef struct tds_input_stream
55 {
60  int (*read)(struct tds_input_stream *stream, void *ptr, size_t len);
61 } TDSINSTREAM;
62 
64 typedef struct tds_output_stream {
66  int (*write)(struct tds_output_stream *stream, size_t len);
75  char *buffer;
76  size_t buf_len;
77 } TDSOUTSTREAM;
78 
82 typedef struct tds_fileout_stream
83 {
84  TDSOUTSTREAM stream;
85  FILE *fp;
86  char block[4096];
89  int buff_mode;
91 
92 void tds_fileout_stream_init(TDSFILEOUTSTREAM * s, FILE * fp, int buff_mode);
93 
94 TDSRET tds_fileout_stream_flush(TDSFILEOUTSTREAM * s);
95 TDSRET tds_fileout_stream_put(TDSFILEOUTSTREAM * s, const void *src, size_t n);
96 
98 TDSRET tds_convert_stream(TDSSOCKET * tds, TDSICONV * char_conv, TDS_ICONV_DIRECTION direction,
99  TDSINSTREAM * istream, TDSOUTSTREAM *ostream);
101 TDSRET tds_copy_stream(TDSINSTREAM * istream, TDSOUTSTREAM * ostream);
102 
103 /* Additional streams */
104 
106 typedef struct tds_datain_stream {
107  TDSINSTREAM stream;
108  size_t wire_size;
109  TDSSOCKET *tds;
111 
112 void tds_datain_stream_init(TDSDATAINSTREAM * stream, TDSSOCKET * tds, size_t wire_size);
113 
115 typedef struct tds_dataout_stream {
116  TDSOUTSTREAM stream;
117  TDSSOCKET *tds;
118  size_t written;
120 
122 
124 typedef struct tds_staticin_stream {
125  TDSINSTREAM stream;
126  const char *buffer;
127  size_t buf_left;
129 
130 void tds_staticin_stream_init(TDSSTATICINSTREAM * stream, const void *ptr, size_t len);
131 
135 typedef struct tds_staticout_stream {
136  TDSOUTSTREAM stream;
138 
139 void tds_staticout_stream_init(TDSSTATICOUTSTREAM * stream, void *ptr, size_t len);
140 
142 typedef struct tds_dynamic_stream {
143  TDSOUTSTREAM stream;
145  void **buf;
147  size_t allocated;
149  size_t size;
151 
152 TDSRET tds_dynamic_stream_init(TDSDYNAMICSTREAM * stream, void **ptr, size_t allocated);
153 
154 /* input stream to read a file with option to terminate the read
155  * when a delimiter sequence is read.
156  *
157  * This structure may read more byte from the file than are consumed by this
158  * operation. This structure expects no other seek, read or write operations
159  * are performed on the file pointer while it's active in this structure.
160  */
161 enum
162 { TDSFILESTREAM_BLOCKSIZE = 512 };
163 
164 typedef struct tds_file_stream
165 {
168 
170  FILE *f;
171  offset_type offset;
172 
174  char cbuf[50];
175  size_t cpos;
176 
180  const char *terminator;
181  size_t term_len;
182 
183  /* Read buffering */
184  char inbuf[TDSFILESTREAM_BLOCKSIZE];
185  size_t inpos;
186  size_t inlen;
187 
188 } TDSFILESTREAM;
189 
190 TDSRET tds_file_stream_init(TDSFILESTREAM * stream, FILE * f);
191 
192 /* Read raw data from stream (no terminator or iconv). Return number of bytes read. */
193 size_t tds_file_stream_read_raw(TDSFILESTREAM * stream, void *ptr, size_t n);
194 TDSRET tds_file_stream_seek_set(TDSFILESTREAM * stream, offset_type seek_to);
195 offset_type tds_file_stream_tell(TDSFILESTREAM * stream);
196 TDSRET tds_file_stream_close(TDSFILESTREAM * stream);
197 
198 #include <freetds/popvis.h>
199 
200 #endif
Definition: stream.h:164
void ** buf
where is stored the pointer
Definition: stream.h:145
void tds_staticin_stream_init(TDSSTATICINSTREAM *stream, const void *ptr, size_t len)
Initialize an input stream for read from a static allocated buffer.
Definition: stream.c:286
size_t size
size of data inside buffer
Definition: stream.h:149
char * buffer
write buffer.
Definition: stream.h:75
TDSRET tds_copy_stream(TDSINSTREAM *istream, TDSOUTSTREAM *ostream)
Reads and writes from a stream to another.
Definition: stream.c:163
TDSRET tds_convert_stream(TDSSOCKET *tds, TDSICONV *char_conv, TDS_ICONV_DIRECTION direction, TDSINSTREAM *istream, TDSOUTSTREAM *ostream)
Reads and writes from a stream converting characters.
Definition: stream.c:71
const char * terminator
Terminator to compare against - Memory not owned by the TDSFILESTREAM; make sure to not leave danglin...
Definition: stream.h:180
void tds_datain_stream_init(TDSDATAINSTREAM *stream, TDSSOCKET *tds, size_t wire_size)
Initialize a data input stream.
Definition: stream.c:204
TDSINSTREAM stream
common fields, must be the first field
Definition: stream.h:167
size_t allocated
currently allocated buffer
Definition: stream.h:147
int(* read)(struct tds_input_stream *stream, void *ptr, size_t len)
read some data Return 0 if end of stream Return <0 if error (actually not defined) ...
Definition: stream.h:60
input stream to read data from tds protocol
Definition: stream.h:106
define a stream of data used for input
Definition: stream.h:54
void tds_staticout_stream_init(TDSSTATICOUTSTREAM *stream, void *ptr, size_t len)
Initialize an output stream for write into a static allocated buffer.
Definition: stream.c:313
input stream to read data from a static buffer
Definition: stream.h:124
Information for a server connection.
Definition: tds.h:1189
size_t wire_size
bytes still to read
Definition: stream.h:108
Output stream that writes to file.
Definition: stream.h:82
void tds_dataout_stream_init(TDSDATAOUTSTREAM *stream, TDSSOCKET *tds)
Initialize a data output stream.
Definition: stream.c:244
Definition: iconv.h:97
int buff_mode
How to buffer the data – _IONBF, _IOLBF or _IOFBF (default).
Definition: stream.h:89
output stream to write data to a dynamic buffer
Definition: stream.h:142
output stream to write data to tds protocol
Definition: stream.h:115
TDSRET tds_dynamic_stream_init(TDSDYNAMICSTREAM *stream, void **ptr, size_t allocated)
Initialize a dynamic output stream.
Definition: stream.c:354
define a stream of data used for output
Definition: stream.h:64
FILE * f
file to read from
Definition: stream.h:170
output stream to write data to a static buffer.
Definition: stream.h:135