/[debian]/mimetic/trunk/examples/structure.cxx
ViewVC logotype

Contents of /mimetic/trunk/examples/structure.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2649 - (show annotations)
Sat Sep 10 15:14:42 2011 UTC (9 years, 10 months ago) by gregoa
File size: 4924 byte(s)
New upstream release.
1 /***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: structure.cxx,v 1.6 2008-10-07 11:06:25 tat Exp $
6 ***************************************************************************/
7
8 /** \example structure.cc
9 * Usage: structure [-ed] [in_file [out_file]]
10 *
11 * Reads in_file (or standard input) and writes its MIME structure to out_file
12 * (or standard output)
13 */
14
15 #include <iostream>
16 #include <fstream>
17 #include <sstream>
18 #include <iterator>
19 #include <streambuf>
20 #include <mimetic/mimetic.h>
21 #include <mimetic/utils.h>
22
23 using namespace std;
24 using namespace mimetic;
25
26 int g_verbose; // verbose mode on/off
27 int g_quiet; // quiet mode
28 int g_entityCount; // num of entities found
29
30 void printTabs(int c)
31 {
32 while(c--)
33 cout << " ";
34 }
35
36 void printMimeStructure(MimeEntity* pMe, int tabcount = 0)
37 {
38 ++g_entityCount;
39 if(!g_quiet)
40 {
41 Header& h = pMe->header();
42 ContentType ct = h.contentType();
43 cout << g_entityCount << " ";
44 printTabs(tabcount);
45 cout << ct.type() << "/" << ct.subtype() << endl;
46 if(g_verbose)
47 {
48 ContentType::ParamList::iterator bit, eit;
49 bit = ct.paramList().begin();
50 eit = ct.paramList().end();
51 for(; bit != eit; ++bit)
52 {
53 printTabs(tabcount);
54 cout << "param: " << bit->name() << " = "
55 << bit->value() << endl;
56 }
57 if(h.hasField(ContentTransferEncoding::label))
58 {
59 printTabs(tabcount);
60 cout << "encoding: "
61 << h.contentTransferEncoding().mechanism()
62 << endl;
63 }
64 if(h.hasField(ContentDisposition::label))
65 {
66 printTabs(tabcount);
67 const ContentDisposition cd = h.contentDisposition();
68 cout << "disposition: " << cd.type() << endl;
69 ContentDisposition::ParamList::const_iterator
70 bit, eit;
71 bit = cd.paramList().begin();
72 eit = cd.paramList().end();
73 for(; bit != eit; ++bit)
74 {
75 printTabs(tabcount);
76 cout << "param: " << bit->name() << " = "
77 << bit->value() << endl;
78 }
79 }
80 Header::iterator hbit, heit;
81 hbit = pMe->header().begin();
82 heit = pMe->header().end();
83 for(; hbit != heit; ++hbit)
84 {
85 printTabs(tabcount);
86 cout << "h: " << hbit->name() << " = "
87 << hbit->value() << endl;
88 }
89 if(pMe->body().preamble().length())
90 {
91 printTabs(tabcount);
92 cout << "preamble: " << pMe->body().preamble()
93 << endl;
94 }
95 if(pMe->body().epilogue().length())
96 {
97 printTabs(tabcount);
98 cout << "epilogue: " << pMe->body().epilogue()
99 << endl;
100 }
101 printTabs(tabcount);
102 cout << "part size: " << pMe->size() << endl;
103 printTabs(tabcount);
104 cout << "body size: " << pMe->body().length() << endl;
105 cout << endl;
106 }
107 }
108 MimeEntityList::iterator mbit = pMe->body().parts().begin(),
109 meit = pMe->body().parts().end();
110 for(;mbit!=meit;++mbit)
111 printMimeStructure(*mbit, 1 + tabcount);
112 }
113
114
115
116 void usage()
117 {
118 cout << "structure [-v] [in_file]..." << endl;
119 cout << " -v Verbose mode" << endl;
120 cout << " -q totaly quiet; exit code = num of entities" << endl;
121 cout << endl;
122 exit(1);
123 }
124
125 int main(int argc, char** argv)
126 {
127 std::ios_base::sync_with_stdio(false);
128 int fidx = 1;
129 int iMask = imBody | imPreamble | imEpilogue;
130
131 if(argc > 1)
132 {
133 g_verbose = 0;
134 string first = argv[1];
135 if(first == "-h")
136 usage();
137 else if(first == "-v")
138 g_verbose = 1;
139 else if(first == "-q")
140 g_quiet = 1;
141 fidx = (g_verbose || g_quiet ? 2 : 1); // first filename idx
142 }
143 if(argc == fidx)
144 {
145 istreambuf_iterator<char> bit(std::cin), eit;
146 MimeEntity me(bit,eit, iMask);
147 printMimeStructure(&me);
148 } else {
149 for(int fc = fidx; fc < argc; ++fc)
150 {
151 File in(argv[fc]);
152 if(!in)
153 {
154 cerr << "ERR: unable to open file "
155 << argv[fc]
156 << endl;
157 continue;
158 }
159 MimeEntity me(in.begin(), in.end(),iMask);
160 printMimeStructure(&me);
161 }
162 }
163 return g_entityCount;
164 }
165

  ViewVC Help
Powered by ViewVC 1.1.26