コード例 #1
0
int Initialize(int argc, char *argv[])
{
	g_mode = atoi(argv[1]);
	if (g_mode != 1 && g_mode != 2)
	{
		cout << "mode must be 1 or 2" << endl;
		return 0;
	}

	g_fileIn.open(argv[2], ios::binary | _IOS_Nocreate | ios::in);
	if (!g_fileIn)
	{
		cout << argv[1] << " can not be open!\n";
		return 0;
	}
	
	strcpy_s(g_flvFile, argv[3]);

	g_fileIn.seekg(0, ios::end);
	std::streampos ps = g_fileIn.tellg();
	g_nFileSize = ps;
	g_fileIn.seekg(0, ios_base::beg);

	g_pBufferIn = new unsigned char[g_nFileSize];
	g_pBufferOut = new unsigned char[g_nFileSize];
	if (g_pBufferIn == NULL && g_pBufferOut == NULL)
		return 0;

	g_fileIn.read((char *)g_pBufferIn, g_nFileSize);
	if (g_nFileSize != g_fileIn.gcount())
		return 0;

	return 1;
}
コード例 #2
0
ファイル: csv_main.cpp プロジェクト: wvengen/LGI
string ReadStringFromFile( fstream &File )
{
    char TempBuffer[ 1024 ];
    string Buffer;

    Buffer.reserve( 4096 );
    Buffer.clear();

    while( File )
    {
        File.read( TempBuffer, 1024 );
        if( File.gcount() )
            Buffer.append( string( TempBuffer, File.gcount() ) );
    }

    return( Buffer );
}
コード例 #3
0
ファイル: buffer.cpp プロジェクト: ShaderManager/udt
int CSndBuffer::addBufferFromFile(fstream& ifs, int len)
{
   int size = len / m_iMSS;
   if ((len % m_iMSS) != 0)
      size ++;

   // dynamically increase sender buffer
   while (size + m_iCount >= m_iSize)
      increase();

   Block* s = m_pLastBlock;
   int total = 0;
   for (int i = 0; i < size; ++ i)
   {
      if (ifs.bad() || ifs.fail() || ifs.eof())
         break;

      size_t pktlen = len - i * m_iMSS;
      if (pktlen > m_iMSS)
         pktlen = m_iMSS;

      ifs.read(s->m_pcData, pktlen);
	  pktlen = ifs.gcount();

      if (pktlen <= 0)
         break;

      // currently file transfer is only available in streaming mode, message is always in order, ttl = infinite
      s->m_iMsgNo = m_iNextMsgNo | 0x20000000;
      if (i == 0)
         s->m_iMsgNo |= 0x80000000;
      if (i == size - 1)
         s->m_iMsgNo |= 0x40000000;

      s->m_iLength = pktlen;
      s->m_iTTL = -1;
      s = s->m_pNext;

      total += pktlen;
   }
   m_pLastBlock = s;

   CGuard::enterCS(m_BufLock);
   m_iCount += size;
   CGuard::leaveCS(m_BufLock);

   m_iNextMsgNo ++;
   if (m_iNextMsgNo == CMsgNo::m_iMaxMsgNo)
      m_iNextMsgNo = 1;

   return total;
}
コード例 #4
0
ファイル: ORDER.CPP プロジェクト: pyal/eos_cpp
void ReadCollection(fstream &in,MyIndexCollection &col)//,FieldInd)
{
	int SizeStruct=col.SizeEl;
	char *buf;
	while (!in.eof())
		{ 
		 buf=new char[SizeStruct];
		 in.read( buf, SizeStruct);
		 int Nread=in.gcount();
		 if (Nread==0) {delete buf;return ;}
		 if (Nread!=SizeStruct) 
			{ErrorOut<<" Error reading file. File size do not match struct size";throw int();};
		 col.AddElement(buf);//if (FstElement==NULL) FstElement=buf;
		}
};
コード例 #5
0
//1ページ分をダンプ
void DumpFile::Dump(){
	cout << endl;
	m_file.clear();
	m_file.seekg(m_page * PAGE_SIZE);

	for (int i = 0; i < PAGE_HEIGHT; ++i){
		unsigned char buf[PAGE_WIDTH];
		m_file.read((char*)buf,sizeof buf);

		for (int i = 0; i < m_file.gcount(); ++i){
			printf("%02X ", buf[i]);
		}
		cout << endl;
	}
}
コード例 #6
0
ファイル: c4s_util.cpp プロジェクト: merenluotoa/cpp4scripts
// ==================================================================================================
bool c4s::search_file(fstream &target, const string &needle)
/*!  Uses Boyer-Moore algorithm to search for a text in a given stream. Stream needs to be
  opened before this function is called. Search begins from the current position. If match is found
  the file pointer is positioned to the start of the next needle.
  On error an exception is thrown.

  \param target Opened file stream to search for.
  \param needle String that should be found
  \retval bool True if needle was found, false if not.
*/
{
    const SIZE_T BMAX = 0x1000;
    char buffer[BMAX];
    streamsize tg;
    SIZE_T br, boffset, total_offset, overlap=0;
    SIZE_T nsize = needle.size();
    if(!target.good())
        throw c4s_exception("search_file: given stream does not have 'good' status.");
    if(nsize >= BMAX)
        throw c4s_exception("search_file: size of search text exceeds internal read buffer size.");
    tg = target.tellg();
    if(tg<0)
        throw c4s_exception("search_file: unable to get file position information.");
    total_offset = SIZE_T(tg);
    do {
        target.read(buffer+overlap,BMAX-overlap);
        br = SIZE_T(target.gcount());
        if(search_bmh((unsigned char*)buffer, br+overlap, (unsigned char*)needle.c_str(), nsize, &boffset)) {
            target.clear();
            target.seekg(total_offset+boffset,ios_base::beg);
            return true;
        }
        total_offset += br;
        memcpy(buffer, buffer+BMAX-nsize, nsize);
        if(!overlap) {
            overlap = nsize;
            total_offset -= nsize;
        }
    }while(!target.eof());
    return false;
}
コード例 #7
0
ファイル: buffer.cpp プロジェクト: systembugtj/crash-report
//这个函数存在问题,不能够处理大于2G的文件。
int CSndBuffer::addBufferFromFile(fstream& ifs, const int& len)
{
   int size = len / m_iMSS;
   if ((len % m_iMSS) != 0)
      size ++;

   // dynamically increase sender buffer
   while (size + m_iCount >= m_iSize)
      increase();

   Block* s = m_pLastBlock;
   int total = 0;
   for (int i = 0; i < size; ++ i)
   {
      if (ifs.bad() || ifs.fail() || ifs.eof())
         break;

      int pktlen = len - i * m_iMSS;
      if (pktlen > m_iMSS)
         pktlen = m_iMSS;

      ifs.read(s->m_pcData, pktlen);
      if ((pktlen = ifs.gcount()) <= 0)
         break;

      s->m_iLength = pktlen;
      s->m_iTTL = -1;
      s = s->m_pNext;

      total += pktlen;
   }
   m_pLastBlock = s;

   CGuard::enterCS(m_BufLock);
   m_iCount += size;
   CGuard::leaveCS(m_BufLock);

   return total;
}
コード例 #8
0
ファイル: crypt.cpp プロジェクト: enejp/ciyam
void create_checksum_test_file( fstream& fs, const string& test_file_name )
{
   fs.seekg( 0, ios::end );
   fstream::pos_type size = fs.tellg( );
   fs.seekg( 0, ios::beg );

   MD5 md5;
   unsigned char buffer[ c_buffer_size ];

   int size_left = ( int )size;
   while( size_left )
   {
      int next_len( min( size_left, c_buffer_size ) );

      fs.read( ( char* )buffer, next_len );

      int len = fs.gcount( );
      md5.update( buffer, len );

      size_left -= next_len;
   }
   md5.finalize( );
   fs.seekg( 0, ios::beg );

   stringstream ss;
   ss << md5.hex_digest( );

   ofstream outf( test_file_name.c_str( ), ios::out | ios::binary );
   if( !outf )
      throw runtime_error( "Unable to open file '" + test_file_name + "' for output." );

   outf << ss.str( );
   outf.flush( );
   if( !outf.good( ) )
      throw runtime_error( "Unexpected error occurred whilst writing to file '" + test_file_name + "'." );
}
コード例 #9
0
ファイル: main.cpp プロジェクト: fracture91/Overlay
//When called in a loop, implements end-host send file functionality
void hostTryToSend(void) {
	static enum {
		STATE_LOOKFORFILE,
		STATE_FOUNDFILE,
		STATE_SENDPACKET,
		STATE_TRYAGAIN,
		STATE_FINISHED,
		STATE_IDLE
	} state = STATE_LOOKFORFILE;
	static fstream sendFile;
	static u_int32_t destIP;
	static u_int16_t srcPort, destPort;
	static u_int8_t buffer[PAYLOAD_LEN] = "";
	static int bufLen;
	static u_int16_t sequence = 0; //host byte order
	static int totalBytes = 0;
	char tempDestIP[32] = "";
	int sendResult;
	
	switch(state) {
		case STATE_LOOKFORFILE:
			sendFile.open("send.txt", fstream::in);
			if(sendFile.is_open()) {
				state = STATE_FOUNDFILE;
			}
			else {
				break;
			}
		case STATE_FOUNDFILE:
			sendFile.getline((char*)buffer, PAYLOAD_LEN-1);
			sscanf((char*)buffer, "%s %hu %hu", tempDestIP, &srcPort, &destPort);
			destIP = strIPtoBin(tempDestIP);
			srcPort = htons(srcPort);
			destPort = htons(destPort);
			state = STATE_SENDPACKET;
		case STATE_SENDPACKET:
			if(sendFile.good()) {
				bufLen = 0;
				memset(buffer, 0, PAYLOAD_LEN);
				
				sendFile.read((char*)buffer, PAYLOAD_LEN);
				bufLen = sendFile.gcount();
				sendResult = hostSendPacket(buffer, bufLen, destIP, srcPort, destPort, htons(sequence));
				if(sendResult == 0) {
					state = STATE_TRYAGAIN;
				}
				else {
					totalBytes += bufLen;
					sequence++;
				}
			}
			else {
				state = STATE_FINISHED;
			}
			break;
		case STATE_TRYAGAIN:
			sendResult = hostSendPacket(buffer, bufLen, destIP, srcPort, destPort, htons(sequence));
			if(sendResult == 1) {
				sequence++;
				totalBytes += bufLen;
				state = STATE_SENDPACKET;
			}
			break;
		case STATE_FINISHED:
			cout<<"File transmitted."<<endl
				<<"Size: "<<totalBytes<<" bytes."<<endl
				<<"Packets trasmitted: "<<sequence<<endl;
			state = STATE_IDLE;
			break;
		case STATE_IDLE:
			break;
	}
}
コード例 #10
0
ファイル: tag_file.cpp プロジェクト: pontocom/opensdrm
size_t RenderV2ToFile(const ID3_TagImpl& tag, fstream& file)
{
  ID3D_NOTICE( "RenderV2ToFile: starting" );
  if (!file)
  {
    ID3D_WARNING( "RenderV2ToFile: error in file" );
    return 0;
  }

  String tagString; 
  io::StringWriter writer(tagString);
  id3::v2::render(writer, tag);
  ID3D_NOTICE( "RenderV2ToFile: rendered v2" );

  const char* tagData = tagString.data();
  size_t tagSize = tagString.size();

  // if the new tag fits perfectly within the old and the old one
  // actually existed (ie this isn't the first tag this file has had)
  if ((!tag.GetPrependedBytes() && !ID3_GetDataSize(tag)) ||
      (tagSize == tag.GetPrependedBytes()))
  {
    file.seekp(0, ios::beg);
    file.write(tagData, tagSize);
  }
  else
  {
    String filename = tag.GetFileName();
#if !defined HAVE_MKSTEMP
    // This section is for Windows folk

    FILE *tempOut = tmpfile();
    if (NULL == tempOut)
    {
      // log this
      return 0;
      //ID3_THROW(ID3E_ReadOnly);
    }
    
    fwrite(tagData, 1, tagSize, tempOut);
    
    file.seekg(tag.GetPrependedBytes(), ios::beg);
    
    uchar tmpBuffer[BUFSIZ];
    while (!file)
    {
      file.read((char *)tmpBuffer, BUFSIZ);
      size_t nBytes = file.gcount();
      fwrite(tmpBuffer, 1, nBytes, tempOut);
    }
    
    rewind(tempOut);
    openWritableFile(filename, file);
    
    while (!feof(tempOut))
    {
      size_t nBytes = fread(tmpBuffer, 1, BUFSIZ, tempOut);
      file.write((char *)tmpBuffer, nBytes);
    }
    
    fclose(tempOut);
    
#else

    // else we gotta make a temp file, copy the tag into it, copy the
    // rest of the old file after the tag, delete the old file, rename
    // this new file to the old file's name and update the handle
    String sTmpSuffix = ".XXXXXX";
    if (filename.size() + sTmpSuffix.size() > ID3_PATH_LENGTH)
    {
      // log this
      return 0;
      //ID3_THROW_DESC(ID3E_NoFile, "filename too long");
    }
    char sTempFile[ID3_PATH_LENGTH];
    strcpy(sTempFile, filename.c_str());
    strcat(sTempFile, sTmpSuffix.c_str());
    
    int fd = mkstemp(sTempFile);
    if (fd < 0)
    {
      remove(sTempFile);
      //ID3_THROW_DESC(ID3E_NoFile, "couldn't open temp file");
    }

    ofstream tmpOut(fd);
    if (!tmpOut)
    {
      tmpOut.close();
      remove(sTempFile);
      return 0;
      // log this
      //ID3_THROW(ID3E_ReadOnly);
    }

    tmpOut.write(tagData, tagSize);
    file.seekg(tag.GetPrependedBytes(), ios::beg);
    uchar tmpBuffer[BUFSIZ];
    while (file)
    {
      file.read(tmpBuffer, BUFSIZ);
      size_t nBytes = file.gcount();
      tmpOut.write(tmpBuffer, nBytes);
    }
      
    tmpOut.close();

    file.close();

    remove(filename.c_str());
    rename(sTempFile, filename.c_str());

    openWritableFile(filename, file);
#endif
  }

  return tagSize;
}
コード例 #11
0
ファイル: tag_file.cpp プロジェクト: Johnny-Martin/toys
size_t RenderV2ToFile(const ID3_TagImpl& tag, fstream& file)
{
  ID3D_NOTICE( "RenderV2ToFile: starting" );
  if (!file)
  {
    ID3D_WARNING( "RenderV2ToFile: error in file" );
    return 0;
  }

  String tagString;
  io::StringWriter writer(tagString);
  id3::v2::render(writer, tag);
  ID3D_NOTICE( "RenderV2ToFile: rendered v2" );

  const char* tagData = tagString.data();
  size_t tagSize = tagString.size();
  // if the new tag fits perfectly within the old and the old one
  // actually existed (ie this isn't the first tag this file has had)
  if ((!tag.GetPrependedBytes() && !ID3_GetDataSize(tag)) ||
      (tagSize == tag.GetPrependedBytes()))
  {
    file.seekp(0, ios::beg);
    file.write(tagData, tagSize);
  }
  else
  {
    String filename = tag.GetFileName();
    String sTmpSuffix = ".XXXXXX";
    if (filename.size() + sTmpSuffix.size() > ID3_PATH_LENGTH)
    {
      // log this
      return 0;
      //ID3_THROW_DESC(ID3E_NoFile, "filename too long");
    }
    char sTempFile[ID3_PATH_LENGTH];
    strcpy(sTempFile, filename.c_str());
    strcat(sTempFile, sTmpSuffix.c_str());

#if ((defined(__GNUC__) && __GNUC__ >= 3  ) || !defined(HAVE_MKSTEMP))
    // This section is for Windows folk && gcc 3.x folk
    fstream tmpOut;
    createFile(sTempFile, tmpOut);

    tmpOut.write(tagData, tagSize);
    file.seekg(tag.GetPrependedBytes(), ios::beg);
    char *tmpBuffer[BUFSIZ];
    while (!file.eof())
    {
      file.read((char *)tmpBuffer, BUFSIZ);
      size_t nBytes = file.gcount();
      tmpOut.write((char *)tmpBuffer, nBytes);
    }

#else //((defined(__GNUC__) && __GNUC__ >= 3  ) || !defined(HAVE_MKSTEMP))

    // else we gotta make a temp file, copy the tag into it, copy the
    // rest of the old file after the tag, delete the old file, rename
    // this new file to the old file's name and update the handle

    int fd = mkstemp(sTempFile);
    if (fd < 0)
    {
      remove(sTempFile);
      //ID3_THROW_DESC(ID3E_NoFile, "couldn't open temp file");
    }

    ofstream tmpOut(fd);
    if (!tmpOut)
    {
      tmpOut.close();
      remove(sTempFile);
      return 0;
      // log this
      //ID3_THROW(ID3E_ReadOnly);
    }

    tmpOut.write(tagData, tagSize);
    file.seekg(tag.GetPrependedBytes(), ios::beg);
    uchar tmpBuffer[BUFSIZ];
    while (file)
    {
      file.read(tmpBuffer, BUFSIZ);
      size_t nBytes = file.gcount();
      tmpOut.write(tmpBuffer, nBytes);
    }

    close(fd); //closes the file

#endif ////((defined(__GNUC__) && __GNUC__ >= 3  ) || !defined(HAVE_MKSTEMP))

    tmpOut.close();
    file.close();

    // the following sets the permissions of the new file
    // to be the same as the original
#if defined(HAVE_SYS_STAT_H)
    struct stat fileStat;
    if(stat(filename.c_str(), &fileStat) == 0)
    {
#endif //defined(HAVE_SYS_STAT_H)
      remove(filename.c_str());
      rename(sTempFile, filename.c_str());
#if defined(HAVE_SYS_STAT_H)
      chmod(filename.c_str(), fileStat.st_mode);
    }
#endif //defined(HAVE_SYS_STAT_H)

//    file = tmpOut;
    file.clear();//to clear the eof mark
    openWritableFile(filename, file);
  }

  return tagSize;
}