/[debian]/mimetic/branches/upstream/current/examples/mbox.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/examples/mbox.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File size: 5768 byte(s)
[svn-inject] Installing original source of mimetic
1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: mbox.cxx,v 1.2 2005/02/23 10:26:14 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 /** \example mbox.cc
17 * extract a Part based on command line parameters
18 * more info on:
19 * mbox -h
20 */
21 #include <iostream>
22 #include <sstream>
23 #include <memory>
24 #include <algorithm>
25 #include <iterator>
26 #include <fstream>
27 #include <cassert>
28 #include <mimetic/mimetic.h>
29 #include <mimetic/parser/itparser.h>
30 //#include <mimetic/mbx/mbox.h>
31
32 using namespace std;
33 using namespace mimetic;
34
35 unsigned int g_matches = 0;
36
37 int g_verbose; // verbose mode on/off
38 int g_quiet; // quiet mode
39 int g_entityCount; // num of entities found
40
41 void usage()
42 {
43 cout << "mbox [params] [in_file...]" << endl;
44 exit(-1);
45 }
46
47
48 void die_if(bool b, const string& msg)
49 {
50 if(b)
51 {
52 cerr << "Error: " << msg << endl << endl;
53 usage();
54 }
55 }
56
57 inline int isnl(char c)
58 {
59 return c == '\n' || c == '\r';
60 }
61
62 void printTabs(int c)
63 {
64 while(c--)
65 cout << " ";
66 }
67
68 void printMimeStructure(MimeEntity* pMe, int tabcount = 0)
69 {
70 ++g_entityCount;
71 if(!g_quiet)
72 {
73 Header& h = pMe->header();
74 ContentType ct = h.contentType();
75 printTabs(tabcount);
76 cout << ct.type() << "/" << ct.subtype() << endl;
77 if(g_verbose)
78 {
79 ContentType::ParamList::iterator bit, eit;
80 bit = ct.paramList().begin();
81 eit = ct.paramList().end();
82 for(; bit != eit; ++bit)
83 {
84 printTabs(tabcount);
85 cout << "param: " << bit->name() << " = "
86 << bit->value() << endl;
87 }
88 if(h.hasField(ContentTransferEncoding::label))
89 {
90 printTabs(tabcount);
91 cout << "encoding: "
92 << h.contentTransferEncoding().mechanism()
93 << endl;
94 }
95 if(h.hasField(ContentDisposition::label))
96 {
97 printTabs(tabcount);
98 const ContentDisposition cd = h.contentDisposition();
99 cout << "disposition: " << cd.type() << endl;
100 ContentDisposition::ParamList::const_iterator
101 bit, eit;
102 bit = cd.paramList().begin();
103 eit = cd.paramList().end();
104 for(; bit != eit; ++bit)
105 {
106 printTabs(tabcount);
107 cout << "param: " << bit->name() << " = "
108 << bit->value() << endl;
109 }
110 }
111 Header::iterator hbit, heit;
112 hbit = pMe->header().begin();
113 heit = pMe->header().end();
114 for(; hbit != heit; ++hbit)
115 {
116 printTabs(tabcount);
117 cout << "h: " << hbit->name() << " = "
118 << hbit->value() << endl;
119 }
120 if(pMe->body().preamble().length())
121 {
122 printTabs(tabcount);
123 cout << "preamble: " << pMe->body().preamble()
124 << endl;
125 }
126 if(pMe->body().epilogue().length())
127 {
128 printTabs(tabcount);
129 cout << "epilogue: " << pMe->body().epilogue()
130 << endl;
131 }
132 printTabs(tabcount);
133 cout << "part size: " << pMe->size() << endl;
134 printTabs(tabcount);
135 cout << "body size: " << pMe->body().length() << endl;
136 cout << endl;
137 }
138 }
139 MimeEntityList::iterator mbit = pMe->body().parts().begin(),
140 meit = pMe->body().parts().end();
141 for(;mbit!=meit;++mbit)
142 printMimeStructure(*mbit, 1 + tabcount);
143 }
144
145 int main(int argc, char** argv)
146 {
147 std::ios_base::sync_with_stdio(false);
148
149 die_if(argc < 2, "mbox filename required");
150 die_if(!File::exists(argv[1]),
151 string("Unable to access file: ") + argv[1]);
152
153 File f(argv[1]);
154 File::iterator bit = f.begin(), eit = f.end(), it;
155 int count = 0;
156 do
157 {
158 // skip "From blah blah" line
159 for(; !isnl(*bit); ++bit)
160 ;
161 if(isnl(*++bit))
162 ++bit; // jump 2 chars new lines
163 it = utils::find_bm(bit, eit, "From ");
164 if(!isnl(*(it-1)))
165 continue;
166 MimeEntity me(bit, it);
167 printMimeStructure(&me);
168 bit = it;
169 ++count;
170 } while(bit != eit);
171
172 cerr << count << " messages parsed" << endl;
173 /*
174 MboxMailbox mm(argv[1], ~Parser::imHeader);
175 copy(mm.begin(), mm.end(), osi);
176 cout << endl;
177 return 0;
178
179 int count;
180 MboxMailbox mm(argv[1], ~Parser::imHeader);
181 MboxMailbox::const_iterator mbit = mm.begin(), meit = mm.end();
182 for(count = 0; mbit != meit; ++mbit, ++count, ++osi)
183 {
184 const MimeEntity& e = *mbit;
185 *osi = *mbit;
186 }
187 cerr << count << " messages parsed" << endl;
188 */
189 return 0;
190 }
191

  ViewVC Help
Powered by ViewVC 1.1.26