bool eSettings::AddReplace(const wxString& pattern) { wxJSONValue& replacements = m_jsonRoot[wxT("replaceHistory")]; // Don't add duplicates if (replacements.Size() > 0) { const wxJSONValue last = replacements.ItemAt(0); if (last.AsString() == pattern) return false; // Check if there should be a duplicate lower down for (int i = 0; i < replacements.Size(); ++i) { if (replacements[i].AsString() == pattern) { replacements.Remove(i); break; } } } // Add the new item replacements.Insert(0, pattern); // Limit number of items to save if (replacements.Size() > 20) replacements.Remove(replacements.Size()-1); return true; }
bool eSettings::AddFilterCommand(const wxString& command) { wxJSONValue& values = m_jsonRoot[wxT("filterCommandHistory")]; // Don't add duplicates if (values.Size() > 0) { const wxJSONValue last = values.ItemAt(0); if (last.AsString() == command) return false; // Check if there should be a duplicate lower down for (int i = 0; i < values.Size(); ++i) { if (values[i].AsString() == command) { values.Remove(i); break; } } } // Add the new item values.Insert(0, command); // Limit number of items to save if (values.Size() > 20) values.Remove(values.Size()-1); return true; }
bool eSettings::GetSettingString(const wxString& name, wxString& value) const { if (!m_jsonRoot.HasMember(wxT("settings"))) return false; const wxJSONValue settings = m_jsonRoot.ItemAt(wxT("settings")); if (!settings.HasMember(name)) return false; const wxJSONValue val = settings.ItemAt(name); if (!val.IsString()) return false; value = val.AsString(); return true; }
/*! This is a recursive function that gets the type of the \c value object and calls several protected functions depending on the type: \li \c WriteNullvalue for type NULL \li \c WriteStringValue() for STRING and CSTRING types \li \c WriteIntValue for INT types \li \c WriteUIntValue for UINT types \li \c WriteBoolValue for BOOL types \li \c WriteDoubleValue for DOUBLE types \li \c WriteMemoryBuff for MEMORYBUFF types If the value is an array or key/value map (types ARRAY and OBJECT), the function iterates through all JSON value object in the array/map and calls itself for every item in the container. */ int wxJSONWriter::DoWrite( wxOutputStream& os, const wxJSONValue& value, const wxString* key, bool comma ) { // note that this function is recursive // some variables that cannot be allocated in the switch statement const wxJSONInternalMap* map = 0; int size; m_colNo = 1; m_lineNo = 1; // determine the comment position; it is one of: // // wxJSONVALUE_COMMENT_BEFORE // wxJSONVALUE_COMMENT_AFTER // wxJSONVALUE_COMMENT_INLINE // // or -1 if comments have not to be written int commentPos = -1; if ( value.GetCommentCount() > 0 && (m_style & wxJSONWRITER_WRITE_COMMENTS)) { commentPos = value.GetCommentPos(); if ( ( m_style & wxJSONWRITER_COMMENTS_BEFORE) != 0 ) { commentPos = wxJSONVALUE_COMMENT_BEFORE; } else if ( (m_style & wxJSONWRITER_COMMENTS_AFTER) != 0 ) { commentPos = wxJSONVALUE_COMMENT_AFTER; } } int lastChar = 0; // check if WriteComment() writes the last LF char // first write the comment if it is BEFORE if ( commentPos == wxJSONVALUE_COMMENT_BEFORE ) { lastChar = WriteComment( os, value, true ); if ( lastChar < 0 ) { return lastChar; } else if ( lastChar != '\n' ) { WriteSeparator( os ); } } lastChar = WriteIndent( os ); if ( lastChar < 0 ) { return lastChar; } // now write the key if it is not NULL if ( key ) { lastChar = WriteKey( os, *key ); } if ( lastChar < 0 ) { return lastChar; } // now write the value wxJSONInternalMap::const_iterator it; // declare the map object long int count = 0; wxJSONType t = value.GetType(); switch ( t ) { case wxJSONTYPE_INVALID : WriteInvalid( os ); wxFAIL_MSG( _T("wxJSONWriter::WriteEmpty() cannot be called (not a valid JSON text")); break; case wxJSONTYPE_INT : case wxJSONTYPE_SHORT : case wxJSONTYPE_LONG : case wxJSONTYPE_INT64 : lastChar = WriteIntValue( os, value ); break; case wxJSONTYPE_UINT : case wxJSONTYPE_USHORT : case wxJSONTYPE_ULONG : case wxJSONTYPE_UINT64 : lastChar = WriteUIntValue( os, value ); break; case wxJSONTYPE_NULL : lastChar = WriteNullValue( os ); break; case wxJSONTYPE_BOOL : lastChar = WriteBoolValue( os, value ); break; case wxJSONTYPE_DOUBLE : lastChar = WriteDoubleValue( os, value ); break; case wxJSONTYPE_STRING : case wxJSONTYPE_CSTRING : lastChar = WriteStringValue( os, value.AsString()); break; case wxJSONTYPE_MEMORYBUFF : lastChar = WriteMemoryBuff( os, value.AsMemoryBuff()); break; case wxJSONTYPE_ARRAY : ++m_level; os.PutC( '[' ); // the inline comment for objects and arrays are printed in the open char if ( commentPos == wxJSONVALUE_COMMENT_INLINE ) { commentPos = -1; // we have already written the comment lastChar = WriteComment( os, value, false ); if ( lastChar < 0 ) { return lastChar; } if ( lastChar != '\n' ) { lastChar = WriteSeparator( os ); } } else { // comment is not to be printed inline, so write a LF lastChar = WriteSeparator( os ); if ( lastChar < 0 ) { return lastChar; } } // now iterate through all sub-items and call DoWrite() recursively size = value.Size(); for ( int i = 0; i < size; i++ ) { bool comma = false; if ( i < size - 1 ) { comma = true; } wxJSONValue v = value.ItemAt( i ); lastChar = DoWrite( os, v, 0, comma ); if ( lastChar < 0 ) { return lastChar; } } --m_level; lastChar = WriteIndent( os ); if ( lastChar < 0 ) { return lastChar; } os.PutC( ']' ); break; case wxJSONTYPE_OBJECT : ++m_level; os.PutC( '{' ); // the inline comment for objects and arrays are printed in the open char if ( commentPos == wxJSONVALUE_COMMENT_INLINE ) { commentPos = -1; // we have already written the comment lastChar = WriteComment( os, value, false ); if ( lastChar < 0 ) { return lastChar; } if ( lastChar != '\n' ) { WriteSeparator( os ); } } else { lastChar = WriteSeparator( os ); } map = value.AsMap(); size = value.Size(); count = 0; for ( it = map->begin(); it != map->end(); ++it ) { // get the key and the value wxString key = it->first; const wxJSONValue& v = it->second; bool comma = false; if ( count < size - 1 ) { comma = true; } lastChar = DoWrite( os, v, &key, comma ); if ( lastChar < 0 ) { return lastChar; } count++; } --m_level; lastChar = WriteIndent( os ); if ( lastChar < 0 ) { return lastChar; } os.PutC( '}' ); break; default : // a not yet defined wxJSONType: we FAIL wxFAIL_MSG( _T("wxJSONWriter::DoWrite() undefined wxJSONType type")); break; } // writes the comma character before the inline comment if ( comma ) { os.PutC( ',' ); } if ( commentPos == wxJSONVALUE_COMMENT_INLINE ) { lastChar = WriteComment( os, value, false ); if ( lastChar < 0 ) { return lastChar; } } else if ( commentPos == wxJSONVALUE_COMMENT_AFTER ) { WriteSeparator( os ); lastChar = WriteComment( os, value, true ); if ( lastChar < 0 ) { return lastChar; } } if ( lastChar != '\n' ) { lastChar = WriteSeparator( os ); } return lastChar; }