libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / apps / cortex / Persistence / Wrappers / FlatMessageIO.cpp
bloba94769b102fff296556691d3d8066e074d3d1662
1 /*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions, and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // FlatMessageIO.cpp
33 // e.moon 6jul99
35 #include "FlatMessageIO.h"
36 //#include "xml_export_utils.h"
38 #include "ExportContext.h"
40 #include <Debug.h>
42 // base64 encoding tools
43 #include <E-mail.h>
45 #include <cstdlib>
46 #include <cstring>
48 __USE_CORTEX_NAMESPACE
50 // -------------------------------------------------------- //
51 // *** constants
52 // -------------------------------------------------------- //
54 const char* const FlatMessageIO::s_element = "flat-BMessage";
56 // -------------------------------------------------------- //
57 // *** ctor/dtor/accessor
58 // -------------------------------------------------------- //
60 FlatMessageIO::~FlatMessageIO() {
61 if(m_ownMessage && m_message)
62 delete m_message;
64 FlatMessageIO::FlatMessageIO() :
65 m_ownMessage(true),
66 m_message(0) {}
67 FlatMessageIO::FlatMessageIO(const BMessage* message) :
68 m_ownMessage(false),
69 m_message(const_cast<BMessage*>(message)) {}
71 void FlatMessageIO::setMessage(BMessage* message) {
72 if(m_ownMessage && m_message)
73 delete m_message;
74 m_ownMessage = false;
75 m_message = message;
78 // -------------------------------------------------------- //
79 // *** static setup method
80 // -------------------------------------------------------- //
82 // call this method to install hooks for the tags needed by
83 // FlatMessageIO into the given document type
84 /*static*/
85 void FlatMessageIO::AddTo(XML::DocumentType* pDocType) {
86 pDocType->addMapping(new Mapping<FlatMessageIO>(s_element));
89 // -------------------------------------------------------- //
90 // *** IPersistent impl.
91 // -------------------------------------------------------- //
94 void FlatMessageIO::xmlExportBegin(
95 ExportContext& context) const {
97 context.beginElement(s_element);
100 void FlatMessageIO::xmlExportAttributes(
101 ExportContext& context) const {
103 context.writeAttr("encoding", "base64");
106 void FlatMessageIO::xmlExportContent(
107 ExportContext& context) const {
109 context.beginContent();
111 // convert message to base64
112 ASSERT(m_message);
113 ssize_t flatSize = m_message->FlattenedSize();
114 ASSERT(flatSize);
115 char* flatData = new char[flatSize];
116 status_t err = m_message->Flatten(flatData, flatSize);
117 ASSERT(err == B_OK);
119 // make plenty of room for encoded content (encode_base64 adds newlines)
120 ssize_t base64Size = ((flatSize * 3) / 2);
121 char* base64Data = new char[base64Size];
122 ssize_t base64Used = encode_base64(base64Data, flatData, flatSize);
123 base64Data[base64Used] = '\0';
125 // write the data
127 const char* pos = base64Data;
128 while(*pos) {
129 ssize_t chunk = 0;
130 const char* nextBreak = strchr(pos, '\n');
131 if(!nextBreak)
132 chunk = strlen(pos);
133 else
134 chunk = nextBreak - pos;
136 context.writeString(context.indentString());
137 context.writeString(pos, chunk);
138 context.writeString("\n");
140 pos += chunk;
141 if(*pos == '\n')
142 ++pos;
145 // clean up
146 delete [] flatData;
147 delete [] base64Data;
150 void FlatMessageIO::xmlExportEnd(
151 ExportContext& context) const {
152 context.endElement();
155 // -------------------------------------------------------- //
157 void FlatMessageIO::xmlImportBegin(
158 ImportContext& context) {
160 m_base64Data = "";
163 void FlatMessageIO::xmlImportAttribute(
164 const char* key,
165 const char* value,
166 ImportContext& context) {
167 if(!strcmp(key, "encoding")) {
168 if(strcmp(value, "base64") != 0)
169 context.reportError("Unexpected value of 'encoding'.");
173 void FlatMessageIO::xmlImportContent(
174 const char* data,
175 uint32 length,
176 ImportContext& context) {
177 m_base64Data.Append(data, length);
180 void FlatMessageIO::xmlImportChild(
181 IPersistent* child,
182 ImportContext& context) {
183 delete child;
184 context.reportError("Unexpected child object.");
187 void FlatMessageIO::xmlImportComplete(
188 ImportContext& context) {
190 if(m_ownMessage && m_message)
191 delete m_message;
193 // decode the message
194 ssize_t decodedSize = m_base64Data.Length();
195 char* decodedData = new char[decodedSize];
196 decodedSize = decode_base64(
197 decodedData, const_cast<char*>(m_base64Data.String()),
198 m_base64Data.Length(), false);
200 m_message = new BMessage();
201 m_ownMessage = true;
203 status_t err = m_message->Unflatten(decodedData);
204 if(err < B_OK) {
205 // decode failed; report error & clean up
206 BString error = "Unflatten(): ";
207 error << strerror(err);
208 context.reportError(error.String());
210 delete m_message;
211 m_message = 0;
214 m_base64Data = "";
215 delete [] decodedData;
218 // END -- FlatMessageIO.cpp --