home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / libgplus.5 / libio / strops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-18  |  7.3 KB  |  289 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  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)
  8. any later version.
  9.  
  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.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  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. */
  24.  
  25. #include "strfile.h"
  26. #include "libioP.h"
  27.  
  28. #define LEN(fp) (((_IO_strfile*)(fp))->_s._len)
  29.  
  30. #ifdef TODO
  31. /* An "unbounded buffer" is when a buffer is supplied, but with no
  32.    specified length.  An example is the buffer argument to sprintf.
  33.    */
  34. #endif
  35.  
  36. void
  37. _IO_str_init_static (fp, ptr, size, pstart)
  38.      _IO_FILE *fp;
  39.      char *ptr;
  40.      int size;
  41.      char *pstart;
  42. {
  43.   if (size == 0)
  44.     size = strlen(ptr);
  45.   else if (size < 0)
  46.     {
  47.       /* If size is negative 'the characters are assumed to
  48.      continue indefinitely.'  This is kind of messy ... */
  49. #if 1
  50.       int s;
  51.       size = 512;
  52.       /* Try increasing powers of 2, as long as we don't wrap around.
  53.      This can lose in pathological cases (ptr near the end
  54.      of the address space).  A better solution might be to
  55.      adjust the size on underflow/overflow.  FIXME. */
  56.       for (s; s = 2*size, s > 0 && ptr + s > ptr && s < 0x4000000L; )
  57.     size = s;
  58.       size = s;
  59. #else
  60.       /* The following semi-portable kludge assumes that
  61.      sizeof(unsigned long) == sizeof(char*). Hence,
  62.      (unsigned long)(-1) should be the largest possible address. */
  63.       unsigned long highest = (unsigned long)(-1);
  64.       /* Pointers are signed on some brain-damaged systems, in
  65.      which case we divide by two to get the maximum signed address. */
  66.       if  ((char*)highest < ptr)
  67.     highest >>= 1;
  68.       size = (char*)highest - ptr;
  69. #endif
  70.     }
  71.   _IO_setb(fp, ptr, ptr+size, 0);
  72.  
  73.   fp->_IO_write_base = ptr;
  74.   fp->_IO_read_base = ptr;
  75.   fp->_IO_read_ptr = ptr;
  76.   if (pstart)
  77.     {
  78.       fp->_IO_write_ptr = pstart;
  79.       fp->_IO_write_end = ptr+size;
  80.       fp->_IO_read_end = pstart;
  81.     }
  82.   else
  83.     {
  84.       fp->_IO_write_ptr = ptr;
  85.       fp->_IO_write_end = ptr;
  86.       fp->_IO_read_end = ptr+size;
  87.     }
  88.   LEN(fp) = size;
  89. }
  90.  
  91. void
  92. _IO_str_init_readonly (fp, ptr, size)
  93.      _IO_FILE *fp;
  94.      const char *ptr;
  95.      int size;
  96. {
  97.   _IO_str_init_static (fp, (char*)ptr, size, NULL);
  98.   fp->_IO_file_flags |= _IO_NO_WRITES;
  99. }
  100.  
  101. int _IO_str_overflow (fp, c)
  102.      register _IO_FILE* fp;
  103.      int c;
  104. {
  105.   int flush_only = c == EOF;
  106.   _IO_size_t pos = fp->_IO_write_ptr - fp->_IO_write_base;
  107.   _IO_size_t get_pos = fp->_IO_read_ptr - fp->_IO_read_base;
  108.   if (fp->_flags & _IO_NO_WRITES)
  109.       return flush_only ? 0 : EOF;
  110.   if (pos > LEN(fp)) LEN(fp) = pos;
  111.   if ((fp->_flags & _IO_TIED_PUT_GET) && !(fp->_flags & _IO_CURRENTLY_PUTTING))
  112.     {
  113.       pos = get_pos;
  114.       fp->_flags |= _IO_CURRENTLY_PUTTING;
  115.       get_pos = LEN(fp);
  116.     }
  117.   if (pos >= _IO_blen(fp) + flush_only)
  118.     {
  119.       if (fp->_flags & _IO_USER_BUF) /* not allowed to enlarge */
  120.     {
  121. #ifdef TODO
  122.       if (indefinite size)
  123.         {
  124.           fp->_IO_buf_end += 512;
  125.         }
  126.       else
  127. #endif
  128.       return EOF;
  129.     }
  130.       else
  131.     {
  132.       char *new_buf;
  133.       _IO_size_t new_size = 2 * _IO_blen(fp);
  134.       new_buf
  135.         = (char*)(*((_IO_strfile*)fp)->_s._allocate_buffer)(new_size);
  136.       if (new_buf == NULL)
  137.         {
  138.           /*      __ferror(fp) = 1; */
  139.           return EOF;
  140.         }
  141.       memcpy(new_buf, fp->_IO_buf_base, _IO_blen(fp));
  142. #if 0
  143.       if (lenp == &LEN(fp)) /* use '\0'-filling */
  144.           memset(new_buf + pos, 0, blen() - pos);
  145. #endif
  146.       if (fp->_IO_buf_base)
  147.         {
  148.           (*((_IO_strfile*)fp)->_s._free_buffer)(fp->_IO_buf_base);
  149.           /* Make sure _IO_setb won't try to delete _base. */
  150.           fp->_IO_buf_base = NULL;
  151.         }
  152.       _IO_setb(fp, new_buf, new_buf + new_size, 1);
  153.       fp->_IO_write_base = new_buf;
  154.     }
  155.       fp->_IO_write_end = fp->_IO_buf_end;
  156.     }
  157.  
  158.   fp->_IO_write_ptr = fp->_IO_buf_base + pos;
  159.  
  160.   fp->_IO_read_base = fp->_IO_buf_base;
  161.   fp->_IO_read_ptr = fp->_IO_buf_base + get_pos;;
  162.   fp->_IO_read_end = fp->_IO_buf_base + LEN(fp);;
  163.  
  164.   if (!flush_only)
  165.     *fp->_IO_write_ptr++ = (unsigned char) c;
  166.   return c;
  167. }
  168.  
  169. int
  170. _IO_str_underflow (fp)
  171.      register _IO_FILE* fp;
  172. {
  173.   _IO_size_t ppos = fp->_IO_write_ptr - fp->_IO_write_base;
  174.   if (ppos > LEN(fp)) LEN(fp) = ppos;
  175.   if ((fp->_flags & _IO_TIED_PUT_GET) && (fp->_flags & _IO_CURRENTLY_PUTTING))
  176.     {
  177.       fp->_flags &= ~_IO_CURRENTLY_PUTTING;
  178.       fp->_IO_write_ptr = fp->_IO_write_end;
  179.     }
  180.   fp->_IO_read_end = fp->_IO_read_base + LEN(fp);
  181.   if (fp->_IO_read_ptr < fp->_IO_read_end)
  182.     return *fp->_IO_read_ptr;
  183.   else
  184.     return EOF;
  185. }
  186.  
  187. _IO_ssize_t
  188. _IO_str_count (fp)
  189.      register _IO_FILE *fp;
  190. {
  191.   _IO_ssize_t put_len = fp->_IO_write_ptr - fp->_IO_write_base;
  192.   if (put_len < ((_IO_strfile*)fp)->_s._len)
  193.     put_len = ((_IO_strfile*)fp)->_s._len;
  194.   return put_len;
  195. }     
  196.  
  197. _IO_pos_t
  198. _IO_str_seekoff(fp, offset, mode)
  199.      register _IO_FILE *fp;
  200.      _IO_off_t offset;
  201.      _IO_seekflags mode;
  202. {
  203.   _IO_ssize_t cur_size = _IO_str_count(fp);
  204.   _IO_pos_t new_pos = EOF;
  205.   int dir = mode & 3;
  206.  
  207.   /* Move the get pointer, if requested. */
  208.   if (!(mode & _IO_seek_not_in))
  209.     {
  210.       switch (dir)
  211.     {
  212.     case _IO_seek_end:
  213.       offset += cur_size;
  214.       break;
  215.     case _IO_seek_cur:
  216.       offset += fp->_IO_read_ptr - fp->_IO_read_base;
  217.       break;
  218.     default: /* case _IO_seek_set: */
  219.       break;
  220.     }
  221.       if (offset < 0 || (_IO_size_t)offset > cur_size)
  222.     return EOF;
  223.       fp->_IO_read_ptr = fp->_IO_read_base + offset;
  224.       fp->_IO_read_end = fp->_IO_read_base + cur_size;
  225.       new_pos = offset;
  226.     }
  227.  
  228.   /* Move the put pointer, if requested. */
  229.   if (!(mode & _IO_seek_not_out))
  230.     {
  231.       switch (dir)
  232.     {
  233.     case _IO_seek_end:
  234.       offset += cur_size;
  235.       break;
  236.     case _IO_seek_cur:
  237.       offset += fp->_IO_write_ptr - fp->_IO_write_base;
  238.       break;
  239.     default: /* case _IO_seek_set: */
  240.       break;
  241.     }
  242.       if (offset < 0 || (_IO_size_t)offset > cur_size)
  243.     return EOF;
  244.       fp->_IO_write_ptr = fp->_IO_write_base + offset;
  245.       new_pos = offset;
  246.     }
  247.   return new_pos;
  248. }
  249.  
  250. int
  251. _IO_str_pbackfail(fp, c)
  252.      register _IO_FILE *fp;
  253.      int c;
  254. {
  255.   if ((fp->_flags & _IO_NO_WRITES) && c != EOF)
  256.     return EOF;
  257.   return _IO_default_pbackfail(fp, c);
  258. }
  259.  
  260. void
  261. _IO_str_finish(fp)
  262.      register _IO_FILE* fp;
  263. {
  264.   if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
  265.     (((_IO_strfile*)fp)->_s._free_buffer)(fp->_IO_buf_base);
  266.   fp->_IO_buf_base = NULL;
  267.  
  268.   _IO_default_finish(fp);
  269. }
  270.  
  271. struct _IO_jump_t _IO_str_jumps = {
  272.   _IO_str_overflow,
  273.   _IO_str_underflow,
  274.   _IO_default_xsputn,
  275.   _IO_default_xsgetn,
  276.   _IO_default_read,
  277.   _IO_default_write,
  278.   _IO_default_doallocate,
  279.   _IO_str_pbackfail,
  280.   _IO_default_setbuf,
  281.   _IO_default_sync,
  282.   _IO_str_finish,
  283.   _IO_default_close,
  284.   _IO_default_stat,
  285.   _IO_default_seek,
  286.   _IO_str_seekoff,
  287.   _IO_default_seekpos,
  288. };
  289.