/**************************************************************************
<MODULE NAME>
<FILENAME>CFile.cpp</FILENAME>
<HISTORY>
	<MODIFICATION>
		<DATE></DATE>
		<ATHOR></ATHOR>
		<DESCRIPTION>
		</DESCRIPTION>
	</MODIFICATION>
</HISTORY>
</MODULE NAME>
**************************************************************************/


/**************************************************************************
INCLUDE FILE
**************************************************************************/
#include "..\comp\File\CFile.h"

/**************************************************************************
LOCAL VARIABLES
Always start with « static »-keyword
**************************************************************************/

/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME>CFile</NAME>
	<TYPE> LOCAL  GLOBAL </TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO>IN | OUT | INOUT</ IO >
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
</METHOD>
**************************************************************************/
CFile::CFile()
{
	m_pFile = NULL;
}
/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME>CFile</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION>Constructor and Open file</DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME>pzFileName</NAME>
			<TYPE>char*</TYPE>
			<IO>IN</ IO >
			<DESCRIPTION>
                Name of file,the path included
			</DESCRIPTION>
		</PARAMETER>
		<PARAMETER>
			<NAME>pzOption </NAME>
			<TYPE>char*</TYPE>
			<IO>IN</ IO >
			<DESCRIPTION>
                Option for opening
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
</METHOD>
**************************************************************************/
CFile::CFile(char * pzFileName, char* pzOption )
{
	m_pFile = NULL;
	Open(pzFileName, pzOption);
}

/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME>~CFile</NAME>
	<TYPE> LOCAL | GLOBAL </TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO>IN | OUT | INOUT</ IO >
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
</METHOD>
**************************************************************************/
CFile::~CFile()
{
	if(m_pFile != NULL)
        Close();
}


/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME>Open</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION>Open file</DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME>pzFileName</NAME>
			<TYPE>char*</TYPE>
			<IO>IN</ IO >
			<DESCRIPTION>
                name of the file to be opened. This paramenter must follow
                operating system's specifications and can include a path
                if the system supports it.
			</DESCRIPTION>
		</PARAMETER>
		<PARAMETER>
			<NAME>pzOption </NAME>
			<TYPE>char*</TYPE>
			<IO>IN</ IO >
			<DESCRIPTION>
                type of access requested. It can be:
                 - "r"  Open a file for reading. The file must exist.
                 - "w"  Create an empty file for writing. If a file with the
                    same name already exists its content is erased.
                 - "a"  Append to a file. Writing operations append data at
                    the end of the file. The file is created if it doesn't exist.
                 - "r+" Open a file for reading and writing. The file must exist.
                 - "w+" Create an empty file for reading and writing. If a file
                    with the same name already exists its content is erased
                    before it is opened.
                 - "a+"	Open a file for reading and appending. All writing
                    operations are done at the end of the file protecting
                    the previous content to be overwritten. You can reposition
                    (Seek, Rewind) the pointer to anywhere in the file
                    for reading, but writing operations will move back to
                    the end of file. The file is created if it doesn't exist.
                    The mode parameter serves also to specify whether we want
                    to open the file as text or binary, adding t or b characters
                    to this access mode string.
                 -  t Text mode. In text mode the end of file is assumed
                    to be at first Ctrl-Z character. Some conversions can occur
                    reading and writing with End Of Line / Feedback characters
                    depending on your compiler and your Operating System.
                 -  b Binary mode. End of file is reached at last byte of
                    the file. No conversions.
                This additional t or b characters can be appended to
                the read/write mode at the end of the string (like "rb", "wt",
                "r+t", "w+b"...) or can be inserted between the letter and
                the + character (like "rt+", "wb+").
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE>int</TYPE>
		<DESCRIPTION>
           return 0 if success.
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CFile::Open(char * pzFileName, char* pzOption )
{
    int iResult = 0;
    m_pFile = fopen (pzFileName, pzOption);
    if(m_pFile == NULL)
        iResult = -1;
    return iResult;
}


/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME>Read</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION>allows to reading in an open file</DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME> pvReadByte</NAME>
			<TYPE>void*</TYPE>
			<IO>OUT</ IO >
			<DESCRIPTION>
                Storage of bytes read into opened file
			</DESCRIPTION>
		</PARAMETER>
		<PARAMETER>
			<NAME>lSizeBuffer</NAME>
			<TYPE>long</TYPE>
			<IO>IN</ IO >
			<DESCRIPTION>
                size of buffer (byte)
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE>int</TYPE>
		<DESCRIPTION>
            return 0 if success
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CFile::Read(void* pvReadByte, long lSizeBuffer)
{
	int iResult = -1;

    if(m_pFile != NULL)
    {
        iResult = fread(pvReadByte, 1, lSizeBuffer, m_pFile);
    }
	return iResult;
}

char* CFile::GetLine(char* pvReadByte, long lSizeBuffer)
{
	return fgets((char *)pvReadByte, lSizeBuffer + 1, m_pFile);
}

/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME>Write</NAME>
	<TYPE>LOCAL  GLOBAL</TYPE>
	<DESCRIPTION>Write into opened file</DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME>void* pvWriteByte, long lSizeBuffer</NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</ IO >
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE>int</TYPE>
		<DESCRIPTION>
            return 0 if success
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CFile::Write(void* pvWriteByte, long lSizeBuffer)
{
	int iResult = -1;

    if(m_pFile != NULL)
    {
        iResult = fwrite(pvWriteByte, 1 , lSizeBuffer, m_pFile);
    }
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</ IO >
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CFile::Close()
{
	int iResult = 0;
    if(m_pFile != NULL)
    {
        fclose(m_pFile);
    }
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CFile</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION>
        Reposition stream's position indicator. Sets the position indicator
        associated with the stream to a new position defined by an offset
        from the location given by the origin parameter. The End-Of-File
        indicator of this stream is cleared after a call to fseek. When using
        fseek on files opened in text mode consider that you can only be sure
        of the results if an offset of 0 or an offset equal to a value returned
        from ftell is specified, any other offset value makes result
        unpredictable because of Carriage-Return translations under this mode.
        The next operation with the stream after a call to fseek can be either
        for input or output.
    </DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME>offset</NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</ IO >
			<DESCRIPTION>
                Number of bytes from origin.
			</DESCRIPTION>
		</PARAMETER>
		<PARAMETER>
			<NAME>origin</NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</ IO >
			<DESCRIPTION>
                Initial position from where offset is applied.
                eSeekSet (0) Beginning of file.
                eSeekCur (1) Current position of the file pointer.
                eSeekEnd (2) End of file.
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE>int</TYPE>
		<DESCRIPTION>
            If successful the function returns 0. Otherwise it returns nonzero.
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CFile::Seek (long lOffset , int iOrigin )
{
	int iResult = -1;
    if(m_pFile != NULL)
    {
        iResult = fseek(m_pFile, lOffset, iOrigin);
    }
	return iResult;
}

long CFile::FileSize()
{
    long lResult = -1;
    if(m_pFile != NULL)
    {
        // obtain file size.
      Seek(0 , SEEK_END);
      lResult = ftell(m_pFile);
      rewind(m_pFile);
    }
  return lResult;
}