1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
5 $Id: structure.cxx,v 1.5 2007/01/21 14:55:33 tat Exp $
6 ***************************************************************************/
8 /***************************************************************************
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. *
15 ***************************************************************************/
16 /** \example structure.cc
17 * Usage: structure [-ed] [in_file [out_file]]
19 * Reads in_file (or standard input) and writes its MIME structure to out_file
20 * (or standard output)
28 #include <mimetic/mimetic.h>
29 #include <mimetic/utils.h>
32 using namespace mimetic;
34 int g_verbose; // verbose mode on/off
35 int g_quiet; // quiet mode
36 int g_entityCount; // num of entities found
44 void printMimeStructure(MimeEntity* pMe, int tabcount = 0)
49 Header& h = pMe->header();
50 ContentType ct = h.contentType();
51 cout << g_entityCount << " ";
53 cout << ct.type() << "/" << ct.subtype() << endl;
56 ContentType::ParamList::iterator bit, eit;
57 bit = ct.paramList().begin();
58 eit = ct.paramList().end();
59 for(; bit != eit; ++bit)
62 cout << "param: " << bit->name() << " = "
63 << bit->value() << endl;
65 if(h.hasField(ContentTransferEncoding::label))
69 << h.contentTransferEncoding().mechanism()
72 if(h.hasField(ContentDisposition::label))
75 const ContentDisposition cd = h.contentDisposition();
76 cout << "disposition: " << cd.type() << endl;
77 ContentDisposition::ParamList::const_iterator
79 bit = cd.paramList().begin();
80 eit = cd.paramList().end();
81 for(; bit != eit; ++bit)
84 cout << "param: " << bit->name() << " = "
85 << bit->value() << endl;
88 Header::iterator hbit, heit;
89 hbit = pMe->header().begin();
90 heit = pMe->header().end();
91 for(; hbit != heit; ++hbit)
94 cout << "h: " << hbit->name() << " = "
95 << hbit->value() << endl;
97 if(pMe->body().preamble().length())
100 cout << "preamble: " << pMe->body().preamble()
103 if(pMe->body().epilogue().length())
106 cout << "epilogue: " << pMe->body().epilogue()
110 cout << "part size: " << pMe->size() << endl;
112 cout << "body size: " << pMe->body().length() << endl;
116 MimeEntityList::iterator mbit = pMe->body().parts().begin(),
117 meit = pMe->body().parts().end();
118 for(;mbit!=meit;++mbit)
119 printMimeStructure(*mbit, 1 + tabcount);
126 cout << "structure [-v] [in_file]..." << endl;
127 cout << " -v Verbose mode" << endl;
128 cout << " -q totaly quiet; exit code = num of entities" << endl;
133 int main(int argc, char** argv)
135 std::ios_base::sync_with_stdio(false);
137 int iMask = imBody | imPreamble | imEpilogue;
142 string first = argv[1];
145 else if(first == "-v")
147 else if(first == "-q")
149 fidx = (g_verbose || g_quiet ? 2 : 1); // first filename idx
153 istreambuf_iterator<char> bit(std::cin), eit;
154 MimeEntity me(bit,eit, iMask);
155 printMimeStructure(&me);
157 for(int fc = fidx; fc < argc; ++fc)
162 cerr << "ERR: unable to open file "
167 MimeEntity me(in.begin(), in.end(),iMask);
168 printMimeStructure(&me);
171 return g_entityCount;