bool FileManager::SaveUTF8(const wxString& name, const char* data, size_t len)
{
    if (wxFileExists(name) == false)
    {
        return wxFile(name, wxFile::write_excl).Write(data, len) == len;
    }
	else
	{
		wxString temp(name);
		temp.append(wxT(".temp"));

		if(wxFile(temp, wxFile::write).Write(data, len) == len)
		{
			if(platform::move(temp, name))
			{
				return true;
			}
			else
			{
				wxString failed(name);
				failed.append(wxT(".save-failed"));
				platform::move(temp, failed);
			}
		}
		return false;
	}
}
CompilerPtr CompilerLocatorCrossGCC::Locate(const wxString& folder, bool clear)
{
    if(clear) {
        m_compilers.clear();
    }

    wxArrayString matches;
    wxFileName fnFolder(folder, "");

    // We collect "*-gcc" files
    wxString pattern = "*-gcc";
#ifdef __WXMSW__
    pattern << ".exe";
#endif

    int count = wxDir::GetAllFiles(fnFolder.GetPath(), &matches, pattern, wxDIR_FILES);
    if(count == 0) {
        // try to see if we have a 'bin' folder under 'folder'
        fnFolder.AppendDir("bin");
        if(wxDir::Exists(fnFolder.GetPath())) {
            count = wxDir::GetAllFiles(fnFolder.GetPath(), &matches, pattern, wxDIR_FILES);
        }
    }

    if(count == 0) return NULL;

    for(int i = 0; i < count; ++i) {
#ifndef __WXMSW__
        // Check if this is a script
        char sha[2];
        wxFile(matches[i]).Read(sha, 2);
        if(strncmp(sha, "#!", 2) == 0) {
            continue;
        }
#endif
        wxFileName filename(matches.Item(i));
        if(filename.GetName() == "mingw32-gcc" || filename.GetName() == "x86_64-w64-mingw32-gcc") {
            // Don't include standard mingw32-gcc (32 and 64 bit) binaries
            // they will be picked up later by the MinGW locator
            continue;
        }

        CompilerPtr compiler(new Compiler(NULL));
        compiler->SetCompilerFamily(COMPILER_FAMILY_GCC);

        // get the compiler version
        compiler->SetName(filename.GetName());
        compiler->SetGenerateDependeciesFile(true);
        m_compilers.push_back(compiler);

        // we path the bin folder
        AddTools(compiler, filename.GetPath(), filename.GetName().BeforeLast('-'), filename.GetExt());
    }

    if(m_compilers.empty()) {
        return NULL;
    } else {
        return *m_compilers.begin();
    }
}
Exemple #3
0
void FileNameTestCase::TestIsSame()
{
    wxFileName fn1( wxFileName::CreateTempFileName( "filenametest1" ) );
    CPPUNIT_ASSERT( fn1.IsOk() );
    wxON_BLOCK_EXIT1( wxRemoveFile, fn1.GetFullPath() );

    wxFileName fn2( wxFileName::CreateTempFileName( "filenametest2" ) );
    CPPUNIT_ASSERT( fn2.IsOk() );
    wxON_BLOCK_EXIT1( wxRemoveFile, fn2.GetFullPath() );

    CPPUNIT_ASSERT( fn1.SameAs( fn1 ) );
    CPPUNIT_ASSERT( !fn1.SameAs( fn2 ) );

#if defined(__UNIX__)
    // We need to create a temporary directory and a temporary link.
    // Unfortunately we can't use wxFileName::CreateTempFileName() for neither
    // as it creates plain files, so use tempnam() explicitly instead.
    char* tn = tempnam(NULL, "wxfn1");
    const wxString tempdir1 = wxString::From8BitData(tn);
    free(tn);

    CPPUNIT_ASSERT( wxFileName::Mkdir(tempdir1) );
    // Unfortunately the casts are needed to select the overload we need here.
    wxON_BLOCK_EXIT2( static_cast<bool (*)(const wxString&, int)>(wxFileName::Rmdir),
                      tempdir1, static_cast<int>(wxPATH_RMDIR_RECURSIVE) );

    tn = tempnam(NULL, "wxfn2");
    const wxString tempdir2 = wxString::From8BitData(tn);
    free(tn);
    CPPUNIT_ASSERT_EQUAL( 0, symlink(tempdir1.c_str(), tempdir2.c_str()) );
    wxON_BLOCK_EXIT1( wxRemoveFile, tempdir2 );


    wxFileName fn3(tempdir1, "foo");
    wxFileName fn4(tempdir2, "foo");

    // These files have different paths, hence are different.
    CPPUNIT_ASSERT( !fn3.SameAs(fn4) );

    // Create and close a file to trigger creating it.
    wxFile(fn3.GetFullPath(), wxFile::write);

    // Now that both files do exist we should be able to detect that they are
    // actually the same file.
    CPPUNIT_ASSERT( fn3.SameAs(fn4) );
#endif // __UNIX__
}
bool FileManager::SaveUTF8(const wxString& name, const char* data, size_t len)
{
    if (wxFileExists(name) == false)
    {
        return wxFile(name, wxFile::write_excl).Write(data, len) == len;
    }
	else
	{
        if (!wxFile::Access(name, wxFile::write))
            return false;

        wxString temp(name);
        temp.append(wxT(".temp"));

        wxStructStat buff;
        wxLstat( name, &buff );

        wxFile f;
        f.Create(temp, true, buff.st_mode);

		if(f.Write(data, len) == len)
		{
			f.Close();
			if(platform::move(temp, name))
			{
				return true;
			}
			else
			{
				wxString failed(name);
				failed.append(wxT(".save-failed"));
				platform::move(temp, failed);
			}
		}
		return false;
	}
}
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;
        }
    }
}