bool XmlWriter::AppendAttributeF( const char *psAttrName, const char *psValueFmt, ... ) { EA_ASSERT_MESSAGE( mnState != kStateChars, "Attempt to append an attribute to an element that has already been closed." ); if ((mnState != kStateElement) && (mnState != kStateProcessingInstruction)) return false; /// For now, we assume that 256 is the max length for a formatted attribute. char sBuffer[ 256 ]; va_list arg; va_start( arg, psValueFmt ); const int result = vsnprintf_local( sBuffer, sizeof(sBuffer), psValueFmt, arg ); va_end( arg ); if ((unsigned)result >= sizeof(sBuffer)) // If (result < 0 or result >= sizeof(sBuffer))... return false; return (WriteText( " ", 1 ) && WriteName( psAttrName ) && WriteText( "=\"", 2 ) && WriteEscapedString( sBuffer, (unsigned)result ) && WriteText( "\"", 1 ) ); }
static int LuaJsonWriteString(lua_State* L) { LuaJsonWriter* self = (LuaJsonWriter*) luaL_checkudata(L, 1, "tundra_jsonw"); size_t value_len; const char* value = lua_tolstring(L, 2, &value_len); WriteCommon(L, self, 3); WriteEscapedString(&self->m_Writer, value, value_len); return 0; }
static void WriteCommon(lua_State* L, LuaJsonWriter* self, int name_index) { WriteComma(self); if (lua_gettop(L) >= name_index) { size_t len; const char* str = luaL_checklstring(L, name_index, &len); WriteEscapedString(&self->m_Writer, str, len); BufferedWriterAppend(&self->m_Writer, ':'); } }
//////////////////////////////////////////////////////////////////////////////// // AppendAttribute // bool XmlWriter::AppendAttribute( const char *psAttrName, const char16_t *psValue ) { EA_ASSERT_MESSAGE( mnState != kStateChars, "Attempt to append an attribute to an element that has already been closed." ); if ((mnState != kStateElement) && (mnState != kStateProcessingInstruction)) return false; return (WriteText( " ", 1 ) && WriteName( psAttrName ) && WriteText( "=\"", 2 ) && WriteEscapedString( psValue, kSizeTypeNull ) && WriteText( "\"", 1 ) ); }
void ConfigManager::WriteData(std::ostream& out, DataNode *node) { switch(node->GetNodeType()) { case CHAR_NODE: out << node->AsChar(); break; case UNSIGNED_CHAR_NODE: out << (int)node->AsUnsignedChar(); break; case INT_NODE: out << node->AsInt(); break; case LONG_NODE: out << node->AsLong(); break; case FLOAT_NODE: out << std::setprecision(7) << node->AsFloat(); break; case DOUBLE_NODE: out << std::setprecision(15) << node->AsDouble(); break; case STRING_NODE: { // new scope bool hasSpaces = node->AsString().find(" ") != std::string::npos; if(hasSpaces) out << "\""; WriteEscapedString(out, node->AsString()); if(hasSpaces) out << "\""; } break; case BOOL_NODE: if(node->AsBool()) out << "true"; else out << "false"; break; case CHAR_ARRAY_NODE: { // new scope const char *cptr = node->AsCharArray(); for(int i = 0; i < node->GetLength(); ++i) out << (char)*cptr++ << " "; } break; case UNSIGNED_CHAR_ARRAY_NODE: { // new scope const unsigned char *uptr = node->AsUnsignedCharArray(); for(int i = 0; i < node->GetLength(); ++i) out << (int)*uptr++ << " "; } break; case INT_ARRAY_NODE: { // new scope const int *iptr = node->AsIntArray(); for(int i = 0; i < node->GetLength(); ++i) out << *iptr++ << " "; } break; case LONG_ARRAY_NODE: { // new scope const long *lptr = node->AsLongArray(); for(int i = 0; i < node->GetLength(); ++i) out << *lptr++ << " "; } break; case FLOAT_ARRAY_NODE: { // new scope const float *fptr = node->AsFloatArray(); for(int i = 0; i < node->GetLength(); ++i) out << std::setprecision(7) << *fptr++ << " "; } break; case DOUBLE_ARRAY_NODE: { // new scope const double *dptr = node->AsDoubleArray(); for(int i = 0; i < node->GetLength(); ++i) out << std::setprecision(15) << *dptr++ << " "; } break; case STRING_ARRAY_NODE: { // new scope const std::string *sptr = node->AsStringArray(); for(int i = 0; i < node->GetLength(); ++i) WriteQuotedStringData(out,*sptr++); } break; case BOOL_ARRAY_NODE: { // new scope const bool *bptr = node->AsBoolArray(); for(int i = 0; i < node->GetLength(); ++i) { if(*bptr++) out << "true "; else out << "false "; } } break; case CHAR_VECTOR_NODE: { // new scope const charVector &cvec = node->AsCharVector(); for(size_t i = 0; i < cvec.size(); ++i) out << cvec[i] << " "; } break; case UNSIGNED_CHAR_VECTOR_NODE: { // new scope const unsignedCharVector &uvec = node->AsUnsignedCharVector(); for(size_t i = 0; i < uvec.size(); ++i) out << (int)uvec[i] << " "; } break; case INT_VECTOR_NODE: { // new scope const intVector &ivec = node->AsIntVector(); for(size_t i = 0; i < ivec.size(); ++i) out << ivec[i] << " "; } break; case LONG_VECTOR_NODE: { // new scope const longVector &lvec = node->AsLongVector(); for(size_t i = 0; i < lvec.size(); ++i) out << lvec[i] << " "; } break; case FLOAT_VECTOR_NODE: { // new scope const floatVector &fvec = node->AsFloatVector(); for(size_t i = 0; i < fvec.size(); ++i) out << std::setprecision(7) << fvec[i] << " "; } break; case DOUBLE_VECTOR_NODE: { // new scope const doubleVector &dvec = node->AsDoubleVector(); for(size_t i = 0; i < dvec.size(); ++i) out << std::setprecision(15) << dvec[i] << " "; } break; case STRING_VECTOR_NODE: { // new scope const stringVector &svec = node->AsStringVector(); for(size_t i = 0; i < svec.size(); ++i) WriteQuotedStringData(out, svec[i]); } break; default: fprintf(stderr, "ConfigManager::WriteData: Unsupported type\n"); } }
bool XmlWriter::WriteCharData( const char16_t *psCharData, size_t nCount ) { return CloseCurrentElement() && WriteEscapedString( psCharData, nCount ); }