Back to index

lightning-sunbird  0.9+nobinonly
nsScanner.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 
00051 #ifndef SCANNER
00052 #define SCANNER
00053 
00054 #include "nsCOMPtr.h"
00055 #include "nsString.h"
00056 #include "nsIParser.h"
00057 #include "prtypes.h"
00058 #include "nsIUnicodeDecoder.h"
00059 #include "nsScannerString.h"
00060 #include "nsIInputStream.h"
00061 
00062 class nsParser;
00063 
00064 class nsReadEndCondition {
00065 public:
00066   const PRUnichar *mChars;
00067   PRUnichar mFilter;
00068   explicit nsReadEndCondition(const PRUnichar* aTerminateChars);
00069 private:
00070   nsReadEndCondition(const nsReadEndCondition& aOther); // No copying
00071   void operator=(const nsReadEndCondition& aOther); // No assigning
00072 };
00073 
00074 class nsScanner {
00075   public:
00076 
00088       nsScanner(const nsAString& anHTMLString, const nsACString& aCharset, PRInt32 aSource);
00089 
00100       nsScanner(nsString& aFilename,PRBool aCreateStream, const nsACString& aCharset, PRInt32 aSource);
00101 
00111       nsScanner(const nsAString& aFilename, nsIInputStream* aStream, const nsACString& aCharset, PRInt32 aSource);
00112 
00113 
00114       ~nsScanner();
00115 
00123       nsresult GetChar(PRUnichar& ch);
00124 
00133       nsresult Peek(PRUnichar& ch, PRUint32 aOffset=0);
00134 
00135       nsresult Peek(nsAString& aStr, PRInt32 aNumChars, PRInt32 aOffset = 0);
00136 
00144       nsresult SkipOver(nsString& SkipChars);
00145 
00153       nsresult SkipOver(PRUnichar aSkipChar);
00154 
00162       nsresult SkipTo(nsString& aValidSet);
00163 
00171       nsresult SkipPast(nsString& aSequence);
00172 
00179       nsresult SkipWhitespace(PRInt32& aNewlinesSkipped);
00180 
00187       nsresult ReadTagIdentifier(nsScannerSharedSubstring& aString);
00188 
00196       nsresult ReadEntityIdentifier(nsString& aString);
00197       nsresult ReadNumber(nsString& aString,PRInt32 aBase);
00198       nsresult ReadWhitespace(nsScannerSharedSubstring& aString, 
00199                               PRInt32& aNewlinesSkipped,
00200                               PRBool& aHaveCR);
00201       nsresult ReadWhitespace(nsScannerIterator& aStart, 
00202                               nsScannerIterator& aEnd,
00203                               PRInt32& aNewlinesSkipped);
00204 
00214       nsresult ReadUntil(nsAString& aString,
00215                          PRUnichar aTerminal,
00216                          PRBool addTerminal);
00217 
00228       nsresult ReadUntil(nsAString& aString,
00229                          const nsReadEndCondition& aEndCondition, 
00230                          PRBool addTerminal);
00231 
00232       nsresult ReadUntil(nsScannerSharedSubstring& aString,
00233                          const nsReadEndCondition& aEndCondition,
00234                          PRBool addTerminal);
00235 
00236       nsresult ReadUntil(nsScannerIterator& aStart,
00237                          nsScannerIterator& aEnd,
00238                          const nsReadEndCondition& aEndCondition, 
00239                          PRBool addTerminal);
00240 
00241 
00251       nsresult ReadWhile(nsString& aString,nsString& anInputSet,PRBool addTerminal);
00252 
00262       void Mark(void);
00263 
00274       void RewindToMark(void);
00275 
00276 
00284       PRBool UngetReadable(const nsAString& aBuffer);
00285 
00293       nsresult Append(const nsAString& aBuffer);
00294 
00302       nsresult Append(const char* aBuffer, PRUint32 aLen,
00303                       nsIRequest *aRequest);
00304 
00313       void CopyUnusedData(nsString& aCopyBuffer);
00314 
00323       nsString& GetFilename(void);
00324 
00325       static void SelfTest();
00326 
00335       nsresult SetDocumentCharset(const nsACString& aCharset, PRInt32 aSource);
00336 
00337       void BindSubstring(nsScannerSubstring& aSubstring, const nsScannerIterator& aStart, const nsScannerIterator& aEnd);
00338       void CurrentPosition(nsScannerIterator& aPosition);
00339       void EndReading(nsScannerIterator& aPosition);
00340       void SetPosition(nsScannerIterator& aPosition,
00341                        PRBool aTruncate = PR_FALSE,
00342                        PRBool aReverse = PR_FALSE);
00343       void ReplaceCharacter(nsScannerIterator& aPosition,
00344                             PRUnichar aChar);
00345 
00352       PRBool    IsIncremental(void) {return mIncremental;}
00353       void      SetIncremental(PRBool anIncrValue) {mIncremental=anIncrValue;}
00354 
00360       PRInt32 FirstNonWhitespacePosition()
00361       {
00362         return mFirstNonWhitespacePosition;
00363       }
00364 
00365       void SetParser(nsParser *aParser)
00366       {
00367         mParser = aParser;
00368       }
00369 
00370 
00377       nsresult FillBuffer(void);
00378 
00379   protected:
00380 
00381       enum {eBufferSizeThreshold=0x1000};  //4K
00382 
00383       void AppendToBuffer(nsScannerString::Buffer *, nsIRequest *aRequest);
00384       void AppendToBuffer(const nsAString& aStr)
00385       {
00386         AppendToBuffer(nsScannerString::AllocBufferFromString(aStr), nsnull);
00387       }
00388       void AppendASCIItoBuffer(const char* aData, PRUint32 aLen,
00389                                nsIRequest *aRequest);
00390 
00391       nsCOMPtr<nsIInputStream>     mInputStream;
00392       nsScannerString*             mSlidingBuffer;
00393       nsScannerIterator            mCurrentPosition; // The position we will next read from in the scanner buffer
00394       nsScannerIterator            mMarkPosition;    // The position last marked (we may rewind to here)
00395       nsScannerIterator            mEndPosition;     // The current end of the scanner buffer
00396       nsString        mFilename;
00397       PRUint32        mCountRemaining; // The number of bytes still to be read
00398                                        // from the scanner buffer
00399       PRUint32        mTotalRead;
00400       PRPackedBool    mIncremental;
00401       PRInt32         mFirstNonWhitespacePosition;
00402       PRInt32         mCharsetSource;
00403       nsCString       mCharset;
00404       nsIUnicodeDecoder *mUnicodeDecoder;
00405       nsParser        *mParser;
00406 };
00407 
00408 #endif
00409 
00410