| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osg/Timer> |
|---|
| 13 | #include <osg/ArgumentParser> |
|---|
| 14 | #include <osg/ApplicationUsage> |
|---|
| 15 | |
|---|
| 16 | #include <osgDB/Archive> |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <osgDB/WriteFile> |
|---|
| 19 | #include <osgDB/FileUtils> |
|---|
| 20 | |
|---|
| 21 | #include <iostream> |
|---|
| 22 | #include <algorithm> |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | int main( int argc, char **argv ) |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 32 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an application for collecting a set of separate files into a single archive file that can be later read in OSG applications.."); |
|---|
| 33 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 37 | { |
|---|
| 38 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 39 | return 1; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | std::string archiveFilename; |
|---|
| 43 | while (arguments.read("-a",archiveFilename) || arguments.read("--archive",archiveFilename)) |
|---|
| 44 | { |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | bool insert = false; |
|---|
| 48 | while (arguments.read("-i") || arguments.read("--insert")) |
|---|
| 49 | { |
|---|
| 50 | insert = true; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | bool extract = false; |
|---|
| 54 | while (arguments.read("-e") || arguments.read("--extract")) |
|---|
| 55 | { |
|---|
| 56 | extract = true; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | bool list = false; |
|---|
| 60 | while (arguments.read("-l") || arguments.read("--list")) |
|---|
| 61 | { |
|---|
| 62 | list = true; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | typedef std::vector<std::string> FileNameList; |
|---|
| 66 | FileNameList files; |
|---|
| 67 | for(int pos=1;pos<arguments.argc();++pos) |
|---|
| 68 | { |
|---|
| 69 | if (!arguments.isOption(pos)) |
|---|
| 70 | { |
|---|
| 71 | if (insert) |
|---|
| 72 | { |
|---|
| 73 | std::string filePath = osgDB::findDataFile(arguments[pos]); |
|---|
| 74 | osgDB::FileType fileType = osgDB::fileType(filePath); |
|---|
| 75 | if (fileType==osgDB::REGULAR_FILE) |
|---|
| 76 | { |
|---|
| 77 | files.push_back(arguments[pos]); |
|---|
| 78 | } |
|---|
| 79 | else if (fileType==osgDB::DIRECTORY) |
|---|
| 80 | { |
|---|
| 81 | osgDB::DirectoryContents directory = osgDB::getDirectoryContents(arguments[pos]); |
|---|
| 82 | osgDB::DirectoryContents::iterator it = directory.begin(); |
|---|
| 83 | while( it != directory.end()) |
|---|
| 84 | { |
|---|
| 85 | files.push_back(filePath + "/" + (*it)); |
|---|
| 86 | ++it; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | { |
|---|
| 92 | files.push_back(arguments[pos]); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if (arguments.errors()) |
|---|
| 102 | { |
|---|
| 103 | arguments.writeErrorMessages(std::cout); |
|---|
| 104 | return 1; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | if (archiveFilename.empty()) |
|---|
| 108 | { |
|---|
| 109 | std::cout<<"Please specify an archive name using --archive filename"<<std::endl; |
|---|
| 110 | return 1; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (!insert && !extract && !list) |
|---|
| 114 | { |
|---|
| 115 | std::cout<<"Please specify an operation on the archive, either --insert, --extract or --list"<<std::endl; |
|---|
| 116 | return 1; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if (insert && extract) |
|---|
| 120 | { |
|---|
| 121 | std::cout<<"Cannot insert and extract files from the archive at one time, please use either --insert or --extract."<<std::endl; |
|---|
| 122 | return 1; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | osg::ref_ptr<osgDB::Archive> archive; |
|---|
| 126 | |
|---|
| 127 | if (insert) |
|---|
| 128 | { |
|---|
| 129 | archive = osgDB::openArchive(archiveFilename, osgDB::Archive::WRITE); |
|---|
| 130 | |
|---|
| 131 | if (archive.valid()) |
|---|
| 132 | { |
|---|
| 133 | for (FileNameList::iterator itr=files.begin(); |
|---|
| 134 | itr!=files.end(); |
|---|
| 135 | ++itr) |
|---|
| 136 | { |
|---|
| 137 | std::cout<<"reading "<<*itr<<std::endl; |
|---|
| 138 | osg::ref_ptr<osg::Object> obj = osgDB::readObjectFile(*itr); |
|---|
| 139 | if (obj.valid()) |
|---|
| 140 | { |
|---|
| 141 | std::cout<<" write to archive "<<*itr<<std::endl; |
|---|
| 142 | archive->writeObject(*obj, *itr); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | archive = osgDB::openArchive(archiveFilename, osgDB::Archive::READ); |
|---|
| 150 | |
|---|
| 151 | if (extract && archive.valid()) |
|---|
| 152 | { |
|---|
| 153 | for (FileNameList::iterator itr=files.begin(); |
|---|
| 154 | itr!=files.end(); |
|---|
| 155 | ++itr) |
|---|
| 156 | { |
|---|
| 157 | osg::Timer_t start = osg::Timer::instance()->tick(); |
|---|
| 158 | osgDB::ReaderWriter::ReadResult result = archive->readObject(*itr); |
|---|
| 159 | osg::ref_ptr<osg::Object> obj = result.getObject(); |
|---|
| 160 | std::cout<<"readObejct time = "<<osg::Timer::instance()->delta_m(start,osg::Timer::instance()->tick())<<std::endl; |
|---|
| 161 | if (obj.valid()) |
|---|
| 162 | { |
|---|
| 163 | osgDB::writeObjectFile(*obj, *itr); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | if (list && archive.valid()) |
|---|
| 170 | { |
|---|
| 171 | std::cout<<"List of files in archive:"<<std::endl; |
|---|
| 172 | osgDB::Archive::FileNameList fileNames; |
|---|
| 173 | if (archive->getFileNames(fileNames)) |
|---|
| 174 | { |
|---|
| 175 | for(osgDB::Archive::FileNameList::const_iterator itr=fileNames.begin(); |
|---|
| 176 | itr!=fileNames.end(); |
|---|
| 177 | ++itr) |
|---|
| 178 | { |
|---|
| 179 | std::cout<<" "<<*itr<<std::endl; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | std::cout<<std::endl; |
|---|
| 184 | std::cout<<"Master file "<<archive->getMasterFileName()<<std::endl; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | return 0; |
|---|
| 188 | } |
|---|