/*! 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; }
/*! 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; }
/*! 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; }
/*! 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; }
/*! 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; }