1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "file/FStringFunctions.hxx"
32*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33*cdf0e10cSrcweir #include <rtl/logfile.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir using namespace connectivity;
36*cdf0e10cSrcweir using namespace connectivity::file;
37*cdf0e10cSrcweir //------------------------------------------------------------------
38*cdf0e10cSrcweir ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Upper::operate" );
41*cdf0e10cSrcweir 	if ( lhs.isNull() )
42*cdf0e10cSrcweir 		return lhs;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 	return lhs.getString().toAsciiUpperCase();
45*cdf0e10cSrcweir }
46*cdf0e10cSrcweir //------------------------------------------------------------------
47*cdf0e10cSrcweir ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Lower::operate" );
50*cdf0e10cSrcweir 	if ( lhs.isNull() )
51*cdf0e10cSrcweir 		return lhs;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	return lhs.getString().toAsciiLowerCase();
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir //------------------------------------------------------------------
56*cdf0e10cSrcweir ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Ascii::operate" );
59*cdf0e10cSrcweir 	if ( lhs.isNull() )
60*cdf0e10cSrcweir 		return lhs;
61*cdf0e10cSrcweir 	::rtl::OString sStr(::rtl::OUStringToOString(lhs,RTL_TEXTENCODING_ASCII_US));
62*cdf0e10cSrcweir 	sal_Int32 nAscii = sStr.toChar();
63*cdf0e10cSrcweir 	return nAscii;
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir //------------------------------------------------------------------
66*cdf0e10cSrcweir ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_CharLength::operate" );
69*cdf0e10cSrcweir 	if ( lhs.isNull() )
70*cdf0e10cSrcweir 		return lhs;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	return lhs.getString().getLength();
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir //------------------------------------------------------------------
75*cdf0e10cSrcweir ORowSetValue OOp_Char::operate(const ::std::vector<ORowSetValue>& lhs) const
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Char::operate" );
78*cdf0e10cSrcweir 	if ( lhs.empty() )
79*cdf0e10cSrcweir 		return ORowSetValue();
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	::rtl::OUString sRet;
82*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
83*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
84*cdf0e10cSrcweir 	for (; aIter != aEnd; ++aIter)
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		if ( !aIter->isNull() )
87*cdf0e10cSrcweir 		{
88*cdf0e10cSrcweir 			sal_Char c = static_cast<sal_Char>(static_cast<sal_Int32>(*aIter));
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 			sRet += ::rtl::OUString(&c,1,RTL_TEXTENCODING_ASCII_US);
91*cdf0e10cSrcweir 		}
92*cdf0e10cSrcweir 	}
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	return sRet;
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir //------------------------------------------------------------------
97*cdf0e10cSrcweir ORowSetValue OOp_Concat::operate(const ::std::vector<ORowSetValue>& lhs) const
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Concat::operate" );
100*cdf0e10cSrcweir 	if ( lhs.empty() )
101*cdf0e10cSrcweir 		return ORowSetValue();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	::rtl::OUStringBuffer sRet;
104*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
105*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
106*cdf0e10cSrcweir 	for (; aIter != aEnd; ++aIter)
107*cdf0e10cSrcweir 	{
108*cdf0e10cSrcweir 		if ( aIter->isNull() )
109*cdf0e10cSrcweir 			return ORowSetValue();
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 		sRet.append(aIter->operator ::rtl::OUString());
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	return sRet.makeStringAndClear();
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir //------------------------------------------------------------------
117*cdf0e10cSrcweir ORowSetValue OOp_Locate::operate(const ::std::vector<ORowSetValue>& lhs) const
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Locate::operate" );
120*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
121*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
122*cdf0e10cSrcweir 	for (; aIter != aEnd; ++aIter)
123*cdf0e10cSrcweir 	{
124*cdf0e10cSrcweir 		if ( aIter->isNull() )
125*cdf0e10cSrcweir 			return ORowSetValue();
126*cdf0e10cSrcweir 	}
127*cdf0e10cSrcweir 	if ( lhs.size() == 2 )
128*cdf0e10cSrcweir 		return ::rtl::OUString::valueOf(lhs[0].getString().indexOf(lhs[1].getString())+1);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	else if ( lhs.size() != 3 )
131*cdf0e10cSrcweir 		return ORowSetValue();
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	return lhs[1].getString().indexOf(lhs[2].getString(),lhs[0]) + 1;
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir //------------------------------------------------------------------
136*cdf0e10cSrcweir ORowSetValue OOp_SubString::operate(const ::std::vector<ORowSetValue>& lhs) const
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_SubString::operate" );
139*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
140*cdf0e10cSrcweir 	::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
141*cdf0e10cSrcweir 	for (; aIter != aEnd; ++aIter)
142*cdf0e10cSrcweir 	{
143*cdf0e10cSrcweir 		if ( aIter->isNull() )
144*cdf0e10cSrcweir 			return ORowSetValue();
145*cdf0e10cSrcweir 	}
146*cdf0e10cSrcweir 	if ( lhs.size() == 2 && static_cast<sal_Int32>(lhs[0]) >= sal_Int32(0) )
147*cdf0e10cSrcweir 		return lhs[1].getString().copy(static_cast<sal_Int32>(lhs[0])-1);
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 	else if ( lhs.size() != 3 || static_cast<sal_Int32>(lhs[1]) < sal_Int32(0))
150*cdf0e10cSrcweir 		return ORowSetValue();
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	return lhs[2].getString().copy(static_cast<sal_Int32>(lhs[1])-1,lhs[0]);
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir //------------------------------------------------------------------
155*cdf0e10cSrcweir ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_LTrim::operate" );
158*cdf0e10cSrcweir 	if ( lhs.isNull() )
159*cdf0e10cSrcweir 		return lhs;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	::rtl::OUString sRet = lhs;
162*cdf0e10cSrcweir 	::rtl::OUString sNew = sRet.trim();
163*cdf0e10cSrcweir 	return sRet.copy(sRet.indexOf(sNew));
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir //------------------------------------------------------------------
166*cdf0e10cSrcweir ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_RTrim::operate" );
169*cdf0e10cSrcweir 	if ( lhs.isNull() )
170*cdf0e10cSrcweir 		return lhs;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	::rtl::OUString sRet = lhs;
173*cdf0e10cSrcweir 	::rtl::OUString sNew = sRet.trim();
174*cdf0e10cSrcweir 	return sRet.copy(0,sRet.lastIndexOf(sNew.getStr()[sNew.getLength()-1])+1);
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir //------------------------------------------------------------------
177*cdf0e10cSrcweir ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Space::operate" );
180*cdf0e10cSrcweir 	if ( lhs.isNull() )
181*cdf0e10cSrcweir 		return lhs;
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	const sal_Char c = ' ';
184*cdf0e10cSrcweir 	::rtl::OUStringBuffer sRet;
185*cdf0e10cSrcweir 	sal_Int32 nCount = lhs;
186*cdf0e10cSrcweir 	for (sal_Int32 i=0; i < nCount; ++i)
187*cdf0e10cSrcweir 	{
188*cdf0e10cSrcweir 		sRet.appendAscii(&c,1);
189*cdf0e10cSrcweir 	}
190*cdf0e10cSrcweir 	return sRet.makeStringAndClear();
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir //------------------------------------------------------------------
193*cdf0e10cSrcweir ORowSetValue OOp_Replace::operate(const ::std::vector<ORowSetValue>& lhs) const
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Replace::operate" );
196*cdf0e10cSrcweir 	if ( lhs.size() != 3 )
197*cdf0e10cSrcweir 		return ORowSetValue();
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	::rtl::OUString sStr  = lhs[2];
200*cdf0e10cSrcweir 	::rtl::OUString sFrom = lhs[1];
201*cdf0e10cSrcweir 	::rtl::OUString sTo   = lhs[0];
202*cdf0e10cSrcweir 	sal_Int32 nIndexOf = sStr.indexOf(sFrom);
203*cdf0e10cSrcweir 	while( nIndexOf != -1 )
204*cdf0e10cSrcweir 	{
205*cdf0e10cSrcweir 		sStr = sStr.replaceAt(nIndexOf,sFrom.getLength(),sTo);
206*cdf0e10cSrcweir 		nIndexOf = sStr.indexOf(sFrom,nIndexOf + sTo.getLength());
207*cdf0e10cSrcweir 	}
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	return sStr;
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir //------------------------------------------------------------------
212*cdf0e10cSrcweir ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Repeat::operate" );
215*cdf0e10cSrcweir 	if ( lhs.isNull() || rhs.isNull() )
216*cdf0e10cSrcweir 		return lhs;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	::rtl::OUString sRet;
219*cdf0e10cSrcweir 	sal_Int32 nCount = rhs;
220*cdf0e10cSrcweir 	for (sal_Int32 i=0; i < nCount; ++i)
221*cdf0e10cSrcweir 	{
222*cdf0e10cSrcweir 		sRet += lhs;
223*cdf0e10cSrcweir 	}
224*cdf0e10cSrcweir 	return sRet;
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir //------------------------------------------------------------------
227*cdf0e10cSrcweir ORowSetValue OOp_Insert::operate(const ::std::vector<ORowSetValue>& lhs) const
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Insert::operate" );
230*cdf0e10cSrcweir 	if ( lhs.size() != 4 )
231*cdf0e10cSrcweir 		return ORowSetValue();
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 	::rtl::OUString sStr = lhs[3];
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 	sal_Int32 nStart = static_cast<sal_Int32>(lhs[2]);
236*cdf0e10cSrcweir 	if ( nStart < 1 )
237*cdf0e10cSrcweir 		nStart = 1;
238*cdf0e10cSrcweir 	return sStr.replaceAt(nStart-1,static_cast<sal_Int32>(lhs[1]),lhs[0]);
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir //------------------------------------------------------------------
241*cdf0e10cSrcweir ORowSetValue OOp_Left::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Left::operate" );
244*cdf0e10cSrcweir 	if ( lhs.isNull() || rhs.isNull() )
245*cdf0e10cSrcweir 		return lhs;
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 	::rtl::OUString sRet = lhs;
248*cdf0e10cSrcweir 	sal_Int32 nCount = rhs;
249*cdf0e10cSrcweir 	if ( nCount < 0 )
250*cdf0e10cSrcweir 		return ORowSetValue();
251*cdf0e10cSrcweir 	return sRet.copy(0,nCount);
252*cdf0e10cSrcweir }
253*cdf0e10cSrcweir //------------------------------------------------------------------
254*cdf0e10cSrcweir ORowSetValue OOp_Right::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Right::operate" );
257*cdf0e10cSrcweir 	if ( lhs.isNull() || rhs.isNull() )
258*cdf0e10cSrcweir 		return lhs;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 	sal_Int32 nCount = rhs;
261*cdf0e10cSrcweir 	::rtl::OUString sRet = lhs;
262*cdf0e10cSrcweir 	if ( nCount < 0 || nCount >= sRet.getLength() )
263*cdf0e10cSrcweir 		return ORowSetValue();
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 	return sRet.copy(sRet.getLength()-nCount,nCount);
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268