/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsIUnicharInputStream.h"
#include "nsIByteBuffer.h"
#include "nsIUnicharBuffer.h"
#include "nsIServiceManager.h"
#include "nsString.h"
#include "nsCRT.h"
#include "nsUTF8Utils.h"
#include <fcntl.h>
#if defined(NS_WIN32)
#include <io.h>
#else
#include <unistd.h>
#endif
public:
void* aClosure,
NS_IMETHOD Close();
private:
};
{
mPos = 0;
}
{
delete mString;
}
}
{
*aReadCount = 0;
return NS_OK;
}
}
*aReadCount = amount;
return NS_OK;
}
void* aClosure,
{
while (aCount) {
// don't propagate errors to the caller
break;
}
aCount -= bytesWritten;
mPos += bytesWritten;
}
return NS_OK;
}
{
delete mString;
mString = 0;
}
return NS_OK;
}
{
return NS_ERROR_NULL_POINTER;
}
return NS_ERROR_OUT_OF_MEMORY;
}
(void**) aInstancePtrResult);
}
//----------------------------------------------------------------------
public:
void* aClosure,
NS_IMETHOD Close();
private:
~UTF8InputStream();
protected:
static void CountValidUTF8Bytes(const char *aBuf, PRUint32 aMaxBytes, PRUint32& aValidUTF8bytes, PRUint32& aValidUCS2bytes);
};
mByteDataOffset(0),
{
}
{
if (aBufferSize == 0) {
aBufferSize = 8192;
}
return NS_OK;
}
{
Close();
}
{
return NS_OK;
}
{
if (0 == rv) {
// Fill the unichar buffer
if (rv <= 0) {
*aReadCount = 0;
return errorCode;
}
}
}
mUnicharDataOffset += rv;
*aReadCount = rv;
return NS_OK;
}
void* aClosure,
{
if (0 == bytesToWrite) {
// Fill the unichar buffer
if (bytesToWrite <= 0) {
*aReadCount = 0;
return rv;
}
}
if (bytesToWrite > aCount)
while (bytesToWrite) {
// don't propagate errors to the caller
break;
}
}
return NS_OK;
}
{
// We already closed the stream!
return -1;
}
if (nb <= 0) {
// Because we assume a many to one conversion, the lingering data
// in the byte buffer must be a partial conversion
// fragment. Because we know that we have recieved no more new
// data to add to it, we can't convert it. Therefore, we discard
// it.
return nb;
}
// Now convert as much of the byte buffer to unicode as possible
// the number of UCS2 characters should always be <= the number of
// UTF8 chars
"Ouch. I would overflow my buffer if I wasn't so careful.");
mUnicharDataOffset = 0;
return dstLen;
}
void
UTF8InputStream::CountValidUTF8Bytes(const char* aBuffer, PRUint32 aMaxBytes, PRUint32& aValidUTF8bytes, PRUint32& aValidUCS2chars)
{
const char *c = aBuffer;
while (c < end && *c) {
lastchar = c;
ucs2bytes++;
if (UTF8traits::isASCII(*c))
c++;
else if (UTF8traits::is2byte(*c))
c += 2;
else if (UTF8traits::is3byte(*c))
c += 3;
else if (UTF8traits::is4byte(*c))
c += 4;
else if (UTF8traits::is5byte(*c))
c += 5;
else if (UTF8traits::is6byte(*c))
c += 6;
else {
NS_WARNING("Unrecognized UTF8 string in UTF8InputStream::CountValidUTF8Bytes()");
break; // Otherwise we go into an infinite loop. But what happens now?
}
}
if (c > end) {
c = lastchar;
ucs2bytes--;
}
aValidUTF8bytes = c - aBuffer;
}
{
// Create converter input stream
return NS_ERROR_OUT_OF_MEMORY;
}
return rv;
(void **) aInstancePtrResult);
}