xml/cxmllibrary/src/utils/src/cxml_file_if.cpp
branchRCL_3
changeset 33 604ca70b6235
parent 32 889504eac4fb
child 40 80a0ad972c63
equal deleted inserted replaced
32:889504eac4fb 33:604ca70b6235
     1 /*
       
     2 * Copyright (c) 2000 - 2001 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 
       
    20 #include <f32file.h>
       
    21 #include <xml/cxml/cxml_file_if.h>
       
    22 #include <xml/cxml/nw_string_char.h>
       
    23 
       
    24 
       
    25 //
       
    26 //CXML_Read_From_File()
       
    27 //
       
    28 
       
    29 EXPORT_C
       
    30 NW_Status_t CXML_Read_From_File(
       
    31 				   NW_Byte* input_file,NW_Uint8** buffer, NW_Int32* bufferLen)
       
    32 {
       
    33   RFile file;
       
    34   RFs aSession;
       
    35   TInt fileErr;
       
    36   CXML_Uint32 fileNameByteCnt;
       
    37   TInt fileNameLen; 
       
    38   NW_Status_t retVal =  NW_STAT_SUCCESS;
       
    39   TUint16* fileName;
       
    40   TInt i;
       
    41   TInt err = KErrNone;
       
    42   HBufC8*  inBuf;
       
    43   CXML_Uint8* bufPtr;
       
    44   TPtr8 inBufDes(NULL,0);
       
    45 
       
    46 
       
    47   if( (input_file == NULL) || (buffer == NULL) || (bufferLen == NULL) )
       
    48   {
       
    49 	retVal = NW_STAT_FAILURE;
       
    50 	return retVal;
       
    51   }
       
    52 
       
    53   *bufferLen = -1; //Default value
       
    54 
       
    55   err = aSession.Connect();
       
    56   if(KErrNone != err)
       
    57   {
       
    58 	retVal = NW_STAT_FAILURE;
       
    59 	return retVal;  	
       
    60   }
       
    61   fileNameLen = NW_String_charBuffGetLength(input_file,HTTP_us_ascii,&fileNameByteCnt);
       
    62 
       
    63   
       
    64   //This is required for TPtrC
       
    65 
       
    66   fileName = new TUint16[fileNameLen];
       
    67 
       
    68   if(fileName == NULL)
       
    69   {
       
    70    retVal = NW_STAT_OUT_OF_MEMORY;
       
    71    return retVal;
       
    72   }
       
    73 
       
    74 
       
    75   for(i=0; i < fileNameLen; i++)
       
    76   {
       
    77 	  fileName[i] = input_file[i];
       
    78   }
       
    79 
       
    80   TPtrC inFileN(fileName,fileNameLen);
       
    81 
       
    82   fileErr = file.Open(aSession,inFileN,EFileRead);
       
    83 
       
    84   if ( fileErr != KErrNone )	
       
    85   {
       
    86     retVal = NW_STAT_FILE_NOT_OPEN; 
       
    87 	delete[] fileName;
       
    88 	return retVal;
       
    89   }
       
    90   
       
    91 
       
    92     TInt fileSize;
       
    93 	
       
    94   //Get File Size
       
    95 
       
    96   fileErr = file.Size( fileSize );
       
    97 
       
    98   if(fileErr != KErrNone )	
       
    99   {
       
   100     retVal = NW_STAT_UNEXPECTED_ERROR;
       
   101 	delete[] fileName;
       
   102 	file.Close();
       
   103 	return retVal;
       
   104   }
       
   105 
       
   106   *bufferLen = fileSize;
       
   107 
       
   108   // Allocate buffer to read the cXML FILE contents
       
   109 
       
   110    inBuf = HBufC8::New(fileSize);
       
   111 
       
   112    //Get the pointer to heap object
       
   113 
       
   114    inBufDes.Set(inBuf->Des()) ;
       
   115 
       
   116   if(inBuf)
       
   117   {
       
   118 	  
       
   119 	  //Now read the file
       
   120 
       
   121       fileErr = file.Read(inBufDes);
       
   122 
       
   123 	  
       
   124      if(fileErr != KErrNone )	
       
   125 	 {
       
   126       delete inBuf;  //TODO: Change to more meaningful error
       
   127 	  delete[] fileName;
       
   128 	  file.Close();
       
   129 	  *bufferLen = -1;
       
   130 	  retVal = NW_STAT_FILE_READ_ERROR;
       
   131 	  return retVal;	
       
   132 	 }
       
   133   }
       
   134   else
       
   135   {
       
   136 	delete[] fileName;
       
   137 	file.Close();
       
   138 	*bufferLen = -1;
       
   139 	retVal = NW_STAT_FAILURE;
       
   140 	return retVal;
       
   141   }
       
   142 
       
   143   
       
   144   //Allocate the buffer and copy the contents to this
       
   145 
       
   146   *buffer = new CXML_Byte[fileSize];
       
   147 
       
   148 
       
   149   if(*buffer == NULL)
       
   150   {
       
   151    retVal = NW_STAT_OUT_OF_MEMORY;
       
   152    return retVal;
       
   153   }
       
   154 
       
   155 
       
   156   bufPtr = (CXML_Byte*) inBuf->Ptr(); //Get pointer to the data
       
   157 
       
   158    for(i=0; i < fileSize; i++)
       
   159 	{
       
   160 	  (*buffer)[i] = bufPtr[i];
       
   161 	}
       
   162 
       
   163   delete inBuf;
       
   164   delete[] fileName;
       
   165   file.Close();
       
   166   aSession.Close();
       
   167   return retVal;
       
   168  
       
   169 }//end CXMLTestParser::CXML_Read_From_File()