home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / osdep / nt / fdstring.c < prev    next >
C/C++ Source or Header  |  1998-09-16  |  3KB  |  102 lines

  1. /*
  2.  * Program:    File descriptor string routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    15 April 1997
  13.  * Last Edited:    1 May 1997
  14.  *
  15.  * Copyright 1997 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #include "mail.h"
  37. #include "osdep.h"
  38. #include "misc.h"
  39. #include "fdstring.h"
  40.  
  41. /* String driver for fd stringstructs */
  42.  
  43. STRINGDRIVER fd_string = {
  44.   fd_string_init,        /* initialize string structure */
  45.   fd_string_next,        /* get next byte in string structure */
  46.   fd_string_setpos        /* set position in string structure */
  47. };
  48.  
  49.  
  50. /* Initialize string structure for fd stringstruct
  51.  * Accepts: string structure
  52.  *        pointer to string
  53.  *        size of string
  54.  */
  55.  
  56. void fd_string_init (STRING *s,void *data,unsigned long size)
  57. {
  58.   FDDATA *d = (FDDATA *) data;
  59.   s->data = (void *) d->fd;    /* note fd */
  60.   s->data1 = d->pos;        /* note file offset */
  61.   s->size = size;        /* note size */
  62.   s->curpos = s->chunk = d->chunk;
  63.   s->chunksize = (unsigned long) d->chunksize;
  64.   s->offset = 0;        /* initial position */
  65.                 /* and size of data */
  66.   s->cursize = min (s->chunksize,size);
  67.                 /* move to that position in the file */
  68.   lseek (d->fd,d->pos,L_SET);
  69.   read (d->fd,s->chunk,(size_t) s->cursize);
  70. }
  71.  
  72. /* Get next character from fd stringstruct
  73.  * Accepts: string structure
  74.  * Returns: character, string structure chunk refreshed
  75.  */
  76.  
  77. char fd_string_next (STRING *s)
  78. {
  79.   char c = *s->curpos++;    /* get next byte */
  80.   SETPOS (s,GETPOS (s));    /* move to next chunk */
  81.   return c;            /* return the byte */
  82. }
  83.  
  84.  
  85. /* Set string pointer position for fd stringstruct
  86.  * Accepts: string structure
  87.  *        new position
  88.  */
  89.  
  90. void fd_string_setpos (STRING *s,unsigned long i)
  91. {
  92.   if (i > s->size) i = s->size;    /* don't permit setting beyond EOF */
  93.   s->offset = i;        /* set new offset */
  94.   s->curpos = s->chunk;        /* reset position */
  95.                 /* set size of data */
  96.   if (s->cursize = min (s->chunksize,SIZE (s))) {
  97.                 /* move to that position in the file */
  98.     lseek ((int) s->data,s->data1 + s->offset,L_SET);
  99.     read ((int) s->data,s->curpos,(size_t) s->cursize);
  100.   }
  101. }
  102.