Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

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.

#include "stdafx.h"


using namespace std;
using namespace DWFCore;



int main()
{
    unsigned long nLoops = 10;

    try
    {
        DWFCore::DWFFile name( L"Strings.txt" );
        DWFCore::DWFStreamFileDescriptor fd( name, L"w+" );
        DWFCore::DWFFileOutputStream file;

        fd.open();
        file.attach( &fd, false );


        unsigned long i = 0;
        for (; i < nLoops; i++)
        {
            char* pUTF8 = NULL;
            size_t nUTF8Bytes = 0;

            DWFCore::DWFString  zTestMeStack;
            DWFCore::DWFString* pTestMeHeap = DWFCORE_ALLOC_OBJECT( DWFString );
            if (pTestMeHeap == NULL)
            {
                _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate DWFString on heap" );
            }

            zTestMeStack.assign( "small " );
            zTestMeStack.append( "smaller " );
            zTestMeStack.append( L"wide " );

            file.write( L"[1] ", 4*sizeof(wchar_t) );
            file.write( (const wchar_t*)zTestMeStack, zTestMeStack.bytes() );
            file.write( L"\n", sizeof(wchar_t) );

            pTestMeHeap->assign( zTestMeStack );
            nUTF8Bytes = pTestMeHeap->getUTF8( &pUTF8 );

            if (pUTF8)
            {
                file.write( L"[2] ", 4*sizeof(wchar_t) );
                file.write( (const wchar_t*)*pTestMeHeap, pTestMeHeap->bytes() );
                file.write( L"\n", sizeof(wchar_t) );

                file.write( L"[3] ", 4*sizeof(wchar_t) );
                file.write( pUTF8, nUTF8Bytes );
                file.write( L"\n", sizeof(wchar_t) );

                DWFCORE_FREE_MEMORY( pUTF8 );
            }

            pTestMeHeap->destroy();

            if ((pTestMeHeap->bytes() == 0) &&
                (pTestMeHeap->chars() == 0) &&
                ((const wchar_t*)*pTestMeHeap == NULL))
            {
                file.write( L"[4] (is empty)\n", 15*sizeof(wchar_t) );
            }

            *pTestMeHeap = L"日本語";
            nUTF8Bytes = pTestMeHeap->getUTF8( &pUTF8 );

            if (pUTF8)
            {
                file.write( L"[5] ", 4*sizeof(wchar_t) );
                file.write( (const wchar_t*)*pTestMeHeap, pTestMeHeap->bytes() );
                file.write( L"\n", 2 );

                file.write( L"[6] ", 4*sizeof(wchar_t) );
                file.write( pUTF8, nUTF8Bytes );
                file.write( L"\n", sizeof(wchar_t) );
            }

            DWFCORE_FREE_MEMORY( pUTF8 );

            zTestMeStack = *pTestMeHeap;

            if (zTestMeStack == *pTestMeHeap)
            {
                file.write( L"[7] ", 4*sizeof(wchar_t) );
                file.write( (const wchar_t*)zTestMeStack, zTestMeStack.bytes() );
                file.write( L" == ", 4*sizeof(wchar_t) );
                file.write( (const wchar_t*)*pTestMeHeap, pTestMeHeap->bytes() );
                file.write( L"\n", sizeof(wchar_t) );
            }

            DWFCORE_FREE_OBJECT( pTestMeHeap );

            zTestMeStack.append( zTestMeStack );
            zTestMeStack.append( zTestMeStack );
            zTestMeStack.append( zTestMeStack );
            zTestMeStack.append( zTestMeStack );
            zTestMeStack.append( zTestMeStack );
            zTestMeStack.append( zTestMeStack );
            zTestMeStack.append( zTestMeStack );

            nUTF8Bytes = zTestMeStack.getUTF8( &pUTF8 );

            if (pUTF8)
            {
                file.write( L"[8] ", 4*sizeof(wchar_t) );
                file.write( (const wchar_t*)zTestMeStack, zTestMeStack.bytes() );
                file.write( L"\n", sizeof(wchar_t) );

                file.write( L"[9] ", 4*sizeof(wchar_t) );
                file.write( pUTF8, nUTF8Bytes );
                file.write( L"\n", sizeof(wchar_t) );
            }

            DWFCORE_FREE_MEMORY( pUTF8 );
        }

        DWFCore::DWFString s1(L"This is my string.");
        DWFCore::DWFString s2 = s1.substring(0);
        DWFCore::DWFString s3 = s1.substring(5);
        DWFCore::DWFString s4 = s1.substring(5, 0);
        DWFCore::DWFString s5 = s1.substring(5, 2);
        DWFCore::DWFString s6 = s1.substring(5, 100);

        cout << "OK\n";
    }
    catch (DWFCore::DWFException& ex)
    {
        wcout << ex.type() << endl;
        wcout << ex.message() << endl;
        wcout << ex.function() << endl;
        wcout << ex.file() << endl;
        wcout << ex.line() << endl;
    }


    return 0;
}

Generated on Tue May 17 12:05:10 2005 for Autodesk DWF Core Library by  doxygen 1.4.1