zipios  2.2.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-2019 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
33 #include "zipios/zipfile.hpp"
35 
36 #include <cstring>
37 
38 #include <stdlib.h>
39 
40 
46 namespace
47 {
48 
54 char *g_progname;
55 
56 
62 void usage()
63 {
64  std::cout << "Usage: " << g_progname << " [-opt] [file]" << std::endl;
65  std::cout << "Where -opt is one or more of:" << std::endl;
66  std::cout << " --count count the number of files in a .zip archive" << std::endl;
67  std::cout << " --count-directories count the number of files in a .zip archive" << std::endl;
68  std::cout << " --count-files count the number of files in a .zip archive" << std::endl;
69  std::cout << " --help show this help screen" << std::endl;
70  std::cout << " --version print the library version and exit" << std::endl;
71  std::cout << " --version-tool print the tool version and exit" << std::endl;
72  exit(1);
73 }
74 
75 
81 enum class func_t
82 {
88  UNDEFINED,
89 
95  COUNT,
96 
102  COUNT_DIRECTORIES,
103 
111 };
112 
113 } // no name namespace
114 
115 
116 int main(int argc, char *argv[])
117 {
118  // define program name
119  {
120  g_progname = argv[0];
121  char *e(strrchr(g_progname, '/'));
122  if(e)
123  {
124  g_progname = e + 1;
125  }
126  e = strrchr(g_progname, '\\');
127  if(e)
128  {
129  g_progname = e + 1;
130  }
131  }
132 
133  try
134  {
135  // check the various command line options
136  std::vector<std::string> files;
137  func_t function(func_t::UNDEFINED);
138  for(int i(1); i < argc; ++i)
139  {
140  if(argv[i][0] == '-')
141  {
142  if(strcmp(argv[i], "--help") == 0)
143  {
144  usage();
145  }
146  if(strcmp(argv[i], "--version") == 0)
147  {
148  // version of the .so library
149  std::cout << zipios::getVersion() << std::endl;
150  exit(0);
151  }
152  if(strcmp(argv[i], "--version-tool") == 0)
153  {
154  // version of this tool (compiled with this version)
155  // it should be the same as the --version
156  std::cout << ZIPIOS_VERSION_STRING << std::endl;
157  exit(0);
158  }
159  if(strcmp(argv[i], "--count") == 0)
160  {
161  function = func_t::COUNT;
162  }
163  else if(strcmp(argv[i], "--count-directories") == 0)
164  {
165  function = func_t::COUNT_DIRECTORIES;
166  }
167  else if(strcmp(argv[i], "--count-files") == 0)
168  {
169  function = func_t::COUNT_FILES;
170  }
171  }
172  else
173  {
174  files.push_back(argv[i]);
175  }
176  }
177 
178  switch(function)
179  {
180  case func_t::COUNT:
181  for(auto it(files.begin()); it != files.end(); ++it)
182  {
183  zipios::ZipFile zf(*it);
184  if(files.size() > 1)
185  {
186  // write filename in case there is more than one file
187  std::cout << *it << ": ";
188  }
189  std::cout << zf.entries().size() << std::endl;
190  }
191  break;
192 
193  case func_t::COUNT_DIRECTORIES:
194  for(auto it(files.begin()); it != files.end(); ++it)
195  {
196  zipios::ZipFile zf(*it);
197  if(files.size() > 1)
198  {
199  // write filename in case there is more than one file
200  std::cout << *it << ": ";
201  }
202  int count(0);
203  zipios::FileEntry::vector_t entries(zf.entries());
204  for(auto entry(entries.begin()); entry != entries.end(); ++entry)
205  {
206  if((*entry)->isDirectory())
207  {
208  ++count;
209  }
210  }
211  std::cout << count << std::endl;
212  }
213  break;
214 
215  case func_t::COUNT_FILES:
216  for(auto it(files.begin()); it != files.end(); ++it)
217  {
218  zipios::ZipFile zf(*it);
219  if(files.size() > 1)
220  {
221  // write filename in case there is more than one file
222  std::cout << *it << ": ";
223  }
224  int count(0);
225  zipios::FileEntry::vector_t entries(zf.entries());
226  for(auto entry(entries.begin()); entry != entries.end(); ++entry)
227  {
228  if(!(*entry)->isDirectory())
229  {
230  ++count;
231  }
232  }
233  std::cout << count << std::endl;
234  }
235  break;
236 
237  default:
238  std::cerr << g_progname << ":error: undefined function." << std::endl;
239  usage();
240  break;
241 
242  }
243  }
244  catch(zipios::Exception const & e)
245  {
246  std::cerr << g_progname << ":error: an exception occurred: "
247  << e.what() << std::endl;
248  }
249 
250  return 0;
251 }
252 
253 
254 // Local Variables:
255 // mode: cpp
256 // indent-tabs-mode: nil
257 // c-basic-offset: 4
258 // tab-width: 4
259 // End:
260 
261 // vim: ts=4 sw=4 et
zipios::FileCollection::entries
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
Definition: filecollection.cpp:396
zipios::getVersion
char const * getVersion()
Definition: zipios-config.hpp.in:53
zipfile.hpp
Define the zipios::ZipFile class.
anonymous_namespace{zipios.cpp}::g_progname
char * g_progname
Name of the program.
Definition: zipios.cpp:54
zipiosexceptions.hpp
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
anonymous_namespace{zipios.cpp}::func_t::UNDEFINED
@ UNDEFINED
Still undefined.
zipios::Exception
Base exception of the zipios environment.
Definition: zipiosexceptions.hpp:58
anonymous_namespace{zipios.cpp}::func_t
func_t
The function to apply.
Definition: zipios.cpp:82
zipios::ZipFile
The ZipFile class represents a collection of files.
Definition: zipfile.hpp:48
anonymous_namespace{zipios.cpp}::usage
void usage()
Usage of the zipios tool.
Definition: zipios.cpp:62
main
int main(int argc, char *argv[])
Definition: zipios.cpp:116
zipios::FileEntry::vector_t
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:79
ZIPIOS_VERSION_STRING
#define ZIPIOS_VERSION_STRING
Definition: zipios-config.hpp.in:46