Пример #1
0
bool CFile::Duplicate(const std::string& newFileName)
{
  if (IsOpen() == false)
  {
    return false;
  }

  if (m_fileName.compare(newFileName) == 0)
  {
    return false;
  }

  Flush();

  long curPos = GetPosition();

  SeekToBegin();

  long  dupLength = GetLength();

  CFile dupFile(newFileName, modeWrite);

  if( dupFile.IsOpen() == false)
  {
    return false;
  }

  char *buf = new char[dupLength];

  if (buf == NULL)
  {
    return false;
  }

  uint32_t written = 0;

  if (ReadToBuffer(buf, dupLength) != 0)
  {
    written = dupFile.WriteFromBuffer(buf, dupLength);
    dupFile.Destroy();
  }

  delete []buf;
  buf = NULL;

  SeekToBegin();
  SetPosition(curPos);

  return (written != 0);
}
Пример #2
0
//--------------------------------------------------------------------------------
bool CSettingsFile::GetLine(int nIndex, CStringArray& sInfo)
	{
	sInfo.RemoveAll();
	if(m_nCurLine == -1 || nIndex < m_nCurLine)
		{
		SeekToBegin();
		m_nCurLine = 0;
		}

	while(ReadLine(sInfo))
		{
		if(sInfo.GetSize() < GetMinSize())
			{
			sInfo.RemoveAll();
			continue;
			}

		if(m_nCurLine == nIndex)
			{
			m_nCurLine++;
			return true;
			}
		
		sInfo.RemoveAll();
		m_nCurLine++;
		}

	return false;
	}
Пример #3
0
bool CScript::FindTextHeader( LPCTSTR pszName ) // Find a section in the current script
{
	ADDTOCALLSTACK("CScript::FindTextHeader");
	// RETURN: false = EOF reached.
	ASSERT(pszName);
	ASSERT( ! IsBinaryMode());

	SeekToBegin();

	size_t len = strlen( pszName );
	ASSERT(len);
	do
	{
		if ( ! ReadTextLine(false))
		{
			return( false );
		}
		if ( IsKeyHead( "[EOF]", 5 ))
		{
			return( false );
		}
	}
	while ( ! IsKeyHead( pszName, len ));
	return( true );
}
Пример #4
0
 void File::FileImpl::SeekTo(unsigned long pos)
 {
   if (!pos)
   {
     SeekToBegin();
     return;
   }
   if (SetFilePointer(FileHandle, static_cast<LONG>(pos), 0, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
     throw FileException("Can't seek to mew position");
 }
Пример #5
0
void CStkFile::ReMapFromBegin(int nCount)
{
	if (m_bShareMem == FALSE && m_hFile == (HANDLE)hFileNull || m_hFileMap == NULL)
	{
		ASSERT(FALSE);
		return;
	}

	SeekToBegin();
	ReMap(nCount);
}
Пример #6
0
void CStkFile::SetStockNumber(int nStock)
{
	if (m_bShareMem == FALSE && m_hFile == (HANDLE)hFileNull || m_hFileMap == NULL)
	{
		ASSERT(FALSE);
		return ;
	}

	SeekToBegin();
	Write(&nStock, 4);
}
Пример #7
0
int CStkFile::GetStockNumber()
{
	if (m_bShareMem == FALSE && m_hFile == (HANDLE)hFileNull || m_hFileMap == NULL)
	{
		ASSERT(FALSE);
		return 0;
	}

	int nData;
	SeekToBegin();
	Read(&nData, 4);

	return nData;
}
BOOL COXRegistryValFile::Open(HKEY hkey, LPCTSTR lpszKey, LPCTSTR lpszValue, LONG& error)
	{
	ASSERT(AfxIsValidString(lpszKey));
	ASSERT(AfxIsValidString(lpszValue));
	m_value = lpszValue;

	error = ::RegCreateKey(hkey, lpszKey, &m_key);
	if (ERROR_SUCCESS != error)
		return FALSE;
	DWORD dwType;
	DWORD dwSize;
	if (ERROR_SUCCESS == ::RegQueryValueEx(m_key, m_value, NULL, &dwType, NULL, &dwSize))
		{
		// the value already exists, check the type
		if (dwType != REG_BINARY)
			{
			error = 0;		// wrong type exists
			return FALSE;
			}

		// the value exists and has the right type
		BYTE * pData = NULL;
		try
			{
			pData = new BYTE[dwSize];
			}
		catch(...)
			{
			error = 0;		// memory low
			return FALSE;
			}

		error = ::RegQueryValueEx(m_key, m_value, NULL, &dwType, pData, &dwSize);
		if (ERROR_SUCCESS != error)
			{
			delete [] pData;
			return FALSE;
			}

		Write(pData, dwSize);
		SeekToBegin();

		delete [] pData;
		return TRUE;
		}

	error = 0;
	return TRUE;
	}
Пример #9
0
BOOL CFileOpt::FilePointerSeek( UINT nMethod, LONG nCurrent )
{
	DWORD dwErrCode = 0;

	if ( 0 == nCurrent )
	{
		switch ( nMethod )
		{
		case 1:                    // 文件首
			SeekToBegin();
			break;
		case 2:                    // 文件尾
			SeekToEnd();
			break;
		}
		dwErrCode = GetLastError();
	}
	else
	{
		switch ( nMethod )
		{
		case 1:
			Seek( nCurrent, CFile::begin );
			break;
		case 2:
			Seek( nCurrent, CFile::end );
			break;
		}
		dwErrCode = GetLastError();
	}

	if ( 0 == dwErrCode )   // 设置文件成功
	{
		return TRUE;
	}

	if ( m_bDebug )                     // 是调试模式
	{
		ErrorMessageBox( dwErrCode );
	}
	
	return FALSE;
}
Пример #10
0
bool CGFile::CopyFileTo( LPCTSTR pszDstFileName )
{
	if ( ! IsFileOpen())
	{
		if ( ! Open())
			return( false );
	}
	else
	{
		SeekToBegin();
	}

	CGFile sDst;
	if ( ! sDst.Open( pszDstFileName, OF_WRITE|OF_CREATE|OF_BINARY ))
	{
		return( false );
	}

	BYTE * pData = new BYTE [ 32 * 1024 ];	// temporary buffer.
	ASSERT(pData);
	bool fSuccess = true;

	while ( true )
	{
		size_t iSize = Read( pData, 32 * 1024 );
		if ( iSize < 0 )
		{
			fSuccess = false;
			break;
		}
		if ( iSize == 0 )
			break;
		sDst.Write( pData, iSize );
		if ( iSize < 32 * 1024 )
			break;
	}

	delete [] pData;
	return( fSuccess );
}
Пример #11
0
bool CFile::Open(const char * lpszFileName, uint32_t nOpenFlags)
{
    if (!Close())
    {
        return false;
    }

    if (lpszFileName == NULL || strlen(lpszFileName) == 0)
    {
        return false;
    }

    m_bCloseOnDelete = true;
#ifdef USE_WINDOWS_API
    m_hFile = INVALID_HANDLE_VALUE;

    ULONG dwAccess = 0;
    switch (nOpenFlags & 3)
    {
    case modeRead:
        dwAccess = GENERIC_READ;
        break;
    case modeWrite:
        dwAccess = GENERIC_WRITE;
        break;
    case modeReadWrite:
        dwAccess = GENERIC_READ | GENERIC_WRITE;
        break;
    default:
        _ASSERTE(false);
    }

    // map share mode
    ULONG dwShareMode = 0;

    dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
    if ((nOpenFlags & shareDenyWrite) == shareDenyWrite) { dwShareMode &= ~FILE_SHARE_WRITE; }
    if ((nOpenFlags & shareDenyRead) == shareDenyRead)   { dwShareMode &= ~FILE_SHARE_READ; }
    if ((nOpenFlags & shareExclusive) == shareExclusive) { dwShareMode = 0; }

    // map modeNoInherit flag
    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = (nOpenFlags & modeNoInherit) == 0;

    // map creation flags
    ULONG dwCreateFlag = OPEN_EXISTING;
    if (nOpenFlags & modeCreate)
    {
        dwCreateFlag = ((nOpenFlags & modeNoTruncate) != 0) ? OPEN_ALWAYS : CREATE_ALWAYS;
    }

    // attempt file creation
    HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa, dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    { //#define ERROR_PATH_NOT_FOUND             3L
        //ULONG err = GetLastError();
        return false;
    }
    m_hFile = hFile;
#else

    if ((nOpenFlags & CFileBase::modeCreate) != CFileBase::modeCreate)
    {
        printf("Checking if %s exists...\n",lpszFileName);
        if (!CPath(lpszFileName).Exists())
        {
            printf("%s does not exist.\n",lpszFileName);
            return false;
        }
    }

    if ((nOpenFlags & CFileBase::modeCreate) == CFileBase::modeCreate)
    {
        CPath file(lpszFileName);
        if (!file.Exists())
        {
            FILE * fp = fopen(lpszFileName,"wb");
            if (fp)
            {
                fclose(fp);
            }
            if (!file.Exists())
            {
                return false;
            }
        }
    }

    if ((nOpenFlags & CFileBase::modeWrite) == CFileBase::modeWrite ||
        (nOpenFlags & CFileBase::modeReadWrite) == CFileBase::modeReadWrite)
    {
        printf("Trying to open %s (rb+).\n", lpszFileName);
        m_hFile = fopen(lpszFileName, "rb+");
        if (m_hFile != NULL)
        {
            SeekToBegin();
        }
    }
    else if ((nOpenFlags & CFileBase::modeRead) == CFileBase::modeRead)
    {
        printf("Trying to open %s (rb).\n", lpszFileName);
        m_hFile = fopen(lpszFileName, "rb");
        if (m_hFile != NULL)
        {
            SeekToBegin();
        }
    }
    else
    {
        return false;
    }
#endif
    m_bCloseOnDelete = true;
    return true;
}
Пример #12
0
int ringFile::ReadAll(char **ppbuf)
{
	if(m_dwFileSize > RF_READALL_SIZE)
		return RFE_FILETOOBIG;		//文件超过预定全读尺寸,放弃
	
	// 初始化返回数据
	if(ppbuf != 0)
		*ppbuf = NULL;

	m_dwRWNum = 0UL;
	
	// 申请缓冲区
	char	*lpbuf;
	if(m_lpFilebuf && m_dwFileSize > m_bufSize)
		lpbuf = (char*)New(m_dwFileSize + 10);
	else if(m_lpFilebuf == NULL)
	{
		m_lpFilebuf = (char*)New(m_dwFileSize + 10);
		lpbuf = (char*)m_lpFilebuf;
		m_bufSize = m_dwFileSize + 10;
	}
	else
	{
		lpbuf = (char*)m_lpFilebuf;
		memset(lpbuf,0,m_bufSize);
	}

	if(lpbuf == NULL)
		return RFE_MEMERROR;
	
	DWORD	trn;
	DWORD	pos;

	// 保留读写位置
	pos = GetRWLocation();

	SeekToBegin();

	// 读入所有文件内容
	if(!ReadFile(m_hFile,lpbuf,m_dwFileSize,&trn,NULL))
	{
		if(lpbuf != m_lpFilebuf)
			Del(lpbuf);
		return RFE_ERROR;
	}

	Seek(pos);

	if(trn != m_dwFileSize)
	{
		if(lpbuf != m_lpFilebuf)
			Del(lpbuf);
		return RFE_ERROR;
	}

	// 返回数据
	if(lpbuf != m_lpFilebuf)
	{
		if(m_lpFilebuf)
			Del(m_lpFilebuf);
		m_lpFilebuf = (LPVOID)lpbuf;
		m_bufSize = m_dwFileSize;
	}

	if(ppbuf)
	{
		*ppbuf = (char*)New(m_dwFileSize + 10);
		memcpy(*ppbuf,lpbuf,m_dwFileSize);
	}		

	m_dwRWNum = m_dwFileSize;

	return m_dwFileSize;
}
Пример #13
0
//----------------------------------------
bool CFile::Open()
{

  Close();

  char szMode[10];
  memset(szMode, '\0', 10);

  if ((m_mode & modeRead) == modeRead)
  {
    strcpy(szMode, "r");
  }
  else if ((m_mode & modeWrite) == modeWrite)
  {
    strcpy(szMode, "w");
  }
  else if ((m_mode & modeAppend) == modeAppend)
  {
    strcpy(szMode, "a");
  }
  else if ((m_mode & modeReadWrite) == modeReadWrite)
  {
    strcpy(szMode, "r+");
  }
  else if ((m_mode & modeRWCreate) == modeRWCreate)
  {
    strcpy(szMode, "w+");
  }
  else if ((m_mode & modeReadAppend) == modeReadAppend)
  {
    strcpy(szMode, "a+");
  }
  else
  {
    strcpy(szMode, "r");
  }


  if ((m_mode & typeText) == typeText)
  {
    strcat(szMode, "t");
  }
  else
  {
    strcat(szMode, "b");
  }

  if(m_fileName.empty())
  {
    CFileException e("Error opening file -  file name is empty", BRATHL_IO_ERROR);
    CTrace::Tracer("%s", e.what());
    Dump(*CTrace::GetDumpContext());
    throw (e);
  }

  m_hFile = fopen(m_fileName.c_str(), szMode);

  if(m_hFile == NULL)
  {
    std::string msg = CTools::Format("Error while opening file - errno:#%d:%s", errno, strerror(errno));
    CFileException e(msg, m_fileName, BRATHL_IO_ERROR);
    CTrace::Tracer("%s", e.what());
    Dump(*CTrace::GetDumpContext());
    throw (e);
  }

  // Make sure this is set before any I/O or fseek is performed
  SetBufferingMode(1);

  SeekToEnd();
  m_length = ftell(m_hFile);
  SeekToBegin();

  return true;
}