/[debian]/mimetic/branches/upstream/current/examples/buildidx.cxx
ViewVC logotype

Annotation of /mimetic/branches/upstream/current/examples/buildidx.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2647 - (hide annotations)
Sat Sep 10 15:09:38 2011 UTC (10 years ago) by gregoa
File size: 2944 byte(s)
[svn-upgrade] new version mimetic (0.9.7)
1 gregoa 128 /***************************************************************************
2 gregoa 1442 copyright : (C) 2002-2008 by Stefano Barbato
3 gregoa 128 email : stefano@codesink.org
4    
5 gregoa 1457 $Id: buildidx.cxx,v 1.3 2008-10-07 11:06:25 tat Exp $
6 gregoa 128 ***************************************************************************/
7    
8     /** \example buildidx.cc
9     * extract a Part based on command line parameters
10     * more info on:
11     * buildidx -h
12     */
13     #include <iostream>
14     #include <sstream>
15     #include <iterator>
16     #include <fstream>
17     #include <cassert>
18     #include <sys/types.h>
19     #include <dirent.h>
20     #include <mimetic/mimetic.h>
21    
22     using namespace std;
23     using namespace mimetic;
24    
25     unsigned int g_indexed = 0;
26    
27     void usage()
28     {
29     cout << "buildidx [file|dir...]" << endl;
30     exit(-1);
31     }
32    
33     void die(bool b, const string& msg)
34     {
35     if(b)
36     {
37     cerr << "Error: " << msg << endl << endl;
38     usage();
39     }
40     }
41    
42     void indexFile(MimeEntity& head, const string& fqn)
43     {
44     File in(fqn);
45     if(in)
46     {
47     MimeEntity* pMe = new MimeEntity;
48     pMe->load(in.begin(), in.end(), ~imHeader);
49     pMe->header().push_back(Field("X-Filename",fqn));
50     head.body().parts().push_back(pMe);
51     cerr << "." << flush;
52     g_indexed++;
53     } else
54     cerr << "ERR: unable to open file " << fqn << endl;
55    
56     }
57    
58     void indexDirectory(MimeEntity& head, const string& dirFqn)
59     {
60     DIR* dir = opendir(dirFqn.c_str());
61     if(dir == 0)
62     {
63     cerr << "ERR: unable to read from " << dirFqn << endl;
64     return;
65     }
66     struct dirent* item;
67     struct stat st;
68     string ffqn;
69     while(0 != (item = readdir(dir)))
70     {
71     ffqn = dirFqn + "/" + item->d_name;
72     memset(&st,0,sizeof(struct stat));
73     if(stat(ffqn.c_str(),&st) == -1)
74     {
75     cerr << "ERR: unable to stat " << item->d_name<< endl;
76     return;
77     }
78     if(S_ISREG(st.st_mode) && item->d_name[0] != '.') // reg file
79     {
80     indexFile(head, ffqn);
81     }
82     }
83     closedir(dir);
84     }
85    
86     void indexItem(MimeEntity& head, const string& item)
87     {
88     struct stat st;
89     if(stat(item.c_str(), &st) == -1)
90     {
91     cerr << "ERR: unable to stat " << item.c_str() << endl;
92     return;
93     }
94     if(S_ISREG(st.st_mode))
95     indexFile(head, item);
96     else if (S_ISDIR(st.st_mode))
97     indexDirectory(head, item);
98     else
99     cerr << "ERR: unknown file type " << item << endl;
100     }
101    
102     int main(int argc, char** argv)
103     {
104     std::ios_base::sync_with_stdio(false);
105    
106     if(argc > 1 && string(argv[1]) == "-h")
107     usage();
108     MultipartMixed head;
109     string fqn;
110     if(argc == 1) {
111     // read filenames from stdin
112     while(getline(cin, fqn))
113     indexItem(head, fqn);
114     } else {
115     for(int fc = 1; fc < argc; ++fc)
116     indexItem(head, argv[fc]);
117     }
118     cout << head << endl;
119     cerr << "index of " << g_indexed << " file(s) built" << endl;
120     return g_indexed;
121     }
122    

  ViewVC Help
Powered by ViewVC 1.1.26