cmArchiveWrite.h 2.7 KB

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