Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes
nsSaveAsCharset Class Reference

#include <nsSaveAsCharset.h>

Inheritance diagram for nsSaveAsCharset:
Inheritance graph
[legend]
Collaboration diagram for nsSaveAsCharset:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsSaveAsCharset ()
virtual ~nsSaveAsCharset ()
NS_DECL_ISUPPORTS NS_IMETHOD Init (const char *charset, PRUint32 attr, PRUint32 entityVersion)
NS_IMETHOD Convert (const PRUnichar *inString, char **_retval)
NS_IMETHODIMP GetCharset (char **aCharset)
void Init (in string charset, in unsigned long attr, in unsigned long entityVersion)
string Convert (in wstring inString)

Public Attributes

const unsigned long mask_Fallback = 0x000000FF
const unsigned long mask_Entity = 0x00000300
const unsigned long mask_CharsetFallback = 0x00000400
const unsigned long mask_IgnorableFallback = 0x00000800
const unsigned long attr_FallbackNone = 0
const unsigned long attr_FallbackQuestionMark = 1
const unsigned long attr_FallbackEscapeU = 2
const unsigned long attr_FallbackDecimalNCR = 3
const unsigned long attr_FallbackHexNCR = 4
const unsigned long attr_EntityNone = 0
const unsigned long attr_EntityBeforeCharsetConv = 0x00000100
const unsigned long attr_EntityAfterCharsetConv = 0x00000200
const unsigned long attr_CharsetFallback = 0x00000400
const unsigned long attr_IgnoreIgnorables = 0x00000800
const unsigned long attr_plainTextDefault = attr_FallbackNone + attr_EntityNone
const unsigned long attr_htmlTextDefault = attr_FallbackDecimalNCR + attr_EntityBeforeCharsetConv
readonly attribute string charset

Protected Member Functions

NS_IMETHOD DoCharsetConversion (const PRUnichar *inString, char **outString)
NS_IMETHOD DoConversionFallBack (PRUint32 inUCS4, char *outString, PRInt32 bufferLength)
NS_IMETHOD HandleFallBack (PRUint32 character, char **outString, PRInt32 *bufferLength, PRInt32 *currentPos, PRInt32 estimatedLength)
nsresult SetupUnicodeEncoder (const char *charset)
nsresult SetupCharsetList (const char *charsetList)
const char * GetNextCharset ()

Protected Attributes

PRUint32 mAttribute
PRUint32 mEntityVersion
nsCOMPtr< nsIUnicodeEncodermEncoder
nsCOMPtr< nsIEntityConvertermEntityConverter
nsCStringArray mCharsetList
PRInt32 mCharsetListIndex

Detailed Description

Definition at line 55 of file nsSaveAsCharset.h.


Constructor & Destructor Documentation

Definition at line 68 of file nsSaveAsCharset.cpp.

{
}

Member Function Documentation

NS_IMETHODIMP nsSaveAsCharset::Convert ( const PRUnichar inString,
char **  _retval 
)

Definition at line 95 of file nsSaveAsCharset.cpp.

{
  if (nsnull == _retval)
    return NS_ERROR_NULL_POINTER;
  if (nsnull == inString)
    return NS_ERROR_NULL_POINTER;
  if (0 == *inString)
    return NS_ERROR_ILLEGAL_VALUE;
  nsresult rv = NS_OK;

  NS_ASSERTION(mEncoder, "need to call Init() before Convert()");
  NS_ENSURE_TRUE(mEncoder, NS_ERROR_FAILURE);

  *_retval = nsnull;

  // make sure to start from the first charset in the list
  if (mCharsetListIndex > 0) {
    mCharsetListIndex = -1;
    rv = SetupUnicodeEncoder(GetNextCharset());
    NS_ENSURE_SUCCESS(rv, rv);
  }

  do {
    // fallback to the next charset in the list if the last conversion failed by an unmapped character
    if (MASK_CHARSET_FALLBACK(mAttribute) && NS_ERROR_UENC_NOMAPPING == rv) {
      const char * charset = GetNextCharset();
      if (!charset)
        break;
      rv = SetupUnicodeEncoder(charset);
      NS_ENSURE_SUCCESS(rv, rv);
      PR_FREEIF(*_retval);
    }

    if (attr_EntityBeforeCharsetConv == MASK_ENTITY(mAttribute)) {
      NS_ASSERTION(mEntityConverter, "need to call Init() before Convert()");
      NS_ENSURE_TRUE(mEntityConverter, NS_ERROR_FAILURE);
      PRUnichar *entity = nsnull;
      // do the entity conversion first
      rv = mEntityConverter->ConvertToEntities(inString, mEntityVersion, &entity);
      if(NS_SUCCEEDED(rv)) {
        rv = DoCharsetConversion(entity, _retval);
        nsMemory::Free(entity);
      }
    }
    else
      rv = DoCharsetConversion(inString, _retval);

  } while (MASK_CHARSET_FALLBACK(mAttribute) && NS_ERROR_UENC_NOMAPPING == rv);

  return rv;
}

Here is the call graph for this function:

string nsISaveAsCharset::Convert ( in wstring  inString) [inherited]

Here is the caller graph for this function:

NS_IMETHODIMP nsSaveAsCharset::DoCharsetConversion ( const PRUnichar inString,
char **  outString 
) [protected]

Definition at line 200 of file nsSaveAsCharset.cpp.

{
  if(nsnull == outString )
    return NS_ERROR_NULL_POINTER;
  NS_ASSERTION(outString, "invalid input");

  *outString = NULL;

  nsresult rv;
  PRInt32 inStringLength = nsCRT::strlen(inString);   // original input string length
  PRInt32 bufferLength;                               // allocated buffer length
  PRInt32 srcLength = inStringLength;
  PRInt32 dstLength;
  char *dstPtr = NULL;
  PRInt32 pos1, pos2;
  nsresult saveResult = NS_OK;                         // to remember NS_ERROR_UENC_NOMAPPING

  // estimate and allocate the target buffer (reserve extra memory for fallback)
  rv = mEncoder->GetMaxLength(inString, inStringLength, &dstLength);
  if (NS_FAILED(rv)) return rv;

  bufferLength = dstLength + 512; // reserve 512 byte for fallback.
  dstPtr = (char *) PR_Malloc(bufferLength);
  if (NULL == dstPtr) return NS_ERROR_OUT_OF_MEMORY;

  
  for (pos1 = 0, pos2 = 0; pos1 < inStringLength;) {
    // convert from unicode
    dstLength = bufferLength - pos2;
    rv = mEncoder->Convert(&inString[pos1], &srcLength, &dstPtr[pos2], &dstLength);

    pos1 += srcLength ? srcLength : 1;
    pos2 += dstLength;
    dstPtr[pos2] = '\0';

    // break: this is usually the case (no error) OR unrecoverable error
    if (NS_ERROR_UENC_NOMAPPING != rv) break;

    // remember this happened and reset the result
    saveResult = rv;
    rv = NS_OK;

    // finish encoder, give it a chance to write extra data like escape sequences
    dstLength = bufferLength - pos2;
    rv = mEncoder->Finish(&dstPtr[pos2], &dstLength);
    if (NS_SUCCEEDED(rv)) {
      pos2 += dstLength;
      dstPtr[pos2] = '\0';
    }

    srcLength = inStringLength - pos1;

    // do the fallback
    if (!ATTR_NO_FALLBACK(mAttribute)) {
      PRUint32 unMappedChar;
      if (IS_HIGH_SURROGATE(inString[pos1-1]) && 
          inStringLength > pos1 && IS_LOW_SURROGATE(inString[pos1])) {
        unMappedChar = SURROGATE_TO_UCS4(inString[pos1-1], inString[pos1]);
        pos1++;
      } else {
        unMappedChar = inString[pos1-1];
      }

      // if we're asked to ignore default ignorable code points, skip them.
      if (MASK_IGNORABLE_FALLBACK(mAttribute) &&
          CCMAP_HAS_CHAR_EXT(gIgnorableCCMapExt, unMappedChar)) 
                            continue;

      rv = mEncoder->GetMaxLength(inString+pos1, inStringLength-pos1, &dstLength);
      if (NS_FAILED(rv)) 
        break;

      rv = HandleFallBack(unMappedChar, &dstPtr, &bufferLength, &pos2, dstLength);
      if (NS_FAILED(rv)) 
        break;
      dstPtr[pos2] = '\0';
    }
  }

  if (NS_SUCCEEDED(rv)) {
    // finish encoder, give it a chance to write extra data like escape sequences
    dstLength = bufferLength - pos2;
    rv = mEncoder->Finish(&dstPtr[pos2], &dstLength);
    if (NS_SUCCEEDED(rv)) {
      pos2 += dstLength;
      dstPtr[pos2] = '\0';
    }
  }

  if (NS_FAILED(rv)) {
    PR_FREEIF(dstPtr);
    return rv;
  }

  *outString = dstPtr;      // set the result string

  // set error code so that the caller can do own fall back
  if (NS_ERROR_UENC_NOMAPPING == saveResult) {
    rv = NS_ERROR_UENC_NOMAPPING;
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsSaveAsCharset::DoConversionFallBack ( PRUint32  inUCS4,
char *  outString,
PRInt32  bufferLength 
) [protected]

Definition at line 305 of file nsSaveAsCharset.cpp.

{
  NS_ASSERTION(outString, "invalid input");
  if(nsnull == outString )
    return NS_ERROR_NULL_POINTER;

  *outString = '\0';

  nsresult rv = NS_OK;

  if (ATTR_NO_FALLBACK(mAttribute)) {
    return NS_OK;
  }
  if (attr_EntityAfterCharsetConv == MASK_ENTITY(mAttribute)) {
    char *entity = NULL;
    rv = mEntityConverter->ConvertUTF32ToEntity(inUCS4, mEntityVersion, &entity);
    if (NS_SUCCEEDED(rv)) {
      if (NULL == entity || (PRInt32)strlen(entity) > bufferLength) {
        return NS_ERROR_OUT_OF_MEMORY;
      }
      PL_strcpy(outString, entity);
      nsMemory::Free(entity);
      return rv;
    }
  }

  switch (MASK_FALLBACK(mAttribute)) {
  case attr_FallbackQuestionMark:
    if(bufferLength>=2) {
      *outString++='?';
      *outString='\0';
      rv = NS_OK;
    } else {
      rv = NS_ERROR_FAILURE;
    }
    break;
  case attr_FallbackEscapeU:
    if (inUCS4 & 0xff0000)
      rv = (PR_snprintf(outString, bufferLength, "\\u%.6x", inUCS4) > 0) ? NS_OK : NS_ERROR_FAILURE;
    else
      rv = (PR_snprintf(outString, bufferLength, "\\u%.4x", inUCS4) > 0) ? NS_OK : NS_ERROR_FAILURE;
    break;
  case attr_FallbackDecimalNCR:
    rv = ( PR_snprintf(outString, bufferLength, "&#%u;", inUCS4) > 0) ? NS_OK : NS_ERROR_FAILURE;
    break;
  case attr_FallbackHexNCR:
    rv = (PR_snprintf(outString, bufferLength, "&#x%x;", inUCS4) > 0) ? NS_OK : NS_ERROR_FAILURE;
    break;
  case attr_FallbackNone:
    rv = NS_OK;
    break;
  default:
    rv = NS_ERROR_ILLEGAL_VALUE;
    break;
  }

       return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 148 of file nsSaveAsCharset.cpp.

{
  NS_ENSURE_ARG(aCharset);
  NS_ASSERTION(mCharsetListIndex >= 0, "need to call Init() first");
  NS_ENSURE_TRUE(mCharsetListIndex >= 0, NS_ERROR_FAILURE);

  const char *charset = mCharsetList[mCharsetListIndex]->get();
  if (!charset) {
    *aCharset = nsnull;
    NS_ASSERTION(charset, "make sure to call Init() with non empty charset list");
    return NS_ERROR_FAILURE;
  }

  *aCharset = nsCRT::strdup(charset);
  return (*aCharset) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
const char * nsSaveAsCharset::GetNextCharset ( ) [protected]

Definition at line 394 of file nsSaveAsCharset.cpp.

{
  if ((mCharsetListIndex + 1) >= mCharsetList.Count())
    return nsnull;

  // bump the index and return the next charset
  return mCharsetList[++mCharsetListIndex]->get();
}

Here is the caller graph for this function:

NS_IMETHODIMP nsSaveAsCharset::HandleFallBack ( PRUint32  character,
char **  outString,
PRInt32 bufferLength,
PRInt32 currentPos,
PRInt32  estimatedLength 
) [protected]

Definition at line 170 of file nsSaveAsCharset.cpp.

{
  if((nsnull == outString ) || (nsnull == bufferLength) ||(nsnull ==currentPos))
    return NS_ERROR_NULL_POINTER;
  char fallbackStr[256];
  nsresult rv = DoConversionFallBack(character, fallbackStr, 256);
  if (NS_SUCCEEDED(rv)) {
    PRInt32 tempLen = (PRInt32) PL_strlen(fallbackStr);

    // reallocate if the buffer is not large enough
    if ((tempLen + estimatedLength) >= (*bufferLength - *currentPos)) {
      char *temp = (char *) PR_Realloc(*outString, *bufferLength + tempLen);
      if (NULL != temp) {
        // adjust length/pointer after realloc
        *bufferLength += tempLen;
        *outString = temp;
      } else {
        *outString = NULL;
        *bufferLength =0;
        return NS_ERROR_OUT_OF_MEMORY;
      }
    }
    memcpy((*outString + *currentPos), fallbackStr, tempLen);
    *currentPos += tempLen;
  }
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsSaveAsCharset::Init ( const char *  charset,
PRUint32  attr,
PRUint32  entityVersion 
)

Definition at line 73 of file nsSaveAsCharset.cpp.

Here is the call graph for this function:

void nsISaveAsCharset::Init ( in string  charset,
in unsigned long  attr,
in unsigned long  entityVersion 
) [inherited]

Here is the caller graph for this function:

nsresult nsSaveAsCharset::SetupCharsetList ( const char *  charsetList) [protected]

Definition at line 376 of file nsSaveAsCharset.cpp.

{
  NS_ENSURE_ARG(charsetList);

  NS_ASSERTION(charsetList[0], "charsetList should not be empty");
  if (!charsetList[0])
    return NS_ERROR_INVALID_ARG;

  if (mCharsetListIndex >= 0) {
    mCharsetList.Clear();
    mCharsetListIndex = -1;
  }

  mCharsetList.ParseString(charsetList, ", ");

  return NS_OK;
}

Here is the caller graph for this function:

nsresult nsSaveAsCharset::SetupUnicodeEncoder ( const char *  charset) [protected]

Definition at line 364 of file nsSaveAsCharset.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

const unsigned long nsISaveAsCharset::attr_CharsetFallback = 0x00000400 [inherited]

Definition at line 65 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::attr_EntityAfterCharsetConv = 0x00000200 [inherited]

Definition at line 63 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::attr_EntityBeforeCharsetConv = 0x00000100 [inherited]

Definition at line 62 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::attr_EntityNone = 0 [inherited]

Definition at line 61 of file nsISaveAsCharset.idl.

Definition at line 58 of file nsISaveAsCharset.idl.

Definition at line 57 of file nsISaveAsCharset.idl.

Definition at line 59 of file nsISaveAsCharset.idl.

Definition at line 55 of file nsISaveAsCharset.idl.

Definition at line 56 of file nsISaveAsCharset.idl.

Definition at line 74 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::attr_IgnoreIgnorables = 0x00000800 [inherited]

Definition at line 67 of file nsISaveAsCharset.idl.

Definition at line 70 of file nsISaveAsCharset.idl.

Definition at line 76 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::mask_CharsetFallback = 0x00000400 [inherited]

Definition at line 52 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::mask_Entity = 0x00000300 [inherited]

Definition at line 51 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::mask_Fallback = 0x000000FF [inherited]

Definition at line 50 of file nsISaveAsCharset.idl.

const unsigned long nsISaveAsCharset::mask_IgnorableFallback = 0x00000800 [inherited]

Definition at line 53 of file nsISaveAsCharset.idl.

Definition at line 96 of file nsSaveAsCharset.h.

nsCStringArray nsSaveAsCharset::mCharsetList [protected]

Definition at line 100 of file nsSaveAsCharset.h.

Definition at line 101 of file nsSaveAsCharset.h.

Definition at line 98 of file nsSaveAsCharset.h.

Definition at line 99 of file nsSaveAsCharset.h.

Definition at line 97 of file nsSaveAsCharset.h.


The documentation for this class was generated from the following files: