void CAeonRowValue::SerializeKey (IByteStream &Stream, const CString &sKey, DWORD *retdwSize) // SerializeKey // // Serializes a key and returns the serialized size. // // DWORD key size // BYTES[] key (padded to DWORD align) { DWORD dwKeySize = sKey.GetLength(); // Write out the key length Stream.Write(&dwKeySize, sizeof(DWORD)); // Write out the key (add 1 for NULL termination) Stream.Write(sKey.GetParsePointer(), dwKeySize + 1); // Save padding DWORD dwZero = 0; DWORD dwAlignedKeySize = AlignUp(dwKeySize + 1, (DWORD)sizeof(DWORD)); Stream.Write(&dwZero, dwAlignedKeySize - (dwKeySize + 1)); // Done if (retdwSize) *retdwSize = sizeof(DWORD) + dwAlignedKeySize; }
bool CDatum::DeserializeTextUTF8 (IByteStream &Stream, CDatum *retDatum) // DeserializeTextUTF8 // // Loads straight UTF-8 into a single string value. { CStringBuffer Buffer; // See if we have an encoding mark BYTE BOM[3]; Stream.Read(BOM, sizeof(BOM)); if (BOM[0] == 0xef && BOM[1] == 0xbb && BOM[2] == 0xbf) ; // UTF-8 // Otherwise, not an encoding mark, so write it to the buffer else Buffer.Write(BOM, sizeof(BOM)); // Write the rest Buffer.Write(Stream, Stream.GetStreamLength()); return CreateStringFromHandoff(Buffer, retDatum); }
void CComplexBinary::OnSerialize (CDatum::ESerializationFormats iFormat, IByteStream &Stream) const // OnSerialize // // Serialize { DWORD dwLength = GetLength(); Stream.Write(&dwLength, sizeof(DWORD)); if (dwLength) Stream.Write(m_pData, dwLength); }
bool CComplexBinary::OnDeserialize (CDatum::ESerializationFormats iFormat, const CString &sTypename, IByteStream &Stream) // OnDeserialize // // Deserialize { if (m_pData) { delete [] GetBuffer(); m_pData = NULL; } DWORD dwLength; Stream.Read(&dwLength, sizeof(DWORD)); if (dwLength > 0) { CComplexBinary Temp(Stream, dwLength); m_pData = Temp.m_pData; Temp.m_pData = NULL; } return true; }
void CComplexDateTime::Serialize (CDatum::ESerializationFormats iFormat, IByteStream &Stream) const // Serialize // // Serialize to the given format. { switch (iFormat) { case CDatum::formatAEONScript: case CDatum::formatAEONLocal: { CString sDate = strPattern("#%d-%02d-%02dT%02d:%02d:%02d.%04d", m_DateTime.Year(), m_DateTime.Month(), m_DateTime.Day(), m_DateTime.Hour(), m_DateTime.Minute(), m_DateTime.Second(), m_DateTime.Millisecond()); Stream.Write(sDate); break; } case CDatum::formatJSON: { CString sDate = strPattern("\"%d-%02d-%02dT%02d:%02d:%02d.%04d\"", m_DateTime.Year(), m_DateTime.Month(), m_DateTime.Day(), m_DateTime.Hour(), m_DateTime.Minute(), m_DateTime.Second(), m_DateTime.Millisecond()); Stream.Write(sDate); break; } default: IComplexDatum::Serialize(iFormat, Stream); break; } }
bool CDatum::CreateBinary (IByteStream &Stream, int iSize, CDatum *retDatum) // CreateBinary // // Creates a string datum containing binary data from the stream. // If iSize is -1 then we read as much as the stream has. // Otherwise we read up to iSize. { // LATER: Handle streams more than 2GB. Instead of asking how much space // is left, maybe we should just ask to truncate the size that we're // requesting. int iDataRemaining = Stream.GetStreamLength() - Stream.GetPos(); int iBinarySize = (iSize < 0 ? iDataRemaining : Min(iDataRemaining, iSize)); // 0-size if (iBinarySize == 0) { *retDatum = CDatum(); return true; } // Read the stream CComplexBinary *pBinary; try { pBinary = new CComplexBinary(Stream, iBinarySize); } catch (...) { return false; } // Done *retDatum = CDatum(pBinary); // Done return true; }
void CAeonRowValue::Serialize (IByteStream &Stream) // Serialize // // Serialize the row (we always write out aligned to DWORD) { if (m_pFixedBlock) Stream.Write(m_pFixedBlock, GetFixedBlockSize()); }
bool CRawMediaType::EncodeToBuffer (IByteStream &Stream, DWORD dwOffset, DWORD dwSize) const // EncodeToBuffer { if (dwSize == 0xffffffff) Stream.Write(m_sBody); else { DWORD dwBodyLen = m_sBody.GetLength(); if (dwOffset < dwBodyLen) { DWORD dwActualSize = Min(dwSize, dwBodyLen - dwOffset); Stream.Write(m_sBody.GetParsePointer() + dwOffset, dwSize); } } return true; }
void CComplexArray::Serialize (CDatum::ESerializationFormats iFormat, IByteStream &Stream) const // Serialize // // Serialize to the given format. { int i; switch (iFormat) { case CDatum::formatAEONScript: case CDatum::formatAEONLocal: { Stream.Write("(", 1); for (i = 0; i < m_Array.GetCount(); i++) { if (i != 0) Stream.Write(" ", 1); m_Array[i].Serialize(iFormat, Stream); } Stream.Write(")", 1); break; } case CDatum::formatJSON: { Stream.Write("[", 1); for (int i = 0; i < m_Array.GetCount(); i++) { if (i != 0) Stream.Write(", ", 2); m_Array[i].Serialize(iFormat, Stream); } Stream.Write("]", 1); break; } default: IComplexDatum::Serialize(iFormat, Stream); break; } }
void IComplexDatum::WriteBinaryToStream (IByteStream &Stream, int iPos, int iLength, IProgressEvents *pProgress) const // WriteBinaryToStream { const CString &sData = CastCString(); if (iPos >= sData.GetLength()) return; if (pProgress) pProgress->OnProgressStart(); if (iLength == -1) iLength = Max(0, sData.GetLength() - iPos); else iLength = Min(iLength, sData.GetLength() - iPos); Stream.Write(sData.GetPointer() + iPos, iLength); if (pProgress) pProgress->OnProgressDone(); }
void CMnemosynthDb::DebugDump (IByteStream &Stream) // DebugDump // // Dumps the entire database { CSmartLock Lock(m_cs); int i, j; for (i = 0; i < m_Collections.GetCount(); i++) { SCollection *pCollection = &m_Collections[i]; if (pCollection->Entries.GetCount() == 0) continue; // Write out the collection name const CString &sCollectionName = m_Collections.GetKey(i); Stream.Write(sCollectionName); Stream.Write("\n", 1); // Write out each entry in the collection for (j = 0; j < pCollection->Entries.GetCount(); j++) { // Write the key const CString &sKey = pCollection->Entries.GetKey(j); Stream.Write("\t", 1); Stream.Write(sKey); Stream.Write("\n", 1); SEntry *pEntry = &pCollection->Entries[j]; Stream.Write("\t\t", 2); pEntry->dValue.Serialize(CDatum::formatAEONScript, Stream); Stream.Write("\n", 1); } } }
CComplexBinary::CComplexBinary (IByteStream &Stream, int iLength) // CComplexBinary constructor { ASSERT(iLength >= 0); if (iLength == 0) m_pData = NULL; else { char *pPos = new char [sizeof(DWORD) + iLength + 1]; *(DWORD *)pPos = iLength; pPos += sizeof(DWORD); m_pData = pPos; Stream.Read(pPos, iLength); pPos += iLength; *pPos = '\0'; } }
void CDatum::WriteBinaryToStream (IByteStream &Stream, int iPos, int iLength, IProgressEvents *pProgress) const // WriteBinaryToStream // // Writes a binary blob object to a stream. { switch (m_dwData & AEON_TYPE_MASK) { case AEON_TYPE_COMPLEX: raw_GetComplex()->WriteBinaryToStream(Stream, iPos, iLength, pProgress); break; default: { const CString &sData = *this; if (iPos >= sData.GetLength()) return; if (iLength == -1) iLength = Max(0, sData.GetLength() - iPos); else iLength = Min(iLength, sData.GetLength() - iPos); if (pProgress) pProgress->OnProgressStart(); Stream.Write(sData.GetPointer() + iPos, iLength); if (pProgress) pProgress->OnProgressDone(); break; } } }
void htmlWriteAttributeValue (const CString &sText, IByteStream &Output) // htmlWriteAttributeValue // // Writes attribute value text, escaping all appropriate characters. { char *pPos = sText.GetParsePointer(); char *pPosEnd = pPos + sText.GetLength(); char *pStart = pPos; while (pPos < pPosEnd) { if (*pPos == '<') { Output.Write(pStart, pPos - pStart); pPos++; pStart = pPos; Output.Write("<", 4); } else if (*pPos == '>') { Output.Write(pStart, pPos - pStart); pPos++; pStart = pPos; Output.Write(">", 4); } else if (*pPos == '&') { Output.Write(pStart, pPos - pStart); pPos++; pStart = pPos; Output.Write("&", 5); } else if (*pPos == '\"') { Output.Write(pStart, pPos - pStart); pPos++; pStart = pPos; Output.Write(""", 6); } else if (*pPos == '\'') { Output.Write(pStart, pPos - pStart); pPos++; pStart = pPos; Output.Write("'", 6); } else if (*pPos == '`') { Output.Write(pStart, pPos - pStart); pPos++; pStart = pPos; Output.Write("`", 6); } else // LATER: Should check for lower-ASCII characters pPos++; } Output.Write(pStart, pPos - pStart); }
void htmlWriteText (char *pPos, char *pPosEnd, IByteStream &Output) // htmlWriteText // // Writes text, escaping all appropriate HTML characters. // // NOTE: This is only for the text content of HTML element; for escaping // attribute values, use htmlWriteAttributeValue { char *pStart = pPos; while (pPos < pPosEnd) { switch (*pPos) { case '<': Output.Write(pStart, (int)(pPos - pStart)); pPos++; pStart = pPos; Output.Write("<", 4); break; case '>': Output.Write(pStart, (int)(pPos - pStart)); pPos++; pStart = pPos; Output.Write(">", 4); break; case '&': Output.Write(pStart, (int)(pPos - pStart)); pPos++; pStart = pPos; Output.Write("&", 5); break; default: // Most control codes (other than whitespace) are illegal, // so we strip them out. if ((BYTE)*pPos <= 0x08 || *pPos == 0x0b || *pPos == 0x0c || ((BYTE)*pPos >= 0x0e && (BYTE)*pPos <= 0x1f)) { Output.Write(pStart, (int)(pPos - pStart)); pPos++; pStart = pPos; } // Otherwise, keep parsing else pPos++; break; } } Output.Write(pStart, (int)(pPos - pStart)); }
void CComplexStruct::Serialize (CDatum::ESerializationFormats iFormat, IByteStream &Stream) const // Serialize // // Serialize to the given format. { int i; switch (iFormat) { case CDatum::formatAEONScript: case CDatum::formatAEONLocal: { Stream.Write("{", 1); for (i = 0; i < m_Map.GetCount(); i++) { if (i != 0) Stream.Write(" ", 1); // Write the key CDatum Key(m_Map.GetKey(i)); Key.Serialize(iFormat, Stream); // Separator Stream.Write(":", 1); // Write the value m_Map[i].Serialize(iFormat, Stream); } Stream.Write("}", 1); break; } case CDatum::formatJSON: { Stream.Write("{", 1); for (i = 0; i < m_Map.GetCount(); i++) { if (i != 0) Stream.Write(", ", 2); // Write the key CDatum Key(m_Map.GetKey(i)); Key.Serialize(iFormat, Stream); // Separator Stream.Write(": ", 2); // Write the value m_Map[i].Serialize(iFormat, Stream); } Stream.Write("}", 1); break; } default: IComplexDatum::Serialize(iFormat, Stream); break; } }
void IComplexDatum::Serialize (CDatum::ESerializationFormats iFormat, IByteStream &Stream) const // Serialize // // Serialize the datum { DWORD dwFlags = OnGetSerializeFlags(); switch (iFormat) { case CDatum::formatAEONScript: case CDatum::formatAEONLocal: { if (!(dwFlags & FLAG_SERIALIZE_NO_TYPENAME)) { Stream.Write("[", 1); Stream.Write(GetTypename()); Stream.Write(":", 1); } // If this is object is serializable as a struct, then we do that. if (dwFlags & FLAG_SERIALIZE_AS_STRUCT) { CComplexStruct *pStruct = new CComplexStruct; OnSerialize(iFormat, pStruct); CDatum dDatum(pStruct); dDatum.Serialize(iFormat, Stream); } // Otherwise, serialize as base64 encoding else { CBase64Encoder Encoder(&Stream); OnSerialize(iFormat, Encoder); Encoder.Close(); } if (!(dwFlags & FLAG_SERIALIZE_NO_TYPENAME)) Stream.Write("]", 1); break; } case CDatum::formatJSON: { if (!(dwFlags & FLAG_SERIALIZE_NO_TYPENAME)) { Stream.Write("[\"AEON2011:", 11); Stream.Write(GetTypename()); Stream.Write(":v1\", \"", 7); } // LATER: Handle serialization/deserialization of struct-based objects CBase64Encoder Encoder(&Stream); OnSerialize(iFormat, Encoder); Encoder.Close(); if (!(dwFlags & FLAG_SERIALIZE_NO_TYPENAME)) Stream.Write("\"]", 2); break; } default: ASSERT(false); } }