コード例 #1
0
ファイル: jsonwriter.cpp プロジェクト: hihihippp/kiku
/*!
 This function is called for every value objects of INT type.
 This function uses the \n snprintf function to get the US-ASCII
 representation of the integer and simply copy it to the output stream.
 Returns -1 on stream errors or ZERO if no errors.
*/
int
wxJSONWriter::WriteIntValue( wxOutputStream& os, const wxJSONValue& value )
{
    int r = 0;
    char buffer[32];        // need to store 64-bits integers (max 20 digits)
    size_t len;

    wxJSONRefData* data = value.GetRefData();
    wxASSERT( data );

#if defined( wxJSON_64BIT_INT )

        // this is for wxW 2.8 Unicode: in order to use the cross-platform
        // format specifier, we use the wxString's sprintf() function and then
        // convert to UTF-8 before writing to the stream
        wxString s;
        s.Printf( _T("%") wxLongLongFmtSpec _T("d"),
                                                data->m_value.m_valInt64 );
        wxCharBuffer cb = s.ToUTF8();
        const char* cbData = cb.data();
        len = strlen( cbData );
        wxASSERT( len < 32 );
        memcpy( buffer, cbData, len );
        buffer[len] = 0;
    
#else
    snprintf( buffer, 32, "%ld", data->m_value.m_valLong );
#endif
    len = strlen( buffer );
    os.Write( buffer, len );
    if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
        r = -1;
    }
    return r;
}
コード例 #2
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 The function writes the \b null literal string to the output stream.
*/
int
wxJSONWriter::WriteNullValue( wxOutputStream& os )
{
    os.Write( "null", 4 );
    if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
        return -1;
    }
    return 0;
}
コード例 #3
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 This function is called for every value objects of DOUBLE type.
 This function uses the \n snprintf function to get the US-ASCII
 representation of the integer and simply copy it to the output stream.
 Returns -1 on stream errors or ZERO if no errors.

 Note that writing a double to a decimal ASCII representation could
 lay to unexpected results depending on the format string used in the
 conversion.
 See SetDoubleFmtString for details.
*/
int
wxJSONWriter::WriteDoubleValue( wxOutputStream& os, const wxJSONValue& value )
{
    int r = 0;

    char buffer[32];
    wxJSONRefData* data = value.GetRefData();
    wxASSERT( data );
    snprintf( buffer, 32, m_fmt, data->m_value.m_valDouble );
    size_t len = strlen( buffer );
    os.Write( buffer, len );
    if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
        r = -1;
    }
    return r;
}
コード例 #4
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 This function is called for every value objects of UINT type.
 This function uses the \n snprintf function to get the US-ASCII
 representation of the integer and simply copy it to the output stream.
 The function prepends a \b plus \b sign if the \c wxJSONWRITER_RECOGNIZE_UNSIGNED
 flag is set in the \c m_flags data member.
 Returns -1 on stream errors or ZERO if no errors.
*/
int
wxJSONWriter::WriteUIntValue( wxOutputStream& os, const wxJSONValue& value )
{
    int r = 0; size_t len;

    // prepend a plus sign if the style specifies that unsigned integers
    // have to be recognized by the JSON reader
    if ( m_style & wxJSONWRITER_RECOGNIZE_UNSIGNED )  {
        os.PutC( '+' );
    }

    char buffer[32];        // need to store 64-bits integers (max 20 digits)
    wxJSONRefData* data = value.GetRefData();
    wxASSERT( data );

#if defined( wxJSON_64BIT_INT )
    #if wxCHECK_VERSION(2, 9, 0 ) || !defined( wxJSON_USE_UNICODE )
        // this is fine for wxW 2.9 and for wxW 2.8 ANSI
        snprintf( buffer, 32, "%" wxLongLongFmtSpec "u",
                data->m_value.m_valUInt64 );
    #elif wxCHECK_VERSION(3, 0, 0) || !defined( wxJSON_USE_UNICODE )
        snprintf( buffer, 32, "%" wxLongLongFmtSpec "u",
             data->m_value.m_valUInt64 );
    #else
        // this is for wxW 2.8 Unicode: in order to use the cross-platform
        // format specifier, we use the wxString's sprintf() function and then
        // convert to UTF-8 before writing to the stream
        wxString s;
        s.Printf( _T("%") wxLongLongFmtSpec _T("u"),
                data->m_value.m_valInt64 );
        wxCharBuffer cb = s.ToUTF8();
        const char* cbData = cb.data();
        len = strlen( cbData );
        wxASSERT( len < 32 );
        memcpy( buffer, cbData, len );
        buffer[len] = 0;
    #endif
#else
    snprintf( buffer, 32, "%lu", data->m_value.m_valULong );
#endif
    len = strlen( buffer );
    os.Write( buffer, len );
    if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
        r = -1;
    }
    return r;
}
コード例 #5
0
/*!
 This function is called for every value objects of DOUBLE type.
 This function uses the \n snprintf function to get the US-ASCII
 representation of the integer and simply copy it to the output stream.
 Returns -1 on stream errors or ZERO if no errors.

 Note that writing a double to a decimal ASCII representation could
 lay to unexpected results depending on the format string used in the
 conversion.
 See SetDoubleFmtString for details.
*/
int
wxJSONWriter::WriteDoubleValue( wxOutputStream& os, const wxJSONValue& value )
{
    int r = 0;

    wxJSONRefData* data = value.GetRefData();
    wxASSERT( data );

    char* writeBuff = 0;
    // the buffer that has to be written is either UTF-8 or ANSI c_str() depending
    // on the 'm_noUtf8' flag
    wxCharBuffer utf8CB = wxString::FromCDouble(data->m_value.m_valDouble, 10).ToUTF8();        // the UTF-8 buffer
#if !defined(wxJSON_USE_UNICODE)
    wxCharBuffer ansiCB(str.c_str());        // the ANSI buffer    
    if (m_noUtf8)    
    {
        writeBuff = ansiCB.data();
    }
    else    
    {
        writeBuff = utf8CB.data();
    }
   #else
       writeBuff = utf8CB.data();
   #endif
        
    // NOTE: in ANSI builds UTF-8 conversion may fail (see samples/test5.cpp,
    // test 7.3) although I do not know why
    if (writeBuff == 0)    
    {
        const char* err = "<wxJSONWriter::WriteComment(): error converting the double to UTF-8>";
        os.Write(err, strlen(err));
        return 0;
    }
    size_t len = strlen(writeBuff);
    
    os.Write(writeBuff, len);
    if (os.GetLastError() != wxSTREAM_NO_ERROR)    
    {
        r = -1;
    }
    return r;
}
コード例 #6
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 The function writes the wxString object \c str to the output object.
 The string is written as is; you cannot use it to write JSON strings
 to the output text.
 The function converts the string \c str to UTF-8 and writes the buffer..
*/
int
wxJSONWriter::WriteString( wxOutputStream& os, const wxString& str )
{
    wxLogTrace( writerTraceMask, _T("(%s) string to write=%s"),
                  __PRETTY_FUNCTION__, str.c_str() );
    int lastChar = 0;
    char* writeBuff = 0;

    // the buffer that has to be written is either UTF-8 or ANSI c_str() depending
    // on the 'm_noUtf8' flag
    wxCharBuffer utf8CB = str.ToUTF8();        // the UTF-8 buffer
#if !defined( wxJSON_USE_UNICODE )
    wxCharBuffer ansiCB( str.c_str());        // the ANSI buffer

    if ( m_noUtf8 )    {
        writeBuff = ansiCB.data();
    }
    else    {
        writeBuff = utf8CB.data();
    }
#else
    writeBuff = utf8CB.data();
#endif

    // NOTE: in ANSI builds UTF-8 conversion may fail (see samples/test5.cpp,
    // test 7.3) although I do not know why
    if ( writeBuff == 0 )    {
        const char* err = "<wxJSONWriter::WriteComment(): error converting the string to UTF-8>";
        os.Write( err, strlen( err ));
        return 0;
    }
    size_t len = strlen( writeBuff );

    os.Write( writeBuff, len );
    if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
        return -1;
    }

    wxLogTrace( writerTraceMask, _T("(%s) result=%d"),
                  __PRETTY_FUNCTION__, lastChar );
    return lastChar;
}
コード例 #7
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 This function is called for every value objects of BOOL type.
 This function prints the literals \b true or \b false depending on the
 value in \c value.
 Returns -1 on stream errors or ZERO if no errors.
*/
int
wxJSONWriter::WriteBoolValue( wxOutputStream& os, const wxJSONValue& value )
{
    int r = 0;
    const char* f = "false"; const char* t = "true";
    wxJSONRefData* data = value.GetRefData();
    wxASSERT( data );

    const char* c = f;    // defaults to FALSE

    if ( data->m_value.m_valBool )    {
        c = t;
    }

    size_t len = strlen( c );
    os.Write( c, len );
    if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
        r = -1;
    }
    return r;
}
コード例 #8
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 The function is called by WriteIndent() and other writer's functions.
 It writes the indentation as specified in the \c num parameter which is
 the actual \b level of annidation.
 The function checks if wxJSONWRITER_STYLED is set: if not, no indentation
 is performed.
 Also, the function checks if wxJSONWRITER_TAB_INDENT is set: if it is,
 indentation is done by writing \b num TAB characters otherwise,
 it is performed by writing a number of spaces computed as:
 \code
  numSpaces = m_indent + ( m_step * num )
 \endcode

*/
int
wxJSONWriter::WriteIndent( wxOutputStream& os, int num )
{
    int lastChar = 0;
    if ( !(m_style & wxJSONWRITER_STYLED) || (m_style & wxJSONWRITER_NO_INDENTATION))  {
        return lastChar;
    }

    int numChars = m_indent + ( m_step * num );
    char c = ' ';
    if ( m_style & wxJSONWRITER_TAB_INDENT ) {
        c = '\t';
        numChars = num;
    }

    for ( int i = 0; i < numChars; i++ )  {
        os.PutC( c );
        if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
            return -1;
        }

    }
    return c;
}
コード例 #9
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 The function writes the string \c str to the output object that
 was specified in the wxJSONWriter::Write() function.
 The function may split strings in two or more lines if the
 string contains LF characters if the \c m_style data member contains
 the wxJSONWRITER_SPLIT_STRING flag.

 The function does not actually write the string: for every character
 in the provided string the function calls WriteChar() which does
 the actual character output.

 The function returns ZERO on success or -1 in case of errors.
*/
int
wxJSONWriter::WriteStringValue( wxOutputStream& os, const wxString& str )
{
    // JSON values of type STRING are written by converting the whole string
    // to UTF-8 and then copying the UTF-8 buffer to the 'os' stream
    // one byte at a time and processing them
    os.PutC( '\"' );        // open quotes

    // the buffer that has to be written is either UTF-8 or ANSI c_str() depending
    // on the 'm_noUtf8' flag
    char* writeBuff = 0;
    wxCharBuffer utf8CB = str.ToUTF8();        // the UTF-8 buffer
#if !defined( wxJSON_USE_UNICODE )
    wxCharBuffer ansiCB( str.c_str());        // the ANSI buffer
    if ( m_noUtf8 )    {
        writeBuff = ansiCB.data();
    }
    else    {
        writeBuff = utf8CB.data();
    }
#else
        writeBuff = utf8CB.data();
#endif

    // NOTE: in ANSI builds UTF-8 conversion may fail (see samples/test5.cpp,
    // test 7.3) although I do not know why
    if ( writeBuff == 0 )    {
        const char* err = "<wxJSONWriter::WriteStringValue(): error converting the string to a UTF8 buffer>";
        os.Write( err, strlen( err ));
        return 0;
    }
    size_t len = strlen( writeBuff );
    int lastChar = 0;

    // store the column at which the string starts
    // splitting strings only happen if the string starts within
    // column wxJSONWRITER_LAST_COL (default 50)
    // see 'include/wx/json_defs.h' for the defines
    int tempCol = m_colNo;

    // now write the UTF8 buffer processing the bytes
    size_t i;
    for ( i = 0; i < len; i++ ) {
        bool shouldEscape = false;
        unsigned char ch = *writeBuff;
        ++writeBuff;        // point to the next byte

        // the escaped character
        char escCh = 0;

        // for every character we have to check if it is a character that
        // needs to be escaped: note that characters that should be escaped
        // may be not if some writer's flags are specified
        switch ( ch )  {
        case '\"' :     // quotes
            shouldEscape = true;
            escCh = '\"';
            break;
        case '\\' :     // reverse solidus
            shouldEscape = true;
            escCh = '\\';
            break;
        case '/'  :     // solidus
            shouldEscape = true;
            escCh = '/';
            break;
        case '\b' :     // backspace
            shouldEscape = true;
            escCh = 'b';
            break;
        case '\f' :     // formfeed
            shouldEscape = true;
            escCh = 'f';
            break;
        case '\n' :     // newline
            shouldEscape = true;
            escCh = 'n';
            break;
        case '\r' :     // carriage-return
            shouldEscape = true;
            escCh = 'r';
            break;
        case '\t' :      // horizontal tab
            shouldEscape = true;
            escCh = 't';
            break;
        default :
            shouldEscape = false;
            break;
        }        // end switch


        // if the character is a control character that is not identified by a
        // lowercase letter, we should escape it
        if ( !shouldEscape && ch < 32 )  {
            char b[8];
            snprintf( b, 8, "\\u%04X", (int) ch );
            os.Write( b, 6 );
            if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
                return -1;
            }
        }

        // the char is not a control character
        else {
            // some characters that should be escaped are not escaped
            // if the writer was constructed with some flags
            if ( shouldEscape && !( m_style & wxJSONWRITER_ESCAPE_SOLIDUS) )  {
                if ( ch == '/' )  {
                    shouldEscape = false;
                }
            }
            if ( shouldEscape && (m_style & wxJSONWRITER_MULTILINE_STRING))  {
                if ( ch == '\n' || ch == '\t' )  {
                    shouldEscape = false;
                }
            }


            // now write the character prepended by ESC if it should be escaped
            if ( shouldEscape )  {
                os.PutC( '\\' );
                os.PutC( escCh );
                if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
                    return -1;
                }
            }
            else {
                //  a normal char or a UTF-8 units: write the character
                os.PutC( ch );
                if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
                    return -1;
                }
            }
        }

        // check if SPLIT_STRING flag is set and if the string has to
        // be splitted
        if ( (m_style & wxJSONWRITER_STYLED) && (m_style & wxJSONWRITER_SPLIT_STRING))   {
            // split the string if the character written is LF
            if ( ch == '\n' ) {
                // close quotes and CR
                os.Write( "\"\n", 2 );
                lastChar = WriteIndent( os, m_level + 2 );     // write indentation
                os.PutC( '\"' );               // reopen quotes
                if ( lastChar < 0 )  {
                    return lastChar;
                }
            }
            // split the string only if there is at least wxJSONWRITER_MIN_LENGTH
            // character to write and the character written is a punctuation or space
            // BUG: the following does not work because the columns are not counted
            else if ( (m_colNo >= wxJSONWRITER_SPLIT_COL)
                     && (tempCol <= wxJSONWRITER_LAST_COL )) {
                if ( IsSpace( ch ) || IsPunctuation( ch ))  {
                    if ( len - i > wxJSONWRITER_MIN_LENGTH )  {
                        // close quotes and CR
                        os.Write( "\"\n", 2 );
                        lastChar = WriteIndent( os, m_level + 2 );     // write indentation
                        os.PutC( '\"' );           // reopen quotes
                        if ( lastChar < 0 )  {
                            return lastChar;
                        }
                    }
                }
            }
        }
    }            // end for
    os.PutC( '\"' );    // close quotes
    return 0;
}
コード例 #10
0
ファイル: jsonwriter.cpp プロジェクト: CarCode/Cocoa-OCPN
/*!
 The type wxJSONTYPE_MEMORYBUFF is a \b wxJSON extension that is not correctly read by
 other JSON implementations.
 By default, the function writes such a type as an array of INTs as follows:
 \code
   [ 0,32,45,255,6,...]
 \endcode
 If the writer object was constructed using the \c wxJSONWRITER_MEMORYBUFF flag, then
 the output is much more compact and recognized by the \b wxJSON reader as a memory buffer
 type:
 \code
   '00203FFF06..'
 \endcode

*/
int
wxJSONWriter::WriteMemoryBuff( wxOutputStream& os, const wxMemoryBuffer& buff )
{
#define MAX_BYTES_PER_ROW	20
    char str[16];

    // if STYLED and SPLIT_STRING flags are set, the function writes 20 bytes on every row
    // the following is the counter of bytes written.
    // the string is splitted only for the special meory buffer type, not for array of INTs
    int bytesWritten = 0;
    bool splitString = false;
    if ( (m_style & wxJSONWRITER_STYLED) && 
               (m_style & wxJSONWRITER_SPLIT_STRING))   {
        splitString = true;
    }

    size_t buffLen = buff.GetDataLen();
    unsigned char* ptr = (unsigned char*) buff.GetData();
    wxASSERT( ptr );
    char openChar = '\'';
    char closeChar = '\'';
    bool asArray = false;

    if ( (m_style & wxJSONWRITER_MEMORYBUFF ) == 0 )  {
        // if the special flag is not specified, write as an array of INTs
        openChar = '[';
        closeChar = ']';
        asArray = true;
    }
    // write the open character
    os.PutC( openChar );

    for ( size_t i = 0; i < buffLen; i++ )  {
        unsigned char c = *ptr;
        ++ptr;

        if ( asArray )  {
            snprintf( str, 14, "%d", c );
            size_t len = strlen( str );
            wxASSERT( len <= 3 );
            wxASSERT( len >= 1 );
            str[len] = ',';
            // do not write the comma char for the last element
            if ( i < buffLen - 1 )    {
                ++len;
            }
            os.Write( str, len );
            if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
                return -1;
            }
        }
        else {
            // now convert the byte in two hex digits
            char c1 = c / 16;
            char c2 = c % 16;
            c1 += '0';
            c2 += '0';
            if ( c1 > '9' )  {
                c1 += 7;
            }
            if ( c2 > '9' )  {
                c2 += 7;
            }
            os.PutC( c1 );
            os.PutC( c2 );
            if ( os.GetLastError() != wxSTREAM_NO_ERROR )    {
                return -1;
            }
            if ( splitString )  {
                ++bytesWritten;
            }

            if (( bytesWritten >= MAX_BYTES_PER_ROW ) && ((buffLen - i ) >= 5 ))   {
                // split the string if we wrote 20 bytes, but only is we have to
                // write at least 5 bytes
                os.Write( "\'\n", 2 );
                int lastChar = WriteIndent( os, m_level + 2 );     // write indentation
                os.PutC( '\'' );               // reopen quotes
                if ( lastChar < 0 )  {
                    return lastChar;
                }
                bytesWritten = 0;
            }
        }
    }

    // write the close character
    os.PutC( closeChar );
    return closeChar;
}