xref: /trunk/main/connectivity/source/drivers/file/FStringFunctions.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_connectivity.hxx"
30 
31 #include "file/FStringFunctions.hxx"
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/logfile.hxx>
34 
35 using namespace connectivity;
36 using namespace connectivity::file;
37 //------------------------------------------------------------------
38 ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const
39 {
40     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Upper::operate" );
41     if ( lhs.isNull() )
42         return lhs;
43 
44     return lhs.getString().toAsciiUpperCase();
45 }
46 //------------------------------------------------------------------
47 ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const
48 {
49     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Lower::operate" );
50     if ( lhs.isNull() )
51         return lhs;
52 
53     return lhs.getString().toAsciiLowerCase();
54 }
55 //------------------------------------------------------------------
56 ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const
57 {
58     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Ascii::operate" );
59     if ( lhs.isNull() )
60         return lhs;
61     ::rtl::OString sStr(::rtl::OUStringToOString(lhs,RTL_TEXTENCODING_ASCII_US));
62     sal_Int32 nAscii = sStr.toChar();
63     return nAscii;
64 }
65 //------------------------------------------------------------------
66 ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const
67 {
68     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_CharLength::operate" );
69     if ( lhs.isNull() )
70         return lhs;
71 
72     return lhs.getString().getLength();
73 }
74 //------------------------------------------------------------------
75 ORowSetValue OOp_Char::operate(const ::std::vector<ORowSetValue>& lhs) const
76 {
77     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Char::operate" );
78     if ( lhs.empty() )
79         return ORowSetValue();
80 
81     ::rtl::OUString sRet;
82     ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
83     ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
84     for (; aIter != aEnd; ++aIter)
85     {
86         if ( !aIter->isNull() )
87         {
88             sal_Char c = static_cast<sal_Char>(static_cast<sal_Int32>(*aIter));
89 
90             sRet += ::rtl::OUString(&c,1,RTL_TEXTENCODING_ASCII_US);
91         }
92     }
93 
94     return sRet;
95 }
96 //------------------------------------------------------------------
97 ORowSetValue OOp_Concat::operate(const ::std::vector<ORowSetValue>& lhs) const
98 {
99     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Concat::operate" );
100     if ( lhs.empty() )
101         return ORowSetValue();
102 
103     ::rtl::OUStringBuffer sRet;
104     ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
105     ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
106     for (; aIter != aEnd; ++aIter)
107     {
108         if ( aIter->isNull() )
109             return ORowSetValue();
110 
111         sRet.append(aIter->operator ::rtl::OUString());
112     }
113 
114     return sRet.makeStringAndClear();
115 }
116 //------------------------------------------------------------------
117 ORowSetValue OOp_Locate::operate(const ::std::vector<ORowSetValue>& lhs) const
118 {
119     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Locate::operate" );
120     ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
121     ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
122     for (; aIter != aEnd; ++aIter)
123     {
124         if ( aIter->isNull() )
125             return ORowSetValue();
126     }
127     if ( lhs.size() == 2 )
128         return ::rtl::OUString::valueOf(lhs[0].getString().indexOf(lhs[1].getString())+1);
129 
130     else if ( lhs.size() != 3 )
131         return ORowSetValue();
132 
133     return lhs[1].getString().indexOf(lhs[2].getString(),lhs[0]) + 1;
134 }
135 //------------------------------------------------------------------
136 ORowSetValue OOp_SubString::operate(const ::std::vector<ORowSetValue>& lhs) const
137 {
138     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_SubString::operate" );
139     ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
140     ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
141     for (; aIter != aEnd; ++aIter)
142     {
143         if ( aIter->isNull() )
144             return ORowSetValue();
145     }
146     if ( lhs.size() == 2 && static_cast<sal_Int32>(lhs[0]) >= sal_Int32(0) )
147         return lhs[1].getString().copy(static_cast<sal_Int32>(lhs[0])-1);
148 
149     else if ( lhs.size() != 3 || static_cast<sal_Int32>(lhs[1]) < sal_Int32(0))
150         return ORowSetValue();
151 
152     return lhs[2].getString().copy(static_cast<sal_Int32>(lhs[1])-1,lhs[0]);
153 }
154 //------------------------------------------------------------------
155 ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const
156 {
157     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_LTrim::operate" );
158     if ( lhs.isNull() )
159         return lhs;
160 
161     ::rtl::OUString sRet = lhs;
162     ::rtl::OUString sNew = sRet.trim();
163     return sRet.copy(sRet.indexOf(sNew));
164 }
165 //------------------------------------------------------------------
166 ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const
167 {
168     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_RTrim::operate" );
169     if ( lhs.isNull() )
170         return lhs;
171 
172     ::rtl::OUString sRet = lhs;
173     ::rtl::OUString sNew = sRet.trim();
174     return sRet.copy(0,sRet.lastIndexOf(sNew.getStr()[sNew.getLength()-1])+1);
175 }
176 //------------------------------------------------------------------
177 ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const
178 {
179     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Space::operate" );
180     if ( lhs.isNull() )
181         return lhs;
182 
183     const sal_Char c = ' ';
184     ::rtl::OUStringBuffer sRet;
185     sal_Int32 nCount = lhs;
186     for (sal_Int32 i=0; i < nCount; ++i)
187     {
188         sRet.appendAscii(&c,1);
189     }
190     return sRet.makeStringAndClear();
191 }
192 //------------------------------------------------------------------
193 ORowSetValue OOp_Replace::operate(const ::std::vector<ORowSetValue>& lhs) const
194 {
195     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Replace::operate" );
196     if ( lhs.size() != 3 )
197         return ORowSetValue();
198 
199     ::rtl::OUString sStr  = lhs[2];
200     ::rtl::OUString sFrom = lhs[1];
201     ::rtl::OUString sTo   = lhs[0];
202     sal_Int32 nIndexOf = sStr.indexOf(sFrom);
203     while( nIndexOf != -1 )
204     {
205         sStr = sStr.replaceAt(nIndexOf,sFrom.getLength(),sTo);
206         nIndexOf = sStr.indexOf(sFrom,nIndexOf + sTo.getLength());
207     }
208 
209     return sStr;
210 }
211 //------------------------------------------------------------------
212 ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
213 {
214     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Repeat::operate" );
215     if ( lhs.isNull() || rhs.isNull() )
216         return lhs;
217 
218     ::rtl::OUString sRet;
219     sal_Int32 nCount = rhs;
220     for (sal_Int32 i=0; i < nCount; ++i)
221     {
222         sRet += lhs;
223     }
224     return sRet;
225 }
226 //------------------------------------------------------------------
227 ORowSetValue OOp_Insert::operate(const ::std::vector<ORowSetValue>& lhs) const
228 {
229     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Insert::operate" );
230     if ( lhs.size() != 4 )
231         return ORowSetValue();
232 
233     ::rtl::OUString sStr = lhs[3];
234 
235     sal_Int32 nStart = static_cast<sal_Int32>(lhs[2]);
236     if ( nStart < 1 )
237         nStart = 1;
238     return sStr.replaceAt(nStart-1,static_cast<sal_Int32>(lhs[1]),lhs[0]);
239 }
240 //------------------------------------------------------------------
241 ORowSetValue OOp_Left::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
242 {
243     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Left::operate" );
244     if ( lhs.isNull() || rhs.isNull() )
245         return lhs;
246 
247     ::rtl::OUString sRet = lhs;
248     sal_Int32 nCount = rhs;
249     if ( nCount < 0 )
250         return ORowSetValue();
251     return sRet.copy(0,nCount);
252 }
253 //------------------------------------------------------------------
254 ORowSetValue OOp_Right::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
255 {
256     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Right::operate" );
257     if ( lhs.isNull() || rhs.isNull() )
258         return lhs;
259 
260     sal_Int32 nCount = rhs;
261     ::rtl::OUString sRet = lhs;
262     if ( nCount < 0 || nCount >= sRet.getLength() )
263         return ORowSetValue();
264 
265     return sRet.copy(sRet.getLength()-nCount,nCount);
266 }
267 
268