1998-04-13 20:24:54 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MODULE NOTES:
|
|
|
|
* @update gess 4/1/98
|
|
|
|
*
|
|
|
|
* The scanner is a low-level service class that knows
|
|
|
|
* how to consume characters out of an (internal) stream.
|
|
|
|
* This class also offers a series of utility methods
|
2007-06-27 00:21:47 +00:00
|
|
|
* that most tokenizers want, such as readUntil()
|
|
|
|
* and SkipWhitespace().
|
1998-04-13 20:24:54 +00:00
|
|
|
*/
|
|
|
|
|
1998-05-07 07:19:47 +00:00
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
#ifndef SCANNER
|
|
|
|
#define SCANNER
|
|
|
|
|
2003-03-15 01:04:32 +00:00
|
|
|
#include "nsCOMPtr.h"
|
1998-04-13 20:24:54 +00:00
|
|
|
#include "nsString.h"
|
1999-01-09 01:09:02 +00:00
|
|
|
#include "nsIParser.h"
|
1999-02-01 18:23:31 +00:00
|
|
|
#include "nsIUnicodeDecoder.h"
|
2004-02-19 02:44:03 +00:00
|
|
|
#include "nsScannerString.h"
|
2000-12-12 21:58:14 +00:00
|
|
|
|
2001-08-16 05:24:17 +00:00
|
|
|
class nsReadEndCondition {
|
|
|
|
public:
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *mChars;
|
|
|
|
char16_t mFilter;
|
|
|
|
explicit nsReadEndCondition(const char16_t* aTerminateChars);
|
2001-08-16 05:24:17 +00:00
|
|
|
private:
|
|
|
|
nsReadEndCondition(const nsReadEndCondition& aOther); // No copying
|
|
|
|
void operator=(const nsReadEndCondition& aOther); // No assigning
|
|
|
|
};
|
|
|
|
|
1999-01-09 01:09:02 +00:00
|
|
|
class nsScanner {
|
1998-04-13 20:24:54 +00:00
|
|
|
public:
|
1998-05-14 22:19:08 +00:00
|
|
|
|
1998-07-24 21:57:43 +00:00
|
|
|
/**
|
2012-11-06 11:57:51 +00:00
|
|
|
* Use this constructor for the XML fragment parsing case
|
1998-07-24 21:57:43 +00:00
|
|
|
*/
|
2014-09-01 22:04:52 +00:00
|
|
|
explicit nsScanner(const nsAString& anHTMLString);
|
1998-07-24 21:57:43 +00:00
|
|
|
|
1998-05-14 22:19:08 +00:00
|
|
|
/**
|
1998-07-13 21:13:09 +00:00
|
|
|
* Use this constructor if you want i/o to be based on
|
|
|
|
* a file (therefore a stream) or just data you provide via Append().
|
1998-05-14 22:19:08 +00:00
|
|
|
*/
|
2012-11-06 11:57:51 +00:00
|
|
|
nsScanner(nsString& aFilename, bool aCreateStream);
|
1998-05-14 22:19:08 +00:00
|
|
|
|
1999-01-09 01:09:02 +00:00
|
|
|
~nsScanner();
|
1998-05-07 07:19:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* retrieve next char from internal input stream
|
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
|
|
|
* @param ch is the char to accept new value
|
|
|
|
* @return error code reflecting read status
|
|
|
|
*/
|
2014-01-04 15:02:17 +00:00
|
|
|
nsresult GetChar(char16_t& ch);
|
1998-05-07 07:19:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* peek ahead to consume next char from scanner's internal
|
|
|
|
* input buffer
|
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
|
|
|
* @param ch is the char to accept new value
|
|
|
|
* @return error code reflecting read status
|
|
|
|
*/
|
2014-01-04 15:02:17 +00:00
|
|
|
nsresult Peek(char16_t& ch, uint32_t aOffset=0);
|
1998-05-07 07:19:47 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult Peek(nsAString& aStr, int32_t aNumChars, int32_t aOffset = 0);
|
1998-05-07 07:19:47 +00:00
|
|
|
|
1998-05-14 22:19:08 +00:00
|
|
|
/**
|
|
|
|
* Records current offset position in input stream. This allows us
|
|
|
|
* to back up to this point if the need should arise, such as when
|
|
|
|
* tokenization gets interrupted.
|
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t Mark(void);
|
1998-05-14 22:19:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets current offset position of input stream to marked position.
|
|
|
|
* This allows us to back up to this point if the need should arise,
|
|
|
|
* such as when tokenization gets interrupted.
|
|
|
|
* NOTE: IT IS REALLY BAD FORM TO CALL RELEASE WITHOUT CALLING MARK FIRST!
|
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2000-12-12 21:58:14 +00:00
|
|
|
void RewindToMark(void);
|
1998-05-14 22:19:08 +00:00
|
|
|
|
|
|
|
|
2000-01-15 20:35:57 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @update harishd 01/12/99
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool UngetReadable(const nsAString& aBuffer);
|
2000-01-15 20:35:57 +00:00
|
|
|
|
1998-05-14 22:19:08 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @update gess 5/13/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2002-03-24 00:16:18 +00:00
|
|
|
nsresult Append(const nsAString& aBuffer);
|
1998-05-14 22:19:08 +00:00
|
|
|
|
1998-05-21 20:38:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @update gess 5/21/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult Append(const char* aBuffer, uint32_t aLen,
|
2004-11-05 06:50:27 +00:00
|
|
|
nsIRequest *aRequest);
|
1998-11-06 02:07:17 +00:00
|
|
|
|
1999-02-01 04:24:37 +00:00
|
|
|
/**
|
|
|
|
* Call this to copy bytes out of the scanner that have not yet been consumed
|
|
|
|
* by the tokenization process.
|
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @param aCopyBuffer is where the scanner buffer will be copied to
|
2016-02-09 10:45:59 +00:00
|
|
|
* @return true if OK or false on OOM
|
1999-02-01 04:24:37 +00:00
|
|
|
*/
|
2016-02-09 10:45:59 +00:00
|
|
|
bool CopyUnusedData(nsString& aCopyBuffer);
|
1999-02-01 04:24:37 +00:00
|
|
|
|
1998-07-02 08:14:22 +00:00
|
|
|
/**
|
1998-07-13 21:13:09 +00:00
|
|
|
* Retrieve the name of the file that the scanner is reading from.
|
|
|
|
* In some cases, it's just a given name, because the scanner isn't
|
|
|
|
* really reading from a file.
|
1998-07-02 08:14:22 +00:00
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @return
|
|
|
|
*/
|
1998-07-13 21:13:09 +00:00
|
|
|
nsString& GetFilename(void);
|
1998-07-02 08:14:22 +00:00
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
static void SelfTest();
|
|
|
|
|
1999-02-01 18:23:31 +00:00
|
|
|
/**
|
1999-02-16 18:32:02 +00:00
|
|
|
* Use this setter to change the scanner's unicode decoder
|
|
|
|
*
|
1999-03-08 20:00:23 +00:00
|
|
|
* @update ftang 3/02/99
|
1999-02-16 18:32:02 +00:00
|
|
|
* @param aCharset a normalized (alias resolved) charset name
|
1999-03-08 20:00:23 +00:00
|
|
|
* @param aCharsetSource- where the charset info came from
|
1999-02-16 18:32:02 +00:00
|
|
|
* @return
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult SetDocumentCharset(const nsACString& aCharset, int32_t aSource);
|
1999-02-01 18:23:31 +00:00
|
|
|
|
2004-02-19 02:44:03 +00:00
|
|
|
void BindSubstring(nsScannerSubstring& aSubstring, const nsScannerIterator& aStart, const nsScannerIterator& aEnd);
|
|
|
|
void CurrentPosition(nsScannerIterator& aPosition);
|
|
|
|
void EndReading(nsScannerIterator& aPosition);
|
|
|
|
void SetPosition(nsScannerIterator& aPosition,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aTruncate = false,
|
|
|
|
bool aReverse = false);
|
2004-02-19 02:44:03 +00:00
|
|
|
void ReplaceCharacter(nsScannerIterator& aPosition,
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t aChar);
|
2000-12-12 21:58:14 +00:00
|
|
|
|
1999-07-25 17:23:24 +00:00
|
|
|
/**
|
|
|
|
* Internal method used to cause the internal buffer to
|
|
|
|
* be filled with data.
|
|
|
|
*
|
|
|
|
* @update gess4/3/98
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsIncremental(void) {return mIncremental;}
|
|
|
|
void SetIncremental(bool anIncrValue) {mIncremental=anIncrValue;}
|
1999-07-25 17:23:24 +00:00
|
|
|
|
2004-08-24 18:37:33 +00:00
|
|
|
/**
|
|
|
|
* Return the position of the first non-whitespace
|
|
|
|
* character. This is only reliable before consumers start
|
|
|
|
* reading from this scanner.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t FirstNonWhitespacePosition()
|
2004-08-24 18:37:33 +00:00
|
|
|
{
|
|
|
|
return mFirstNonWhitespacePosition;
|
|
|
|
}
|
|
|
|
|
2009-02-16 12:22:47 +00:00
|
|
|
/**
|
|
|
|
* Override replacement character used by nsIUnicodeDecoder.
|
|
|
|
* Default behavior is that it uses nsIUnicodeDecoder's mapping.
|
|
|
|
*
|
|
|
|
* @param aReplacementCharacter the replacement character
|
|
|
|
* XML (expat) parser uses 0xffff
|
|
|
|
*/
|
2014-01-04 15:02:17 +00:00
|
|
|
void OverrideReplacementCharacter(char16_t aReplacementCharacter);
|
2009-02-16 12:22:47 +00:00
|
|
|
|
2005-01-03 22:06:27 +00:00
|
|
|
protected:
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
bool AppendToBuffer(nsScannerString::Buffer *, nsIRequest *aRequest, int32_t aErrorPos = -1);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool AppendToBuffer(const nsAString& aStr)
|
2004-11-05 06:50:27 +00:00
|
|
|
{
|
2008-07-14 13:05:15 +00:00
|
|
|
nsScannerString::Buffer* buf = nsScannerString::AllocBufferFromString(aStr);
|
|
|
|
if (!buf)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2012-07-30 14:20:58 +00:00
|
|
|
AppendToBuffer(buf, nullptr);
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2004-11-05 06:50:27 +00:00
|
|
|
}
|
2000-12-12 21:58:14 +00:00
|
|
|
|
|
|
|
nsScannerString* mSlidingBuffer;
|
2004-02-19 02:44:03 +00:00
|
|
|
nsScannerIterator mCurrentPosition; // The position we will next read from in the scanner buffer
|
|
|
|
nsScannerIterator mMarkPosition; // The position last marked (we may rewind to here)
|
|
|
|
nsScannerIterator mEndPosition; // The current end of the scanner buffer
|
2009-02-16 12:22:47 +00:00
|
|
|
nsScannerIterator mFirstInvalidPosition; // The position of the first invalid character that was detected
|
1998-07-13 21:13:09 +00:00
|
|
|
nsString mFilename;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mCountRemaining; // The number of bytes still to be read
|
2000-12-12 21:58:14 +00:00
|
|
|
// from the scanner buffer
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mIncremental;
|
|
|
|
bool mHasInvalidCharacter;
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t mReplacementCharacter;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mFirstNonWhitespacePosition;
|
|
|
|
int32_t mCharsetSource;
|
2003-06-11 18:16:03 +00:00
|
|
|
nsCString mCharset;
|
2008-10-01 06:48:47 +00:00
|
|
|
nsCOMPtr<nsIUnicodeDecoder> mUnicodeDecoder;
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsScanner &operator =(const nsScanner &); // Not implemented.
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
1998-04-25 19:45:14 +00:00
|
|
|
|
|
|
|
|