3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
12 long int ftell( struct _PDCLIB_file_t * stream )
14 /* ftell() must take into account:
15 - the actual *physical* offset of the file, i.e. the offset as recognized
16 by the operating system (and stored in stream->pos.offset); and
17 - any buffers held by PDCLib, which
18 - in case of unwritten buffers, count in *addition* to the offset; or
19 - in case of unprocessed pre-read buffers, count in *substraction* to
20 the offset. (Remember to count ungetidx into this number.)
21 Conveniently, the calculation ( ( bufend - bufidx ) + ungetidx ) results
22 in just the right number in both cases:
23 - in case of unwritten buffers, ( ( 0 - unwritten ) + 0 )
24 i.e. unwritten bytes as negative number
25 - in case of unprocessed pre-read, ( ( preread - processed ) + unget )
26 i.e. unprocessed bytes as positive number.
27 That is how the somewhat obscure return-value calculation works.
29 /* If offset is too large for return type, report error instead of wrong
32 /* TODO: Check what happens when ungetc() is called on a stream at offset 0 */
33 if ( ( stream->pos.offset - stream->bufend ) > ( LONG_MAX - ( stream->bufidx - stream->ungetidx ) ) )
35 /* integer overflow */
36 _PDCLIB_errno = _PDCLIB_ERANGE;
39 return (long int)( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + stream->ungetidx ) );
45 #include <_PDCLIB_test.h>
51 /* Testing all the basic I/O functions individually would result in lots
52 of duplicated code, so I took the liberty of lumping it all together
55 /* The following functions delegate their tests to here:
56 fgetc fflush rewind fputc ungetc fseek
57 flushbuffer seek fillbuffer prepread prepwrite
59 char * buffer = (char*)malloc( 4 );
61 TESTCASE( ( fh = tmpfile() ) != NULL );
62 TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
63 /* Testing ungetc() at offset 0 */
65 TESTCASE( ungetc( 'x', fh ) == 'x' );
66 TESTCASE( ftell( fh ) == -1l );
68 TESTCASE( ftell( fh ) == 0l );
69 /* Commence "normal" tests */
70 TESTCASE( fputc( '1', fh ) == '1' );
71 TESTCASE( fputc( '2', fh ) == '2' );
72 TESTCASE( fputc( '3', fh ) == '3' );
73 /* Positions incrementing as expected? */
74 TESTCASE( ftell( fh ) == 3l );
75 TESTCASE_NOREG( fh->pos.offset == 0l );
76 TESTCASE_NOREG( fh->bufidx == 3l );
77 /* Buffer properly flushed when full? */
78 TESTCASE( fputc( '4', fh ) == '4' );
79 TESTCASE_NOREG( fh->pos.offset == 4l );
80 TESTCASE_NOREG( fh->bufidx == 0 );
81 /* fflush() resetting positions as expected? */
82 TESTCASE( fputc( '5', fh ) == '5' );
83 TESTCASE( fflush( fh ) == 0 );
84 TESTCASE( ftell( fh ) == 5l );
85 TESTCASE_NOREG( fh->pos.offset == 5l );
86 TESTCASE_NOREG( fh->bufidx == 0l );
87 /* rewind() resetting positions as expected? */
89 TESTCASE( ftell( fh ) == 0l );
90 TESTCASE_NOREG( fh->pos.offset == 0 );
91 TESTCASE_NOREG( fh->bufidx == 0 );
92 /* Reading back first character after rewind for basic read check */
93 TESTCASE( fgetc( fh ) == '1' );
95 TESTCASE( fclose( fh ) == 0 );