Ejemplo n.º 1
0
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();
}
Ejemplo n.º 2
0
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());
    }
}
Ejemplo n.º 3
0
    void index(const tstring& sDir)
    {
        IndexWriterPtr pIndexWriter = m_pIndex->acquireWriter();

        DirectoryIterator di(sDir, false);
        while(di.hasNext())
        {
            const File& f = di.next();
            if(f.isFile())
            {
                BinaryFile bf;
                bf.open(f.getPath().c_str(), BinaryFile::READ);
                if(bf.isFileOpen())
                {
                    size_t nRead = (size_t)bf.getLength();
                    if (nRead > 0)
                    {
                        DocumentPtr pDoc = new Document(pIndexWriter->getDocSchema());
                        pDoc->addField(0, f.getPath().c_str());
                        char* buf = new char[nRead + 1];
                        bf.read(buf, nRead);
                        buf[nRead] = 0;
                        pDoc->addField(1, buf, nRead, false);
                        delete[] buf;
                        
                        pIndexWriter->addDocument(pDoc);
                    }
                }
            }
        }
        docPool.commit();
        pIndexWriter->close();
    }
Ejemplo n.º 4
0
ExpressionValue expFuncRead(const std::vector<ExpressionValue>& parameters)
{
	ExpressionValue result;
	T buffer;

	if (parameters[0].isString() == false || (parameters.size() >= 2 && parameters[1].isInt() == false))
	{
		Logger::queueError(Logger::Error,L"Invalid parameter");
		return result;
	}

	std::wstring fileName = getFullPathName(parameters[0].strValue);
	u64 pos = parameters.size() >= 2 ? parameters[1].intValue : 0;

	BinaryFile file;
	if (file.open(fileName,BinaryFile::Read) == false)
	{
		Logger::queueError(Logger::Error,L"Could not open %s",fileName);
		return result;
	}

	file.setPos((long)pos);

	if (file.read(&buffer,sizeof(T)) == sizeof(T))
	{
		result.type = ExpressionValueType::Integer;
		result.intValue = (u64) buffer;
	}

	return result;
}
Ejemplo n.º 5
0
Loader::LoaderVersion Loader::checkFile(std::string f) {
    BinaryFile file;
    if (file.open(f.c_str()) != 0)
        return TR_UNKNOWN;

    uint32_t start = file.readU32();
    switch (start) {
        case 0x00000020:
            return TR_1;

        case 0x0000002D:
            return TR_2;

        case 0xFF080038:
        case 0xFF180038:
            return TR_3;

        case 0xFFFFFFF0: // bogus
        case 0x00345254: // "TR4\0"
            return TR_4;
    }

    Log::get(LOG_ERROR) << "Unknown TR level version: \"" << start << "\"" << Log::endl;

    return TR_UNKNOWN;
}
Ejemplo n.º 6
0
ExpressionValue expFuncRead(const std::wstring& funcName, const std::vector<ExpressionValue>& parameters)
{
	const std::wstring* fileName;
	u64 pos;

	GET_PARAM(parameters,0,fileName);
	GET_OPTIONAL_PARAM(parameters,1,pos,0);

	std::wstring fullName = getFullPathName(*fileName);

	BinaryFile file;
	if (file.open(fullName,BinaryFile::Read) == false)
	{
		Logger::queueError(Logger::Error,L"Could not open %s",fileName);
		return ExpressionValue();
	}

	file.setPos((long)pos);

	T buffer;
	if (file.read(&buffer,sizeof(T)) != sizeof(T))
		return ExpressionValue();

	return ExpressionValue((u64) buffer);
}
Ejemplo n.º 7
0
void XMLDocumentWrapper::printToFile(std::string& sFile)
{
    ostringstream os;
    print(os);
    BinaryFile bf;
    bf.open(sFile, BinaryFile::CREATE);
    bf.write(os.str().c_str(), os.str().length());
}
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;
}
Ejemplo n.º 9
0
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();
}
Ejemplo n.º 10
0
void BinaryFileTestCase::testLargeFileSeekAndRead()
{
#ifdef TEST_LARGE_FILE
	tstring str = TestHelper::getTestDataPath(_T("BinaryFileTestLargeFile"), false);
	BinaryFile bf;
	bf.open(str.c_str(), BinaryFile::READ);
	CPPUNIT_ASSERT( bf.isFileOpen() );
	int64_t nPos = 1000000000;//1 GB
	nPos <<= 4;//16 GB
	int64_t nData = 123456789;
	bf.seek(nPos);
	bf.read(&nData,sizeof(int64_t));
	CPPUNIT_ASSERT_EQUAL(nData,(int64_t)123456789);
#endif
}
Ejemplo n.º 11
0
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
}
Ejemplo n.º 12
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());
    }
}
Ejemplo n.º 13
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;
}