WXDLLIMPEXP_CORE wxCharBuffer
wxConvertToGTK(const wxString& s, wxFontEncoding enc)
{
    wxWCharBuffer wbuf;
    if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
    {
        wbuf = wxConvUI->cMB2WC(s.c_str());
    }
    else // another encoding, use generic conversion class
    {
        wbuf = wxCSConv(enc).cMB2WC(s.c_str());
    }

    if ( !wbuf && !s.empty() )
    {
        // conversion failed, but we still want to show something to the user
        // even if it's going to be wrong it is better than nothing
        //
        // we choose ISO8859-1 here arbitrarily, it's just the most common
        // encoding probably and, also importantly here, conversion from it
        // never fails as it's done internally by wxCSConv
        wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
    }

    return wxConvUTF8.cWC2MB(wbuf);
}
Beispiel #2
0
void UnicodeTestCase::IsConvOk()
{
    CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM).IsOk() );
    CPPUNIT_ASSERT( wxCSConv("US-ASCII").IsOk() );
    CPPUNIT_ASSERT( wxCSConv("UTF-8").IsOk() );
    CPPUNIT_ASSERT( !wxCSConv("NoSuchConversion").IsOk() );

#ifdef __WINDOWS__
    CPPUNIT_ASSERT( wxCSConv("WINDOWS-437").IsOk() );
#endif
}
Beispiel #3
0
bool Exword::RemoveDictionary(RemoteDictionary *dict)
{
    bool success = false;
    exword_cryptkey_t ck;
    int rsp;

    if (!IsConnected())
        return success;

    if (dict->Exists()) {
        memset(&ck, 0, sizeof(exword_cryptkey_t));
        memcpy(ck.blk1, key1, 2);
        memcpy(ck.blk1 + 10, key1 + 10, 2);
        memcpy(ck.blk2, key1 + 2, 8);
        memcpy(ck.blk2 + 8, key1 + 12, 4);
        exword_setpath(m_device, (uint8_t*)GetStoragePath().utf8_str().data(), 0);
        rsp = exword_unlock(m_device);
        rsp |= exword_cname(m_device, (char*)dict->GetName().mb_str(wxCSConv(wxT("SJIS"))).data(), (char*)dict->GetId().utf8_str().data());
        rsp |= exword_cryptkey(m_device, &ck);
        if (rsp == EXWORD_SUCCESS)
            rsp |= exword_remove_file(m_device, (char*)dict->GetId().utf8_str().data(), 0);
        rsp |= exword_lock(m_device);
        success = (rsp == EXWORD_SUCCESS);
    }
    return success;
}
Beispiel #4
0
std::vector<wxString> iDBF::GetFieldNames() 
{
	std::vector<wxString> names(NumOfFields);
    for (int i=0; i < NumOfFields; i++)
		names[i] = wxString(Field[i]->Name, wxCSConv("utf-8"));
	return names;
}
Beispiel #5
0
bool CContactPage::DoExport(const wxString& strPath)
{
  wxFileOutputStream file(strPath);
  if (!file.IsOk()) return false;

  wxTextOutputStream tos(file, wxEOL_DOS, wxCSConv(wxFONTENCODING_UTF8));

  const TContactList& rEntries = m_listCtrl->getEntries();
  size_t itemCount = rEntries.size();
  wxProgressDialog progress(_("CSV Export"), _("Exporting..."),
      itemCount, this, wxPD_APP_MODAL);
  int numTel = 0;
  for (size_t i = 0; i < itemCount; ++i) {
    if (rEntries[i].getConstPhones().size() > numTel) {
      numTel = rEntries[i].getConstPhones().size();
    }
  }
  wxString strLine;
  strLine += wxT("\"FN\",\"MN\",\"LN\",\"NN\",\"Title\",\"SN\",\"Email\",\"Organization\",\"Image\"");
  for (size_t i = 0; i < numTel; ++i) {
    strLine += ",";
    strLine += wxString::Format(wxT("\"TelNumber%02d\""), i);
  }
  strLine += "\n"; tos.WriteString(strLine);
  for (size_t i = 0; i < itemCount; ++i) {
    strLine.Clear();
    strLine += CSVQuote( rEntries[i].getFN() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getMN() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getLN() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getNN() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getTitle() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getSN() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getEmail() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getOrganization() );
    strLine += ",";
    strLine += CSVQuote( rEntries[i].getImage() );
    for (size_t j = 0; j < numTel; ++j) {
      strLine += ",";
      if (j < rEntries[i].getConstPhones().size()) {
        strLine += CSVQuote( rEntries[i].getConstPhones()[j].getNumber() );
      }
    }
    strLine += "\n"; tos.WriteString(strLine);
    progress.Update(i);
  }

  return true;
}
pgsOperand pgsGenDictionary::eval(pgsVarMap &vars) const
{
	// Evaluate parameters
	pgsOperand file_path(m_file_path->eval(vars));
	pgsOperand sequence(m_sequence->eval(vars));
	pgsOperand seed(m_seed->eval(vars));
	pgsOperand wx_conv(m_wx_conv->eval(vars));

	// Check parameters and create the generator
	if (file_path->is_string() && sequence->is_integer() && seed->is_integer()
	        && wx_conv->is_string())
	{
		wxFileName file(file_path->value());
		if (file.FileExists() && file.IsFileReadable())
		{
			long aux_sequence, aux_seed;
			sequence->value().ToLong(&aux_sequence);
			seed->value().ToLong(&aux_seed);
			return pnew pgsGenerator(pgsVariable::pgsTString,
			                         pnew pgsDictionaryGen(file_path->value(), aux_sequence != 0,
			                                 aux_seed, wxCSConv(wx_conv->value())));
		}
		else
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nFile <") << file_path->value()
			                            << wxT("> does not exist"));
		}
	}
	else
	{
		// Deal with errors
		if (!file_path->is_string())
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nfile should be a string"));
		}
		else if (!sequence->is_integer())
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nsequence should be an integer"));
		}
		else if (!seed->is_integer())
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nseed should be an integer"));
		}
		else
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nencoding should be a string"));
		}
	}
}
Beispiel #7
0
void wxGxArchiveFolder::LoadChildren(void)
{
	if(m_bIsChildrenLoaded)
		return;
    char **papszItems = CPLReadDir(m_sPath);
    if(papszItems == NULL)
        return;

    char **papszFileList = NULL;
    //remove unused items
    for(int i = CSLCount(papszItems) - 1; i >= 0; i-- )
    {
        if( wxGISEQUAL(papszItems[i],".") || wxGISEQUAL(papszItems[i],"..") )
            continue;
        CPLString szFileName = m_sPath;
        szFileName += "/";
        szFileName += papszItems[i];

        VSIStatBufL BufL;
        int ret = VSIStatL(szFileName, &BufL);
        if(ret == 0)
        {
            if(VSI_ISDIR(BufL.st_mode))
            {
		        wxString sCharset(wxT("cp-866"));
		        wxGISAppConfig oConfig = GetConfig();
                if(oConfig.IsOk())
			        sCharset = oConfig.Read(enumGISHKCU, wxString(wxT("wxGISCommon/zip/charset")), sCharset);
                wxString sFileName(papszItems[i], wxCSConv(sCharset));
				GetArchiveFolder(this, sFileName, szFileName);
            }
            else
            {
                papszFileList = CSLAddString( papszFileList, szFileName );
            }
        }
    }
    CSLDestroy( papszItems );

    //load names
    wxGxCatalog *pCatalog = wxDynamicCast(GetGxCatalog(), wxGxCatalog);

	if(pCatalog)
    {
        wxArrayLong ChildrenIds;
        pCatalog->CreateChildren(this, papszFileList, ChildrenIds);
        for(size_t i = 0; i < ChildrenIds.GetCount(); ++i)
            pCatalog->ObjectAdded(ChildrenIds[i]);
	}

    CSLDestroy( papszFileList );

	m_bIsChildrenLoaded = true;
}
WXDLLIMPEXP_CORE wxCharBuffer
wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
{
    // this conversion should never fail as GTK+ always uses UTF-8 internally
    // so there are no complications here
    const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
    if ( enc == wxFONTENCODING_SYSTEM )
        return wxConvUI->cWC2MB(wbuf);

    return wxCSConv(enc).cWC2MB(wbuf);
}
Beispiel #9
0
bool Exword::InstallDictionary(LocalDictionary *dict)
{
    bool success = false;
    exword_cryptkey_t ck;
    Capacity cap;
    wxArrayString files;
    int rsp;

    if (!IsConnected())
        return success;

    wxString content_path = GetStoragePath() + dict->GetId() + wxT("\\_CONTENT");
    wxString user_path = GetStoragePath() + dict->GetId() + wxT("\\_USER");
    memset(&ck, 0, sizeof(exword_cryptkey_t));
    memcpy(ck.blk1, key1, 2);
    memcpy(ck.blk1 + 10, key1 + 10, 2);
    memcpy(ck.blk2, key1 + 2, 8);
    memcpy(ck.blk2 + 8, key1 + 12, 4);
    if (dict->Exists()) {
        rsp = exword_unlock(m_device);
        rsp |= exword_cname(m_device, (char*)dict->GetName().mb_str(wxCSConv(wxT("SJIS"))).data(), (char*)dict->GetId().utf8_str().data());
        rsp |= exword_cryptkey(m_device, &ck);
        if (rsp == EXWORD_SUCCESS) {
            files = dict->GetFiles();
            exword_setpath(m_device, (uint8_t*)content_path.utf8_str().data(), 1);
            for (unsigned int i = 0; i < files.GetCount(); ++i) {
                wxFile file(files[i]);
                wxFileName filename(files[i]);
                wxString ext = filename.GetExt();
                if (file.IsOpened()) {
                    char *data = new char[file.Length()];
                    file.Read(data, file.Length());
                    if (ext == wxT("txt") ||
                        ext == wxT("bmp") ||
                        ext == wxT("htm") ||
                        ext == wxT("TXT") ||
                        ext == wxT("BMP") ||
                        ext == wxT("HTM")) {
                        crypt_data(data, file.Length(), (char *)ck.xorkey);
                    }
                    rsp |= exword_send_file(m_device, (char*)filename.GetFullName().utf8_str().data(), data, file.Length());
                    delete [] data;
                }
            }
            exword_setpath(m_device, (uint8_t*)user_path.utf8_str().data(), 1);
        }
        rsp |= exword_lock(m_device);
        success = (rsp == EXWORD_SUCCESS);
    }
    return success;
}
Beispiel #10
0
void UnicodeTestCase::ConversionUTF7()
{
    static const StringConversionData utf7data[] =
    {
        // normal fragments
        StringConversionData("+AKM-", L"\xa3"),
        StringConversionData("+AOk-t+AOk-", L"\xe9t\xe9"),

        // this one is an alternative valid encoding of the same string
        StringConversionData("+AOk-t+AOk", L"\xe9t\xe9",
                             StringConversionData::ONLY_MB2WC),

        // some special cases
        StringConversionData("+-", L"+"),
        StringConversionData("+--", L"+-"),

        // the following are invalid UTF-7 sequences
        StringConversionData("\xa3", NULL),
        StringConversionData("+", NULL),
        StringConversionData("+~", NULL),
        StringConversionData("a+", NULL),
    };

    for ( size_t n = 0; n < WXSIZEOF(utf7data); n++ )
    {
        const StringConversionData& d = utf7data[n];

        // converting to/from UTF-7 using iconv() currently doesn't work
        // because of several problems:
        //  - GetMBNulLen() doesn't return correct result (iconv converts L'\0'
        //    to an incomplete and anyhow nonsensical "+AA" string)
        //  - iconv refuses to convert "+-" (although it converts "+-\n" just
        //    fine, go figure)
        //
        // I have no idea how to fix this so just disable the test for now
#if 0
        d.Test(n, wxCSConv("utf-7"));
#endif
        d.Test(n, wxConvUTF7);
    }
}
Beispiel #11
0
//! @brief Receive data from connection
wxString Socket::Receive()
{
	wxString ret;
  if ( m_sock == 0 )
  {
    wxLogError( _T("Socket NULL") );
    return ret;
  }

  LOCK_SOCKET;

  const int chunk_size = 1337;

  std::vector<char> buff;
  int readnum = 0;
  int totalbytes = 0;

  do
  {
  	buff.resize( totalbytes + chunk_size ); // increase buffer capacity to fit incoming chunk
    m_sock->Read( &buff[totalbytes], chunk_size );
    readnum = m_sock->LastCount();
    totalbytes += readnum;
  } while ( readnum >= chunk_size );

	if ( totalbytes > 0 )
	{
		ret = wxString( &buff[0], wxConvUTF8, totalbytes );
		if ( ret.IsEmpty() )
		{
			ret = wxString( &buff[0], wxConvLocal, totalbytes );
			if ( ret.IsEmpty() )
			{
				 ret = wxString( &buff[0], wxCSConv(_T("latin-1")), totalbytes );
			}
		}
	}

	return ret;
}
Beispiel #12
0
wxCharBuffer CControlSocket::ConvToServer(const wxString& str, bool force_utf8 /*=false*/)
{
	if (m_useUTF8 || force_utf8)
	{
		wxCharBuffer buffer = wxConvUTF8.cWX2MB(str);
		if (buffer || force_utf8)
			return buffer;
	}

	if (m_pCSConv)
	{
		wxCharBuffer buffer = m_pCSConv->cWX2MB(str);
		if (buffer)
			return buffer;
	}

	wxCharBuffer buffer = wxConvCurrent->cWX2MB(str);
	if (!buffer)
		buffer = wxCSConv(_T("ISO8859-1")).cWX2MB(str);

	return buffer;
}
Beispiel #13
0
wxCharBuffer CControlSocket::ConvToServer(const wxString& str, bool force_utf8 /*=false*/)
{
	if (m_useUTF8 || force_utf8)
	{
#if wxUSE_UNICODE
		wxCharBuffer buffer = wxConvUTF8.cWX2MB(str);
#else
		wxWCharBuffer unicode = wxConvCurrent->cMB2WC(str);
		wxCharBuffer buffer;
		if (unicode)
			 buffer = wxConvUTF8.cWC2MB(unicode);
#endif

		if (buffer || force_utf8)
			return buffer;
	}

	if (m_pCSConv)
	{
#if wxUSE_UNICODE
		wxCharBuffer buffer = m_pCSConv->cWX2MB(str);
#else
		wxWCharBuffer unicode = wxConvCurrent->cMB2WC(str);
		wxCharBuffer buffer;
		if (unicode)
			 buffer = m_pCSConv->cWC2MB(unicode);
#endif

		if (buffer)
			return buffer;
	}

	wxCharBuffer buffer = wxConvCurrent->cWX2MB(str);
	if (!buffer)
		buffer = wxCSConv(_T("ISO8859-1")).cWX2MB(str);

	return buffer;
}
Beispiel #14
0
/* static */
wxString wxURI::Unescape(const wxString& uri)
{
    // the unescaped version can't be longer than the original one
    wxCharBuffer buf(uri.length());
    char *p = buf.data();

    for ( wxString::const_iterator i = uri.begin(); i != uri.end(); ++i, ++p )
    {
        char c = *i;
        if ( c == '%' )
        {
            int n = wxURI::DecodeEscape(i);
            if ( n == -1 )
                return wxString();

            wxASSERT_MSG( n >= 0 && n <= 0xff, "unexpected character value" );

            c = static_cast<char>(n);
        }

        *p = c;
    }

    *p = '\0';

    // by default assume that the URI is in UTF-8, this is the most common
    // practice
    wxString s = wxString::FromUTF8(buf);
    if ( s.empty() )
    {
        // if it isn't, use latin-1 as a fallback -- at least this always
        // succeeds
        s = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(buf);
    }

    return s;
}
Beispiel #15
0
void MainFrame::OnOpenYUVFile(const wxString& sFile, const wxString& sName, bool bWrongOpened)
{
    int w, h;
    bool bit;
    bool bShowConfigDlg = true;
    if(GetYUVConfigData(sFile, w, h, bit))
        bShowConfigDlg = false;
    if(bWrongOpened)
        bShowConfigDlg = true;
    if(bShowConfigDlg)
    {
        YUVConfigDlg cdlg(this, bWrongOpened);
        wxString width,height;
        if(g_parseResolutionFromFilename(sName, width, height))
        {
            cdlg.SetWidth(width);
            cdlg.SetHeight(height);
        }
        int ret = sName.find(_T("_10bit_"));
        if(ret != wxNOT_FOUND)
            cdlg.SetBitFlag(true);
        if(cdlg.ShowModal() == wxID_CANCEL)
            return;

        m_iSourceWidth = cdlg.GetWidth();
        m_iSourceHeight = cdlg.GetHeight();
        m_iYUVBit = (cdlg.Is10bitYUV() ? 10 : 8);
        if(wxFile::Exists((const wxChar*)sFile))
            m_FileLength = wxFile((const wxChar*)sFile).Length();
        else
        {
            wxMessageBox(_T("No such file in the disk"));
            return;
        }
        int t = (cdlg.Is10bitYUV() ? 2 : 1);
        if(!(m_FileLength/(m_iSourceWidth*m_iSourceHeight*3/2*t) > 0))
        {
            wxMessageBox(_T("Not enough data for one frame, open YUV file failed"));
            return;
        }
        StoreYUVConfigData(sFile, m_iSourceWidth, m_iSourceHeight, (m_iYUVBit > 8));
    }
    else
    {
        m_iSourceWidth = w;
        m_iSourceHeight = h;
        m_iYUVBit = (bit ? 10 : 8);
        if(wxFile::Exists((const wxChar*)sFile))
            m_FileLength = wxFile((const wxChar*)sFile).Length();
        else
        {
            wxMessageBox(_T("No such file in the disk"));
            return;
        }
        int t = (bit ? 2 : 1);
        if(!(m_FileLength/(m_iSourceWidth*m_iSourceHeight*3/2*t) > 0))
        {
            wxMessageBox(_T("Not enough data for one frame, open YUV file failed"));
            return;
        }
    }
    // multi-thread
    wxString lastFile = sFile;
    wxString lastName = sName;
    wxCommandEvent event;
    OnCloseFile(event);
    m_sCurOpenedFilePath = lastFile;
    m_sCurOpenedFileName = lastName;
    m_bOPened = true;
    m_bPlaying = false;
    m_FileLength = wxFile((const wxChar*)lastFile).Length();
    SetTotalFrameNumber();
    m_pCenterPageManager->GetPicViewCtrl(0)->SetScale(1.0);
    m_pCenterPageManager->GetPicViewCtrl(0)->SetFitMode(true);
#if defined(__WXMSW__)
    m_cYUVIO.open(lastFile.mb_str(wxCSConv(wxFONTENCODING_SYSTEM)).data(), false, m_iYUVBit, m_iYUVBit, m_iYUVBit, m_iYUVBit);
#else
    m_cYUVIO.open(lastFile.mb_str(wxConvUTF8).data(), false, m_iYUVBit, m_iYUVBit, m_iYUVBit, m_iYUVBit);
#endif
    m_pcPicYuvOrg = new TComPicYuv;
    m_pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, 64, 64, 4 );
    double scaleRate = 165.0/m_iSourceWidth;
    InitThumbnailListView();
    g_LogMessage(_T("Initialize thumbnail finished"));
    m_pImageList = new wxImageList((int)m_iSourceWidth*scaleRate, (int)m_iSourceHeight*scaleRate);
    m_pThumbThread = new ThumbnailThread(this, m_pImageList, m_iSourceWidth, m_iSourceHeight, m_iYUVBit, lastFile);
    if(m_pThumbThread->Create() != wxTHREAD_NO_ERROR)
    {
        g_LogError(_T("Can't create the thread!"));
        delete m_pThumbThread;
        m_pThumbThread = NULL;
    }
    else
    {
        if(m_pThumbThread->Run() != wxTHREAD_NO_ERROR)
        {
            g_LogError(_T("Can't create the thread!"));
            delete m_pThumbThread;
            m_pThumbThread = NULL;
        }
    }
}
Beispiel #16
0
/*
   See RFC 2047 for the description of the encodings used in the mail headers.
   Briefly, "encoded words" can be inserted which have the form of

      encoded-word = "=?" charset "?" encoding "?" encoded-text "?="

   where charset and encoding can't contain space, control chars or "specials"
   and encoded-text can't contain spaces nor "?".

   NB: don't be confused by 2 meanings of encoding here: it is both the
       charset encoding for us and also QP/Base64 encoding for RFC 2047
 */
static
String DecodeHeaderOnce(const String& in, wxFontEncoding *pEncoding)
{
   // we don't enforce the sanity checks on charset and encoding - should we?
   // const char *specials = "()<>@,;:\\\"[].?=";

   // there can be words in different encodings inside the same header so this
   // variable doesn't really make sense but in practice only one encoding will
   // be used in the entire header
   wxFontEncoding encodingHeader = wxFONTENCODING_SYSTEM;

   // if the header starts with an encoded word, preceding whitespace must be
   // ignored, so the flag must be set to true initially
   bool maybeBetweenEncodedWords = true;

   String out,
          space;
   // we can't define a valid "last" iterator below for empty string
   if ( in.empty() )
      return String();

   out.reserve(in.length());
   for ( wxString::const_iterator p = in.begin(),
                                end = in.end(),
                               last = in.end() - 1; p != end; ++p )
   {
      if ( *p == '=' && p != last && *(p + 1) == '?' )
      {
         // found encoded word

         // save the start of it
         const wxString::const_iterator encWordStart = p++;

         // get the charset
         String csName;
         for ( ++p; p != end && *p != '?'; ++p ) // initial "++" to skip '?'
         {
            csName += *p;
         }

         if ( p == end )
         {
            wxLogDebug(_T("Invalid encoded word syntax in '%s': missing charset."),
                       in.c_str());
            out += wxString(encWordStart, end);

            break;
         }

         // pass false to prevent asking the user from here: we can be called
         // during non-interactive operations and popping up a dialog for an
         // unknown charset can be inappropriate
         const wxFontEncoding encodingWord = wxFontMapperBase::Get()->
                                               CharsetToEncoding(csName, false);

         if ( encodingWord == wxFONTENCODING_SYSTEM )
         {
            wxLogDebug(_T("Unrecognized charset name \"%s\""), csName.mb_str());
         }

         // this is not a problem in Unicode build
#if !wxUSE_UNICODE
         if ( encodingHeader != wxFONTENCODING_SYSTEM &&
               encodingHeader != encodingWord )
         {
            // this is a bug (well, missing feature) in ANSI build of Mahogany
            wxLogDebug(_T("This header contains encoded words with different ")
                       _T("encodings and won't be rendered correctly."));
         }
#endif // !wxUSE_UNICODE

         encodingHeader = encodingWord;


         // get the encoding in RFC 2047 sense
         enum
         {
            Encoding_Unknown,
            Encoding_Base64,
            Encoding_QuotedPrintable
         } enc2047 = Encoding_Unknown;

         ++p; // skip '?'
         if ( *(p + 1) == '?' )
         {
            if ( *p == 'B' || *p == 'b' )
               enc2047 = Encoding_Base64;
            else if ( *p == 'Q' || *p == 'q' )
               enc2047 = Encoding_QuotedPrintable;
         }
         //else: multi letter encoding unrecognized

         if ( enc2047 == Encoding_Unknown )
         {
            wxLogDebug(_T("Unrecognized header encoding in '%s'."), in.c_str());

            // scan until the end of the encoded word
            const size_t posEncWordStart = p - in.begin();
            const size_t posEncWordEnd = in.find("?=", p - in.begin());
            if ( posEncWordEnd == wxString::npos )
            {
               wxLogDebug(_T("Missing encoded word end marker in '%s'."),
                          in.c_str());
               out += wxString(encWordStart, end);

               break;
            }

            // +1 to skip '?' of "?=" (don't skip '=', this will be accounted
            // for by p increment in the loop statement)
            p += posEncWordEnd - posEncWordStart + 1;

            // leave this word undecoded
            out += wxString(encWordStart, p + 1);

            continue;
         }

         p += 2; // skip "Q?" or "B?"

         // get the encoded text
         bool hasUnderscore = false;
         const wxString::const_iterator encTextStart = p;
         while ( p != last && (*p != '?' || *(p + 1) != '=') )
         {
            // this is needed for QP hack below
            if ( *p == '_' )
               hasUnderscore = true;

            ++p;
         }

         if ( p == last )
         {
            wxLogDebug(_T("Missing encoded word end marker in '%s'."),
                       in.c_str());
            out += wxString(encWordStart, end);

            break;
         }

         // convert the encoded word to char[] buffer for c-client
         wxCharBuffer encWord(wxString(encTextStart, p).To8BitData());

         // skip '=' following '?'
         ++p;

         String textDecoded;
         if ( encWord )
         {
            const unsigned long lenEncWord = strlen(encWord);

            // now decode the text using c-client functions
            unsigned long len;
            void *text;
            if ( enc2047 == Encoding_Base64 )
            {
               text = rfc822_base64(UCHAR_CCAST(encWord), lenEncWord, &len);
            }
            else // QP
            {
               // cclient rfc822_qprint() behaves correctly and leaves '_' in the
               // QP encoded text because this is what RFC says, however many
               // broken clients replace spaces with underscores and so we undo it
               // here -- in this case it's better to be user-friendly than
               // standard-conforming
               if ( hasUnderscore )
               {
                  for ( char *pc = encWord.data(); *pc; ++pc )
                  {
                     if ( *pc == '_' )
                        *pc = ' ';
                  }
               }

               text = rfc822_qprint(UCHAR_CCAST(encWord), lenEncWord, &len);
            }

            if ( text )
            {
               const char * const ctext = static_cast<char *>(text);

               if ( encodingWord == wxFONTENCODING_DEFAULT )
               {
                  // CharsetToEncoding() returns this for US-ASCII but
                  // wxCSConv() doesn't accept it
                  textDecoded = wxString::FromAscii(ctext, len);
               }
               else // real conversion needed
               {
                  textDecoded = wxString(ctext, wxCSConv(encodingWord), len);
               }

               fs_give(&text);
            }
         }

         if ( textDecoded.empty() )
         {
            // if decoding failed it is probably better to show undecoded
            // text than nothing at all
            textDecoded = wxString(encWordStart, p + 1);
         }

         // normally we leave the (8 bit) string as is and remember its
         // encoding so that we may choose the font for displaying it
         // correctly, but in case of UTF-7/8 we really need to transform it
         // here as we don't have any UTF-7/8 fonts, so we should display a
         // different string
#if !wxUSE_UNICODE
         if ( encodingHeader == wxFONTENCODING_UTF7 ||
                  encodingHeader == wxFONTENCODING_UTF8 )
         {
            encodingHeader = ConvertUTFToMB(&textDecoded, encodingHeader);
         }
#endif // !wxUSE_UNICODE

         out += textDecoded;

         // forget the space before this encoded word, it must be ignored
         space.clear();
         maybeBetweenEncodedWords = true;
      }
      else if ( maybeBetweenEncodedWords &&
                  (*p == ' ' || *p == '\r' || *p == '\n') )
      {
         // spaces separating the encoded words must be ignored according
         // to section 6.2 of the RFC 2047, so we don't output them immediately
         // but delay until we know that what follows is not an encoded word
         space += *p;
      }
      else // just another normal char
      {
         // if we got any delayed whitespace (see above), flush it now
         if ( !space.empty() )
         {
            out += space;
            space.clear();
         }

         out += *p;

         maybeBetweenEncodedWords = false;
      }
   }

   if ( pEncoding )
      *pEncoding = encodingHeader;

   return out;
}
Beispiel #17
0
// encode the given text unconditionally, i.e. without checking if it must be
// encoded (this is supposed to be done in the caller) and using the specified
// encodings and charset (which are supposed to be detected by the caller too)
static String
EncodeText(const String& in,
           wxFontEncoding enc,
           MIME::Encoding enc2047,
           const String& csName)
{
   // encode the word splitting it in the chunks such that they will be no
   // longer than 75 characters each
   wxCharBuffer buf(in.mb_str(wxCSConv(enc)));
   if ( !buf )
   {
      // if the header can't be encoded using the given encoding, use UTF-8
      // which always works
      buf = in.utf8_str();
   }

   String out;
   out.reserve(csName.length() + strlen(buf) + 7 /* for =?...?X?...?= */);

   const char *s = buf;
   while ( *s )
   {
      // if we wrapped, insert a line break
      if ( !out.empty() )
         out += "\r\n  ";

      static const size_t RFC2047_MAXWORD_LEN = 75;

      // how many characters may we put in this encoded word?
      size_t len = 0;

      // take into account the length of "=?charset?...?="
      int lenRemaining = RFC2047_MAXWORD_LEN - (5 + csName.length());

      // for QP we need to examine all characters
      if ( enc2047 == MIME::Encoding_QuotedPrintable )
      {
         for ( ; s[len]; len++ )
         {
            const char c = s[len];

            // normal characters stand for themselves in QP, the encoded ones
            // take 3 positions (=XX)
            lenRemaining -= (NeedsEncodingInHeader(c) || strchr(" \t=?", c))
                              ? 3 : 1;

            if ( lenRemaining <= 0 )
            {
               // can't put any more chars into this word
               break;
            }
         }
      }
      else // Base64
      {
         // rfc822_binary() splits lines after 60 characters so don't make
         // chunks longer than this as the base64-encoded headers can't have
         // EOLs in them
         static const int CCLIENT_MAX_BASE64_LEN = 60;

         if ( lenRemaining > CCLIENT_MAX_BASE64_LEN )
            lenRemaining = CCLIENT_MAX_BASE64_LEN;

         // we can calculate how many characters we may put into lenRemaining
         // directly
         len = (lenRemaining / 4) * 3 - 2;

         // but not more than what we have
         size_t lenMax = wxStrlen(s);
         if ( len > lenMax )
         {
            len = lenMax;
         }
      }

      // do encode this word
      unsigned char *text = (unsigned char *)s; // cast for cclient

      // length of the encoded text and the text itself
      unsigned long lenEnc;
      unsigned char *textEnc;

      if ( enc2047 == MIME::Encoding_QuotedPrintable )
      {
            textEnc = rfc822_8bit(text, len, &lenEnc);
      }
      else // Encoding_Base64
      {
            textEnc = rfc822_binary(text, len, &lenEnc);
            while ( textEnc[lenEnc - 2] == '\r' && textEnc[lenEnc - 1] == '\n' )
            {
               // discard eol which we don't need in the header
               lenEnc -= 2;
            }
      }

      // put into string as we might want to do some more replacements...
      String encword(wxString::FromAscii(CHAR_CAST(textEnc), lenEnc));

      // hack: rfc822_8bit() doesn't encode spaces normally but we must
      // do it inside the headers
      //
      // we also have to encode '?'s in the headers which are not encoded by it
      if ( enc2047 == MIME::Encoding_QuotedPrintable )
      {
         String encword2;
         encword2.reserve(encword.length());

         bool replaced = false;
         for ( const wxChar *p = encword.c_str(); *p; p++ )
         {
            switch ( *p )
            {
               case ' ':
                  encword2 += _T("=20");
                  break;

               case '\t':
                  encword2 += _T("=09");
                  break;

               case '?':
                  encword2 += _T("=3F");
                  break;

               default:
                  encword2 += *p;

                  // skip assignment to replaced below
                  continue;
            }

            replaced = true;
         }

         if ( replaced )
         {
            encword = encword2;
         }
      }

      // append this word to the header
      out << _T("=?") << csName << _T('?') << (char)enc2047 << _T('?')
          << encword
          << _T("?=");

      fs_give((void **)&textEnc);

      // skip the already encoded part
      s += len;
   }

   return out;
}
Beispiel #18
0
void CGameListCtrl::InsertItemInReportView(long _Index)
{
	// When using wxListCtrl, there is no hope of per-column text colors.
	// But for reference, here are the old colors that were used: (BGR)
	// title: 0xFF0000
	// company: 0x007030
	int ImageIndex = -1;

#ifdef _WIN32
		wxCSConv SJISConv(*(wxCSConv*)wxConvCurrent);
		static bool validCP932 = ::IsValidCodePage(932) != 0;
		if (validCP932)
		{
			SJISConv = wxCSConv(wxFontMapper::GetEncodingName(wxFONTENCODING_SHIFT_JIS));
		}
		else
		{
			WARN_LOG(COMMON, "Cannot Convert from Charset Windows Japanese cp 932");
		}
#else
		// on linux the wrong string is returned from wxFontMapper::GetEncodingName(wxFONTENCODING_SHIFT_JIS)
		// it returns CP-932, in order to use iconv we need to use CP932
		wxCSConv SJISConv(wxT("CP932"));
#endif

	GameListItem& rISOFile = *m_ISOFiles[_Index];
	m_gamePath.append(rISOFile.GetFileName() + '\n');

	// Insert a first row with nothing in it, that will be used as the Index
	long ItemIndex = InsertItem(_Index, wxEmptyString);

	// Insert the platform's image in the first (visible) column
	SetItemColumnImage(_Index, COLUMN_PLATFORM, m_PlatformImageIndex[rISOFile.GetPlatform()]);

	if (rISOFile.GetImage().IsOk())
		ImageIndex = m_imageListSmall->Add(rISOFile.GetImage());

	// Set the game's banner in the second column
	SetItemColumnImage(_Index, COLUMN_BANNER, ImageIndex);
	
	std::wstring wstring_name;
	const std::wstring& wstring_description = rISOFile.GetDescription();
	std::string company;

	wxString name;
	wxString description;

	// We show the company string on Gamecube only
	// On Wii we show the description instead as the company string is empty
	if (rISOFile.GetPlatform() == GameListItem::GAMECUBE_DISC)
		company = rISOFile.GetCompany().c_str();
	int SelectedLanguage = SConfig::GetInstance().m_LocalCoreStartupParameter.SelectedLanguage;
	switch (rISOFile.GetCountry())
	{
	case DiscIO::IVolume::COUNTRY_TAIWAN:
	case DiscIO::IVolume::COUNTRY_JAPAN:
		{
			rISOFile.GetName(wstring_name, -1);
			name = wxString(rISOFile.GetName(0).c_str(), SJISConv);
			m_gameList.append(StringFromFormat("%s (J)\n", (const char *)name.c_str()));
			description = wxString(company.size() ?	company.c_str() :
								rISOFile.GetDescription(0).c_str(),	SJISConv);
		}
		break;
	case DiscIO::IVolume::COUNTRY_USA:
		SelectedLanguage = 0;
	default:
		{
			wxCSConv WindowsCP1252(wxFontMapper::GetEncodingName(wxFONTENCODING_CP1252));
			rISOFile.GetName(wstring_name, SelectedLanguage);

			name = wxString(rISOFile.GetName(SelectedLanguage).c_str(), WindowsCP1252);
			m_gameList.append(StringFromFormat("%s (%c)\n",
						rISOFile.GetName(SelectedLanguage).c_str(),
						(rISOFile.GetCountry() == DiscIO::IVolume::COUNTRY_USA) ? 'U' : 'E'));
			description = wxString(company.size() ?	company.c_str() :
					rISOFile.GetDescription(SelectedLanguage).c_str(), WindowsCP1252);
		}
		break;
	}

	if (wstring_name.length())
		name = wstring_name.c_str();
	if (wstring_description.length())
		description = wstring_description.c_str();
		
	SetItem(_Index, COLUMN_TITLE, name, -1);
	SetItem(_Index, COLUMN_NOTES, description, -1);

	// Emulation state
	SetItemColumnImage(_Index, COLUMN_EMULATION_STATE, m_EmuStateImageIndex[rISOFile.GetEmuState()]);

	// Country
	SetItemColumnImage(_Index, COLUMN_COUNTRY, m_FlagImageIndex[rISOFile.GetCountry()]);

	// File size
	SetItem(_Index, COLUMN_SIZE, NiceSizeFormat(rISOFile.GetFileSize()), -1);

	// Background color
	SetBackgroundColor();

	// Item data
	SetItemData(_Index, ItemIndex);
}
Beispiel #19
0
void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput()
{
    TestInput(wxMBConvUTF32BE(), txtUtf32be, sizeof(txtUtf32be));
    TestInput(wxCSConv(wxFONTENCODING_UTF32BE), txtUtf32be, sizeof(txtUtf32be));
}
Beispiel #20
0
void TextStreamTestCase::TestUTF8Input()
{
    TestInput(wxConvUTF8, txtUtf8, sizeof(txtUtf8));
    TestInput(wxCSConv(wxFONTENCODING_UTF8), txtUtf8, sizeof(txtUtf8));
}
Beispiel #21
0
wxCharBuffer MIME::EncodeHeader(const String& in, wxFontEncoding enc)
{
   if ( !NeedsEncoding(in) )
      return in.ToAscii();

   // decide about the encoding to use if none specified
   if ( enc == wxFONTENCODING_SYSTEM )
   {
      // try to use the user current encoding first
      enc = wxLocale::GetSystemEncoding();
   }

   if ( wxCSConv(enc).FromWChar(NULL, 0, in.wc_str(wxConvLibc)) == wxCONV_FAILED )
   {
      // but if we can't encode with it, fall back to UTF-8 as it never fails
      enc = wxFONTENCODING_UTF8;
   }

   // get the encoding in RFC 2047 sense
   MIME::Encoding enc2047 = MIME::GetEncodingForFontEncoding(enc);

   if ( enc2047 == MIME::Encoding_Unknown )
   {
      FAIL_MSG( _T("should have valid MIME encoding") );

      enc2047 = MIME::Encoding_QuotedPrintable;
   }

   // get the name of the charset to use
   String csName = MIME::GetCharsetForFontEncoding(enc);
   if ( csName.empty() )
   {
      FAIL_MSG( _T("should have a valid charset name!") );

      csName = _T("UNKNOWN");
   }


   String headerEnc;
   headerEnc.reserve(2*in.length());

   // for QP we encode each header word separately as some might not need being
   // encoded at all and the header remains more readable if we don't encode
   // them unnecessarily, but for Base64 it's useless to do this as it's
   // unreadable anyhow so we just encode everything at once
   if ( enc2047 == MIME::Encoding_QuotedPrintable )
   {
      // encode each word of the header if necessary, taking into account one
      // added complication: white space between 2 consecutive encoded words is
      // ignored during decoding, so we must encode 2 consecutive words both of
      // which need encoding as one single encoded word or the space between
      // them would be lost
      bool lastWordEncoded = false;
      const wxArrayString words(wxStringTokenize(in));
      const size_t count = words.size();
      for ( size_t n = 0; n < count; ++n )
      {
         const wxString& word = words[n];
         if ( NeedsEncoding(word) )
         {
            const String wordEnc = EncodeText(word, enc, enc2047, csName);

            if ( lastWordEncoded )
            {
               // we need to merge the 2 consecutive encoded words together: we
               // do it by removing "?=" suffix from the previous word, adding
               // a space and remove the "=?charset?Q?" prefix from this word
               ASSERT_MSG( headerEnc.length() > 7, "bad QP-encoded last word" );
               headerEnc.RemoveLast(2); // "?="

               headerEnc += '_'; // space can be represented like this in QP

               const size_t posText = wordEnc.find("?Q?");
               ASSERT_MSG( posText != String::npos, "bad QP-encoded word" );
               headerEnc += wordEnc.substr(posText + 3);
            }
            else // last word not encoded, just append this one
            {
               if ( !headerEnc.empty() )
                  headerEnc += ' ';

               headerEnc += wordEnc;
            }

            lastWordEncoded = true;
         }
         else // this word doesn't need to be encoded, simply append it
         {
            if ( !headerEnc.empty() )
               headerEnc += ' ';

            headerEnc += word;

            lastWordEncoded = false;
         }
      }
   }
   else // MIME::Encoding_Base64
   {
      headerEnc = EncodeText(in, enc, enc2047, csName);
   }

   return headerEnc.ToAscii();
}
Beispiel #22
0
// guess the charset of the given Unicode text
wxFontEncoding GuessUnicodeCharset(const wchar_t *pwz)
{
   typedef const unsigned short *codepage;
   struct CodePageInfo
   {
      codepage cp;
      wxFontEncoding enc;
   };
   static const CodePageInfo s_codepages[] =
   {
      { iso8859_2tab,         wxFONTENCODING_ISO8859_2 },
      { iso8859_3tab,         wxFONTENCODING_ISO8859_3 },
      { iso8859_4tab,         wxFONTENCODING_ISO8859_4 },
      { iso8859_5tab,         wxFONTENCODING_ISO8859_5 },
      { iso8859_6tab,         wxFONTENCODING_ISO8859_6 },
      { iso8859_7tab,         wxFONTENCODING_ISO8859_7 },
      { iso8859_8tab,         wxFONTENCODING_ISO8859_8 },
      { iso8859_9tab,         wxFONTENCODING_ISO8859_9 },
      { iso8859_10tab,        wxFONTENCODING_ISO8859_10 },
      { iso8859_13tab,        wxFONTENCODING_ISO8859_13 },
      { iso8859_14tab,        wxFONTENCODING_ISO8859_14 },
      { iso8859_15tab,        wxFONTENCODING_ISO8859_15 },
      { windows_1250tab,      wxFONTENCODING_CP1250 },
      { windows_1251tab,      wxFONTENCODING_CP1251 },
      { windows_1252tab,      wxFONTENCODING_CP1252 },
      { windows_1253tab,      wxFONTENCODING_CP1253 },
      { windows_1254tab,      wxFONTENCODING_CP1254 },
      { windows_1255tab,      wxFONTENCODING_CP1255 },
      { windows_1256tab,      wxFONTENCODING_CP1256 },
      { windows_1257tab,      wxFONTENCODING_CP1257 },
      { koi8rtab,             wxFONTENCODING_KOI8 },
   };

   // default value: use system default font
   wxFontEncoding enc = wxFONTENCODING_SYSTEM;

   // first find a non ASCII character as ASCII ones are present in all (well,
   // many) code pages
   while ( *pwz && *pwz < 0x80 )
      pwz++;

   const wchar_t wch = *pwz;

   if ( !wch )
      return enc;

   // build the array of encodings in which the character appears
   // (+1 is needed to account for wxFONTENCODING_ISO8859_1)
   wxFontEncoding encodings[WXSIZEOF(s_codepages) + 1];
   size_t numEncodings = 0;

   // special test for iso8859-1 which is identical to first 256 Unicode
   // characters
   if ( wch < 0xff )
   {
      encodings[numEncodings++] = wxFONTENCODING_ISO8859_1;
   }

   for ( size_t nPage = 0; nPage < WXSIZEOF(s_codepages); nPage++ )
   {
      codepage cp = s_codepages[nPage].cp;
      for ( size_t i = 0; i < 0x80; i++ )
      {
         if ( wch == cp[i] )
         {
            ASSERT_MSG( numEncodings < WXSIZEOF(encodings),
                           _T("encodings array index out of bounds") );

            encodings[numEncodings++] = s_codepages[nPage].enc;
            break;
         }
      }
   }

   // now find an encoding which is available on this system
   for ( size_t nEnc = 0; nEnc < numEncodings; nEnc++ )
   {
      if ( wxFontMapper::Get()->IsEncodingAvailable(encodings[nEnc]) )
      {
         enc = encodings[nEnc];

         // test if we can convert the entire text using it
         //
         // NB: normally we shouldn't do the real conversion here as it is
         //     awfully wasteful but currently it's possible for wxCSConv::
         //     WC2MB() to return successfully when it's used for just testing
         //     and not real conversion under Win32
         if ( !wxString(pwz, wxCSConv(enc)).empty() )
            break;
      }
   }

   return enc;
}
Beispiel #23
0
   /**
   * Method called when the save button is pressed.
   * Gets the (possibly) edited values from the data table and saves it to the specified file.
   * @param event not used
   */
   void OnSaveTable (wxCommandEvent &WXUNUSED(event))
   {
      const wxString fn = dynamic_cast <wxTextCtrl *>(FindWindowById(MonkeyTable_FileName))->GetValue();
      const int filter = dynamic_cast <wxChoice *>(FindWindowById(MonkeyTable_FileFmt))->GetSelection();
      const int enc = dynamic_cast <wxChoice *>(FindWindowById(MonkeyTable_FileEnc))->GetSelection();

      const long flags = wxFD_SAVE | wxFD_OVERWRITE_PROMPT;
      wxFileDialog dialog(this, MM_MSG_SAVETBL, prefs.get(wxT("directories/save-table")), fn, filters[filter], flags);

      wxDataViewListCtrl *datavw = dynamic_cast <wxDataViewListCtrl *> (FindWindowById(MonkeyTable_DataTable));
      wxDataViewListStore *store = datavw->GetStore();
      wxString buf;

      // iterates through the data table rows
      for (int i = 0; i < static_cast<int>(store->GetCount()); i++)
      {
         wxVariant byte, value;

         store->GetValueByRow(byte, i, 0);
         store->GetValueByRow(value, i, 1);

         buf += byte.GetString() + wxT("=") + value.GetString() + wxT("\r\n");
      }

      // checks if the table contains non-ANSI characters
      if (enc == 0 && !buf.IsAscii())
         return ShowWarning(_("Can't save in the specified encoding:\nThe table contains non-ANSI characters."));

      if (dialog.ShowModal() == wxID_CANCEL) return;

      prefs.set(wxT("directories/save-table"), dialog.GetDirectory());
      wxFile tbl(dialog.GetPath(), wxFile::write);

      if (!tbl.IsOpened())
         return ShowWarning(_("The table file couldn't be created. Try again."));

      switch (enc)
      {
         // encodes the file in ANSI (ISO-8859-1)
         case 0: tbl.Write(buf, wxCSConv(wxT("iso-8859-1")));
                 break;

         // encodes the file in Unicode (UTF-8)
         case 1: tbl.Write(buf, wxConvUTF8);
                 break;

         // encodes the file in Unicode (UTF-16)
         case 2: tbl.Write(buf.data().AsWChar(), buf.size() * sizeof(wxChar));
                 break;

         // Does not work. Delved deep in the wx implementation and didn't find shit yet.
         // So... WIP.
         /*case 3: {
            wxCSConv enc(wxFONTENCODING_SHIFT_JIS);
            
            if (enc.IsOk())
               tbl.Write(buf, enc);
            else
               ShowWarning(_("Your system does not support conversion to Shift JIS."));
         }
         break;*/
      }

      tbl.Close();
      ShowInfo(_("Table saved successfully."), wxT("Monkey-Moore"));

      Close();
   }
void DatabaseStringConverter::SetEncoding(wxFontEncoding encoding)
{
  m_Encoding = wxCSConv(encoding);
}