Files/Files.cpp

This sample program exercises a variety of file operations including streaming I/O, zip archiving and processing using several file-oriented core classes and interfaces including:

Also shown in this example are other useful concepts such as timing operations, string usage, and stream buffering and monitoring using the following core classes and interfaces:

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 // Files.cpp : Defines the entry point for the console application.
00023 //
00024 
00025 #include "stdafx.h"
00026 
00027 #ifdef  _DWFCORE_WIN32_SYSTEM
00028 #include <crtdbg.h>
00029 #endif
00030 
00031 using namespace std;
00032 using namespace DWFCore;
00033 
00034 
00035 #define DUMP( s )       wcout << s << endl
00036 
00037 int main()
00038 {
00039 #ifdef  _DWFCORE_WIN32_SYSTEM
00040     //
00041     // Enable memory leak reporting in Debug mode under Win32.
00042     //
00043     int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
00044     // Turn on leak-checking bit
00045     tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
00046     // Turn off CRT block checking bit
00047     tmpFlag &= ~_CRTDBG_CHECK_CRT_DF;
00048     // Set flag to the new value
00049     _CrtSetDbgFlag( tmpFlag );
00050 
00051     // For mem leak debugging... Please do not delete.
00052     //long foo = 7221;
00053     //_CrtSetBreakAlloc(foo);
00054 #endif
00055 
00056 #define DATA_BUFFER_BYTES   4096
00057 
00058         DWFCore::DWFString zDataString( L"Autodesk, Inc. is engaged in the development and marketing of design and drafting software and multimedia tools, primarily for the business and professional environment. For the 3 months ended 4/30/04, net revenues rose 41% to $297.9M. Net income totalled $42.5M, up from $7.5M. Revenues reflect increased upgrade revenues and higher subscription bookings. Net income also reflects improved gross margins." );
00059 
00060         unsigned char* pDataBuffer = DWFCORE_ALLOC_MEMORY( unsigned char, DATA_BUFFER_BYTES );
00061         if (pDataBuffer == NULL)
00062         {
00063             _DWFCORE_THROW( DWFMemoryException, L"No memory for data buffer" );
00064         }
00065 
00066             //
00067             // fill up random buffer
00068             //
00069         unsigned int i = 0;
00070         uint64_t nData64 = _DWFCORE_LARGE_CONSTANT( 0U );
00071         for (; i < DATA_BUFFER_BYTES;)
00072         {
00073              nData64 = DWFTimer::Tick64();
00074              DWFCORE_COPY_MEMORY( (pDataBuffer+i), &nData64, sizeof(nData64) );
00075              i += sizeof(nData64);
00076         }
00077 
00078         //
00079         // create a text file
00080         //
00081         DWFCore::DWFFile oTextFilename( L"Files.txt" );
00082         DWFCore::DWFStreamFileDescriptor oTextFile( oTextFilename, L"w+" );
00083         oTextFile.open();
00084 
00085         //
00086         // create a binary file
00087         //
00088         DWFCore::DWFFile oBinFilename( L"Files.bin" );
00089         DWFCore::DWFStreamFileDescriptor* pBinFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oBinFilename, L"w+b") );
00090         if (pBinFile == NULL)
00091         {
00092             _DWFCORE_THROW( DWFCore::DWFMemoryException, L"No memory for file descriptor" );
00093         }
00094         pBinFile->open();
00095 
00096             //
00097             // see note with binary file descriptor below
00098             //
00099         {
00100             //
00101             // create a stream to write to the files
00102             //
00103             DWFCore::DWFFileOutputStream oFilestream;
00104 
00105             //
00106             // first, attach the descriptor for the text file to the stream
00107             // and write the text buffer to the file
00108             //
00109             oFilestream.attach( &oTextFile, false );
00110             oFilestream.write( (const wchar_t*)zDataString, zDataString.bytes() );
00111             oFilestream.detach();
00112 
00113             //
00114             // close the file
00115             //
00116             oTextFile.close();
00117 
00118             DUMP( L"Text file created." );
00119 
00120             //
00121             // second, attach the descriptor for the binary file to the stream
00122             // we can reuse the file stream; also note we pass ownership of the descriptor
00123             // so the stream can control the deletion of the object. we do this for illustration,
00124             // notice the use of oFilestream in it's own scope block for this reason
00125             //
00126             oFilestream.attach( pBinFile, true );
00127 
00128             //
00129             // let's buffer the file i/o...
00130             // we should get 2 writes
00131             //
00132             DWFCore::DWFBufferOutputStream oBufferedStream( &oFilestream, false, DATA_BUFFER_BYTES/2 );
00133 
00134             //
00135             // for this example, let's assume we buffered the fil i/o because we've
00136             // got some crappy writing code...
00137             //
00138             for (i = 0; i < DATA_BUFFER_BYTES; i += 4)
00139             {
00140                 oBufferedStream.write( (pDataBuffer+i), 4 );
00141             }
00142 
00143             //
00144             // it's always a good idea to explicitly flush your output streams
00145             //
00146             oBufferedStream.flush();
00147 
00148             //
00149             // since the stream owns the descriptor,
00150             // detach is going to do all the clean up (and close the file)
00151             //
00152             oFilestream.detach();
00153 
00154             DUMP( L"Binary file created." );
00155         }
00156 
00157         //
00158         // create 2 temporary files
00159         // they will be automatically deleted when we are done
00160         //
00161         DWFCore::DWFString zTemplate( L"Files_" );
00162         DWFCore::DWFTempFile* pTempTextFile = DWFTempFile::Create( zTemplate, true );
00163 
00164         zTemplate.assign( L"Files_" );
00165         DWFCore::DWFTempFile* pTempBinFile = DWFTempFile::Create( zTemplate, true );
00166 
00167         //
00168         // let's open our sample files
00169         //
00170         DWFCore::DWFStreamFileDescriptor oTextFile2( oTextFilename, L"r" );
00171         DWFCore::DWFStreamFileDescriptor oBinFile2( oBinFilename, L"rb" );
00172         oTextFile2.open();
00173         oBinFile2.open();
00174 
00175         //
00176         // create some streams to read from the files
00177         //
00178         DWFCore::DWFFileInputStream oTextFilestream;
00179         DWFCore::DWFFileInputStream oBinFilestream;
00180 
00181         oTextFilestream.attach( &oTextFile2, false );
00182         oBinFilestream.attach( &oBinFile2, false );
00183 
00184         size_t nBytes = 0;
00185 
00186         {
00187             //
00188             // copy the files into the temps
00189             //
00190             DWFCore::DWFFileOutputStream& rTempFilestream = pTempTextFile->getOutputStream();
00191 
00192             while (oTextFilestream.available() > 0)
00193             {
00194                 nBytes = oTextFilestream.read( pDataBuffer, DATA_BUFFER_BYTES );
00195 
00196                 rTempFilestream.write( pDataBuffer, nBytes );
00197             }
00198 
00199             //
00200             // clean up
00201             //
00202             rTempFilestream.flush();
00203 
00204             DUMP( L"Copied text file into temporary file" );
00205         }
00206         {
00207             DWFCore::DWFFileOutputStream& rTempFilestream = pTempBinFile->getOutputStream();
00208 
00209             while (oBinFilestream.available() > 0)
00210             {
00211                 nBytes = oBinFilestream.read( pDataBuffer, DATA_BUFFER_BYTES );
00212 
00213                 rTempFilestream.write( pDataBuffer, nBytes );
00214             }
00215 
00216             //
00217             // clean up
00218             //
00219             rTempFilestream.flush();
00220 
00221             DUMP( L"Copied binary file into temporary file" );
00222         }
00223 
00224         //
00225         // let's create a ZIP file and archive all these files
00226         //
00227         DWFCore::DWFFile oZipFilename( L"FilesArchive.zip" );
00228         DWFCore::DWFZipFileDescriptor oZipFile( oZipFilename, DWFZipFileDescriptor::eZip );
00229 
00230         //
00231         // open the archive
00232         //
00233         oZipFile.open();
00234 
00235         DUMP( L"Created Zip file" );
00236 
00237         //
00238         // add the original text file (with a password, and non-pkzip compliant passwording (aka salting) )
00239         //
00240         DWFCore::DWFOutputStream* pZipStream = oZipFile.zip( oTextFilename.name(), L"a password", false );
00241 
00242         //
00243         // the filestream is still open, let's rewind it
00244         //
00245         oTextFilestream.seek( SEEK_SET, 0 );
00246 
00247             //
00248             // add to zip
00249             //
00250         while (oTextFilestream.available() > 0)
00251         {
00252             nBytes = oTextFilestream.read( pDataBuffer, DATA_BUFFER_BYTES );
00253 
00254             pZipStream->write( pDataBuffer, nBytes );
00255         }
00256 
00257         //
00258         // clean up
00259         //
00260         pZipStream->flush();
00261         DWFCORE_FREE_OBJECT( pZipStream );
00262 
00263         DUMP( L"Original text file added to Zip" );
00264 
00265         //
00266         // done with this file
00267         //
00268         oTextFilestream.detach();
00269         oTextFile2.close();
00270 
00271         //
00272         // the binary file is still open but for kicks open it again
00273         //
00274         DWFCore::DWFStreamFileDescriptor oBinFile3( oBinFilename, "rb" );
00275         oBinFile3.open();
00276 
00277         //
00278         // auto-detach old descriptor
00279         //
00280         oBinFilestream.attach( &oBinFile3, false );
00281 
00282         //
00283         // add the original binary file (with a password, with pkzip compliant passwording )
00284         //
00285         pZipStream = oZipFile.zip( oBinFilename.name(), L"a password", true );
00286 
00287             //
00288             // add to zip
00289             //
00290         while (oBinFilestream.available() > 0)
00291         {
00292             nBytes = oBinFilestream.read( pDataBuffer, DATA_BUFFER_BYTES );
00293 
00294             pZipStream->write( pDataBuffer, nBytes );
00295         }
00296 
00297         //
00298         // clean up
00299         //
00300         pZipStream->flush();
00301         DWFCORE_FREE_OBJECT( pZipStream );
00302 
00303         DUMP( L"Original binary file added to Zip" );
00304 
00305         //
00306         // close all open handles
00307         //
00308         oBinFilestream.detach();
00309         oBinFile3.close();
00310         oBinFile2.close();
00311 
00312         //
00313         // add our temp files
00314         //
00315         DWFCore::DWFFileInputStream* pTempInputStream = pTempTextFile->getInputStream();
00316 
00317         DWFCore::DWFString zTextTempName( pTempInputStream->descriptor()->file().name() );
00318                            zTextTempName.append( ".txt" );
00319         pZipStream = oZipFile.zip( zTextTempName );
00320 
00321             //
00322             // add to zip
00323             //
00324         while (pTempInputStream->available() > 0)
00325         {
00326             nBytes = pTempInputStream->read( pDataBuffer, DATA_BUFFER_BYTES );
00327 
00328             pZipStream->write( pDataBuffer, nBytes );
00329         }
00330 
00331         //
00332         // clean up
00333         //
00334         pZipStream->flush();
00335         DWFCORE_FREE_OBJECT( pZipStream );
00336         DWFCORE_FREE_OBJECT( pTempInputStream );
00337 
00338         DUMP( L"Temporary text file added to Zip" );
00339 
00340         pTempInputStream = pTempBinFile->getInputStream();
00341 
00342         DWFCore::DWFString zBinTempName( pTempInputStream->descriptor()->file().name() );
00343                           zBinTempName.append( ".bin" );
00344         pZipStream = oZipFile.zip( zBinTempName );
00345 
00346             //
00347             // add to zip
00348             //
00349         while (pTempInputStream->available() > 0)
00350         {
00351             nBytes = pTempInputStream->read( pDataBuffer, DATA_BUFFER_BYTES );
00352 
00353             pZipStream->write( pDataBuffer, nBytes );
00354         }
00355 
00356         //
00357         // clean up
00358         //
00359         pZipStream->flush();
00360         DWFCORE_FREE_OBJECT( pZipStream );
00361         DWFCORE_FREE_OBJECT( pTempInputStream );
00362 
00363         DUMP( L"Temporary binary file added to Zip" );
00364 
00365         //
00366         // finish (and delete) the temp files
00367         //
00368         DWFCORE_FREE_OBJECT( pTempBinFile );
00369         DWFCORE_FREE_OBJECT( pTempTextFile );
00370 
00371         DUMP( L"Temporary files destroyed" );
00372 
00373         oZipFile.close();
00374 
00375         DUMP( L"Zip file closed" );
00376 
00377         //
00378         // finally, let's reopen the zip, open each file and simultaneously
00379         // stream the contents to an in memory buffer
00380         //
00381         DWFCore::DWFZipFileDescriptor oUnzipFile( oZipFilename, DWFZipFileDescriptor::eUnzip );
00382         oUnzipFile.open();
00383 
00384         //
00385         // auto-resizing buffer
00386         //
00387         DWFCore::DWFBufferOutputStream oMemoryStream( DATA_BUFFER_BYTES );
00388 
00389             //
00390             // original text file
00391             //
00392         {
00393             // with a password, and salting.
00394             DWFCore::DWFInputStream* pUnzipStream = oUnzipFile.unzip( oTextFilename.name(), L"a password" );
00395             DWFCore::DWFMonitoredInputStream oMonitorStream( pUnzipStream, true );
00396 
00397             //
00398             // attach memory stream
00399             //
00400             oMonitorStream.attach( &oMemoryStream, false );
00401 
00402             //
00403             // read
00404             //
00405             while (oMonitorStream.available() > 0)
00406             {
00407                 //
00408                 // NOP read
00409                 //
00410                 oMonitorStream.read( pDataBuffer, DATA_BUFFER_BYTES );
00411             }
00412 
00413             //DWFCORE_FREE_OBJECT( pUnzipStream );
00414 
00415             DUMP( L"Original text file read from archive" );
00416         }
00417 
00418             //
00419             // original bin file
00420             //
00421         {
00422             // with a password, no salting.
00423             DWFCore::DWFInputStream* pUnzipStream = oUnzipFile.unzip( oBinFilename.name(), L"a password"  );
00424             DWFCore::DWFMonitoredInputStream oMonitorStream( pUnzipStream, true );
00425 
00426             //
00427             // attach memory stream
00428             //
00429             oMonitorStream.attach( &oMemoryStream, false );
00430 
00431             //
00432             // read
00433             //
00434             while (oMonitorStream.available() > 0)
00435             {
00436                 //
00437                 // NOP read
00438                 //
00439                 oMonitorStream.read( pDataBuffer, DATA_BUFFER_BYTES );
00440             }
00441 
00442             //DWFCORE_FREE_OBJECT( pUnzipStream );
00443 
00444             DUMP( L"Original bin file read from archive" );
00445         }
00446 
00447             //
00448             // copied text file
00449             //
00450         {
00451             DWFCore::DWFInputStream* pUnzipStream = oUnzipFile.unzip( zTextTempName );
00452             DWFCore::DWFMonitoredInputStream oMonitorStream( pUnzipStream, true );
00453 
00454             //
00455             // attach memory stream
00456             //
00457             oMonitorStream.attach( &oMemoryStream, false );
00458 
00459             //
00460             // read
00461             //
00462             while (oMonitorStream.available() > 0)
00463             {
00464                 //
00465                 // NOP read
00466                 //
00467                 oMonitorStream.read( pDataBuffer, DATA_BUFFER_BYTES );
00468             }
00469 
00470             //DWFCORE_FREE_OBJECT( pUnzipStream );
00471 
00472             DUMP( L"Copied text file read from archive" );
00473         }
00474 
00475             //
00476             // copied bin file
00477             //
00478         {
00479             DWFCore::DWFInputStream* pUnzipStream = oUnzipFile.unzip( zBinTempName );
00480             DWFCore::DWFMonitoredInputStream oMonitorStream( pUnzipStream, true );
00481 
00482             //
00483             // attach memory stream
00484             //
00485             oMonitorStream.attach( &oMemoryStream, false );
00486 
00487             //
00488             // read
00489             //
00490             while (oMonitorStream.available() > 0)
00491             {
00492                 //
00493                 // NOP read
00494                 //
00495                 oMonitorStream.read( pDataBuffer, DATA_BUFFER_BYTES );
00496             }
00497 
00498             //DWFCORE_FREE_OBJECT( pUnzipStream );
00499 
00500             DUMP( L"Copied binary file read from archive" );
00501         }
00502 
00503         //
00504         // close
00505         //
00506         oUnzipFile.close();
00507 
00508         DUMP( L"Zip archive closed" );
00509 
00510         //
00511         // final output
00512         //
00513         _DWFCORE_SWPRINTF( (wchar_t*)pDataBuffer, DATA_BUFFER_BYTES, L"Total buffered bytes: %lu", oMemoryStream.bytes() );
00514         DUMP( (wchar_t*)pDataBuffer );
00515 
00516         cout << "OK\n";
00517 
00518         DWFCORE_FREE_MEMORY(pDataBuffer);
00519  
00520     return 0;
00521 }

Generated on Tue Jan 6 22:39:28 2009 for Autodesk DWF Core Library by  doxygen 1.4.5