0
|
1 /* ========================================================================
|
|
2 * Copyright 1988-2006 University of Washington
|
|
3 *
|
|
4 * Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 * you may not use this file except in compliance with the License.
|
|
6 * You may obtain a copy of the License at
|
|
7 *
|
|
8 * http://www.apache.org/licenses/LICENSE-2.0
|
|
9 *
|
|
10 *
|
|
11 * ========================================================================
|
|
12 */
|
|
13
|
|
14 /*
|
|
15 * Program: File string routines
|
|
16 *
|
|
17 * Author: Mark Crispin
|
|
18 * Networks and Distributed Computing
|
|
19 * Computing & Communications
|
|
20 * University of Washington
|
|
21 * Administration Building, AG-44
|
|
22 * Seattle, WA 98195
|
|
23 * Internet: MRC@CAC.Washington.EDU
|
|
24 *
|
|
25 * Date: 15 April 1997
|
|
26 * Last Edited: 6 December 2006
|
|
27 */
|
|
28
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include "mail.h"
|
|
32 #include "flstring.h"
|
|
33
|
|
34 /* String driver for stdio file strings */
|
|
35
|
|
36 static void file_string_init (STRING *s,void *data,unsigned long size);
|
|
37 static char file_string_next (STRING *s);
|
|
38 static void file_string_setpos (STRING *s,unsigned long i);
|
|
39
|
|
40 STRINGDRIVER file_string = {
|
|
41 file_string_init, /* initialize string structure */
|
|
42 file_string_next, /* get next byte in string structure */
|
|
43 file_string_setpos /* set position in string structure */
|
|
44 };
|
|
45
|
|
46
|
|
47 /* Initialize mail string structure for file
|
|
48 * Accepts: string structure
|
|
49 * pointer to string
|
|
50 * size of string
|
|
51 */
|
|
52
|
|
53 static void file_string_init (STRING *s,void *data,unsigned long size)
|
|
54 {
|
|
55 s->data = data; /* note file descriptor */
|
|
56 /* big enough for one byte */
|
|
57 s->chunk = s->curpos = (char *) &s->data1;
|
|
58 s->size = size; /* data size */
|
|
59 s->cursize = s->chunksize = 1;/* always call stdio */
|
|
60 file_string_setpos (s,0); /* initial offset is 0 */
|
|
61 }
|
|
62
|
|
63
|
|
64 /* Get next character from string
|
|
65 * Accepts: string structure
|
|
66 * Returns: character, string structure chunk refreshed
|
|
67 */
|
|
68
|
|
69 static char file_string_next (STRING *s)
|
|
70 {
|
|
71 char ret = *s->curpos;
|
|
72 s->offset++; /* advance position */
|
|
73 s->cursize = 1; /* reset size */
|
|
74 *s->curpos = (char) getc ((FILE *) s->data);
|
|
75 return ret;
|
|
76 }
|
|
77
|
|
78
|
|
79 /* Set string pointer position
|
|
80 * Accepts: string structure
|
|
81 * new position
|
|
82 */
|
|
83
|
|
84 static void file_string_setpos (STRING *s,unsigned long i)
|
|
85 {
|
|
86 s->offset = i; /* note new offset */
|
|
87 fseek ((FILE *) s->data,i,SEEK_SET);
|
|
88 /* in case using returnstringstruct hack */
|
|
89 s->chunk = s->curpos = (char *) &s->data1;
|
|
90 *s->curpos = (char) getc ((FILE *) s->data);
|
|
91 }
|