Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
mime-inputsource.h
Go to the documentation of this file.
1
7#ifndef mime_inputsource_h_included
8#define mime_inputsource_h_included
9
10#include <string.h>
11#include <unistd.h>
12
13namespace Binc {
14
16 public:
17 inline MimeInputSource(int fd, unsigned int start = 0);
18 virtual inline ~MimeInputSource(void);
19
20 virtual inline bool fillInputBuffer(void);
21 virtual inline void reset(void);
22
23 inline void seek(unsigned int offset);
24 inline bool getChar(char *c);
25 inline void ungetChar(void);
26 inline int getFileDescriptor(void) const;
27
28 inline unsigned int getOffset(void) const;
29
30 private:
31 int fd;
32 char data[16384];
33 unsigned int offset;
34 unsigned int tail;
35 unsigned int head;
36 unsigned int start;
37 char lastChar;
38 };
39
40 inline MimeInputSource::MimeInputSource(int fd, unsigned int start)
41 {
42 this->fd = fd;
43 this->start = start;
44 offset = 0;
45 tail = 0;
46 head = 0;
47 lastChar = '\0';
48 memset(data, '\0', sizeof(data));
49
50 seek(start);
51 }
52
54 {
55 }
56
58 {
59 char raw[4096];
60 ssize_t nbytes = read(fd, raw, sizeof(raw));
61 if (nbytes <= 0) {
62 // FIXME: If ferror(crlffile) we should log this.
63 return false;
64 }
65
66 for (ssize_t i = 0; i < nbytes; ++i) {
67 const char c = raw[i];
68 if (c == '\r') {
69 if (lastChar == '\r') {
70 data[tail++ & (0x4000-1)] = '\r';
71 data[tail++ & (0x4000-1)] = '\n';
72 }
73 } else if (c == '\n') {
74 data[tail++ & (0x4000-1)] = '\r';
75 data[tail++ & (0x4000-1)] = '\n';
76 } else {
77 if (lastChar == '\r') {
78 data[tail++ & (0x4000-1)] = '\r';
79 data[tail++ & (0x4000-1)] = '\n';
80 }
81 data[tail++ & (0x4000-1)] = c;
82 }
83
84 lastChar = c;
85 }
86
87 return true;
88 }
89
90 inline void MimeInputSource::reset(void)
91 {
92 offset = head = tail = 0;
93 lastChar = '\0';
94
95 if (fd != -1)
96 lseek(fd, 0, SEEK_SET);
97 }
98
99 inline void MimeInputSource::seek(unsigned int seekToOffset)
100 {
101 if (offset > seekToOffset)
102 reset();
103
104 char c;
105 int n = 0;
106 while (seekToOffset > offset) {
107 if (!getChar(&c)) break;
108 ++n;
109 }
110 }
111
112 inline bool MimeInputSource::getChar(char *c)
113 {
114 if (head == tail && !fillInputBuffer())
115 return false;
116
117 *c = data[head++ & (0x4000-1)];
118 ++offset;
119 return true;
120 }
121
123 {
124 --head;
125 --offset;
126 }
127
129 {
130 return fd;
131 }
132
133 inline unsigned int MimeInputSource::getOffset(void) const
134 {
135 return offset;
136 }
137}
138
140
141#endif
virtual void reset(void)
int getFileDescriptor(void) const
MimeInputSource(int fd, unsigned int start=0)
virtual bool fillInputBuffer(void)
void seek(unsigned int offset)
unsigned int getOffset(void) const
virtual ~MimeInputSource(void)
Binc::MimeInputSource * mimeSource
Definition: bincimapd.cc:9