archive.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "archive.h"
  18. #include "idatastream.h"
  19. #include "iarchive.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <vector>
  23. #include "stream/filestream.h"
  24. #include "stream/textfilestream.h"
  25. #include "string/string.h"
  26. #include "os/path.h"
  27. #include "os/file.h"
  28. #include "os/dir.h"
  29. #include "archivelib.h"
  30. #include "fs_path.h"
  31. #include "vfspk3.h"
  32. class DirectoryArchive : public Archive
  33. {
  34. CopiedString m_root;
  35. public:
  36. DirectoryArchive(const char* root) : m_root(root)
  37. {
  38. }
  39. void release()
  40. {
  41. delete this;
  42. }
  43. virtual ArchiveFile* openFile(const char* name)
  44. {
  45. UnixPath path(m_root.c_str());
  46. path.push_filename(name);
  47. DirectoryArchiveFile* file = new DirectoryArchiveFile(name, path.c_str());
  48. if(!file->failed())
  49. {
  50. return file;
  51. }
  52. file->release();
  53. return 0;
  54. }
  55. virtual ArchiveTextFile* openTextFile(const char* name)
  56. {
  57. UnixPath path(m_root.c_str());
  58. path.push_filename(name);
  59. DirectoryArchiveTextFile* file = new DirectoryArchiveTextFile(name, path.c_str());
  60. if(!file->failed())
  61. {
  62. return file;
  63. }
  64. file->release();
  65. return 0;
  66. }
  67. virtual bool containsFile(const char* name)
  68. {
  69. UnixPath path(m_root.c_str());
  70. path.push_filename(name);
  71. return file_readable(path.c_str());
  72. }
  73. virtual void forEachFile(VisitorFunc visitor, const char* root)
  74. {
  75. std::vector<Directory*> dirs;
  76. UnixPath path(m_root.c_str());
  77. path.push(root);
  78. dirs.push_back(directory_open(path.c_str()));
  79. while(!dirs.empty() && directory_good(dirs.back()))
  80. {
  81. const char* name = directory_read_and_increment(dirs.back());
  82. if(name == 0)
  83. {
  84. directory_close(dirs.back());
  85. dirs.pop_back();
  86. path.pop();
  87. }
  88. else if(!string_equal(name, ".") && !string_equal(name, ".."))
  89. {
  90. path.push_filename(name);
  91. bool is_directory = file_is_directory(path.c_str());
  92. if(!is_directory)
  93. visitor.file(path_make_relative(path.c_str(), m_root.c_str()));
  94. path.pop();
  95. if(is_directory)
  96. {
  97. path.push(name);
  98. if(!visitor.directory(path_make_relative(path.c_str(), m_root.c_str()), dirs.size()))
  99. dirs.push_back(directory_open(path.c_str()));
  100. else
  101. path.pop();
  102. }
  103. }
  104. }
  105. }
  106. };
  107. Archive* OpenArchive(const char* name)
  108. {
  109. return new DirectoryArchive(name);
  110. }
  111. #if 0
  112. class TestArchive
  113. {
  114. class TestVisitor : public Archive::IVisitor
  115. {
  116. public:
  117. virtual void visit(const char* name)
  118. {
  119. int bleh = 0;
  120. }
  121. };
  122. public:
  123. void test1()
  124. {
  125. Archive* archive = OpenArchive("d:/quake/id1/");
  126. ArchiveFile* file = archive->openFile("quake101.wad");
  127. if(file != 0)
  128. {
  129. char buffer[1024];
  130. file->getInputStream().read(buffer, 1024);
  131. file->release();
  132. }
  133. TestVisitor visitor;
  134. archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 0), "");
  135. archive->release();
  136. }
  137. void test2()
  138. {
  139. Archive* archive = OpenArchive("d:/gtkradiant_root/baseq3/");
  140. TestVisitor visitor;
  141. archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 2), "");
  142. archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFiles, 1), "textures");
  143. archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eDirectories, 1), "textures");
  144. archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "textures");
  145. archive->release();
  146. }
  147. TestArchive()
  148. {
  149. test1();
  150. test2();
  151. }
  152. };
  153. TestArchive g_test;
  154. #endif