xref: /AOO41X/main/connectivity/source/drivers/file/quotedstring.cxx (revision 707fc0d4d52eb4f69d89a98ffec6918ca5de6326)
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 #include "file/quotedstring.hxx"
27 #include <rtl/logfile.hxx>
28 
29 namespace connectivity
30 {
31     //==================================================================
32     //= QuotedTokenizedString
33     //==================================================================
34     //------------------------------------------------------------------
35     xub_StrLen QuotedTokenizedString::GetTokenCount( sal_Unicode cTok, sal_Unicode cStrDel ) const
36     {
37         RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "QuotedTokenizedString::GetTokenCount" );
38         const xub_StrLen nLen = m_sString.Len();
39         if ( !nLen )
40             return 0;
41 
42         xub_StrLen nTokCount = 1;
43         sal_Bool bStart = sal_True;     // Stehen wir auf dem ersten Zeichen im Token?
44         sal_Bool bInString = sal_False; // Befinden wir uns INNERHALB eines (cStrDel delimited) String?
45 
46         // Suche bis Stringende nach dem ersten nicht uebereinstimmenden Zeichen
47         for( xub_StrLen i = 0; i < nLen; ++i )
48         {
49             const sal_Unicode cChar = m_sString.GetChar(i);
50             if (bStart)
51             {
52                 bStart = sal_False;
53                 // Erstes Zeichen ein String-Delimiter?
54                 if ( cChar == cStrDel )
55                 {
56                     bInString = sal_True;   // dann sind wir jetzt INNERHALB des Strings!
57                     continue;           // dieses Zeichen ueberlesen!
58                 }
59             }
60 
61             if (bInString)
62             {
63                 // Wenn jetzt das String-Delimiter-Zeichen auftritt ...
64                 if ( cChar == cStrDel )
65                 {
66                     if ((i+1 < nLen) && (m_sString.GetChar(i+1) == cStrDel))
67                     {
68                         // Verdoppeltes String-Delimiter-Zeichen:
69                         ++i;    // kein String-Ende, naechstes Zeichen ueberlesen.
70                     }
71                     else
72                     {
73                         // String-Ende
74                         bInString = sal_False;
75                     }
76                 }
77             } // if (bInString)
78             else
79             {
80                 // Stimmt das Tokenzeichen ueberein, dann erhoehe TokCount
81                 if ( cChar == cTok )
82                 {
83                     ++nTokCount;
84                     bStart = sal_True;
85                 }
86             }
87         }
88         //OSL_TRACE("QuotedTokenizedString::nTokCount = %d\n", ((OUtoCStr(::rtl::OUString(nTokCount))) ? (OUtoCStr(::rtl::OUString(nTokCount))):("NULL")) );
89 
90         return nTokCount;
91     }
92 
93     //------------------------------------------------------------------
94     void QuotedTokenizedString::GetTokenSpecial( String& _rStr,xub_StrLen& nStartPos, sal_Unicode cTok, sal_Unicode cStrDel ) const
95     {
96         RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "QuotedTokenizedString::GetTokenCount" );
97         _rStr.Erase();
98         const xub_StrLen nLen = m_sString.Len();
99         if ( nLen )
100         {
101             sal_Bool bInString = (nStartPos < nLen) && (m_sString.GetChar(nStartPos) == cStrDel);   // Befinden wir uns INNERHALB eines (cStrDel delimited) String?
102 
103             // Erstes Zeichen ein String-Delimiter?
104             if (bInString )
105                 ++nStartPos;            // dieses Zeichen ueberlesen!
106             if ( nStartPos >= nLen )
107                 return;
108 
109             sal_Unicode* pData = _rStr.AllocBuffer( nLen - nStartPos + 1 );
110             const sal_Unicode* pStart = pData;
111             // Suche bis Stringende nach dem ersten nicht uebereinstimmenden Zeichen
112             for( xub_StrLen i = nStartPos; i < nLen; ++i )
113             {
114                 const sal_Unicode cChar = m_sString.GetChar(i);
115                 if (bInString)
116                 {
117                     // Wenn jetzt das String-Delimiter-Zeichen auftritt ...
118                     if ( cChar == cStrDel )
119                     {
120                         if ((i+1 < nLen) && (m_sString.GetChar(i+1) == cStrDel))
121                         {
122                             // Verdoppeltes String-Delimiter-Zeichen:
123                             // kein String-Ende, naechstes Zeichen ueberlesen.
124                             ++i;
125                             *pData++ = m_sString.GetChar(i);    // Zeichen gehoert zum Resultat-String
126                         }
127                         else
128                         {
129                             // String-Ende
130                             bInString = sal_False;
131                             *pData = 0;
132                         }
133                     }
134                     else
135                     {
136                         *pData++ = cChar;   // Zeichen gehoert zum Resultat-String
137                     }
138 
139                 }
140                 else
141                 {
142                     // Stimmt das Tokenzeichen ueberein, dann erhoehe nTok
143                     if ( cChar == cTok )
144                     {
145                         // Vorzeitiger Abbruch der Schleife moeglich, denn
146                         // wir haben, was wir wollten.
147                         nStartPos = i+1;
148                         break;
149                     }
150                     else
151                     {
152                         *pData++ = cChar;   // Zeichen gehoert zum Resultat-String
153                     }
154                 }
155             } // for( xub_StrLen i = nStartPos; i < nLen; ++i )
156             *pData = 0;
157             _rStr.ReleaseBufferAccess(xub_StrLen(pData - pStart));
158         }
159     }
160 }
161