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