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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26