1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
5 $Id: buildidx.cxx,v 1.2 2005/02/23 10:26:14 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 buildidx.cc
17 * extract a Part based on command line parameters
26 #include <sys/types.h>
28 #include <mimetic/mimetic.h>
31 using namespace mimetic;
33 unsigned int g_indexed = 0;
37 cout << "buildidx [file|dir...]" << endl;
41 void die(bool b, const string& msg)
45 cerr << "Error: " << msg << endl << endl;
50 void indexFile(MimeEntity& head, const string& fqn)
55 MimeEntity* pMe = new MimeEntity;
56 pMe->load(in.begin(), in.end(), ~imHeader);
57 pMe->header().push_back(Field("X-Filename",fqn));
58 head.body().parts().push_back(pMe);
62 cerr << "ERR: unable to open file " << fqn << endl;
66 void indexDirectory(MimeEntity& head, const string& dirFqn)
68 DIR* dir = opendir(dirFqn.c_str());
71 cerr << "ERR: unable to read from " << dirFqn << endl;
77 while(0 != (item = readdir(dir)))
79 ffqn = dirFqn + "/" + item->d_name;
80 memset(&st,0,sizeof(struct stat));
81 if(stat(ffqn.c_str(),&st) == -1)
83 cerr << "ERR: unable to stat " << item->d_name<< endl;
86 if(S_ISREG(st.st_mode) && item->d_name[0] != '.') // reg file
88 indexFile(head, ffqn);
94 void indexItem(MimeEntity& head, const string& item)
97 if(stat(item.c_str(), &st) == -1)
99 cerr << "ERR: unable to stat " << item.c_str() << endl;
102 if(S_ISREG(st.st_mode))
103 indexFile(head, item);
104 else if (S_ISDIR(st.st_mode))
105 indexDirectory(head, item);
107 cerr << "ERR: unknown file type " << item << endl;
110 int main(int argc, char** argv)
112 std::ios_base::sync_with_stdio(false);
114 if(argc > 1 && string(argv[1]) == "-h")
119 // read filenames from stdin
120 while(getline(cin, fqn))
121 indexItem(head, fqn);
123 for(int fc = 1; fc < argc; ++fc)
124 indexItem(head, argv[fc]);
126 cout << head << endl;
127 cerr << "index of " << g_indexed << " file(s) built" << endl;