TextPath (xml as simple text; read utf8)

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
MoRLOKru
Posts: 3
Joined: Thu Oct 21, 2010 9:10 pm

TextPath (xml as simple text; read utf8)

Post by MoRLOKru »

I wrote a simple patch (first attempt at writing :) ) :

1. Now you can read plain text files. Open them as XML, and use the function IXMLReader:: getData().

2. Also, I finished the converter utf8 (now even supported by the "rare Chinese characters")

PS Sorry for the language (translator used)
PP How do your put text in listbox (part of forum message)?
  • Index: include/irrXML.h
    ===================================================================
    --- include/irrXML.h (revision 3903)
    +++ include/irrXML.h (working copy)
    @@ -347,6 +347,9 @@
    data and it is of type EXN_TEXT or EXN_UNKNOWN. */
    virtual const char_type* getNodeData() const = 0;

    + //! Return whole text of the file
    + virtual const char_type* getData() const = 0;
    +
    //! Returns if an element is an empty element, like <foo />
    virtual bool isEmptyElement() const = 0;

    Index: source/Irrlicht/CIrrDeviceConsole.cpp
    ===================================================================
    --- source/Irrlicht/CIrrDeviceConsole.cpp (revision 3903)
    +++ source/Irrlicht/CIrrDeviceConsole.cpp (working copy)
    @@ -79,11 +79,12 @@
    {
    // Some mingw versions lack this define, so avoid it in case it does not exist
    #if (_WIN32_WINNT >= 0x0501) && defined(CONSOLE_FULLSCREEN_MODE)
    - if (SetConsoleDisplayMode(WindowsSTDOut, CONSOLE_FULLSCREEN_MODE, Dimensions))
    - {
    - CreationParams.WindowSize.Width = Dimensions->X;
    - CreationParams.WindowSize.Width = Dimensions->Y;
    - }
    + // _WIN32_WINNT == 0x0600
    + // if (SetConsoleDisplayMode(WindowsSTDOut, CONSOLE_FULLSCREEN_MODE, Dimensions))
    + // {
    + // CreationParams.WindowSize.Width = Dimensions->X;
    + // CreationParams.WindowSize.Width = Dimensions->Y;
    + // }
    #endif
    }
    else
    Index: source/Irrlicht/CIrrDeviceWin32.cpp
    ===================================================================
    --- source/Irrlicht/CIrrDeviceWin32.cpp (revision 3903)
    +++ source/Irrlicht/CIrrDeviceWin32.cpp (working copy)
    @@ -1567,7 +1567,8 @@
    break;
    case PRODUCT_STARTER:
    case PRODUCT_STARTER_E:
    - case PRODUCT_STARTER_N:
    + // .\CIrrDeviceWin32.cpp(1570) : error C2065: 'PRODUCT_STARTER_N' : undeclared identifier
    + // case PRODUCT_STARTER_N:
    out.append("Starter Edition ");
    break;
    }
    Index: source/Irrlicht/CXMLReaderImpl.h
    ===================================================================
    --- source/Irrlicht/CXMLReaderImpl.h (revision 3903)
    +++ source/Irrlicht/CXMLReaderImpl.h (working copy)
    @@ -638,13 +638,23 @@
    return true;
    }

    + virtual const char_type* getData() const
    + {
    + return TextBegin;
    + }

    + // Extracting a sequence of bits
    + template<class T>
    + T getBits(T value, int first_bit, int count_bit)
    + {
    + return (T(value << (first_bit))) >> (sizeof(T)*8 - count_bit);
    + }
    +
    //! converts the text file into the desired format.
    - /** \param source: begin of the text (without byte order mark)
    - \param pointerToStore: pointer to text data block which can be
    - stored or deleted based on the nesessary conversion.
    - \param sizeWithoutHeader: Text size in characters without header
    - */
    + //! \param source: begin of the text (without byte order mark)
    + //! \param pointerToStore: pointer to text data block which can be
    + //! stored or deleted based on the nesessary conversion.
    + //! \param sizeWithoutHeader: Text size in characters without header
    template<class src_char_type>
    void convertTextData(src_char_type* source, char* pointerToStore, int sizeWithoutHeader)
    {
    @@ -672,10 +682,74 @@

    if ( sizeof(src_char_type) == 1 )
    {
    - // we have to cast away negative numbers or results might add the sign instead of just doing a copy
    - for (int i=0; i<sizeWithoutHeader; ++i)
    + if (ETF_UTF8 == getSourceFormat())
    + { // UTF8 converter
    +
    + // decoded symbol
    + u32 res = 0;
    +
    + // Encryption character takes no more than four bytes
    + u8 src[4];
    +
    + int iBuf = 0;
    + int iText = 0;
    + int symbol_size = 0;
    +
    + // Iterate through all the bytes of the file (shift to the width of the last character)
    + for (iBuf = 0; iBuf<sizeWithoutHeader; iBuf += symbol_size)
    + {
    + // Take four bytes (needed for one to four)
    + for (int k=0; k < 4; k++)
    + src[k] = (iBuf + k < sizeWithoutHeader ? source[iBuf+k] : 0);
    +
    +
    + // 0x00000000 — 0x0000007F 0xxxxxxx
    + if (src[0] < 128) // 0 == getBits(src[0], 0, 1)
    + {
    + res = src[0];
    + symbol_size = 1;
    + }
    + else
    + // 0x00000080 — 0x000007FF 110xxxxx 10xxxxxx
    + if (6 == getBits(src[0], 0, 3) && 2 == getBits(src[1], 0, 2))
    + {
    + res = (getBits(src[0], 3, 5) << 6) | getBits(src[1], 2, 6);
    + symbol_size = 2;
    + }
    + else
    + // 0x00000800 — 0x0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
    + if (14 == getBits(src[0], 0, 4) && 2 == getBits(src[1], 0, 2) && 2 == getBits(src[2], 0, 2))
    + {
    + res = (getBits(src[0], 4, 4) << 12) | (getBits(src[1], 2, 6) << 6) | getBits(src[2], 2, 6);
    + symbol_size = 3;
    + }
    + else
    + // 0x00010000 — 0x001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
    + if (30 == getBits(src[0], 0, 5) && 2 == getBits(src[1], 0, 2) && 2 == getBits(src[2], 0, 2) && 2 == getBits(src[3], 0, 2))
    + {
    + res = (getBits(src[0], 5, 3) << 18) | (getBits(src[1], 2, 6) << 12) | (getBits(src[2], 2, 6) << 6) | getBits(src[3], 2, 6);
    + symbol_size = 4;
    + }
    + else
    + {
    + // wrong char:
    + res = (int)L"?";
    + symbol_size = 1;
    + }
    + TextData[iText++] = res;
    + }
    +
    + // The size of the original data and the final text differs
    + sizeWithoutHeader = iText;
    +
    + } // end UTF8 converter
    + else
    {
    - TextData = static_cast<char_type>(static_cast<unsigned char>(source));
    + // we have to cast away negative numbers or results might add the sign instead of just doing a copy
    + for (int i=0; i<sizeWithoutHeader; ++i)
    + {
    + TextData = static_cast<char_type>(static_cast<unsigned char>(source));
    + }
    }
    }
    else
Post Reply