Strings/Strings.cpp

This sample program exercises some of the operations available in DWFCore::DWFString.

Also shown is some simple file I/O using the following core classes and interfaces:

This file may contain Unicode text that may display incorrectly in the documentation and/or not display/save/load correctly on certain filesystems and text editors, etc.

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 "stdafx.h"
00023 
00024 
00025 using namespace std;
00026 using namespace DWFCore;
00027 
00028 
00029 
00030 int main()
00031 {
00032         unsigned long nLoops = 10;
00033 
00034         DWFCore::DWFFile name( L"Strings.txt" );
00035         DWFCore::DWFStreamFileDescriptor fd( name, L"w+" );
00036         DWFCore::DWFFileOutputStream file;
00037 
00038         fd.open();
00039         file.attach( &fd, false );
00040 
00041 
00042         unsigned long i = 0;
00043         for (; i < nLoops; i++)
00044         {
00045             char* pUTF8 = NULL;
00046             size_t nUTF8Bytes = 0;
00047 
00048 
00049             DWFCore::DWFString  zTestMeStack;
00050             DWFCore::DWFString* pTestMeHeap = DWFCORE_ALLOC_OBJECT( DWFString );
00051             if (pTestMeHeap == NULL)
00052             {
00053                 _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate DWFString on heap" );
00054             }
00055 
00056             zTestMeStack.assign( "small " );
00057             zTestMeStack.append( "smaller " );
00058             zTestMeStack.append( L"wide " );
00059 
00060             //
00061             // Test != and == operator corner cases.
00062             //
00063             DWFCore::DWFString  zEmpty1;
00064             DWFCore::DWFString  zEmpty2;
00065             if(zEmpty1 != zTestMeStack) {;}
00066             if(zTestMeStack!= zEmpty1) {;}
00067             if(zEmpty1 != zEmpty2) {;}
00068             if(zEmpty1 != L"ABC") {;}
00069             if(zEmpty1 != L"") {;}
00070             if(zEmpty1 == zTestMeStack) {;}
00071             if(zTestMeStack == zEmpty1) {;}
00072             if(zEmpty1 == zEmpty2) {;}
00073             if(zEmpty1 == L"ABC") {;}
00074             if(zEmpty1 == L"") {;}
00075 
00076 
00077             // added to test Find(). doesn't write to the file. you'll need the debugger to verify.
00078             off_t offset;
00079             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 0, false);
00080             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 0, true);
00081             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 1, false);
00082             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 1, true);
00083             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 18, false);
00084             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 18, true);
00085             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 19, false);
00086             offset = DWFCore::DWFString::Find(zTestMeStack, L's', 19, true);
00087             offset = DWFCore::DWFString::Find(zTestMeStack, L'X', 0, false);
00088             offset = DWFCore::DWFString::Find(zTestMeStack, L'X', 0, true);
00089             offset = DWFCore::DWFString::Find(zEmpty1, L"ABC", 0, true);
00090             offset = DWFCore::DWFString::Find(zTestMeStack, L"", 0, true);
00091             offset = DWFCore::DWFString::Find(zEmpty1, L"", 0, true);
00092             offset = DWFCore::DWFString::Find(zEmpty1, L'X', 0, true);
00093             offset = DWFCore::DWFString::Find(L"", L"", 0, true);
00094             offset = DWFCore::DWFString::Find(L"", L'X', 0, true);
00095             offset = zEmpty1.find( L'X', 0, false);
00096             offset = zEmpty1.find( L"ABC", 0, false);
00097             offset = zEmpty1.find( L"", 0, false);
00098             offset = zEmpty1.find( zTestMeStack, 0, false);
00099             offset = zEmpty1.find( zEmpty2, 0, false);
00100             offset = zTestMeStack.find( L"", 0, false);
00101             offset = zTestMeStack.find( zEmpty1, 0, false);
00102 
00103             if(offset)
00104             {
00105             }
00106 
00107             file.write( L"[1] ", 4*sizeof(wchar_t) );
00108             file.write( (const wchar_t*)zTestMeStack, zTestMeStack.bytes() );
00109             file.write( L"\n", sizeof(wchar_t) );
00110 
00111             pTestMeHeap->assign( zTestMeStack );
00112             nUTF8Bytes = pTestMeHeap->getUTF8( &pUTF8 );
00113 
00114             if (pUTF8)
00115             {
00116                 file.write( L"[2] ", 4*sizeof(wchar_t) );
00117                 file.write( (const wchar_t*)*pTestMeHeap, pTestMeHeap->bytes() );
00118                 file.write( L"\n", sizeof(wchar_t) );
00119 
00120                 file.write( L"[3] ", 4*sizeof(wchar_t) );
00121                 file.write( pUTF8, nUTF8Bytes );
00122                 file.write( L"\n", sizeof(wchar_t) );
00123 
00124                 DWFCORE_FREE_MEMORY( pUTF8 );
00125             }
00126 
00127             pTestMeHeap->destroy();
00128 
00129             if ((pTestMeHeap->bytes() == 0) &&
00130                 (pTestMeHeap->chars() == 0) &&
00131                 ((const wchar_t*)*pTestMeHeap == NULL))
00132             {
00133                 file.write( L"[4] (is empty)\n", 15*sizeof(wchar_t) );
00134             }
00135 
00136             *pTestMeHeap = L"日本語";
00137             nUTF8Bytes = pTestMeHeap->getUTF8( &pUTF8 );
00138 
00139             if (pUTF8)
00140             {
00141                 file.write( L"[5] ", 4*sizeof(wchar_t) );
00142                 file.write( (const wchar_t*)*pTestMeHeap, pTestMeHeap->bytes() );
00143                 file.write( L"\n", 2 );
00144 
00145                 file.write( L"[6] ", 4*sizeof(wchar_t) );
00146                 file.write( pUTF8, nUTF8Bytes );
00147                 file.write( L"\n", sizeof(wchar_t) );
00148             }
00149 
00150             DWFCORE_FREE_MEMORY( pUTF8 );
00151 
00152             zTestMeStack = *pTestMeHeap;
00153 
00154             if (zTestMeStack == *pTestMeHeap)
00155             {
00156                 file.write( L"[7] ", 4*sizeof(wchar_t) );
00157                 file.write( (const wchar_t*)zTestMeStack, zTestMeStack.bytes() );
00158                 file.write( L" == ", 4*sizeof(wchar_t) );
00159                 file.write( (const wchar_t*)*pTestMeHeap, pTestMeHeap->bytes() );
00160                 file.write( L"\n", sizeof(wchar_t) );
00161             }
00162 
00163             DWFCORE_FREE_OBJECT( pTestMeHeap );
00164 
00165             zTestMeStack.append( zTestMeStack );
00166             zTestMeStack.append( zTestMeStack );
00167             zTestMeStack.append( zTestMeStack );
00168             zTestMeStack.append( zTestMeStack );
00169             zTestMeStack.append( zTestMeStack );
00170             zTestMeStack.append( zTestMeStack );
00171             zTestMeStack.append( zTestMeStack );
00172 
00173             nUTF8Bytes = zTestMeStack.getUTF8( &pUTF8 );
00174 
00175             if (pUTF8)
00176             {
00177                 file.write( L"[8] ", 4*sizeof(wchar_t) );
00178                 file.write( (const wchar_t*)zTestMeStack, zTestMeStack.bytes() );
00179                 file.write( L"\n", sizeof(wchar_t) );
00180 
00181                 file.write( L"[9] ", 4*sizeof(wchar_t) );
00182                 file.write( pUTF8, nUTF8Bytes );
00183                 file.write( L"\n", sizeof(wchar_t) );
00184             }
00185 
00186             DWFCORE_FREE_MEMORY( pUTF8 );
00187         }
00188 
00189         DWFCore::DWFString s1(L"This is my string.");
00190         DWFCore::DWFString s2 = s1.substring(0);
00191         DWFCore::DWFString s3 = s1.substring(5);
00192         DWFCore::DWFString s4 = s1.substring(5, 0);
00193         DWFCore::DWFString s5 = s1.substring(5, 2);
00194         DWFCore::DWFString s6 = s1.substring(5, 100);
00195 
00196         cout << "OK\n";
00197 
00198     return 0;
00199 }

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