1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FStringFunctions.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include "file/FStringFunctions.hxx"
35 #include <rtl/ustrbuf.hxx>
36 #include <rtl/logfile.hxx>
38 using namespace connectivity
;
39 using namespace connectivity::file
;
40 //------------------------------------------------------------------
41 ORowSetValue
OOp_Upper::operate(const ORowSetValue
& lhs
) const
43 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Upper::operate" );
47 return lhs
.getString().toAsciiUpperCase();
49 //------------------------------------------------------------------
50 ORowSetValue
OOp_Lower::operate(const ORowSetValue
& lhs
) const
52 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Lower::operate" );
56 return lhs
.getString().toAsciiLowerCase();
58 //------------------------------------------------------------------
59 ORowSetValue
OOp_Ascii::operate(const ORowSetValue
& lhs
) const
61 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Ascii::operate" );
64 ::rtl::OString
sStr(::rtl::OUStringToOString(lhs
,RTL_TEXTENCODING_ASCII_US
));
65 sal_Int32 nAscii
= sStr
.toChar();
68 //------------------------------------------------------------------
69 ORowSetValue
OOp_CharLength::operate(const ORowSetValue
& lhs
) const
71 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_CharLength::operate" );
75 return lhs
.getString().getLength();
77 //------------------------------------------------------------------
78 ORowSetValue
OOp_Char::operate(const ::std::vector
<ORowSetValue
>& lhs
) const
80 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Char::operate" );
82 return ORowSetValue();
85 ::std::vector
<ORowSetValue
>::const_reverse_iterator aIter
= lhs
.rbegin();
86 ::std::vector
<ORowSetValue
>::const_reverse_iterator aEnd
= lhs
.rend();
87 for (; aIter
!= aEnd
; ++aIter
)
89 if ( !aIter
->isNull() )
91 sal_Char c
= static_cast<sal_Char
>(static_cast<sal_Int32
>(*aIter
));
93 sRet
+= ::rtl::OUString(&c
,1,RTL_TEXTENCODING_ASCII_US
);
99 //------------------------------------------------------------------
100 ORowSetValue
OOp_Concat::operate(const ::std::vector
<ORowSetValue
>& lhs
) const
102 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Concat::operate" );
104 return ORowSetValue();
106 ::rtl::OUStringBuffer sRet
;
107 ::std::vector
<ORowSetValue
>::const_reverse_iterator aIter
= lhs
.rbegin();
108 ::std::vector
<ORowSetValue
>::const_reverse_iterator aEnd
= lhs
.rend();
109 for (; aIter
!= aEnd
; ++aIter
)
111 if ( aIter
->isNull() )
112 return ORowSetValue();
114 sRet
.append(aIter
->operator ::rtl::OUString());
117 return sRet
.makeStringAndClear();
119 //------------------------------------------------------------------
120 ORowSetValue
OOp_Locate::operate(const ::std::vector
<ORowSetValue
>& lhs
) const
122 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Locate::operate" );
123 ::std::vector
<ORowSetValue
>::const_iterator aIter
= lhs
.begin();
124 ::std::vector
<ORowSetValue
>::const_iterator aEnd
= lhs
.end();
125 for (; aIter
!= aEnd
; ++aIter
)
127 if ( aIter
->isNull() )
128 return ORowSetValue();
130 if ( lhs
.size() == 2 )
131 return ::rtl::OUString::valueOf(lhs
[0].getString().indexOf(lhs
[1].getString())+1);
133 else if ( lhs
.size() != 3 )
134 return ORowSetValue();
136 return lhs
[1].getString().indexOf(lhs
[2].getString(),lhs
[0]) + 1;
138 //------------------------------------------------------------------
139 ORowSetValue
OOp_SubString::operate(const ::std::vector
<ORowSetValue
>& lhs
) const
141 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_SubString::operate" );
142 ::std::vector
<ORowSetValue
>::const_iterator aIter
= lhs
.begin();
143 ::std::vector
<ORowSetValue
>::const_iterator aEnd
= lhs
.end();
144 for (; aIter
!= aEnd
; ++aIter
)
146 if ( aIter
->isNull() )
147 return ORowSetValue();
149 if ( lhs
.size() == 2 && static_cast<sal_Int32
>(lhs
[0]) >= sal_Int32(0) )
150 return lhs
[1].getString().copy(static_cast<sal_Int32
>(lhs
[0])-1);
152 else if ( lhs
.size() != 3 || static_cast<sal_Int32
>(lhs
[1]) < sal_Int32(0))
153 return ORowSetValue();
155 return lhs
[2].getString().copy(static_cast<sal_Int32
>(lhs
[1])-1,lhs
[0]);
157 //------------------------------------------------------------------
158 ORowSetValue
OOp_LTrim::operate(const ORowSetValue
& lhs
) const
160 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_LTrim::operate" );
164 ::rtl::OUString sRet
= lhs
;
165 ::rtl::OUString sNew
= sRet
.trim();
166 return sRet
.copy(sRet
.indexOf(sNew
));
168 //------------------------------------------------------------------
169 ORowSetValue
OOp_RTrim::operate(const ORowSetValue
& lhs
) const
171 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_RTrim::operate" );
175 ::rtl::OUString sRet
= lhs
;
176 ::rtl::OUString sNew
= sRet
.trim();
177 return sRet
.copy(0,sRet
.lastIndexOf(sNew
.getStr()[sNew
.getLength()-1])+1);
179 //------------------------------------------------------------------
180 ORowSetValue
OOp_Space::operate(const ORowSetValue
& lhs
) const
182 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Space::operate" );
186 const sal_Char c
= ' ';
187 ::rtl::OUStringBuffer sRet
;
188 sal_Int32 nCount
= lhs
;
189 for (sal_Int32 i
=0; i
< nCount
; ++i
)
191 sRet
.appendAscii(&c
,1);
193 return sRet
.makeStringAndClear();
195 //------------------------------------------------------------------
196 ORowSetValue
OOp_Replace::operate(const ::std::vector
<ORowSetValue
>& lhs
) const
198 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Replace::operate" );
199 if ( lhs
.size() != 3 )
200 return ORowSetValue();
202 ::rtl::OUString sStr
= lhs
[2];
203 ::rtl::OUString sFrom
= lhs
[1];
204 ::rtl::OUString sTo
= lhs
[0];
205 sal_Int32 nIndexOf
= sStr
.indexOf(sFrom
);
206 while( nIndexOf
!= -1 )
208 sStr
= sStr
.replaceAt(nIndexOf
,sFrom
.getLength(),sTo
);
209 nIndexOf
= sStr
.indexOf(sFrom
,nIndexOf
+ sTo
.getLength());
214 //------------------------------------------------------------------
215 ORowSetValue
OOp_Repeat::operate(const ORowSetValue
& lhs
,const ORowSetValue
& rhs
) const
217 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Repeat::operate" );
218 if ( lhs
.isNull() || rhs
.isNull() )
221 ::rtl::OUString sRet
;
222 sal_Int32 nCount
= rhs
;
223 for (sal_Int32 i
=0; i
< nCount
; ++i
)
229 //------------------------------------------------------------------
230 ORowSetValue
OOp_Insert::operate(const ::std::vector
<ORowSetValue
>& lhs
) const
232 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Insert::operate" );
233 if ( lhs
.size() != 4 )
234 return ORowSetValue();
236 ::rtl::OUString sStr
= lhs
[3];
238 sal_Int32 nStart
= static_cast<sal_Int32
>(lhs
[2]);
241 return sStr
.replaceAt(nStart
-1,static_cast<sal_Int32
>(lhs
[1]),lhs
[0]);
243 //------------------------------------------------------------------
244 ORowSetValue
OOp_Left::operate(const ORowSetValue
& lhs
,const ORowSetValue
& rhs
) const
246 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Left::operate" );
247 if ( lhs
.isNull() || rhs
.isNull() )
250 ::rtl::OUString sRet
= lhs
;
251 sal_Int32 nCount
= rhs
;
253 return ORowSetValue();
254 return sRet
.copy(0,nCount
);
256 //------------------------------------------------------------------
257 ORowSetValue
OOp_Right::operate(const ORowSetValue
& lhs
,const ORowSetValue
& rhs
) const
259 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OOp_Right::operate" );
260 if ( lhs
.isNull() || rhs
.isNull() )
263 sal_Int32 nCount
= rhs
;
264 ::rtl::OUString sRet
= lhs
;
265 if ( nCount
< 0 || nCount
>= sRet
.getLength() )
266 return ORowSetValue();
268 return sRet
.copy(sRet
.getLength()-nCount
,nCount
);