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