summaryrefslogtreecommitdiff
path: root/src/libio/mpu-genops.c
blob: ab001305a250690b21a3aa1ea80f64bb98d106d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/***************************************************************
  MPU-GENOPS.C

       This file contains generic stream dispatch operations.

       PART OF : MPUIO - library .

       USAGE   : Internal only .

       NOTE    : Backend-specific operations are selected through
                 the FILE_plus jump table.

       Copyright (C) 2000 - 2026  by Andrey V.Kosteltsev.
       All Rights Reserved.
 ***************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libmpuio.h>
#include <mpu-symbols.h>
#include <mpu-libio.h>


/***************************************************************
  Finish backend-specific stream state before close/destruction.
 ***************************************************************/
int __mpu_IO_finish_unlocked( mpu_FILE *stream )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->finish == (int (*)(mpu_FILE *))0 )
    return( 0 );

  return( (*__MPU_IO_JUMPS( stream )->finish)( stream ) );

} /* End of __mpu_IO_finish_unlocked() */


/***************************************************************
  Dispatch stream buffer configuration through the backend.
 ***************************************************************/
int __mpu_IO_setbuf_unlocked( mpu_FILE *stream, unsigned char *buf,
                              int mode, size_t size )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->setbuf ==
         (int (*)(mpu_FILE *, unsigned char *, int, size_t))0 )
  {
    errno = EINVAL;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( -1 );
  }

  return( (*__MPU_IO_JUMPS( stream )->setbuf)( stream, buf, mode, size ) );

} /* End of __mpu_IO_setbuf_unlocked() */


/***************************************************************
  Seek relative to an origin and return the resulting position.
 ***************************************************************/
off_t __mpu_IO_seekoff_unlocked( mpu_FILE *stream,
                                  off_t offset, int whence )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->seekoff ==
         (off_t (*)(mpu_FILE *, off_t, int))0 )
  {
    errno = ESPIPE;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( (off_t)-1 );
  }

  return( (*__MPU_IO_JUMPS( stream )->seekoff)( stream, offset, whence ) );

} /* End of __mpu_IO_seekoff_unlocked() */


/***************************************************************
  Seek to an absolute stream position and return that position.
 ***************************************************************/
off_t __mpu_IO_seekpos_unlocked( mpu_FILE *stream, off_t position )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->seekpos ==
         (off_t (*)(mpu_FILE *, off_t))0 )
  {
    errno = ESPIPE;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( (off_t)-1 );
  }

  return( (*__MPU_IO_JUMPS( stream )->seekpos)( stream, position ) );

} /* End of __mpu_IO_seekpos_unlocked() */


/***************************************************************
  Dispatch an input-buffer underflow operation through the backend.
 ***************************************************************/
int __mpu_IO_underflow_unlocked( mpu_FILE *stream )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->underflow == (int (*)(mpu_FILE *))0 )
  {
    errno = EINVAL;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  return( (*__MPU_IO_JUMPS( stream )->underflow)( stream ) );

} /* End of __mpu_IO_underflow_unlocked() */


/***************************************************************
  Dispatch an output-buffer overflow operation through the backend.
 ***************************************************************/
int __mpu_IO_overflow_unlocked( mpu_FILE *stream, int c )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->overflow ==
         (int (*)(mpu_FILE *, int))0 )
  {
    errno = EINVAL;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  return( (*__MPU_IO_JUMPS( stream )->overflow)( stream, c ) );

} /* End of __mpu_IO_overflow_unlocked() */


/***************************************************************
  Dispatch a stream synchronization operation through the backend.
 ***************************************************************/
int __mpu_IO_sync_unlocked( mpu_FILE *stream )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->sync == (int (*)(mpu_FILE *))0 )
    return( 0 );

  return( (*__MPU_IO_JUMPS( stream )->sync)( stream ) );

} /* End of __mpu_IO_sync_unlocked() */


/***************************************************************
  Dispatch the legacy flush operation through the backend.
 ***************************************************************/
int __mpu_IO_flush_unlocked( mpu_FILE *stream )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->flush == (int (*)(mpu_FILE *))0 )
    return( 0 );

  return( (*__MPU_IO_JUMPS( stream )->flush)( stream ) );

} /* End of __mpu_IO_flush_unlocked() */


/***************************************************************
  Read one raw byte through the selected stream backend.
 ***************************************************************/
int __mpu_IO_get_byte_unlocked( mpu_FILE *stream )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->get_byte == (int (*)(mpu_FILE *))0 )
  {
    errno = EINVAL;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  return( (*__MPU_IO_JUMPS( stream )->get_byte)( stream ) );

} /* End of __mpu_IO_get_byte_unlocked() */


/***************************************************************
  Write one raw byte through the selected stream backend.
 ***************************************************************/
int __mpu_IO_put_byte_unlocked( unsigned char c, mpu_FILE *stream )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->put_byte ==
         (int (*)(unsigned char, mpu_FILE *))0 )
  {
    errno = EINVAL;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  return( (*__MPU_IO_JUMPS( stream )->put_byte)( c, stream ) );

} /* End of __mpu_IO_put_byte_unlocked() */


/***************************************************************
  Read a raw byte block through the stream jump table.  Backends
  without xsgetn support fall back to get_byte operations.
 ***************************************************************/
size_t __mpu_IO_xsgetn_unlocked( mpu_FILE *stream,
                                 void *data, size_t size )
{
  unsigned char  *p = (unsigned char *)data;
  size_t          done = 0;

  if( __MPU_IO_JUMPS( stream ) != (const struct __mpu_IO_jump_t *)0 &&
      __MPU_IO_JUMPS( stream )->xsgetn !=
         (size_t (*)(mpu_FILE *, void *, size_t))0 )
    return( (*__MPU_IO_JUMPS( stream )->xsgetn)( stream, data, size ) );

  while( done < size )
  {
    int  c = __mpu_IO_get_byte_unlocked( stream );

    if( c == mpu_EOF ) break;
    p[done++] = (unsigned char)c;
  }

  return( done );

} /* End of __mpu_IO_xsgetn_unlocked() */


/***************************************************************
  Write a raw byte block through the stream jump table.  Backends
  without xsputn support fall back to put_byte operations.
 ***************************************************************/
size_t __mpu_IO_xsputn_unlocked( mpu_FILE *stream,
                                 const void *data, size_t size )
{
  const unsigned char  *p = (const unsigned char *)data;
  size_t                done = 0;

  if( __MPU_IO_JUMPS( stream ) != (const struct __mpu_IO_jump_t *)0 &&
      __MPU_IO_JUMPS( stream )->xsputn !=
         (size_t (*)(mpu_FILE *, const void *, size_t))0 )
    return( (*__MPU_IO_JUMPS( stream )->xsputn)( stream, data, size ) );

  while( done < size )
  {
    if( __mpu_IO_put_byte_unlocked( p[done], stream ) == mpu_EOF )
      break;
    ++done;
  }

  return( done );

} /* End of __mpu_IO_xsputn_unlocked() */


/***************************************************************
  Return one UCS-2 character from STREAM.  Characters saved by
  __mpu_IO_unget_char_unlocked() are returned before the backend.
 ***************************************************************/
int __mpu_IO_get_char_unlocked( mpu_FILE *stream, __mpu_char16_t *wc )
{
  if( stream->_unget_count )
  {
    *wc = stream->_unget_chars[--stream->_unget_count];
    stream->_flags &= ~__MPU_IO_EOF_SEEN;
    return( 0 );
  }

  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->get_char ==
         (int (*)(mpu_FILE *, __mpu_char16_t *))0 )
  {
    errno = EINVAL;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  return( (*__MPU_IO_JUMPS( stream )->get_char)( stream, wc ) );

} /* End of __mpu_IO_get_char_unlocked() */


/***************************************************************
  Push one UCS-2 character back to STREAM.  The internal stack
  permits scanner rollback of short prefixes/exponents; callers
  may rely on at least one character of public mpu_ungetc().
 ***************************************************************/
int __mpu_IO_unget_char_unlocked( __mpu_char16_t wc, mpu_FILE *stream )
{
  if( stream->_unget_count >=
      sizeof(stream->_unget_chars) / sizeof(stream->_unget_chars[0]) )
  {
    errno = ENOSPC;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  if( wc >= (__mpu_char16_t)0xd800 && wc <= (__mpu_char16_t)0xdfff )
  {
    errno = EILSEQ;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  stream->_unget_chars[stream->_unget_count++] = wc;
  stream->_flags &= ~__MPU_IO_EOF_SEEN;

  return( (int)wc );

} /* End of __mpu_IO_unget_char_unlocked() */


/***************************************************************
  Write one UCS-2 character through the selected stream backend.
 ***************************************************************/
int __mpu_IO_put_char_unlocked( __mpu_char16_t wc, mpu_FILE *stream )
{
  if( __MPU_IO_JUMPS( stream ) == (const struct __mpu_IO_jump_t *)0 ||
      __MPU_IO_JUMPS( stream )->put_char ==
         (int (*)(__mpu_char16_t, mpu_FILE *))0 )
  {
    errno = EINVAL;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  return( (*__MPU_IO_JUMPS( stream )->put_char)( wc, stream ) );

} /* End of __mpu_IO_put_char_unlocked() */

/***************************************************************
  Hide internal symbols:
 ***************************************************************/

__mpu_hidden_decl(__mpu_IO_finish_unlocked);
__mpu_hidden_decl(__mpu_IO_flush_unlocked);
__mpu_hidden_decl(__mpu_IO_get_byte_unlocked);
__mpu_hidden_decl(__mpu_IO_get_char_unlocked);
__mpu_hidden_decl(__mpu_IO_overflow_unlocked);
__mpu_hidden_decl(__mpu_IO_put_byte_unlocked);
__mpu_hidden_decl(__mpu_IO_put_char_unlocked);
__mpu_hidden_decl(__mpu_IO_seekoff_unlocked);
__mpu_hidden_decl(__mpu_IO_seekpos_unlocked);
__mpu_hidden_decl(__mpu_IO_setbuf_unlocked);
__mpu_hidden_decl(__mpu_IO_sync_unlocked);
__mpu_hidden_decl(__mpu_IO_underflow_unlocked);
__mpu_hidden_decl(__mpu_IO_unget_char_unlocked);
__mpu_hidden_decl(__mpu_IO_xsgetn_unlocked);
__mpu_hidden_decl(__mpu_IO_xsputn_unlocked);

/*
  End of hide internal symbols.
 ***************************************************************/

/* End of mpu-genops.c */