void BinaryFileTestCase::testSeek()
{
	tstring str = TestHelper::getTestDataPath(_T("BinaryFileTestSeek"), false);
	BinaryFile fileWriter;
    fileWriter.open(str, BinaryFile::CREATE);
	CPPUNIT_ASSERT( fileWriter.isFileOpen() );
	int32_t nData;
	for(size_t i = 0; i < 1000; ++i)
	{
        nData = (int32_t)i;
		fileWriter.write(&nData, sizeof(int32_t));
	}
	fileWriter.close();

	BinaryFile fileReader;
    fileReader.open(str, BinaryFile::READ);
	CPPUNIT_ASSERT( fileReader.isFileOpen() );
	CPPUNIT_ASSERT((int64_t)1000 * sizeof(int32_t) == fileReader.getLength());

	CPPUNIT_ASSERT_EQUAL((int64_t)0, fileReader.getPos());
    fileReader.seek(10);
	CPPUNIT_ASSERT_EQUAL((int64_t)10, fileReader.getPos());

    fileReader.close();
    
    //Remove file
    File f(str);
    f.remove();
}
void XMLDocumentWrapper::parseFile(const std::string& sFile)
{
    BinaryFile bf;
    bf.open(sFile, BinaryFile::READ);

    size_t len = (size_t)bf.getLength();
    char* buf = m_doc.allocate_string(NULL, len + 1);
    buf[len] = 0;
    try
    {
        bf.read(buf, len);
    }
    catch (const FileIOException& fe)
    {
        bf.close();
        clear();
        FIRTEX_RETHROW(fe);
    }	
    bf.close();

    try 
    {
        m_doc.parse<rapidxml::parse_full>(buf);
    }
    catch(rapidxml::parse_error& e)
    {
        clear();
        FIRTEX_THROW(BadXmlFormatException, "Bad xml format: [%s]", e.what());
    }
}
Beispiel #3
0
void HTMLParser::parse(const tstring& sHtmlFile)
{
    BinaryFile bf;
    try
    {
        bf.open(sHtmlFile, BinaryFile::READ);
    
        m_nFileSize = (size_t)bf.getLength();
        if(m_nFileSize > MAX_FILESIZE - 1)
        {
            m_nFileSize = MAX_FILESIZE - 1;
        }

        if(!m_pReadBuffer)
        {
            m_nReadBufferSize = DEFAULT_READBUFFER_SIZE;
            if(m_nReadBufferSize < m_nFileSize + 1)
                m_nReadBufferSize = m_nFileSize + 1;
            m_pReadBuffer = new char[m_nReadBufferSize];
        }
        else if(m_nFileSize + 1 > m_nReadBufferSize)
        {
            m_nReadBufferSize = m_nFileSize + 1;
            delete[] m_pReadBuffer;
            m_pReadBuffer = new char[m_nReadBufferSize];
        }
        size_t nRet = bf.read(m_pReadBuffer, m_nFileSize);
        if(nRet != m_nFileSize)
        {
            FX_LOG(WARN, "Read file [%s] error", sHtmlFile.c_str());
            bf.close();
            return;
        }
        bf.close();
        parse(m_pReadBuffer, m_nFileSize);
    }
    catch(const FirteXException& e)
    {
        FX_LOG(ERROR, "Parse file: [%s] FAILED. Error message: [%s]",
               sHtmlFile.c_str(), e.what().c_str());
    }
}
string TrecDocumentProcessorTestCase::writeTestFile(const string& sFileName, 
                                                      const char* szFileContent)
{
    std::string sPath = getTestOutputPath();
    sPath += "/" + sFileName;
    
    BinaryFile bf;
    bf.open(sPath, BinaryFile::CREATE);
    bf.write(szFileContent, strlen(szFileContent));
    bf.close();

    return sPath;
}
void BinaryFileTestCase::testRead()
{
	tstring str = TestHelper::getTestDataPath(_T("BinaryFileTestWriteFile"), false);
	BinaryFile bf;
	bf.open(str.c_str(),BinaryFile::READ);
	CPPUNIT_ASSERT( bf.isFileOpen() );
	int32_t nData;
	for(size_t i = 0; i < 10000;i += 1000)
	{
		bf.seek(i);
		bf.read(&nData,sizeof(int32_t));
		CPPUNIT_ASSERT_EQUAL(nData,(int32_t)i);
	}
	bf.close();
}
void BinaryFileTestCase::testLargeFileWrite()
{
#ifdef TEST_LARGE_FILE
    BinaryFile bf;
	tstring str = TestHelper::getTestDataPath(_T("BinaryFileTestLargeFile"), false);
	bf.open(str.c_str(),BinaryFile::CRTR);
	CPPUNIT_ASSERT( bf.isFileOpen() );	
	int64_t nPos = 1000000000;//1 GB
	nPos <<= 4;//16 GB	
	bf.seek(nPos);
	int64_t nData = 123456789;
	bf.write(&nData,sizeof(int64_t));	
	bf.close();
#endif
}
Beispiel #7
0
bool GenericAssemblerFile::open(bool onlyCheck)
{
	headerSize = originalHeaderSize;
	virtualAddress = headerSize;

	if (onlyCheck == false)
	{
		// actually open the file
		bool success;
		switch (mode)
		{
		case Open:
			success = handle.open(fileName,BinaryFile::ReadWrite);
			if (success == false)
			{
				Logger::printError(Logger::FatalError,L"Could not open file %s",fileName);
				return false;
			}
			return true;

		case Create:
			success = handle.open(fileName,BinaryFile::Write);
			if (success == false)
			{
				Logger::printError(Logger::FatalError,L"Could not create file %s",fileName);
				return false;
			}
			return true;

		case Copy:
			success = copyFile(originalName,fileName);
			if (success == false)
			{
				Logger::printError(Logger::FatalError,L"Could not copy file %s",originalName);
				return false;
			}

			success = handle.open(fileName,BinaryFile::ReadWrite);
			if (success == false)
			{
				Logger::printError(Logger::FatalError,L"Could not open file %s",fileName);
				return false;
			}
			return true;

		default:
			return false;
		}
	}

	// else only check if it can be done, don't actually do it permanently
	bool success, exists;
	BinaryFile temp;
	switch (mode)
	{
	case Open:
		success = temp.open(fileName,BinaryFile::ReadWrite);
		if (success == false)
		{
			Logger::queueError(Logger::FatalError,L"Could not open file %s",fileName);
			return false;
		}
		temp.close();
		return true;

	case Create:
		// if it exists, check if you can open it with read/write access
		// otherwise open it with write access and remove it afterwards
		exists = fileExists(fileName);
		success = temp.open(fileName,exists ? BinaryFile::ReadWrite : BinaryFile::Write);
		if (success == false)
		{
			Logger::queueError(Logger::FatalError,L"Could not create file %s",fileName);
			return false;
		}
		temp.close();

		if (exists == false)
			deleteFile(fileName);

		return true;

	case Copy:
		// check original file
		success = temp.open(originalName,BinaryFile::ReadWrite);
		if (success == false)
		{
			Logger::queueError(Logger::FatalError,L"Could not open file %s",originalName);
			return false;
		}
		temp.close();

		// check new file, same as create
		exists = fileExists(fileName);
		success = temp.open(fileName,exists ? BinaryFile::ReadWrite : BinaryFile::Write);
		if (success == false)
		{
			Logger::queueError(Logger::FatalError,L"Could not open file %s",fileName);
			return false;
		}
		temp.close();
		
		if (exists == false)
			deleteFile(fileName);

		return true;

	default:
		return false;
	};

	return false;
}