zipios  2.1.0
Zipios++ – a small C++ library that provides easy access to .zip files.
zipios.cpp
Go to the documentation of this file.
1 /*
2  Zipios++ - a small C++ library that provides easy access to .zip files.
3 
4  Copyright (C) 2000-2007 Thomas Sondergaard
5  Copyright (C) 2015 Made to Order Software Corporation
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
33 #include "zipios/zipfile.hpp"
34 
35 #include <cstring>
36 
37 #include <stdlib.h>
38 
39 
45 namespace
46 {
47 
53 char *g_progname;
54 
55 
61 void usage()
62 {
63  std::cout << "Usage: " << g_progname << " [-opt] [file]" << std::endl;
64  std::cout << "Where -opt is one or more of:" << std::endl;
65  std::cout << " --count count the number of files in a .zip archive" << std::endl;
66  std::cout << " --count-directories count the number of files in a .zip archive" << std::endl;
67  std::cout << " --count-files count the number of files in a .zip archive" << std::endl;
68  std::cout << " --help show this help screen" << std::endl;
69  std::cout << " --version print the library version and exit" << std::endl;
70  std::cout << " --version-tool print the tool version and exit" << std::endl;
71  exit(1);
72 }
73 
74 
80 enum class func_t
81 {
87  UNDEFINED,
88 
94  COUNT,
95 
102 
110 };
111 
112 } // no name namespace
113 
114 
115 int main(int argc, char *argv[])
116 {
117  // define program name
118  g_progname = argv[0];
119  char *e(strrchr(g_progname, '/'));
120  if(e)
121  {
122  g_progname = e + 1;
123  }
124  e = strrchr(g_progname, '\\');
125  if(e)
126  {
127  g_progname = e + 1;
128  }
129 
130  // check the various command line options
131  std::vector<std::string> files;
132  func_t function(func_t::UNDEFINED);
133  for(int i(1); i < argc; ++i)
134  {
135  if(argv[i][0] == '-')
136  {
137  if(strcmp(argv[i], "--help") == 0)
138  {
139  usage();
140  }
141  if(strcmp(argv[i], "--version") == 0)
142  {
143  // version of the .so library
144  std::cout << zipios::getVersion() << std::endl;
145  exit(0);
146  }
147  if(strcmp(argv[i], "--version-tool") == 0)
148  {
149  // version of this tool (compiled with this version)
150  // it should be the same as the --version
151  std::cout << ZIPIOS_VERSION_STRING << std::endl;
152  exit(0);
153  }
154  if(strcmp(argv[i], "--count") == 0)
155  {
156  function = func_t::COUNT;
157  }
158  else if(strcmp(argv[i], "--count-directories") == 0)
159  {
160  function = func_t::COUNT_DIRECTORIES;
161  }
162  else if(strcmp(argv[i], "--count-files") == 0)
163  {
164  function = func_t::COUNT_FILES;
165  }
166  }
167  else
168  {
169  files.push_back(argv[i]);
170  }
171  }
172 
173  switch(function)
174  {
175  case func_t::COUNT:
176  for(auto it(files.begin()); it != files.end(); ++it)
177  {
178  zipios::ZipFile zf(*it);
179  if(files.size() > 1)
180  {
181  // write filename in case there is more than one file
182  std::cout << *it << ": ";
183  }
184  std::cout << zf.entries().size() << std::endl;
185  }
186  break;
187 
188  case func_t::COUNT_DIRECTORIES:
189  for(auto it(files.begin()); it != files.end(); ++it)
190  {
191  zipios::ZipFile zf(*it);
192  if(files.size() > 1)
193  {
194  // write filename in case there is more than one file
195  std::cout << *it << ": ";
196  }
197  int count(0);
198  zipios::FileEntry::vector_t entries(zf.entries());
199  for(auto entry(entries.begin()); entry != entries.end(); ++entry)
200  {
201  if((*entry)->isDirectory())
202  {
203  ++count;
204  }
205  }
206  std::cout << count << std::endl;
207  }
208  break;
209 
210  case func_t::COUNT_FILES:
211  for(auto it(files.begin()); it != files.end(); ++it)
212  {
213  zipios::ZipFile zf(*it);
214  if(files.size() > 1)
215  {
216  // write filename in case there is more than one file
217  std::cout << *it << ": ";
218  }
219  int count(0);
220  zipios::FileEntry::vector_t entries(zf.entries());
221  for(auto entry(entries.begin()); entry != entries.end(); ++entry)
222  {
223  if(!(*entry)->isDirectory())
224  {
225  ++count;
226  }
227  }
228  std::cout << count << std::endl;
229  }
230  break;
231 
232  default:
233  std::cerr << g_progname << ":error: undefined function." << std::endl;
234  usage();
235  break;
236 
237  }
238 
239  return 0;
240 }
241 
242 
243 // Local Variables:
244 // mode: cpp
245 // indent-tabs-mode: nil
246 // c-basic-offset: 4
247 // tab-width: 4
248 // End:
249 
250 // vim: ts=4 sw=4 et
The ZipFile class represents a collection of files.
Definition: zipfile.hpp:47
Count the number of files in a Zip archive.
int main(int argc, char *argv[])
Definition: zipios.cpp:115
char const * getVersion()
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
char * g_progname
Name of the program.
Definition: zipios.cpp:53
func_t
The function to apply.
Definition: zipios.cpp:80
Define the zipios::ZipFile class.
Count the number of regular files in a Zip archive.
void usage()
Usage of the zipios tool.
Definition: zipios.cpp:61
Count the number of directories in a Zip archive.
#define ZIPIOS_VERSION_STRING
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:78