Ejemplo n.º 1
0
//////////////////////////////////////////////////////////////////////////////////////////
// Set the new absolute write position
XsensResultValue Cmt1f::setWritePos(const CmtFilePos pos)
{
	if (!m_isOpen)
		return m_lastResult = XRV_NOFILEOPEN;
	if (m_readOnly)
		return m_lastResult = XRV_READONLY;

	if (pos == -1)
	{
		if (m_reading)
			m_reading = false;
		FSEEK_R(0);
		m_writePos = FTELL();
	}
	else
	{
		if (m_writePos != pos)
		{
			m_writePos = pos;
			if (!m_reading)
				FSEEK(m_writePos);
		}
	}

	return m_lastResult = XRV_OK;
}
Ejemplo n.º 2
0
//////////////////////////////////////////////////////////////////////////////////////////
// Open a file.
XsensResultValue Cmt1f::open(const char* filename, const bool create, const bool readOnly)
{
	if (m_isOpen)
		return m_lastResult = XRV_ALREADYOPEN;

	//! \test does this work for non-existing files? Or do we need a check and create?
	m_readOnly = readOnly;
	if (readOnly)
		m_handle = fopen(filename, "rb");	// open for read only (r)
	else
		m_handle = fopen(filename, "r+b");	// open for update (r/w)
	if (m_handle == NULL)
	{
		if (create)
			m_handle = fopen(filename, "w+b");	// create for update (r/w)
		else
		{
			m_handle = fopen(filename, "rb");	// open for read only (r)
			m_readOnly = true;
		}
	}
	if (m_handle == NULL)
		return m_lastResult = XRV_INPUTCANNOTBEOPENED;

	#ifdef _WIN32
		if (_fullpath(m_filename,filename,CMT_MAX_FILENAME_LENGTH) == NULL)
		{
			fclose(m_handle);
			return m_lastResult = XRV_INVALIDPARAM;
		}
	#else
	    // use the same trick again.
		if (realpath(filename, m_filename) == NULL)
		{
		    fclose(m_handle);
		    return m_lastResult = XRV_INVALIDPARAM;
		}
	#endif
	mbstowcs(m_filename_w,m_filename,CMT_MAX_FILENAME_LENGTH);
	m_unicode = false;

	m_isOpen = true;
	m_readPos = 0;
	m_writePos = 0;
	m_reading = true;
	FSEEK_R(0);
	m_fileSize = FTELL();
	FSEEK(0);
	return (m_lastResult = XRV_OK);
}
Ejemplo n.º 3
0
//////////////////////////////////////////////////////////////////////////////////////////
// Open a file.
XsensResultValue Cmt1f::open(const wchar_t* filename, const bool create, const bool readOnly)
{
	if (m_isOpen)
		return m_lastResult = XRV_ALREADYOPEN;

#ifdef _WIN32
	//! \test does this work for non-existing files? Or do we need a check and create?
	m_readOnly = readOnly;
	if (readOnly)
		m_handle = _wfopen(filename, L"rb");	// open for read only (r)
	else
		m_handle = _wfopen(filename, L"r+b");	// open for update (r/w)
	if (m_handle == NULL)
	{
		if (create)
			m_handle = _wfopen(filename, L"w+b");	// create for update (r/w)
		else
		{
			m_handle = _wfopen(filename, L"rb");	// open for read only (r)
			m_readOnly = true;
		}
	}
	if (m_handle == NULL)
		return m_lastResult = XRV_INPUTCANNOTBEOPENED;

	if (_wfullpath(m_filename_w,filename,CMT_MAX_FILENAME_LENGTH) == NULL)
	{
		fclose(m_handle);
		return m_lastResult = XRV_INVALIDPARAM;
	}
	wcstombs(m_filename,m_filename_w,CMT_MAX_FILENAME_LENGTH);

	m_isOpen = true;
	m_readPos = 0;
	m_writePos = 0;
	m_reading = true;
	FSEEK_R(0);
	m_fileSize = FTELL();
	FSEEK(0);
#else
	char tFilename[CMT_MAX_FILENAME_LENGTH*2];
	wcstombs(tFilename,filename,CMT_MAX_FILENAME_LENGTH*2);
	XsensResultValue res = open(tFilename,create,readOnly);
	if (res != XRV_OK)
		return res;
#endif
	m_unicode = true;
	return m_lastResult = XRV_OK;
}
Ejemplo n.º 4
0
//////////////////////////////////////////////////////////////////////////////////////////
// Write data to the end of the file.
XsensResultValue Cmt1f::appendData(const uint32_t length, const void* data)
{
	if (!m_isOpen) return m_lastResult = XRV_NOFILEOPEN;
	if (m_readOnly) return m_lastResult = XRV_READONLY;

	if (m_reading || m_writePos != m_fileSize)
	{
		m_reading = false;
		FSEEK_R(0);
	}
	fwrite(data, 1, length, m_handle);
	m_writePos = FTELL();
	m_fileSize = m_writePos;

	return (m_lastResult = XRV_OK);
}
Ejemplo n.º 5
0
/*! \brief Write data to the end of the file.
	\details The function writes the given data to the file at the end. The
   current write
	position is also moved to the end of the file.
	\param bdata The byte data to append to the file
	\returns XRV_OK if the write was successful
*/
XsResultValue IoInterfaceFile::appendData(const XsByteArray& bdata)
{
	if (!m_handle) return m_lastResult = XRV_NOFILEOPEN;
	if (m_readOnly) return m_lastResult = XRV_READONLY;
	if (!bdata.size()) return m_lastResult = XRV_OK;

	if (m_reading || m_writePos != m_fileSize)
	{
		m_reading = false;
		FSEEK_R(0);  // lint !e534
	}
	size_t bytesWritten = fwrite(bdata.data(), 1, bdata.size(), m_handle);
	(void)bytesWritten;
	m_writePos = FTELL();
	m_fileSize = m_writePos;

	return (m_lastResult = XRV_OK);
}