2 Copyright (C) 1993, 1995 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 /* Generic or default I/O operations. */
34 DEFUN(_IO_un_link
, (fp
),
37 if (fp
->_flags
& _IO_LINKED
) {
39 for (f
= &_IO_list_all
; *f
!= NULL
; f
= &(*f
)->_chain
) {
45 fp
->_flags
&= ~_IO_LINKED
;
50 DEFUN(_IO_link_in
, (fp
),
53 if ((fp
->_flags
& _IO_LINKED
) == 0) {
54 fp
->_flags
|= _IO_LINKED
;
55 fp
->_chain
= _IO_list_all
;
60 /* Return minimum _pos markers
61 Assumes the current get area is the main get area. */
64 DEFUN(_IO_least_marker
, (fp
),
65 register _IO_FILE
*fp
)
67 _IO_ssize_t least_so_far
= fp
->_IO_read_end
- fp
->_IO_read_base
;
68 register struct _IO_marker
*mark
;
69 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
70 if (mark
->_pos
< least_so_far
)
71 least_so_far
= mark
->_pos
;
75 /* Switch current get area from backup buffer to (start of) main get area. */
78 DEFUN(_IO_switch_to_main_get_area
, (fp
),
82 fp
->_flags
&= ~_IO_IN_BACKUP
;
83 /* Swap _IO_read_end and _IO_save_end. */
84 tmp
= fp
->_IO_read_end
; fp
->_IO_read_end
= fp
->_IO_save_end
; fp
->_IO_save_end
= tmp
;
85 /* Swap _IO_read_base and _IO_save_base. */
86 tmp
= fp
->_IO_read_base
; fp
->_IO_read_base
= fp
->_IO_save_base
; fp
->_IO_save_base
= tmp
;
87 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
90 /* Switch current get area from main get area to (end of) backup area. */
93 DEFUN(_IO_switch_to_backup_area
, (fp
),
94 register _IO_FILE
*fp
)
97 fp
->_flags
|= _IO_IN_BACKUP
;
98 /* Swap _IO_read_end and _IO_save_end. */
99 tmp
= fp
->_IO_read_end
; fp
->_IO_read_end
= fp
->_IO_save_end
; fp
->_IO_save_end
= tmp
;
100 /* Swap _gbase and _IO_save_base. */
101 tmp
= fp
->_IO_read_base
; fp
->_IO_read_base
= fp
->_IO_save_base
; fp
->_IO_save_base
= tmp
;
102 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
106 DEFUN(_IO_switch_to_get_mode
, (fp
),
107 register _IO_FILE
*fp
)
109 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
110 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
112 if (_IO_in_backup(fp
))
113 fp
->_IO_read_base
= fp
->_IO_backup_base
;
116 fp
->_IO_read_base
= fp
->_IO_buf_base
;
117 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
118 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
120 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
122 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
124 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
129 DEFUN(_IO_free_backup_area
, (fp
),
130 register _IO_FILE
*fp
)
132 if (_IO_in_backup (fp
))
133 _IO_switch_to_main_get_area(fp
); /* Just in case. */
134 free (fp
->_IO_save_base
);
135 fp
->_IO_save_base
= NULL
;
136 fp
->_IO_save_end
= NULL
;
137 fp
->_IO_backup_base
= NULL
;
142 DEFUN(_IO_switch_to_put_mode
, (fp
),
143 register _IO_FILE
*fp
)
145 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
146 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
147 /* Following is wrong if line- or un-buffered? */
148 fp
->_IO_write_end
= fp
->_flags
& _IO_IN_BACKUP
? fp
->_IO_read_end
: fp
->_IO_buf_end
;
150 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
151 fp
->_IO_read_base
= fp
->_IO_read_end
;
153 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
159 DEFUN(__overflow
, (f
, ch
),
160 _IO_FILE
*f AND
int ch
)
162 return _IO_OVERFLOW (f
, ch
);
166 DEFUN(save_for_backup
, (fp
),
169 /* Append [_IO_read_base.._IO_read_end] to backup area. */
170 int least_mark
= _IO_least_marker(fp
);
171 /* needed_size is how much space we need in the backup area. */
172 int needed_size
= (fp
->_IO_read_end
- fp
->_IO_read_base
) - least_mark
;
173 int current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
174 int avail
; /* Extra space available for future expansion. */
176 struct _IO_marker
*mark
;
177 if (needed_size
> current_Bsize
)
181 new_buffer
= (char*)malloc(avail
+needed_size
);
182 if (new_buffer
== NULL
)
183 return EOF
; /* FIXME */
186 memcpy(new_buffer
+ avail
,
187 fp
->_IO_save_end
+ least_mark
,
189 memcpy(new_buffer
+avail
- least_mark
,
191 fp
->_IO_read_end
- fp
->_IO_read_base
);
194 memcpy(new_buffer
+ avail
,
195 fp
->_IO_read_base
+ least_mark
,
197 if (fp
->_IO_save_base
)
198 free (fp
->_IO_save_base
);
199 fp
->_IO_save_base
= new_buffer
;
200 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
204 avail
= current_Bsize
- needed_size
;
207 memmove(fp
->_IO_save_base
+ avail
,
208 fp
->_IO_save_end
+ least_mark
,
210 memcpy(fp
->_IO_save_base
+ avail
- least_mark
,
212 fp
->_IO_read_end
- fp
->_IO_read_base
);
214 else if (needed_size
> 0)
215 memcpy(fp
->_IO_save_base
+ avail
,
216 fp
->_IO_read_base
+ least_mark
,
219 /* FIXME: Dubious arithmetic if pointers are NULL */
220 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
221 /* Adjust all the streammarkers. */
222 delta
= fp
->_IO_read_end
- fp
->_IO_read_base
;
223 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
229 DEFUN(__underflow
, (fp
),
232 if (_IO_in_put_mode(fp
))
233 if (_IO_switch_to_get_mode(fp
) == EOF
) return EOF
;
234 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
235 return *(unsigned char*)fp
->_IO_read_ptr
;
236 if (_IO_in_backup(fp
))
238 _IO_switch_to_main_get_area(fp
);
239 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
240 return *fp
->_IO_read_ptr
;
242 if (_IO_have_markers(fp
))
244 if (save_for_backup (fp
))
247 else if (_IO_have_backup(fp
))
248 _IO_free_backup_area(fp
);
249 return _IO_UNDERFLOW (fp
);
256 if (_IO_in_put_mode(fp
))
257 if (_IO_switch_to_get_mode(fp
) == EOF
) return EOF
;
258 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
259 return *(unsigned char*)fp
->_IO_read_ptr
++;
260 if (_IO_in_backup(fp
))
262 _IO_switch_to_main_get_area(fp
);
263 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
264 return *fp
->_IO_read_ptr
++;
266 if (_IO_have_markers(fp
))
268 if (save_for_backup (fp
))
271 else if (_IO_have_backup(fp
))
272 _IO_free_backup_area(fp
);
273 return _IO_UFLOW (fp
);
277 DEFUN(_IO_setb
, (f
, b
, eb
, a
),
278 _IO_FILE
*f AND
char *b AND
char *eb AND
int a
)
280 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
281 FREE_BUF(f
->_IO_buf_base
);
285 f
->_flags
&= ~_IO_USER_BUF
;
287 f
->_flags
|= _IO_USER_BUF
;
291 DEFUN(_IO_doallocbuf
, (fp
),
292 register _IO_FILE
*fp
)
294 if (fp
->_IO_buf_base
)
296 if (!(fp
->_flags
& _IO_UNBUFFERED
))
297 if (_IO_DOALLOCATE (fp
) != EOF
)
299 _IO_setb(fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
303 DEFUN(_IO_default_underflow
, (fp
),
310 DEFUN(_IO_default_uflow
, (fp
),
313 int ch
= _IO_UNDERFLOW (fp
);
316 return *(unsigned char*)fp
->_IO_read_ptr
++;
320 DEFUN(_IO_default_xsputn
, (f
, data
, n
),
321 register _IO_FILE
*f AND
const void *data AND _IO_size_t n
)
323 register const char *s
= (char*) data
;
324 register _IO_size_t more
= n
;
329 _IO_ssize_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
; /* Space available. */
336 memcpy(f
->_IO_write_ptr
, s
, count
);
338 f
->_IO_write_ptr
+= count
;
344 register char *p
= f
->_IO_write_ptr
;
345 register _IO_ssize_t i
;
346 for (i
= count
; --i
>= 0; ) *p
++ = *s
++;
347 f
->_IO_write_ptr
= p
;
351 if (more
== 0 || __overflow(f
, (unsigned char)*s
++) == EOF
)
359 DEFUN(_IO_sgetn
, (fp
, data
, n
),
360 _IO_FILE
*fp AND
void *data AND _IO_size_t n
)
362 /* FIXME handle putback buffer here! */
363 return _IO_XSGETN (fp
, data
, n
);
367 DEFUN(_IO_default_xsgetn
, (fp
, data
, n
),
368 _IO_FILE
*fp AND
void *data AND _IO_size_t n
)
370 register _IO_size_t more
= n
;
371 register char *s
= (char*) data
;
374 _IO_ssize_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
; /* Data available. */
381 memcpy(s
, fp
->_IO_read_ptr
, count
);
383 fp
->_IO_read_ptr
+= count
;
389 register char *p
= fp
->_IO_read_ptr
;
390 register int i
= (int)count
;
391 while (--i
>= 0) *s
++ = *p
++;
392 fp
->_IO_read_ptr
= p
;
396 if (more
== 0 || __underflow(fp
) == EOF
)
403 DEFUN(_IO_sync
, (fp
),
404 register _IO_FILE
*fp
)
410 DEFUN(_IO_default_setbuf
, (fp
, p
, len
),
411 register _IO_FILE
*fp AND
char* p AND _IO_ssize_t len
)
413 if (_IO_SYNC (fp
) == EOF
)
415 if (p
== NULL
|| len
== 0)
417 fp
->_flags
|= _IO_UNBUFFERED
;
418 _IO_setb(fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
422 fp
->_flags
&= ~_IO_UNBUFFERED
;
423 _IO_setb(fp
, p
, p
+len
, 0);
425 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
426 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
431 DEFUN(_IO_default_seekpos
, (fp
, pos
, mode
),
432 _IO_FILE
*fp AND _IO_pos_t pos AND
int mode
)
434 return _IO_SEEKOFF (fp
, _IO_pos_as_off(pos
), 0, mode
);
438 DEFUN(_IO_default_doallocate
, (fp
),
441 char *buf
= ALLOC_BUF(_IO_BUFSIZ
);
444 _IO_setb(fp
, buf
, buf
+_IO_BUFSIZ
, 1);
449 DEFUN(_IO_init
, (fp
, flags
),
450 register _IO_FILE
*fp AND
int flags
)
452 fp
->_flags
= _IO_MAGIC
|flags
;
453 fp
->_IO_buf_base
= NULL
;
454 fp
->_IO_buf_end
= NULL
;
455 fp
->_IO_read_base
= NULL
;
456 fp
->_IO_read_ptr
= NULL
;
457 fp
->_IO_read_end
= NULL
;
458 fp
->_IO_write_base
= NULL
;
459 fp
->_IO_write_ptr
= NULL
;
460 fp
->_IO_write_end
= NULL
;
461 fp
->_chain
= NULL
; /* Not necessary. */
463 fp
->_IO_save_base
= NULL
;
464 fp
->_IO_backup_base
= NULL
;
465 fp
->_IO_save_end
= NULL
;
471 DEFUN(_IO_default_sync
, (fp
),
477 /* The way the C++ classes are mapped into the C functions in the
478 current implementation, this function can get called twice! */
481 DEFUN(_IO_default_finish
, (fp
),
484 struct _IO_marker
*mark
;
485 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
487 FREE_BUF(fp
->_IO_buf_base
);
488 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
491 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
494 if (fp
->_IO_save_base
)
496 free (fp
->_IO_save_base
);
497 fp
->_IO_save_base
= NULL
;
504 DEFUN(_IO_default_seekoff
, (fp
, offset
, dir
, mode
),
505 register _IO_FILE
*fp AND _IO_off_t offset AND
int dir AND
int mode
)
511 DEFUN(_IO_sputbackc
, (fp
, c
),
512 register _IO_FILE
*fp AND
int c
)
514 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
515 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
518 return (unsigned char)c
;
520 return _IO_PBACKFAIL (fp
, c
);
524 DEFUN(_IO_sungetc
, (fp
),
525 register _IO_FILE
*fp
)
527 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
530 return (unsigned char)*fp
->_IO_read_ptr
;
533 return _IO_PBACKFAIL (fp
, EOF
);
536 #if 0 /* Work in progress */
538 DEFUN(_IO_set_column
, (fp
, c
),
539 register _IO_FILE
*fp AND
int c
)
544 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
548 DEFUN(_IO_set_column
, (fp
, i
),
549 register _IO_FILE
*fp AND
int i
)
551 fp
->_cur_column
= i
+1;
558 DEFUN(_IO_adjust_column
, (start
, line
, count
),
559 unsigned start AND
const char *line AND
int count
)
561 register const char *ptr
= line
+ count
;
564 return line
+ count
- ptr
- 1;
565 return start
+ count
;
569 DEFUN(_IO_get_column
, (fp
),
570 register _IO_FILE
*fp
)
573 return _IO_adjust_column(fp
->_cur_column
- 1,
575 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
580 DEFUN_VOID(_IO_flush_all
)
584 for (fp
= _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
585 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
586 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
592 DEFUN_VOID(_IO_flush_all_linebuffered
)
595 for (fp
= _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
596 if (fp
->_flags
& _IO_LINE_BUF
)
597 _IO_OVERFLOW (fp
, EOF
);
601 DEFUN_VOID(_IO_unbuffer_all
)
604 for (fp
= _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
605 if (! (fp
->_flags
& _IO_UNBUFFERED
))
606 _IO_SETBUF (fp
, NULL
, 0);
610 DEFUN_VOID(_IO_cleanup
)
614 /* We currently don't have a reliable mechanism for making sure that
615 C++ static destructors are executed in the correct order.
616 So it is possible that other static destructord might want to
617 write to cout - and they're supposed to be able to do so.
619 The following will make the standard streambufs be unbuffered,
620 which forces any output from late destructors to be written out. */
625 DEFUN(_IO_init_marker
, (marker
, fp
),
626 struct _IO_marker
*marker AND _IO_FILE
*fp
)
629 if (_IO_in_put_mode(fp
))
630 _IO_switch_to_get_mode(fp
);
631 if (_IO_in_backup(fp
))
632 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
634 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
636 /* Should perhaps sort the chain? */
637 marker
->_next
= fp
->_markers
;
638 fp
->_markers
= marker
;
642 DEFUN(_IO_remove_marker
, (marker
),
643 register struct _IO_marker
*marker
)
645 /* Unlink from sb's chain. */
646 register struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
647 for (; ; ptr
= &(*ptr
)->_next
)
651 else if (*ptr
== marker
)
653 *ptr
= marker
->_next
;
658 if _sbuf has a backup area that is no longer needed
, should we
delete
659 it now
, or wait until the next underflow
?
663 #define BAD_DELTA EOF
666 DEFUN(_IO_marker_difference
, (mark1
, mark2
),
667 struct _IO_marker
*mark1 AND
struct _IO_marker
*mark2
)
669 return mark1
->_pos
- mark2
->_pos
;
672 /* Return difference between MARK and current posistion of MARK's stream. */
674 DEFUN(_IO_marker_delta
, (mark
),
675 struct _IO_marker
*mark
)
678 if (mark
->_sbuf
== NULL
)
680 if (_IO_in_backup(mark
->_sbuf
))
681 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
683 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
684 return mark
->_pos
- cur_pos
;
688 DEFUN(_IO_seekmark
, (fp
, mark
, delta
),
689 _IO_FILE
*fp AND
struct _IO_marker
*mark AND
int delta
)
691 if (mark
->_sbuf
!= fp
)
695 if (_IO_in_backup(fp
))
696 _IO_switch_to_main_get_area(fp
);
697 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
701 if (!_IO_in_backup(fp
))
702 _IO_switch_to_backup_area(fp
);
703 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
709 DEFUN(_IO_unsave_markers
, (fp
),
710 register _IO_FILE
*fp
)
712 register struct _IO_marker
*mark
= fp
->_markers
;
716 streampos offset
= seekoff(0, ios::cur
, ios::in
);
719 offset
+= eGptr() - Gbase();
720 for ( ; mark
!= NULL
; mark
= mark
->_next
)
721 mark
->set_streampos(mark
->_pos
+ offset
);
725 for ( ; mark
!= NULL
; mark
= mark
->_next
)
726 mark
->set_streampos(EOF
);
732 if (_IO_have_backup(fp
))
733 _IO_free_backup_area(fp
);
737 DEFUN(_IO_nobackup_pbackfail
, (fp
, c
),
738 register _IO_FILE
*fp AND
int c
)
740 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
742 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
743 *fp
->_IO_read_ptr
= c
;
744 return (unsigned char)c
;
748 DEFUN(_IO_default_pbackfail
, (fp
, c
),
749 register _IO_FILE
*fp AND
int c
)
751 if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
753 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
754 if (_IO_have_backup(fp
) && !_IO_in_backup(fp
))
755 _IO_switch_to_backup_area(fp
);
757 if (!_IO_have_backup(fp
))
759 /* No backup buffer: allocate one. */
760 /* Use nshort buffer, if unused? (probably not) FIXME */
761 int backup_size
= 128;
762 char *bbuf
= (char*)malloc(backup_size
);
765 fp
->_IO_save_base
= bbuf
;
766 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
767 fp
->_IO_backup_base
= fp
->_IO_save_end
;
768 _IO_switch_to_backup_area(fp
);
770 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
772 /* Increase size of existing backup buffer. */
774 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
776 new_size
= 2 * old_size
;
777 new_buf
= (char*)malloc(new_size
);
780 memcpy(new_buf
+(new_size
-old_size
), fp
->_IO_read_base
, old_size
);
781 free (fp
->_IO_read_base
);
783 new_buf
, new_buf
+(new_size
-old_size
), new_buf
+new_size
);
784 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
788 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
789 *fp
->_IO_read_ptr
= c
;
790 return (unsigned char)*fp
->_IO_read_ptr
;
794 DEFUN(_IO_default_seek
, (fp
, offset
, dir
),
795 _IO_FILE
*fp AND _IO_off_t offset AND
int dir
)
801 DEFUN(_IO_default_stat
, (fp
, st
),
802 _IO_FILE
*fp AND
void* st
)
808 DEFUN(_IO_default_read
, (fp
, data
, n
),
809 register _IO_FILE
* fp AND
void* data AND _IO_ssize_t n
)
815 DEFUN(_IO_default_write
, (fp
, data
, n
),
816 register _IO_FILE
* fp AND
const void* data AND _IO_ssize_t n
)
832 ~__io_defs() { _IO_cleanup(); }