packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmArchiveWrite.h
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2010 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #ifndef cmArchiveWrite_h
13 #define cmArchiveWrite_h
14
15 #include "cmStandardIncludes.h"
16
17 #if !defined(CMAKE_BUILD_WITH_CMAKE)
18 # error "cmArchiveWrite not allowed during bootstrap build!"
19 #endif
20
21 /** \class cmArchiveWrite
22  * \brief Wrapper around libarchive for writing.
23  *
24  */
25 class cmArchiveWrite
26 {
27   typedef void (cmArchiveWrite::* safe_bool)();
28   void safe_bool_true() {}
29 public:
30   /** Compression type.  */
31   enum Compress
32   {
33     CompressNone,
34     CompressCompress,
35     CompressGZip,
36     CompressBZip2,
37     CompressLZMA,
38     CompressXZ
39   };
40
41   /** Archive Type */
42   enum Type
43   {
44     TypeTAR,
45     TypeZIP
46   };
47
48   /** Construct with output stream to which to write archive.  */
49   cmArchiveWrite(std::ostream& os, Compress c = CompressNone, Type = TypeTAR);
50   ~cmArchiveWrite();
51
52   /**
53    * Add a path (file or directory) to the archive.  Directories are
54    * added recursively.  The "path" must be readable on disk, either
55    * full path or relative to current working directory.  The "skip"
56    * value indicates how many leading bytes from the input path to
57    * skip.  The remaining part of the input path is appended to the
58    * "prefix" value to construct the final name in the archive.
59    */
60   bool Add(std::string path, size_t skip = 0, const char* prefix = 0);
61
62   /** Returns true if there has been no error.  */
63   operator safe_bool() const
64     { return this->Okay()? &cmArchiveWrite::safe_bool_true : 0; }
65
66   /** Returns true if there has been an error.  */
67   bool operator!() const { return !this->Okay(); }
68
69   /** Return the error string; empty if none.  */
70   std::string GetError() const { return this->Error; }
71
72   // TODO: More general callback instead of hard-coding calls to
73   // std::cout.
74   void SetVerbose(bool v) { this->Verbose = v; }
75
76 private:
77   bool Okay() const { return this->Error.empty(); }
78   bool AddPath(const char* path, size_t skip, const char* prefix);
79   bool AddFile(const char* file, size_t skip, const char* prefix);
80   bool AddData(const char* file, size_t size);
81
82   struct Callback;
83   friend struct Callback;
84
85   class Entry;
86
87   std::ostream& Stream;
88   struct archive* Archive;
89   struct archive* Disk;
90   bool Verbose;
91   std::string Error;
92 };
93
94 #endif