Bump for 3.6-28
[LibreOffice.git] / connectivity / source / drivers / file / FStringFunctions.cxx
blob16f1fc5d2551ccc3d1b2dd1642f3fb16418e5dba
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "file/FStringFunctions.hxx"
31 #include <rtl/ustrbuf.hxx>
32 #include <rtl/logfile.hxx>
34 using namespace connectivity;
35 using namespace connectivity::file;
36 //------------------------------------------------------------------
37 ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const
39 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Upper::operate" );
40 if ( lhs.isNull() )
41 return lhs;
43 return lhs.getString().toAsciiUpperCase();
45 //------------------------------------------------------------------
46 ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const
48 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Lower::operate" );
49 if ( lhs.isNull() )
50 return lhs;
52 return lhs.getString().toAsciiLowerCase();
54 //------------------------------------------------------------------
55 ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const
57 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Ascii::operate" );
58 if ( lhs.isNull() )
59 return lhs;
60 ::rtl::OString sStr(::rtl::OUStringToOString(lhs,RTL_TEXTENCODING_ASCII_US));
61 sal_Int32 nAscii = sStr.toChar();
62 return nAscii;
64 //------------------------------------------------------------------
65 ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const
67 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_CharLength::operate" );
68 if ( lhs.isNull() )
69 return lhs;
71 return lhs.getString().getLength();
73 //------------------------------------------------------------------
74 ORowSetValue OOp_Char::operate(const ::std::vector<ORowSetValue>& lhs) const
76 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Char::operate" );
77 if ( lhs.empty() )
78 return ORowSetValue();
80 ::rtl::OUString sRet;
81 ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
82 ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
83 for (; aIter != aEnd; ++aIter)
85 if ( !aIter->isNull() )
87 sal_Char c = static_cast<sal_Char>(static_cast<sal_Int32>(*aIter));
89 sRet += ::rtl::OUString(&c,1,RTL_TEXTENCODING_ASCII_US);
93 return sRet;
95 //------------------------------------------------------------------
96 ORowSetValue OOp_Concat::operate(const ::std::vector<ORowSetValue>& lhs) const
98 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Concat::operate" );
99 if ( lhs.empty() )
100 return ORowSetValue();
102 ::rtl::OUStringBuffer sRet;
103 ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
104 ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
105 for (; aIter != aEnd; ++aIter)
107 if ( aIter->isNull() )
108 return ORowSetValue();
110 sRet.append(aIter->operator ::rtl::OUString());
113 return sRet.makeStringAndClear();
115 //------------------------------------------------------------------
116 ORowSetValue OOp_Locate::operate(const ::std::vector<ORowSetValue>& lhs) const
118 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Locate::operate" );
119 ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
120 ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
121 for (; aIter != aEnd; ++aIter)
123 if ( aIter->isNull() )
124 return ORowSetValue();
126 if ( lhs.size() == 2 )
127 return ::rtl::OUString::valueOf(lhs[0].getString().indexOf(lhs[1].getString())+1);
129 else if ( lhs.size() != 3 )
130 return ORowSetValue();
132 return lhs[1].getString().indexOf(lhs[2].getString(),lhs[0]) + 1;
134 //------------------------------------------------------------------
135 ORowSetValue OOp_SubString::operate(const ::std::vector<ORowSetValue>& lhs) const
137 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_SubString::operate" );
138 ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
139 ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
140 for (; aIter != aEnd; ++aIter)
142 if ( aIter->isNull() )
143 return ORowSetValue();
145 if ( lhs.size() == 2 && static_cast<sal_Int32>(lhs[0]) >= sal_Int32(0) )
146 return lhs[1].getString().copy(static_cast<sal_Int32>(lhs[0])-1);
148 else if ( lhs.size() != 3 || static_cast<sal_Int32>(lhs[1]) < sal_Int32(0))
149 return ORowSetValue();
151 return lhs[2].getString().copy(static_cast<sal_Int32>(lhs[1])-1,lhs[0]);
153 //------------------------------------------------------------------
154 ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const
156 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_LTrim::operate" );
157 if ( lhs.isNull() )
158 return lhs;
160 ::rtl::OUString sRet = lhs;
161 ::rtl::OUString sNew = sRet.trim();
162 return sRet.copy(sRet.indexOf(sNew));
164 //------------------------------------------------------------------
165 ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const
167 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_RTrim::operate" );
168 if ( lhs.isNull() )
169 return lhs;
171 ::rtl::OUString sRet = lhs;
172 ::rtl::OUString sNew = sRet.trim();
173 return sRet.copy(0,sRet.lastIndexOf(sNew.getStr()[sNew.getLength()-1])+1);
175 //------------------------------------------------------------------
176 ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const
178 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Space::operate" );
179 if ( lhs.isNull() )
180 return lhs;
182 const sal_Char c = ' ';
183 ::rtl::OUStringBuffer sRet;
184 sal_Int32 nCount = lhs;
185 for (sal_Int32 i=0; i < nCount; ++i)
187 sRet.appendAscii(&c,1);
189 return sRet.makeStringAndClear();
191 //------------------------------------------------------------------
192 ORowSetValue OOp_Replace::operate(const ::std::vector<ORowSetValue>& lhs) const
194 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Replace::operate" );
195 if ( lhs.size() != 3 )
196 return ORowSetValue();
198 ::rtl::OUString sStr = lhs[2];
199 ::rtl::OUString sFrom = lhs[1];
200 ::rtl::OUString sTo = lhs[0];
201 sal_Int32 nIndexOf = sStr.indexOf(sFrom);
202 while( nIndexOf != -1 )
204 sStr = sStr.replaceAt(nIndexOf,sFrom.getLength(),sTo);
205 nIndexOf = sStr.indexOf(sFrom,nIndexOf + sTo.getLength());
208 return sStr;
210 //------------------------------------------------------------------
211 ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
213 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Repeat::operate" );
214 if ( lhs.isNull() || rhs.isNull() )
215 return lhs;
217 ::rtl::OUString sRet;
218 sal_Int32 nCount = rhs;
219 for (sal_Int32 i=0; i < nCount; ++i)
221 sRet += lhs;
223 return sRet;
225 //------------------------------------------------------------------
226 ORowSetValue OOp_Insert::operate(const ::std::vector<ORowSetValue>& lhs) const
228 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Insert::operate" );
229 if ( lhs.size() != 4 )
230 return ORowSetValue();
232 ::rtl::OUString sStr = lhs[3];
234 sal_Int32 nStart = static_cast<sal_Int32>(lhs[2]);
235 if ( nStart < 1 )
236 nStart = 1;
237 return sStr.replaceAt(nStart-1,static_cast<sal_Int32>(lhs[1]),lhs[0]);
239 //------------------------------------------------------------------
240 ORowSetValue OOp_Left::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
242 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Left::operate" );
243 if ( lhs.isNull() || rhs.isNull() )
244 return lhs;
246 ::rtl::OUString sRet = lhs;
247 sal_Int32 nCount = rhs;
248 if ( nCount < 0 )
249 return ORowSetValue();
250 return sRet.copy(0,nCount);
252 //------------------------------------------------------------------
253 ORowSetValue OOp_Right::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
255 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Right::operate" );
256 if ( lhs.isNull() || rhs.isNull() )
257 return lhs;
259 sal_Int32 nCount = rhs;
260 ::rtl::OUString sRet = lhs;
261 if ( nCount < 0 || nCount >= sRet.getLength() )
262 return ORowSetValue();
264 return sRet.copy(sRet.getLength()-nCount,nCount);
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */