toolsandutils/e32tools/host/h_file.cpp
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifdef __LINUX__
       
    17 #include <unistd.h>
       
    18 #else	// Windows specific
       
    19 #include <io.h>
       
    20 #endif
       
    21 #include <fcntl.h>
       
    22 #include <sys/types.h>
       
    23 #include <sys/stat.h>
       
    24 #include <stdio.h>
       
    25 #include <stdlib.h>
       
    26 #include <errno.h>
       
    27 
       
    28 #include "h_utl.h"
       
    29 
       
    30 TInt HFile::Open(const TText * const aFileName, TInt32 * const aFileHandle)
       
    31 	{
       
    32 #ifdef __LINUX__
       
    33 	TInt32 hFile = open((const char *)aFileName, O_RDONLY );
       
    34 #else
       
    35 	TInt32 hFile = _open((const char *)aFileName, _O_RDONLY | _O_BINARY);
       
    36 #endif
       
    37 
       
    38 	if (hFile == -1)
       
    39 		{
       
    40 		switch (errno)
       
    41 			{
       
    42 		case EACCES:
       
    43 			Print(EError,"Can't open file %s - access violation.\n",aFileName);
       
    44 			break;
       
    45 		case EEXIST:
       
    46 			Print(EAlways,"Tried to create existing file %s\n",aFileName);
       
    47 			break;
       
    48 		case EINVAL:
       
    49 			Print(EError,"Can't open file %s - invalid open flags.\n",aFileName);
       
    50 			break;
       
    51 		case EMFILE:
       
    52 			Print(EError,"Can't open file %s - too many open files.\n",aFileName);
       
    53 			break;
       
    54 		case ENOENT:
       
    55 			Print(EError,"Can't open file %s - file or path not found.\n",aFileName);
       
    56 			break;
       
    57 		default:
       
    58 			Print(EError,"Can't open file %s, error %d (decimal).\n",aFileName,errno);
       
    59 			break;
       
    60 			}
       
    61 		return errno;
       
    62 		}
       
    63  	*aFileHandle = hFile;
       
    64 	return 0;
       
    65 	}	
       
    66 
       
    67 /******************************************************************************/
       
    68 TBool HFile::Read(const TInt32 aFileHandle, TAny * const aBuffer, const TUint32 aCount)
       
    69 	{
       
    70 	TInt32 bytesRead =  _read(aFileHandle, aBuffer, aCount);
       
    71 	if (bytesRead != (TInt32)aCount)
       
    72 		return EFalse;
       
    73 	else
       
    74 		return ETrue;
       
    75 	}  
       
    76 
       
    77 /******************************************************************************/
       
    78 TBool HFile::Seek(const TInt32 aFileHandle, const TUint32 aOffset)
       
    79 	{
       
    80 	TInt32 newPos = _lseek(aFileHandle, aOffset, SEEK_SET);
       
    81 	if (newPos == -1)
       
    82 		return(EFalse);
       
    83 	else
       
    84 		return ETrue;
       
    85 	}
       
    86 
       
    87 /******************************************************************************/
       
    88 TUint32 HFile::GetPos(const TInt32 aFileHandle)
       
    89 	{
       
    90 	TUint32 pos = _lseek(aFileHandle, 0, SEEK_CUR);
       
    91 	return pos;
       
    92 	}
       
    93 
       
    94 /******************************************************************************/
       
    95 TUint32 HFile::GetLength(TText *aName)
       
    96 	{
       
    97 	TInt32 handle;
       
    98 	if (HFile::Open(aName, &handle)==0)
       
    99 		{
       
   100 		TUint32 size = _filelength(handle);
       
   101 		HFile::Close(handle);
       
   102 		return size;
       
   103 		}
       
   104 	else
       
   105 		return 0;
       
   106 	}
       
   107 
       
   108 /******************************************************************************/
       
   109 TUint32 HFile::GetLength(const TInt32 aFileHandle)
       
   110 	{
       
   111 
       
   112 	TUint32 size = _filelength(aFileHandle);
       
   113 	return size;
       
   114 	}
       
   115 
       
   116 
       
   117 /******************************************************************************/
       
   118 void HFile::Close(const TInt32 aFileHandle)
       
   119 	{
       
   120 	_close(aFileHandle);
       
   121 	}
       
   122 
       
   123 
       
   124 /******************************************************************************/
       
   125 TUint32 HFile::Read(TText *aName, TAny *someMem)
       
   126  	{
       
   127 	TInt32 handle;
       
   128 	if (HFile::Open(aName, &handle)==0)
       
   129 		{
       
   130 		TUint32 size = HFile::GetLength(handle);
       
   131 		if (HFile::Read(handle, someMem, size))
       
   132 			{
       
   133 			HFile::Close(handle);
       
   134 			return size;
       
   135 			}
       
   136 		else 
       
   137 			{
       
   138 			HFile::Close(handle);
       
   139 			return 0;
       
   140 			}
       
   141 		}
       
   142 	else
       
   143 		return 0;
       
   144 	}
       
   145 
       
   146 
       
   147 /******************************************************************************/