xref: /AOO41X/main/connectivity/source/drivers/file/FStringFunctions.cxx (revision 9b5730f6ddef7eb82608ca4d31dc0d7678e652cf)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 
27 #include "file/FStringFunctions.hxx"
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/logfile.hxx>
30 
31 using namespace connectivity;
32 using namespace connectivity::file;
33 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const34 ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const
35 {
36     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Upper::operate" );
37     if ( lhs.isNull() )
38         return lhs;
39 
40     return lhs.getString().toAsciiUpperCase();
41 }
42 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const43 ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const
44 {
45     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Lower::operate" );
46     if ( lhs.isNull() )
47         return lhs;
48 
49     return lhs.getString().toAsciiLowerCase();
50 }
51 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const52 ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const
53 {
54     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Ascii::operate" );
55     if ( lhs.isNull() )
56         return lhs;
57     ::rtl::OString sStr(::rtl::OUStringToOString(lhs,RTL_TEXTENCODING_ASCII_US));
58     sal_Int32 nAscii = sStr.toChar();
59     return nAscii;
60 }
61 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const62 ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const
63 {
64     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_CharLength::operate" );
65     if ( lhs.isNull() )
66         return lhs;
67 
68     return lhs.getString().getLength();
69 }
70 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const71 ORowSetValue OOp_Char::operate(const ::std::vector<ORowSetValue>& lhs) const
72 {
73     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Char::operate" );
74     if ( lhs.empty() )
75         return ORowSetValue();
76 
77     ::rtl::OUString sRet;
78     ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
79     ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
80     for (; aIter != aEnd; ++aIter)
81     {
82         if ( !aIter->isNull() )
83         {
84             sal_Char c = static_cast<sal_Char>(static_cast<sal_Int32>(*aIter));
85 
86             sRet += ::rtl::OUString(&c,1,RTL_TEXTENCODING_ASCII_US);
87         }
88     }
89 
90     return sRet;
91 }
92 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const93 ORowSetValue OOp_Concat::operate(const ::std::vector<ORowSetValue>& lhs) const
94 {
95     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Concat::operate" );
96     if ( lhs.empty() )
97         return ORowSetValue();
98 
99     ::rtl::OUStringBuffer sRet;
100     ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
101     ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
102     for (; aIter != aEnd; ++aIter)
103     {
104         if ( aIter->isNull() )
105             return ORowSetValue();
106 
107         sRet.append(aIter->operator ::rtl::OUString());
108     }
109 
110     return sRet.makeStringAndClear();
111 }
112 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const113 ORowSetValue OOp_Locate::operate(const ::std::vector<ORowSetValue>& lhs) const
114 {
115     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Locate::operate" );
116     ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
117     ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
118     for (; aIter != aEnd; ++aIter)
119     {
120         if ( aIter->isNull() )
121             return ORowSetValue();
122     }
123     if ( lhs.size() == 2 )
124         return ::rtl::OUString::valueOf(lhs[0].getString().indexOf(lhs[1].getString())+1);
125 
126     else if ( lhs.size() != 3 )
127         return ORowSetValue();
128 
129     return lhs[1].getString().indexOf(lhs[2].getString(),lhs[0]) + 1;
130 }
131 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const132 ORowSetValue OOp_SubString::operate(const ::std::vector<ORowSetValue>& lhs) const
133 {
134     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_SubString::operate" );
135     ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
136     ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
137     for (; aIter != aEnd; ++aIter)
138     {
139         if ( aIter->isNull() )
140             return ORowSetValue();
141     }
142     if ( lhs.size() == 2 && static_cast<sal_Int32>(lhs[0]) >= sal_Int32(0) )
143         return lhs[1].getString().copy(static_cast<sal_Int32>(lhs[0])-1);
144 
145     else if ( lhs.size() != 3 || static_cast<sal_Int32>(lhs[1]) < sal_Int32(0))
146         return ORowSetValue();
147 
148     return lhs[2].getString().copy(static_cast<sal_Int32>(lhs[1])-1,lhs[0]);
149 }
150 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const151 ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const
152 {
153     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_LTrim::operate" );
154     if ( lhs.isNull() )
155         return lhs;
156 
157     ::rtl::OUString sRet = lhs;
158     ::rtl::OUString sNew = sRet.trim();
159     return sRet.copy(sRet.indexOf(sNew));
160 }
161 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const162 ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const
163 {
164     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_RTrim::operate" );
165     if ( lhs.isNull() )
166         return lhs;
167 
168     ::rtl::OUString sRet = lhs;
169     ::rtl::OUString sNew = sRet.trim();
170     return sRet.copy(0,sRet.lastIndexOf(sNew.getStr()[sNew.getLength()-1])+1);
171 }
172 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const173 ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const
174 {
175     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Space::operate" );
176     if ( lhs.isNull() )
177         return lhs;
178 
179     const sal_Char c = ' ';
180     ::rtl::OUStringBuffer sRet;
181     sal_Int32 nCount = lhs;
182     for (sal_Int32 i=0; i < nCount; ++i)
183     {
184         sRet.appendAscii(&c,1);
185     }
186     return sRet.makeStringAndClear();
187 }
188 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const189 ORowSetValue OOp_Replace::operate(const ::std::vector<ORowSetValue>& lhs) const
190 {
191     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Replace::operate" );
192     if ( lhs.size() != 3 )
193         return ORowSetValue();
194 
195     ::rtl::OUString sStr  = lhs[2];
196     ::rtl::OUString sFrom = lhs[1];
197     ::rtl::OUString sTo   = lhs[0];
198     sal_Int32 nIndexOf = sStr.indexOf(sFrom);
199     while( nIndexOf != -1 )
200     {
201         sStr = sStr.replaceAt(nIndexOf,sFrom.getLength(),sTo);
202         nIndexOf = sStr.indexOf(sFrom,nIndexOf + sTo.getLength());
203     }
204 
205     return sStr;
206 }
207 //------------------------------------------------------------------
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const208 ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
209 {
210     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Repeat::operate" );
211     if ( lhs.isNull() || rhs.isNull() )
212         return lhs;
213 
214     ::rtl::OUString sRet;
215     sal_Int32 nCount = rhs;
216     for (sal_Int32 i=0; i < nCount; ++i)
217     {
218         sRet += lhs;
219     }
220     return sRet;
221 }
222 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const223 ORowSetValue OOp_Insert::operate(const ::std::vector<ORowSetValue>& lhs) const
224 {
225     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Insert::operate" );
226     if ( lhs.size() != 4 )
227         return ORowSetValue();
228 
229     ::rtl::OUString sStr = lhs[3];
230 
231     sal_Int32 nStart = static_cast<sal_Int32>(lhs[2]);
232     if ( nStart < 1 )
233         nStart = 1;
234     return sStr.replaceAt(nStart-1,static_cast<sal_Int32>(lhs[1]),lhs[0]);
235 }
236 //------------------------------------------------------------------
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const237 ORowSetValue OOp_Left::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
238 {
239     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Left::operate" );
240     if ( lhs.isNull() || rhs.isNull() )
241         return lhs;
242 
243     ::rtl::OUString sRet = lhs;
244     sal_Int32 nCount = rhs;
245     if ( nCount < 0 )
246         return ORowSetValue();
247     return sRet.copy(0,nCount);
248 }
249 //------------------------------------------------------------------
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const250 ORowSetValue OOp_Right::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
251 {
252     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OOp_Right::operate" );
253     if ( lhs.isNull() || rhs.isNull() )
254         return lhs;
255 
256     sal_Int32 nCount = rhs;
257     ::rtl::OUString sRet = lhs;
258     if ( nCount < 0 || nCount >= sRet.getLength() )
259         return ORowSetValue();
260 
261     return sRet.copy(sRet.getLength()-nCount,nCount);
262 }
263 
264