Aggregate/Aggregate.cpp

This sample shows how to aggregate two or more dwfs (DWFToolkit::DWFPackageReader DWFToolkit::DWFPackageWriter DWFToolkit::DWFContentManager).

00001 //
00002 //  Copyright (c) 2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED,
00008 //  AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE
00009 //  WORKS WHICH INCORPORATE IT.
00010 //
00011 //  AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS
00012 //  AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING
00013 //  CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00014 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00015 //
00016 //  Use, duplication, or disclosure by the U.S. Government is subject to
00017 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00018 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00019 //  Data and Computer Software), as applicable.
00020 //
00021 
00022 //#include <vld.h>
00023 #include <iostream>
00024 #include <set>
00025 
00026 #include "dwfcore/File.h"
00027 #include "dwfcore/String.h"
00028 #include "dwfcore/MIME.h"
00029 #include "dwfcore/StreamFileDescriptor.h"
00030 #include "dwfcore/FileInputStream.h"
00031 
00032 #include "dwf/Version.h"
00033 #include "dwf/package/Constants.h"
00034 #include "dwf/package/Manifest.h"
00035 #include "dwf/package/ContentManager.h"
00036 #include "dwf/package/writer/DWF6PackageWriter.h"
00037 #include "dwf/package/reader/PackageReader.h"
00038 
00039 using namespace DWFCore;
00040 using namespace DWFToolkit;
00041 
00042 #define _DO_CRTDBG_MEMORY_LEAK_CHECK_
00043 #if defined(_DWFCORE_WIN32_SYSTEM) && defined(_DO_CRTDBG_MEMORY_LEAK_CHECK_)
00044     #include <crtdbg.h>
00045 #endif
00046 
00047 bool processArgs( int argc, 
00048                   char* argv[], 
00049                   std::vector<DWFString>& oInFiles, 
00050                   DWFString& zOutFile, 
00051                   DWFPackageWriter::teMergeContent& eMergeType );
00052 
00054 
00055 int main(int argc, char* argv[])
00056 {
00057 
00058 #if defined(_DO_CRTDBG_MEMORY_LEAK_CHECK_) && defined(_DWFCORE_WIN32_SYSTEM)
00059     //
00060     // Enable memory leak reporting in Debug mode under Win32.
00061     //
00062     int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
00063     // Turn on leak-checking bit
00064     tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
00065     // Turn off CRT block checking bit
00066     tmpFlag &= ~_CRTDBG_CHECK_CRT_DF;
00067     // Set flag to the new value
00068     _CrtSetDbgFlag( tmpFlag );
00069 
00070     // For mem leak debugging... Please do not delete.
00071     /*
00072     long foo = 1;
00073     _CrtSetBreakAlloc(foo);
00074     */
00075 #endif
00076 
00077     if (argc < 3)
00078     {
00079         wcout << L"Usage: " << argv[0] << L"[-o output.dwf] [-m [p|s]] {files to aggregate}.dwf" << endl;
00080         wcout << "-o optionally specify output filename. If it is not provided then the name is Aggregated.dwf." << endl;
00081         wcout << "-m should be specified to merge the content in the aggregated package. The p implies that in the case of" << endl;
00082         wcout << "   conflicts, the primary content element will be kept (default for a merge). If s is specified, then " << endl;
00083         wcout << "   the elements being merged into the primary content have priority." << endl;
00084         wcout << endl;
00085         return 1;
00086     }
00087 
00088 
00089     try
00090     {
00091         std::vector<DWFString>      oInfiles(0);
00092         DWFString                   zOutfile( L"Aggregated.dwf" );
00093         DWFPackageWriter::teMergeContent eMergeType = DWFPackageWriter::eNoMerge;
00094 
00095         if (!processArgs(argc, argv, oInfiles, zOutfile, eMergeType))
00096         {
00097             wcout << L"Usage: " << argv[0] << L"[-o output.dwf] [-m [p|s]] {files to aggregate}.dwf" << endl;
00098             wcout << "-o optionally specify output filename. If it is not provided then the name is Aggregated.dwf." << endl;
00099             wcout << "-m should be specified to merge the content in the aggregated package. The p implies that in the case of" << endl;
00100             wcout << "   conflicts, the primary content element will be kept (default for a merge). If s is specified, then " << endl;
00101             wcout << "   the elements being merged into the primary content have priority." << endl;
00102             wcout << endl;
00103             return 1;
00104         }
00105 
00106         //
00107         //  Create the destination DWF
00108         //
00109         DWFFile oOutfile( zOutfile );
00110         DWF6PackageWriter oWriter( oOutfile );
00111 
00112         //  If we are going to aggregate sections from other packages then we need to initialize
00113         //  for aggregation
00114         oWriter.initializeForAggregation( eMergeType );
00115 
00116         //
00117         //  The source readers are going to be stored in here until the write is complete
00118         //
00119         DWFOrderedVector<DWFPackageReader*> oReaders;
00120 
00121         std::vector<DWFString>::iterator iFile = oInfiles.begin();
00122         for (; iFile != oInfiles.end(); ++iFile)
00123         {
00124             //
00125             //  Open each source file and grab the sections and contents and add them to the destination
00126             //
00127             DWFFile oInfile( *iFile );
00128             DWFPackageReader* pReader = DWFCORE_ALLOC_OBJECT( DWFPackageReader( oInfile ) );
00129 
00130             //
00131             //  Store the reader for later cleanup
00132             //
00133             oReaders.push_back( pReader );
00134 
00135             //
00136             //  Get the manifest
00137             //
00138             DWFManifest& rManifestIn = pReader->getManifest();
00139 
00140             //
00141             //  For each section determine content to get, track the content and copy sections
00142             //
00143             DWFManifest::SectionIterator* piSections = rManifestIn.getSections();
00144             if (piSections)
00145             {
00146                 DWFSection* pSection = NULL;
00147 
00148                 for (; piSections->valid(); piSections->next())
00149                 {
00150                     pSection = piSections->get();
00151                     pSection->readDescriptor();
00152 
00153                     //
00154                     //  Add the section
00155                     //
00156                     oWriter.addSection( pSection );
00157                 }
00158 
00159                 DWFCORE_FREE_OBJECT( piSections );
00160             }
00161         }
00162 
00163         oWriter.write();
00164 
00165         DWFOrderedVector<DWFPackageReader*>::Iterator* piReader = oReaders.iterator();
00166         if (piReader)
00167         {
00168             for (; piReader->valid(); piReader->next())
00169             {
00170                 DWFCORE_FREE_OBJECT( piReader->get() );
00171             }
00172             DWFCORE_FREE_OBJECT( piReader );
00173         }
00174 
00175     }
00176 
00177     catch (DWFException& ex)
00178     {
00179         wcout << ex.type() << L": " << ex.message() << endl;
00180         wcout << L"(function) " << ex.function() << endl;
00181         wcout << L"(file) " << ex.file() << endl;
00182         wcout << L"(line) " << ex.line() << endl;
00183     }
00184 
00185     return 0;
00186 }
00187 
00189 
00190 bool processArgs( int argc, 
00191                   char* argv[], 
00192                   std::vector<DWFString>& oInfiles, 
00193                   DWFString& zOutfile, 
00194                   DWFPackageWriter::teMergeContent& eMergeType )
00195 {
00196     zOutfile.assign( L"Aggregated.dwf" );
00197     eMergeType = DWFPackageWriter::eMergePriorityPrimary;
00198 
00199     int i=1;
00200     while (i < argc)
00201     {
00202         if (argv[i][0] == '-')
00203         {
00204             if (argv[i][1] == 'o')
00205             {
00206                 if (argc == i+1)
00207                 {
00208                     wcout << L"No output file specified with the -o option" << endl;
00209                     return false;
00210                 }
00211                 zOutfile.assign( argv[++i] );
00212             }
00213 
00214             if (argv[i][1] == 'm')
00215             {
00216                 if (argc > i+1)
00217                 {
00218                     //
00219                     // Compare whole word - not just character since this may be a file name otherwise
00220                     //
00221                     DWFString zMergeValue( argv[i+1] );
00222                     if (zMergeValue == L"P" || zMergeValue == L"p")
00223                     {
00224                         eMergeType = DWFPackageWriter::eMergePriorityPrimary;
00225                         ++i;
00226                     }
00227                     else if (zMergeValue == L"S" || zMergeValue == L"s")
00228                     {
00229                         eMergeType = DWFPackageWriter::eMergePrioritySecondary;
00230                         ++i;
00231                     }
00232                     else
00233                     {
00234                         eMergeType = DWFPackageWriter::eMergePriorityPrimary;
00235                     }
00236                 }
00237                 else
00238                 {
00239                     eMergeType = DWFPackageWriter::eMergePriorityPrimary;
00240                 }
00241             }
00242 
00243             if((argv[i][1] == 'h') ||
00244                (argv[i][1] == '?') ||
00245                (argv[i][1] == '-') && (argv[i][2] == 'h'))
00246             {
00247                 return false;
00248             }
00249         }
00250         else
00251         {
00252             oInfiles.push_back( DWFString(argv[i]) );
00253         }
00254 
00255         i++;
00256     }
00257 
00258     if (oInfiles.empty())
00259     {
00260         wcout << L"No input files were provided for aggregation." << endl;
00261         return false;
00262     }
00263 
00264     return true;
00265 }
00266 
00267 
00268 

Generated on Tue Jan 6 22:40:01 2009 for Autodesk DWF Toolkit by  doxygen 1.4.5