1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_automation.hxx"
30 
31 /*************************************************************************
32  *
33  *	  ATTENTION
34  *	  This file is intended to work inside and outside the StarOffice environment.
35  *	  Only adaption of file commtypes.hxx should be necessary. Else it is a bug!
36  *
37  ************************************************************************/
38 #include <osl/endian.h>
39 
40 #include "cmdbasestream.hxx"
41 #include "rcontrol.hxx"
42 
43 CmdBaseStream::CmdBaseStream()
44 : pCommStream( NULL )
45 {
46 }
47 
48 CmdBaseStream::~CmdBaseStream()
49 {
50 }
51 
52 void CmdBaseStream::GenError (rtl::OString *pUId, comm_String *pString )
53 {
54 	Write(comm_USHORT(SIReturnError));
55 	Write(pUId);
56 	Write(pString);
57 }
58 
59 void CmdBaseStream::GenReturn (comm_USHORT nRet, comm_ULONG nUId )
60 {
61 	Write(comm_USHORT(SIReturn));
62 	Write(nRet);
63 	Write(nUId);
64 	Write(comm_USHORT(PARAM_NONE));				// Typ der folgenden Parameter
65 }
66 
67 void CmdBaseStream::GenReturn (comm_USHORT nRet, rtl::OString *pUId, comm_ULONG nNr )
68 {
69 	Write(comm_USHORT(SIReturn));
70 	Write(nRet);
71 	if ( pUId->equals( rtl::OString( "UID_ACTIVE" ) ) )
72 		Write(comm_ULONG(0));
73 	else
74 		Write(pUId);
75 	Write(comm_USHORT(PARAM_ULONG_1));			// Typ der folgenden Parameter
76 	Write(nNr);
77 }
78 
79 void CmdBaseStream::GenReturn (comm_USHORT nRet, rtl::OString *pUId, comm_String *pString )
80 {
81 	Write(comm_USHORT(SIReturn));
82 	Write(nRet);
83 	if ( pUId->equals( rtl::OString( "UID_ACTIVE" ) ) )
84 		Write(comm_ULONG(0));
85 	else
86 		Write(pUId);
87 	Write(comm_USHORT(PARAM_STR_1));				// Typ der folgenden Parameter
88 	Write(pString);
89 }
90 
91 void CmdBaseStream::GenReturn (comm_USHORT nRet, rtl::OString *pUId, comm_BOOL bBool )
92 {
93 	Write(comm_USHORT(SIReturn));
94 	Write(nRet);
95 	if ( pUId->equals( rtl::OString( "UID_ACTIVE" ) ) )
96 		Write(comm_ULONG(0));
97 	else
98 		Write(pUId);
99 	Write(comm_USHORT(PARAM_BOOL_1));			// Typ der folgenden Parameter
100 	Write(bBool);
101 }
102 
103 void CmdBaseStream::GenReturn (comm_USHORT nRet, rtl::OString *pUId, comm_ULONG nNr, comm_String *pString, comm_BOOL bBool )
104 {
105 	Write(comm_USHORT(SIReturn));
106 	Write(nRet);
107 	if ( pUId->equals( rtl::OString( "UID_ACTIVE" ) ) )
108 		Write(comm_ULONG(0));
109 	else
110 		Write(pUId);
111 	Write(comm_USHORT(PARAM_ULONG_1|PARAM_STR_1|PARAM_BOOL_1));		// Typ der folgenden Parameter
112 	Write(nNr);
113 	Write(pString);
114 	Write(bBool);
115 }
116 
117 void CmdBaseStream::GenReturn (comm_USHORT nRet, comm_USHORT nMethod, comm_ULONG nNr )
118 {
119 	Write(comm_USHORT(SIReturn));
120 	Write(nRet);
121 	Write((comm_ULONG)nMethod); //HELPID BACKWARD (no sal_uLong needed)
122 	Write(comm_USHORT(PARAM_ULONG_1));			// Typ der folgenden Parameter
123 	Write(nNr);
124 }
125 
126 void CmdBaseStream::GenReturn (comm_USHORT nRet, comm_USHORT nMethod, comm_String *pString )
127 {
128 	Write(comm_USHORT(SIReturn));
129 	Write(nRet);
130 	Write((comm_ULONG)nMethod); //HELPID BACKWARD (no sal_uLong needed)
131 	Write(comm_USHORT(PARAM_STR_1));				// Typ der folgenden Parameter
132 	Write(pString);
133 }
134 
135 void CmdBaseStream::GenReturn (comm_USHORT nRet, comm_USHORT nMethod, comm_BOOL bBool )
136 {
137 	Write(comm_USHORT(SIReturn));
138 	Write(nRet);
139 	Write((comm_ULONG)nMethod); //HELPID BACKWARD (no sal_uLong needed)
140 	Write(comm_USHORT(PARAM_BOOL_1));			// Typ der folgenden Parameter
141 	Write(bBool);
142 }
143 
144 void CmdBaseStream::GenReturn (comm_USHORT nRet, comm_USHORT nMethod, comm_USHORT nNr )
145 {
146 	Write(comm_USHORT(SIReturn));
147 	Write(nRet);
148 	Write((comm_ULONG)nMethod); //HELPID BACKWARD (no sal_uLong needed)
149 	Write(comm_USHORT(PARAM_USHORT_1));			// Typ der folgenden Parameter
150 	Write(nNr);
151 }
152 
153 
154 // MacroRecorder
155 void CmdBaseStream::GenReturn( comm_USHORT nRet, rtl::OString *pUId, comm_USHORT nMethod )
156 {
157 	Write(comm_USHORT(SIReturn));
158 	Write(nRet);
159 	Write(pUId);
160 	Write(comm_USHORT(PARAM_USHORT_1));		// Typ der folgenden Parameter
161 	Write(nMethod);
162 }
163 
164 void CmdBaseStream::GenReturn( comm_USHORT nRet, rtl::OString *pUId, comm_USHORT nMethod, comm_String *pString )
165 {
166 	Write(comm_USHORT(SIReturn));
167 	Write(nRet);
168 	Write(pUId);
169 	Write(comm_USHORT(PARAM_USHORT_1|PARAM_STR_1));		// Typ der folgenden Parameter
170 	Write(nMethod);
171 	Write(pString);
172 }
173 
174 void CmdBaseStream::GenReturn( comm_USHORT nRet, rtl::OString *pUId, comm_USHORT nMethod, comm_String *pString, comm_BOOL bBool )
175 {
176 	Write(comm_USHORT(SIReturn));
177 	Write(nRet);
178 	Write(pUId);
179 	Write(comm_USHORT(PARAM_USHORT_1|PARAM_STR_1|PARAM_BOOL_1));		// Typ der folgenden Parameter
180 	Write(nMethod);
181 	Write(pString);
182 	Write(bBool);
183 }
184 
185 void CmdBaseStream::GenReturn( comm_USHORT nRet, rtl::OString *pUId, comm_USHORT nMethod, comm_BOOL bBool )
186 {
187 	Write(comm_USHORT(SIReturn));
188 	Write(nRet);
189 	Write(pUId);
190 	Write(comm_USHORT(PARAM_USHORT_1|PARAM_BOOL_1));		// Typ der folgenden Parameter
191 	Write(nMethod);
192 	Write(bBool);
193 }
194 
195 void CmdBaseStream::GenReturn( comm_USHORT nRet, rtl::OString *pUId, comm_USHORT nMethod, comm_ULONG nNr )
196 {
197 	Write(comm_USHORT(SIReturn));
198 	Write(nRet);
199 	Write(pUId);
200 	Write(comm_USHORT(PARAM_USHORT_1|PARAM_ULONG_1));		// Typ der folgenden Parameter
201 	Write(nMethod);
202 	Write(nNr);
203 }
204 
205 
206 
207 void CmdBaseStream::Read (comm_USHORT &nNr)
208 {
209 	comm_USHORT nId;
210 	*pCommStream >> nId;
211 	if (pCommStream->IsEof()) return;
212 #ifdef DBG_UTIL
213 	if (nId != BinUSHORT) DBG_ERROR1( "Falscher Typ im Stream: Erwartet USHORT, gefunden :%hu", nId );
214 #endif
215 	*pCommStream >> nNr;
216 }
217 
218 void CmdBaseStream::Read (comm_ULONG &nNr)
219 {
220 	comm_USHORT nId;
221 	*pCommStream >> nId;
222 	if (pCommStream->IsEof()) return;
223 #ifdef DBG_UTIL
224 	if (nId != BinULONG) DBG_ERROR1( "Falscher Typ im Stream: Erwartet ULONG, gefunden :%hu", nId );
225 #endif
226 	*pCommStream >> nNr;
227 }
228 
229 void CmdBaseStream::Read (comm_UniChar* &aString, comm_USHORT &nLenInChars )
230 {
231 	comm_USHORT nId;
232 	*pCommStream >> nId;
233 #ifdef DBG_UTIL
234 	if (nId != BinString) DBG_ERROR1( "Falscher Typ im Stream: Erwartet String, gefunden :%hu", nId );
235 #endif
236 
237 	*pCommStream >> nLenInChars;
238 
239 	aString = new comm_UniChar [nLenInChars];
240 	pCommStream->Read( aString, ((comm_ULONG)nLenInChars) * sizeof( comm_UniChar ) );
241 #ifdef OSL_BIGENDIAN
242     // we have to change the byteorder
243     comm_USHORT n;
244     for ( n = 0 ; n < nLenInChars ; n++ )
245         aString[ n ] = aString[ n ] >> 8 | aString[ n ] << 8;
246 #endif
247 }
248 
249 void CmdBaseStream::Read (comm_BOOL &bBool)
250 {
251 	comm_USHORT nId;
252 	*pCommStream >> nId;
253 #ifdef DBG_UTIL
254 	if (nId != BinBool) DBG_ERROR1( "Falscher Typ im Stream: Erwartet BOOL, gefunden :%hu", nId );
255 #endif
256 	*pCommStream >> bBool;
257 }
258 
259 comm_USHORT CmdBaseStream::GetNextType()
260 {
261 	comm_USHORT nId;
262 	*pCommStream >> nId;
263     pCommStream->SeekRel(-2);
264     return nId;
265 }
266 
267 
268 void CmdBaseStream::Write( comm_USHORT nNr )
269 {
270 	*pCommStream << comm_USHORT( BinUSHORT );
271 	*pCommStream << nNr;
272 }
273 
274 void CmdBaseStream::Write( comm_ULONG nNr )
275 {
276 	*pCommStream << comm_USHORT( BinULONG );
277 	*pCommStream << nNr;
278 }
279 
280 void CmdBaseStream::Write( const comm_UniChar* aString, comm_USHORT nLenInChars )
281 {
282     *pCommStream << comm_USHORT(BinString);
283 
284     comm_USHORT n;
285 
286     // remove BiDi and zero-width-markers    0x200B - 0x200F
287     // remove BiDi and paragraph-markers     0x2028 - 0x202E
288 
289     comm_UniChar* aNoBiDiString;
290     aNoBiDiString = new comm_UniChar [nLenInChars];
291     comm_USHORT nNewLenInChars = 0;
292     for ( n = 0 ; n < nLenInChars ; n++ )
293     {
294         comm_UniChar c = aString[ n ];
295         if (  ((c >= 0x200B) && (c <= 0x200F))
296             ||((c >= 0x2028) && (c <= 0x202E)) )
297         {   //Ignore character
298         }
299         else
300         {
301             aNoBiDiString[ nNewLenInChars ] = c;
302             nNewLenInChars++;
303         }
304     }
305 
306 	*pCommStream << nNewLenInChars;
307 
308 #ifdef OSL_BIGENDIAN
309     // we have to change the byteorder
310     comm_UniChar* aNewString;
311     aNewString = new comm_UniChar [nNewLenInChars];
312     for ( n = 0 ; n < nNewLenInChars ; n++ )
313         aNewString[ n ] = aNoBiDiString[ n ] >> 8 | aNoBiDiString[ n ] << 8;
314 	pCommStream->Write( aNewString, ((comm_ULONG)nNewLenInChars) * sizeof( comm_UniChar ) );
315     delete [] aNewString;
316 #else
317     pCommStream->Write( aNoBiDiString, ((comm_ULONG)nNewLenInChars) * sizeof( comm_UniChar ) );
318 #endif
319 
320     delete [] aNoBiDiString;
321 }
322 
323 void CmdBaseStream::Write( comm_BOOL bBool )
324 {
325 	*pCommStream << comm_USHORT( BinBool );
326 	*pCommStream << bBool;
327 }
328 
329 void CmdBaseStream::Read ( comm_String* &pString )
330 {
331     (void) pString; /* avoid warning about unused parameter */
332     DBG_ERROR("Read ( comm_String* &pString ) Not Implemented");
333 }
334 void CmdBaseStream::Read ( rtl::OString* &pId )
335 {
336     (void) pId; /* avoid warning about unused parameter */
337     DBG_ERROR("Read ( rtl::OString* &pId ) Not Implemented");
338 }
339 
340 void CmdBaseStream::Write( comm_String *pString )
341 {
342     (void) pString; /* avoid warning about unused parameter */
343     DBG_ERROR("Write( comm_String *pString ) Not Implemented");
344 }
345 void CmdBaseStream::Write( rtl::OString* pId )
346 {
347     (void) pId; /* avoid warning about unused parameter */
348     DBG_ERROR("Write( rtl::OString* pId ) Not Implemented");
349 }
350 
351