mailnews/import/src/ImportTranslate.cpp
author Ehsan Akhgari <ehsan@mozilla.com>
Wed, 22 Aug 2012 12:14:35 -0400
changeset 13324 3f9b812e72471c2172bfde0ebbeeb210bf5f1a95
parent 13187 0a10c274a1fe037f599647f38fe166060801934b
child 30485 e8f902aac0b0dc1ab894220b2280dec75d7799cc
permissions -rw-r--r--
Bug 579517 - Part 6: Automated conversion of NSPR numeric types to stdint types in Gecko; r=mconley This patch was generated by a script. Here's the source of the script for future reference: function convert() { echo "Converting $1 to $2..." find . ! -wholename "*/mozilla*" \ ! -wholename "*ldap/sdks/c-sdk*" \ ! -wholename "*/.hg*" \ ! -wholename "obj-tb-dbg*" \ ! -wholename "obj-sm-dbg*" \ -type f \ \( -iname "*.cpp" \ -o -iname "*.h" \ -o -iname "*.c" \ -o -iname "*.cc" \ -o -iname "*.idl" \ -o -iname "*.ipdl" \ -o -iname "*.ipdlh" \ -o -iname "*.mm" \) | \ xargs -n 1 sed -i -e "s/\b$1\b/$2/g" } convert PRInt8 int8_t convert PRUint8 uint8_t convert PRInt16 int16_t convert PRUint16 uint16_t convert PRInt32 int32_t convert PRUint32 uint32_t convert PRInt64 int64_t convert PRUint64 uint64_t convert PRIntn int convert PRUintn unsigned convert PRSize size_t convert PROffset32 int32_t convert PROffset64 int64_t convert PRPtrdiff ptrdiff_t convert PRFloat64 double

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */

#include "ImportTranslate.h"

int ImportTranslate::m_useTranslator = -1;


bool ImportTranslate::ConvertString(const nsCString& inStr, nsCString& outStr, bool mimeHeader)
{
  if (inStr.IsEmpty()) {
    outStr = inStr;
    return true;
  }

  nsImportTranslator *pTrans = GetTranslator();
  // int      maxLen = (int) pTrans->GetMaxBufferSize(inStr.Length());
  // int      hLen = 0;
  nsCString  set;
  nsCString  lang;

  if (mimeHeader) {
    // add the charset and language
    pTrans->GetCharset(set);
    pTrans->GetLanguage(lang);
  }

  // Unfortunatly, we didn't implement ConvertBuffer for all translators,
  // just ConvertToFile.  This means that this data will not always
  // be converted to the charset of pTrans.  In that case...
  // We don't always have the data in the same charset as the current
  // translator...
  // It is safer to leave the charset and language field blank
  set.Truncate();
  lang.Truncate();

  uint8_t *  pBuf;
  /*
  pBuf = (P_U8) outStr.GetBuffer(maxLen);
  if (!pBuf) {
    delete pTrans;
    return FALSE;
  }
  pTrans->ConvertBuffer((PC_U8)(PC_S8)inStr, inStr.GetLength(), pBuf);
  outStr.ReleaseBuffer();
  */
  outStr = inStr;
  delete pTrans;


  // Now I need to run the string through the mime-header special char
  // encoder.

  pTrans = new CMHTranslator;
  pBuf = new uint8_t[pTrans->GetMaxBufferSize(outStr.Length())];
  pTrans->ConvertBuffer((const uint8_t *)(outStr.get()), outStr.Length(), pBuf);
  delete pTrans;
  outStr.Truncate();
  if (mimeHeader) {
    outStr = set;
    outStr += "'";
    outStr += lang;
    outStr += "'";
  }
  outStr += (const char *)pBuf;
  delete [] pBuf;

  return true;
}


nsImportTranslator *ImportTranslate::GetTranslator(void)
{
  if (m_useTranslator == -1) {
    // get the translator to use...
    // CString    trans;
    // trans.LoadString(IDS_LANGUAGE_TRANSLATION);
    m_useTranslator = 0;
    // if (!trans.CompareNoCase("iso-2022-jp"))
    //  gWizData.m_useTranslator = 1;
  }

  switch(m_useTranslator) {
  case 0:
    return new nsImportTranslator;
  //case 1:
  //  return new CSJis2JisTranslator;
  default:
    return new nsImportTranslator;
  }
}

nsImportTranslator *ImportTranslate::GetMatchingTranslator(const char *pCharSet)
{
/*
  CString    jp = "iso-2022-jp";
  if (!jp.CompareNoCase(pCharSet))
    return new CSJis2JisTranslator;
*/

  return nullptr;
}