Exemplo n.º 1
0
uint fileLength(std::fstream & file){
    std::streamoff oldPos = file.tellg();
    file.seekg(0, std::ios::end);
    std::streamoff length = file.tellg();
    file.seekg(oldPos);
    return (uint)length;
}
Exemplo n.º 2
0
bool MixHeader::readUnEncrypted(std::fstream &fh)
{
    t_mix_entry entry;
    std::pair<t_mix_index_iter,bool> rv;

    //new format won't have filecount yet
    if(m_file_count) {
        fh.seekg(6, std::ios::beg);
    } else {
        fh.seekg(4, std::ios::beg);
        fh.read(reinterpret_cast<char*>(&m_file_count), sizeof(uint16_t));
        fh.read(reinterpret_cast<char*>(&m_body_size), sizeof(uint32_t));
    }

    m_header_size += m_file_count * 12;

    //read entries
    for (uint16_t i = 0; i < m_file_count; i++) {
        fh.read(reinterpret_cast<char*>(&entry.first), sizeof(int32_t));
        fh.read(reinterpret_cast<char*>(&entry.second), sizeof(t_index_info));
        rv = m_index.insert(entry);
        if(!rv.second) {
            std::cout << "Error reading header, duplicate ID" << std::endl;
            return false;
        }
    }
    return true;
}
Exemplo n.º 3
0
	/**
	 * Obtains the total size of the stream, measured in bytes.
	 * If this value is unknown or can not be computed, -1 is returned.
	 *
	 * @return the size of the stream, or -1 if an error occurred
	 */
	virtual int32 size() const {
		int32 curPos = pos();
		_stream->seekg(0, std::ios::end);
		int32 curSize = pos();
		_stream->seekg(curPos, std::ios::end);
		return curSize;
	}
Exemplo n.º 4
0
Folder::Ptr Folder::readFolder(std::fstream &file, BSAULong fileNamesLength,
                               BSAULong &endPos)
{
  Folder::Ptr result(new Folder());
  result->m_NameHash = readType<BSAHash>(file);
  result->m_FileCount = readType<unsigned long>(file);
  result->m_Offset = readType<unsigned long>(file);
  std::streamoff pos = file.tellg();

  file.seekg(result->m_Offset - fileNamesLength, fstream::beg);

  result->m_Name = readBString(file);

  for (unsigned long i = 0UL; i < result->m_FileCount; ++i) {
    result->m_Files.push_back(File::Ptr(new File(file, result.get())));
  }

  if (static_cast<unsigned long>(file.tellg()) > endPos) {
    endPos = static_cast<BSAULong>(file.tellg());
  }

  file.seekg(pos);

  return result;
}
Exemplo n.º 5
0
bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
{
    std::ios_base::openmode fmode = std::fstream::binary;
    if (mode == File::Write) {
        fmode |= (std::fstream::out | std::fstream::trunc);
        createCache(SNAPPY_CHUNK_SIZE);
    } else if (mode == File::Read) {
        fmode |= std::fstream::in;
    }

    m_stream.open(filename.c_str(), fmode);

    //read in the initial buffer if we're reading
    if (m_stream.is_open() && mode == File::Read) {
        m_stream.seekg(0, std::ios::end);
        m_endPos = m_stream.tellg();
        m_stream.seekg(0, std::ios::beg);

        // read the snappy file identifier
        unsigned char byte1, byte2;
        m_stream >> byte1;
        m_stream >> byte2;
        assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);

        flushReadCache();
    } else if (m_stream.is_open() && mode == File::Write) {
Exemplo n.º 6
0
bool XorEncryptor::encryptData(std::fstream &original, std::fstream &result)
{
	if (!original.is_open() || !result.is_open())
	{
		return false;
	}

	original.seekg(0, std::ios::beg);
	result.seekp(0, std::ios::beg);

	char c = 0;
	unsigned i = 0;
	while (original.good())
	{
		original.read(&c, 1);
		c ^= password[i];
		if(original.gcount() > 0)
		{
			result.write(&c, 1);
		}

		if (++i == passSize)
		{
			i = 0;
		}
	}

	original.seekg(0, std::ios::beg);
	result.seekg(0, std::ios::beg);
	result.flush();

	return true;
}
Exemplo n.º 7
0
	unsigned int fileSize(std::fstream& file)
	{
		unsigned int oldpos = file.tellg();
		file.seekg(0, std::ios::end);
		unsigned int filesize = file.tellg();
		file.seekg(oldpos, std::ios::beg);
		return filesize;
	}
Exemplo n.º 8
0
size_t File::GetLength(std::fstream &file)
{
	file.seekg(0, file.end);
	size_t len = file.tellg();
	file.seekg(0, file.beg);
	
	return len;
}
Exemplo n.º 9
0
int User::readUNPW(std::fstream &fin){
	auto fpos = fin.cur;
	fin.seekg(0, fin.beg);
	fin.read(username, sizeof(str));
	fin.read(password, sizeof(str));
	decode(username);
	decode(password);
	fin.seekg(fpos);
	return 0;
}
Exemplo n.º 10
0
	SeekableFileStream(const char *name) {
		_fileStream = new std::fstream();
		_fileStream->open(name, std::fstream::in | std::fstream::binary);
		if (_fileStream->fail()) {
			_length = -1;
		} else {
			_fileStream->seekg(0, std::ios_base::end);
			_length = _fileStream->tellg();
			_fileStream->seekg(0, std::ios_base::beg);
		}
	}
Exemplo n.º 11
0
void IndexDataStructure::loadAt(std::fstream & _input_file, std::streampos _position)
{
    // remember the read file position
    std::streampos r_pos = _input_file.tellg();

    // go to asked read position
    _input_file.seekg(_position);
    readToNext(_input_file);

    // restore original read position
    _input_file.seekg(r_pos);
}
Exemplo n.º 12
0
	virtual bool seek(int32 offset, int whence = SEEK_SET) {
		_fileStream->clear();
		switch (whence) {
			case SEEK_SET:
				_fileStream->seekg(offset, std::ios_base::beg);
				break;
			case SEEK_CUR:
				_fileStream->seekg(offset, std::ios_base::cur);
				break;
			case SEEK_END:
				_fileStream->seekg(offset, std::ios_base::end);
				break;
		}
	}
Exemplo n.º 13
0
 CXXFStream(const char* _name,std::ios_base::openmode _mode):file(_name,_mode| std::ios::binary) {
     if(!file) {
         size = 0;
         return;
     }
     file.seekg(0, std::ios::end);
     size = (long)file.tellg();
     file.seekg(0, std::ios::beg);//将指针重新定位到文件开头
     int i;
     for(i=0; i<10; ++i) //0~9
         ox2d[i] = i;
     for(i=49; i<55; ++i)    //a~f、A~F
         ox2d[i] = ox2d[i-32] = i-39;
 }
Exemplo n.º 14
0
	/**
	 * Sets the stream position indicator for the stream. The new position,
	 * measured in bytes, is obtained by adding offset bytes to the position
	 * specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or
	 * SEEK_END, the offset is relative to the start of the file, the current
	 * position indicator, or end-of-file, respectively. A successful call
	 * to the seek() method clears the end-of-file indicator for the stream.
	 *
	 * @note The semantics of any implementation of this method are
	 * supposed to match those of ISO C fseek().
	 *
	 * @param offset	the relative offset in bytes
	 * @param whence	the seek reference: SEEK_SET, SEEK_CUR, or SEEK_END
	 * @return true on success, false in case of a failure
	 */
	virtual bool seek(int32 offset, int whence = SEEK_SET) {
		if (whence == SEEK_SET) {
			_stream->seekg(offset, std::ios::beg);
		} else if (whence == SEEK_CUR) {
			_stream->seekg(offset, std::ios::cur);
		} else {
			_stream->seekg(offset, std::ios::end);
		}
		
		if (_stream->fail())
			return false;
		if (_stream->bad())
			return false;
		return true;
	}
Exemplo n.º 15
0
void ID3V2_Header::Get_Header(std::fstream& file)
{
    file.seekg(0); //Sets The Pointer To Points At The Begining Of The File
    //Let's Get The Below Process According To The ID3v2 Standrad
    file.read((char*)Header_id ,3);             // Read 3 Bytes in Header i.e. "I" "D" "3"
    file.read((char*)&Version,1);             // Reads The Next 1 byte actually 8-bits i.e. ID3 Version in The Version Member
    file.read((char*)&Revision,1);            // Reads Next 1 Byte actually 8-bits i.e. ID3 Revision No. in The Revision Member
    file.read((char*)&Header_flags,1);         // Reads Header Flags
    unsigned char memory[4];
    file.read((char*)memory,sizeof(Header_size));  //Reads 4 Bytes (Sync Safe Form) in Memory.

    Header_size =  (memory[3] & 0xFF) |         //Finally Desynchronise The Synch. Safe Integer i.e.0xxxxxxx 0xxxxxxx ......
                  ((memory[2] & 0xFF) << 7 ) |  // Size    =  0xxxxxxx
                  ((memory[1] & 0xFF) << 14 ) | //          +        0xxxxxxx
                  ((memory[0] & 0xFF) << 21 );  //          +               0xxxxxxx
                                                //          +                      0xxxxxxx
                                                //__________________________________________
                                                // Size    =  xxxxxxxxxxxxxxxxxxxxxxxxxxxx      where + = OR Operation

if ((Header_id[0]=='I')and(Header_id[1]=='D')and(Header_id[2]=='3')) // Checks If Header ID  is I D 3
Is_present = 1;
else
Is_present = 0;

}
Exemplo n.º 16
0
void dump_mz(std::fstream &file, MZHeader &mzh)
{
    unsigned headerSize = mzh.n_header_paragraphs * 16;
    unsigned size = mzh.image_length_MOD_512 + mzh.image_length_DIV_512 * 512;
    if (size & 511)
        size -= 512;
    std::cout << "Header Size:        " << std::hex << std::setw(4) << std::setfill('0') << headerSize << std::endl;
    std::cout << "Image Size:         " << std::hex << std::setw(4) << std::setfill('0') << size - headerSize << std::endl;
    std::cout << "Checksum:           " << std::hex << std::setw(4) << std::setfill('0') << mzh.checksum << std::endl;
    std::cout << "Overlay:            " << std::hex << std::setw(4) << std::setfill('0') << mzh.overlay << std::endl;
    std::cout << "Min Paragraphs:     " << std::hex << std::setw(4) << std::setfill('0') << mzh.min_paragraphs_above << std::endl;
    std::cout << "Min Paragraphs:     " << std::hex << std::setw(4) << std::setfill('0') << mzh.max_paragraphs_above << std::endl;
    std::cout << "CS:IP:              " << std::hex << std::setw(4) << std::setfill('0') << mzh.initial_CS << ":" << std::hex << std::setw(4) << std::setfill('0') << mzh.initial_IP << std::endl;
    std::cout << "SS:SP:              " << std::hex << std::setw(4) << std::setfill('0') << mzh.initial_SS << ":" << std::hex << std::setw(4) << std::setfill('0') << mzh.initial_SP << std::endl;
    std::cout << "Relocation items:   " << std::dec << mzh.n_relocation_items << "(" << std::hex << std::setw(4) << std::setfill('0') << mzh.offset_to_relocation_table << ")";
    file.seekg(mzh.offset_to_relocation_table);
    for (int i=0; i < mzh.n_relocation_items; i++)
    {
        if (i % 4 == 0)
            std::cout << std::endl << "        ";
        unsigned short ss[2];
        file.read((char *)ss, sizeof(ss));
        std::cout << std::hex << std::setw(4) << std::setfill('0') << (int)ss[1] << ":" << std::setw(4) << std::setfill('0') << (int)ss[0] << "  ";
    }
    std::cout << std::endl << std::endl;
}
Exemplo n.º 17
0
/******************************************************************************************************
void strip_file(std::ifstream& inStream, std::string fileName)

Description: Opens a input file and strips symbols and outputs to temp file.

Parameters: std::ifstream& inStream, std::string fileName

Pre-Conditions: Input file must be opened.
Post-Conditions: Input file will be stripped of symnbols and output to temp file.
******************************************************************************************************/
void strip_file(std::ifstream& inStream, std::fstream& tempStream)
{
    std::vector<char> lines;//create a vector to hold characters.

    while (inStream)//iterate through each line in file.
    {
	   char ch;
	   int count = 0;
	   ch = inStream.get();//Get character from line.
	   while (ch != EOF)//While not end of line.
	   {
		  if (ch != ',' && ch != '[' && ch != ']')//Do not add these symbols.
		  {
			 lines.push_back(ch);//Push character to vector.
			 count++;//inc count, used to loop through vector.
		  }
		  ch = inStream.get();//get next char in line.
	   }
	   for (int i = 0; i < count; i++)
	   {

		  //std::cout << lines[i];  //Outputs each line in file to console.
		  tempStream << lines[i];//Outputs to temp.txt file

	   }
	   lines.clear();//Clears the vector of all values.
    }
    inStream.clear();//clears the end of file flag (eof).
    inStream.seekg(0L, std::ios::beg);//Move back to the beginning of the input file stream.
    tempStream.clear();//clears the end of file flag (eof).
    tempStream.seekg(0L, std::ios::beg);//Move back to the beginning of the input file stream.

}//end strip_file
Exemplo n.º 18
0
	void Database::_GetLock(uint64_t blockpos, bool readonly) {
		char blocklock = 1;
		backing.seekg(blockpos);
		backing.read(&blocklock, 1);
		while (blocklock == 1)
		{
			std::this_thread::sleep_for(std::chrono::milliseconds(50)); //Thanks StackOverflow!
			backing.seekg(blockpos);
			backing.read(&blocklock, 1);
		}
		if (readonly) {
			blocklock = 1;
			backing.seekp(blockpos);
			backing.write(&blocklock, 1);
		}
	}
Exemplo n.º 19
0
//Moves all records one position lower, and writes latest game at top of file;
void LastTenGames::writeLastTenGames(std::fstream& readfile, 
	double startCredit, int realTime, double playerBet, double playerWin, double coef)
{
	PreviousGame blankGame;
	PreviousGame currentGame(startCredit, realTime, playerBet, playerWin, coef);
	for (int i = 9; i > 0; i--)
	{
		readfile.seekg((i - 1) * sizeof(PreviousGame));
		readfile.read(reinterpret_cast<char*>(&blankGame), sizeof(PreviousGame));
		readfile.seekg(i * sizeof(PreviousGame));
		readfile.write(reinterpret_cast<const char*>(&blankGame), sizeof(PreviousGame));
	}

	readfile.seekg(0 * sizeof(PreviousGame));
	readfile.write(reinterpret_cast<const char*>(&currentGame), sizeof(PreviousGame));
}
Exemplo n.º 20
0
void GameLoaderC3Map::Impl::initClimate(std::fstream &f, CityPtr ioCity)
{
  // read climate
  unsigned int i = 0;
  f.seekg(kClimate, std::ios::beg);
  f.read((char*)&i, 1);

  ClimateType climate = (ClimateType) i;
  ioCity->setClimate(climate);

  StringHelper::debug( 0xff, "Climate type is %d", climate );

  // reload all pics for the given climate
  //   PicLoader &pic_loader = PicLoader::instance();
  //   if (climate == C_CENTRAL)
  //   {
  //      pic_loader.load_archive("resources/pics/pics.zip");
  //   }
  //   else if (climate == C_NORTHERN)
  //   {
  //      pic_loader.load_archive("resources/pics/pics_north.zip");
  //   }
  //   else if (climate == C_DESERT)
  //   {
  //      pic_loader.load_archive("resources/pics/pics_south.zip");
  //   }
}
Exemplo n.º 21
0
long long TCPTransport::sendfile(std::fstream& ifs, long long offset, long long size)
{
    if (!m_bConnected)
        return -1;

    if (ifs.bad() || ifs.fail())
        return -1;

    ifs.seekg(offset);

    int block = 1000000;
    char* buf = new char[block];
    long long sent = 0;
    while (sent < size)
    {
        int unit = int((size - sent) > block ? block : size - sent);
        ifs.read(buf, unit);
        send(buf, unit);
        sent += unit;
    }

    delete [] buf;

    return sent;
}
Exemplo n.º 22
0
void ReadSector(char* buffer, size_t sec) {
    sec += partitionOffset;
    if (sec > partitionLength)
        return;

    f.seekg(sec * 512, std::ios::beg);
    f.read(buffer, 512);
}
Exemplo n.º 23
0
void PcieAccessInterfaceTest::write_file(char* data, const std::string& path, uint32_t size, uint32_t offset) {
    memory_file.open(path,  std::ios::out |std::ios::binary | std::ios::trunc);
    if (memory_file.is_open()) {
        memory_file.seekg(offset, std::ios::beg);
        memory_file.write(data, size);
        memory_file.close();
    }
}
Exemplo n.º 24
0
exp bool Open(const char* fname, unsigned int pO, unsigned int pLen) {
    partitionOffset = pO;
    partitionLength = pLen;

    // Open the image
    f.open(fname, std::ios::in | std::ios::out | std::ios::binary);

    if (!f.is_open()) {
        LastError("Open", "Failed to open disk");
        return false;
    }

    f.seekg(0, std::ios::end);
    fsize = (size_t)f.tellg();
    f.seekg(0);

    // Allocate a buffer
    sectorBuffer = new char[SECTOR_SIZE];

    // Read the BPB
    if (!ReadBPB()) {
        LastError("Open", "Failed to read the BPB");
        return false;
    }

    // Read filesystem info
    fsInfo = new FileSystemInfo;
    ReadSector((char*)fsInfo, bpb->clusterFSInfo);

    // Load the root directory
    if (!DirOpenRoot()) {
        LastError("Open", "Failed to load the root directory");
        return false;
    }

    volumeId = new char[DOS83_MAX_LEN + 1];
    ReadSector(sectorBuffer, ClusterToSector(bpb->clusterRoot));
    DirectoryEntry* entry = FindEntryAttribute(ATTRIB_VOLUME_ID, &directory);
    if (entry) {
        memcpy(volumeId, entry->name, DOS83_MAX_LEN);
        volumeId[11] = 0;
    }

    return true;
}
Exemplo n.º 25
0
bool
StandardFileProvider::seek( Size pos )
{
    if( _seekg )
        _fstream.seekg( pos, ios::beg );
    if( _seekp )
        _fstream.seekp( pos, ios::beg );
    return _fstream.fail();
}
Exemplo n.º 26
0
void GameLoaderC3Map::Impl::initCameraStartPos(std::fstream &f, CityPtr ioCity)
{
  unsigned short int i = 0;
  unsigned short int j = 0;
  f.seekg(kCamera, std::ios::beg);
  f.read((char*)&i, 2);
  f.read((char*)&j, 2);

  ioCity->setCameraPos( TilePos( i, j ) );
}
Exemplo n.º 27
0
String File::GetContents(std::fstream & fileStream)
{
	assert(fileStream.is_open());
	fileStream.seekg( 0, std::ios::beg);
	int start  = (int) fileStream.tellg();
	fileStream.seekg( 0, std::ios::end );
	int fileSize = (int) fileStream.tellg();
	// Empty file?
	if (fileSize == 0)
		return String();
	assert(fileSize);
	char * data = new char [fileSize+2];
	memset(data, 0, fileSize+1);
	fileStream.seekg( 0, std::ios::beg);
	fileStream.read((char*) data, fileSize);
	String strData(data);
	delete[] data;
	return strData;
}
Exemplo n.º 28
0
bool write(std::fstream& file, Json::Value const& root)
{
   Json::StyledWriter writer;
   std::string val = writer.write(root);
   LOG4CPLUS_INFO(CFileStorage::msLogger, val);
   file << val;
   file.flush();
   file.seekg(0, ios::beg);
   return file.good();
}
Exemplo n.º 29
0
bool MixHeader::writeHeader(std::fstream& fh)
{
    //make sure we are at the start of the file stream we were passed
    fh.seekg(0, std::ios::beg);
    if(m_is_encrypted) {
        return writeEncrypted(fh);
    } else {
        return writeUnEncrypted(fh);
    }
}
Exemplo n.º 30
-28
static bool loadY4MHeader(std::fstream& fs, cv::Mat &img)
{
    fs.seekg(fs.beg);
    if (fs.good())
    {
        char inbuf [256];
        fs >> inbuf;
        if (strcmp(inbuf, "YUV4MPEG2") != 0)
        {
            return false;
        }
        fs.get(); // space
        int width, height;
        char c = fs.get();
        if (c != 'W')
        {
            return false;
        }
        fs >> width;
        c = fs.get();// space
        c = fs.get();
        if (c != 'H')
        {
            return false;
        }
        fs >> height;
        img.create(height * 3 / 2, width, CV_8UC1);
    }