Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
mime-printheader.cc
Go to the documentation of this file.
1
7#include "mime.h"
8#include "mime-utils.h"
9#include "mime-inputsource.h"
10#include "convert.h"
11#include "iodevice.h"
12#include "iofactory.h"
13
14#include <string>
15#include <vector>
16#include <map>
17#include <exception>
18#include <iostream>
19
20#include <string.h>
21#include <ctype.h>
22#include <stdio.h>
23#include <errno.h>
24
25using namespace ::std;
26
27//------------------------------------------------------------------------
29 vector<string> headers, bool includeheaders,
30 unsigned int startoffset,
31 unsigned int length, string &store) const
32{
33 if (!mimeSource || mimeSource->getFileDescriptor() != fd) {
34 delete mimeSource;
36 }
37
39
40 string name;
41 string content;
42 char cqueue[2];
43 memset(cqueue, 0, sizeof(cqueue));
44
45 bool quit = false;
46 char c = '\0';
47
48 unsigned int wrotebytes = 0;
49 unsigned int processedbytes = 0;
50 bool hasHeaderSeparator = false;
51
52 while (!quit) {
53 // read name
54 while (1) {
55 // allow EOF to end the header
56 if (!mimeSource->getChar(&c)) {
57 quit = true;
58 break;
59 }
60
61 // assume this character is part of the header name.
62 name += c;
63
64 // break on the first colon
65 if (c == ':') break;
66
67 // break if a '\n' turned up.
68 if (c == '\n') {
69 // end of headers detected
70 if (name == "\r\n") {
71 hasHeaderSeparator = true;
72 quit = true;
73 break;
74 }
75
76 // put all data back in the buffer to the beginning of this
77 // line.
78 for (int i = name.length(); i >= 0; --i)
80
81 // abort printing of header. note that in this case, the
82 // headers will not end with a seperate \r\n.
83 quit = true;
84 name = "";
85 break;
86 }
87 }
88 if (quit) break;
89
90 // at this point, we have a name, that is - the start of a
91 // header. we'll read until the end of the header.
92 while (!quit) {
93 // allow EOF to end the header.
94 if (!mimeSource->getChar(&c)) {
95 quit = true;
96 break;
97 }
98 if (c == '\n') ++nlines;
99
100 // make a fifo queue of the last 4 characters.
101 cqueue[0] = cqueue[1];
102 cqueue[1] = c;
103
104 // print header
105 if (cqueue[0] == '\n' && cqueue[1] != '\t' && cqueue[1] != ' ') {
106 // it wasn't a space, so put it back as it is most likely
107 // the start of a header name. in any case it terminates the
108 // content part of this header.
110
111 string lowername = name;
112 lowercase(lowername);
113 trim(lowername, ": \t");
114 bool foundMatch = false;
115
116 for (vector<string>::const_iterator i = headers.begin();
117 i != headers.end(); ++i) {
118 string nametmp = *i;
119 lowercase(nametmp);
120 if (nametmp == lowername) {
121 foundMatch = true;
122 break;
123 }
124 }
125
126 if (foundMatch == includeheaders || headers.size() == 0) {
127 string out = name + content;
128 for (string::const_iterator i = out.begin(); i != out.end(); ++i)
129 if (processedbytes >= startoffset && wrotebytes < length) {
130 if (processedbytes >= startoffset) {
131 store += *i;
132 ++wrotebytes;
133 }
134 } else
135 ++processedbytes;
136 }
137
138 // move on to the next header
139 content = "";
140 name = "";
141 break;
142 }
143 content += c;
144 }
145 } // end while loop
146
147 if (name != "") {
148 string lowername = name;
149 lowercase(lowername);
150 trim(lowername, ": \t");
151 bool foundMatch = false;
152 for (vector<string>::const_iterator i = headers.begin();
153 i != headers.end(); ++i) {
154 string nametmp = *i;
155 lowercase(nametmp);
156 if (nametmp == lowername) {
157 foundMatch = true;
158 break;
159 }
160 }
161
162 if (hasHeaderSeparator || foundMatch == includeheaders || headers.size() == 0) {
163 string out = name + content;
164 for (string::const_iterator i = out.begin(); i != out.end(); ++i)
165 if (processedbytes >= startoffset && wrotebytes < length) {
166 store += *i;
167 ++wrotebytes;
168 } else
169 ++processedbytes;
170 }
171 }
172}
The IODevice class provides a framework for reading and writing to device.
Definition: iodevice.h:31
int getFileDescriptor(void) const
void seek(unsigned int offset)
unsigned int headerstartoffsetcrlf
Definition: mime.h:57
void printHeader(int fd, Binc::IODevice &output, std::vector< std::string > headers, bool includeheaders, unsigned int startoffset, unsigned int length, std::string &storage) const
unsigned int nlines
Definition: mime.h:62
Declaration of miscellaneous convertion functions.
Declaration of the IODevice class.
Declaration of the IOFactory class.
The base class of the MIME input source.
Binc::MimeInputSource * mimeSource
Declaration of main mime parser components.
void lowercase(std::string &input)
Definition: convert.h:122
void trim(std::string &s_in, const std::string &chars=" \t\r\n")
Definition: convert.h:137