imapext-2007
diff src/osdep/unix/fdstring.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/unix/fdstring.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,99 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2007 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: File descriptor string routines 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 15 April 1997 1.29 + * Last Edited: 4 April 2007 1.30 + */ 1.31 + 1.32 +#include "mail.h" 1.33 +#include "osdep.h" 1.34 +#include "misc.h" 1.35 +#include "fdstring.h" 1.36 + 1.37 +/* String driver for fd stringstructs */ 1.38 + 1.39 +static void fd_string_init (STRING *s,void *data,unsigned long size); 1.40 +static char fd_string_next (STRING *s); 1.41 +static void fd_string_setpos (STRING *s,unsigned long i); 1.42 + 1.43 +STRINGDRIVER fd_string = { 1.44 + fd_string_init, /* initialize string structure */ 1.45 + fd_string_next, /* get next byte in string structure */ 1.46 + fd_string_setpos /* set position in string structure */ 1.47 +}; 1.48 + 1.49 + 1.50 +/* Initialize string structure for fd stringstruct 1.51 + * Accepts: string structure 1.52 + * pointer to string 1.53 + * size of string 1.54 + */ 1.55 + 1.56 +static void fd_string_init (STRING *s,void *data,unsigned long size) 1.57 +{ 1.58 + FDDATA *d = (FDDATA *) data; 1.59 + /* note fd */ 1.60 + s->data = (void *) (unsigned long) d->fd; 1.61 + s->data1 = d->pos; /* note file offset */ 1.62 + s->size = size; /* note size */ 1.63 + s->curpos = s->chunk = d->chunk; 1.64 + s->chunksize = (unsigned long) d->chunksize; 1.65 + s->offset = 0; /* initial position */ 1.66 + /* and size of data */ 1.67 + s->cursize = min (s->chunksize,size); 1.68 + /* move to that position in the file */ 1.69 + lseek (d->fd,d->pos,L_SET); 1.70 + read (d->fd,s->chunk,(size_t) s->cursize); 1.71 +} 1.72 + 1.73 +/* Get next character from fd stringstruct 1.74 + * Accepts: string structure 1.75 + * Returns: character, string structure chunk refreshed 1.76 + */ 1.77 + 1.78 +static char fd_string_next (STRING *s) 1.79 +{ 1.80 + char c = *s->curpos++; /* get next byte */ 1.81 + SETPOS (s,GETPOS (s)); /* move to next chunk */ 1.82 + return c; /* return the byte */ 1.83 +} 1.84 + 1.85 + 1.86 +/* Set string pointer position for fd stringstruct 1.87 + * Accepts: string structure 1.88 + * new position 1.89 + */ 1.90 + 1.91 +static void fd_string_setpos (STRING *s,unsigned long i) 1.92 +{ 1.93 + if (i > s->size) i = s->size; /* don't permit setting beyond EOF */ 1.94 + s->offset = i; /* set new offset */ 1.95 + s->curpos = s->chunk; /* reset position */ 1.96 + /* set size of data */ 1.97 + if (s->cursize = min (s->chunksize,SIZE (s))) { 1.98 + /* move to that position in the file */ 1.99 + lseek ((long) s->data,s->data1 + s->offset,L_SET); 1.100 + read ((long) s->data,s->curpos,(size_t) s->cursize); 1.101 + } 1.102 +}