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