1 |
#include "gd_io_stream.h" |
2 |
|
3 |
#ifdef __cplusplus |
4 |
|
5 |
/** Read into buffer from stream |
6 |
Return the number of bytes successfully read. |
7 |
If an error occurs, or the end-of-file is reached, the return value |
8 |
is a short byte count (or zero). |
9 |
*/ |
10 |
int istreamIOCtx::Getbuf (struct gdIOCtx * ctx, void * buf, int size) |
11 |
{ |
12 |
stream_type * _str = ( (istreamIOCtx * ) ctx )->_M_stream; |
13 |
_str->read((char * )buf, size); |
14 |
return _str->gcount(); |
15 |
} |
16 |
/** Write from buffer to stream |
17 |
Return the number of bytes successfully written. |
18 |
If an error occurs, or the end-of-file is reached, the return value |
19 |
is a short byte count (or zero). |
20 |
*/ |
21 |
int istreamIOCtx::Putbuf (struct gdIOCtx * , const void * , int ) |
22 |
{ |
23 |
return 0; |
24 |
} |
25 |
|
26 |
/** Reads the next character from stream and returns it as an |
27 |
unsigned char cast to an int, or EOF on end of file or error. |
28 |
*/ |
29 |
int istreamIOCtx::Getchar (struct gdIOCtx * ctx) |
30 |
{ |
31 |
stream_type * _str = ( (istreamIOCtx * ) ctx )->_M_stream; |
32 |
return _str->get(); |
33 |
} |
34 |
/** Write the character to stream |
35 |
Character is cast to unsigned char before writing |
36 |
*/ |
37 |
void istreamIOCtx::Putchar (struct gdIOCtx * , int ) |
38 |
{ |
39 |
} |
40 |
|
41 |
/** Seek to position offset from the beginning of the stream |
42 |
must return 1 on SUCCESS, 0 on FAILURE. Unlike fseek! |
43 |
*/ |
44 |
int istreamIOCtx::Seek (struct gdIOCtx * ctx, const int pos) |
45 |
{ |
46 |
stream_type * _str = ( (istreamIOCtx * ) ctx )->_M_stream; |
47 |
_str->seekg(pos); |
48 |
return !_str->fail(); |
49 |
} |
50 |
/** Obtains the current value of the stream position. |
51 |
Returns -1 on error. |
52 |
*/ |
53 |
long istreamIOCtx::Tell (struct gdIOCtx * ctx) |
54 |
{ |
55 |
stream_type * _str = ( (istreamIOCtx * ) ctx )->_M_stream; |
56 |
return _str->tellg(); |
57 |
} |
58 |
|
59 |
void istreamIOCtx::FreeCtx (struct gdIOCtx * ctx) |
60 |
{ |
61 |
delete (istreamIOCtx * )ctx; |
62 |
} |
63 |
|
64 |
/** Read into buffer from stream |
65 |
Return the number of bytes successfully read. |
66 |
If an error occurs, or the end-of-file is reached, the return value |
67 |
is a short byte count (or zero). |
68 |
*/ |
69 |
int ostreamIOCtx::Getbuf (struct gdIOCtx * , void * , int ) |
70 |
{ |
71 |
return 0; |
72 |
} |
73 |
/** Write from buffer to stream |
74 |
Return the number of bytes successfully written. |
75 |
If an error occurs, or the end-of-file is reached, the return value |
76 |
is a short byte count (or zero). |
77 |
*/ |
78 |
int ostreamIOCtx::Putbuf (struct gdIOCtx * ctx, const void * buf, int size) |
79 |
{ |
80 |
stream_type * _str = ( (ostreamIOCtx * ) ctx )->_M_stream; |
81 |
_str->write((const char * )buf, size); |
82 |
return _str->bad()?0:size; |
83 |
} |
84 |
|
85 |
/** Reads the next character from stream and returns it as an |
86 |
unsigned char cast to an int, or EOF on end of file or error. |
87 |
*/ |
88 |
int ostreamIOCtx::Getchar (struct gdIOCtx * ) |
89 |
{ |
90 |
return EOF; |
91 |
} |
92 |
/** Write the character to stream |
93 |
Character is cast to unsigned char before writing |
94 |
*/ |
95 |
void ostreamIOCtx::Putchar (struct gdIOCtx * ctx, int c) |
96 |
{ |
97 |
stream_type * _str = ( (ostreamIOCtx * ) ctx )->_M_stream; |
98 |
_str->put((char)c); |
99 |
} |
100 |
|
101 |
/** Seek to position offset from the beginning of the stream |
102 |
must return 1 on SUCCESS, 0 on FAILURE. Unlike fseek! |
103 |
*/ |
104 |
int ostreamIOCtx::Seek (struct gdIOCtx * ctx, const int pos) |
105 |
{ |
106 |
stream_type * _str = ( (ostreamIOCtx * ) ctx )->_M_stream; |
107 |
_str->seekp(pos); |
108 |
return !_str->fail(); |
109 |
} |
110 |
/** Obtains the current value of the stream position. |
111 |
Returns -1 on error. |
112 |
*/ |
113 |
long ostreamIOCtx::Tell (struct gdIOCtx * ctx) |
114 |
{ |
115 |
stream_type * _str = ( (ostreamIOCtx * ) ctx )->_M_stream; |
116 |
return _str->tellp(); |
117 |
} |
118 |
|
119 |
void ostreamIOCtx::FreeCtx (struct gdIOCtx * ctx) |
120 |
{ |
121 |
delete (ostreamIOCtx * )ctx; |
122 |
} |
123 |
|
124 |
#endif // __cplusplus |