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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26