networkingutils/ipadministrationtool/src/uniload.cpp
changeset 0 9736f095102e
equal deleted inserted replaced
-1:000000000000 0:9736f095102e
       
     1 // Copyright (c) 2004-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 // uniload.cpp - universal file load (work for narrow or wide builds)
       
    15 //
       
    16 
       
    17 #include "uniload.h"
       
    18 
       
    19 HBufC *UnicodeLoad::LoadL(RFs &aFs, const TDesC &aName)
       
    20 	{
       
    21 	RFile pf;
       
    22 	TInt size;
       
    23 	User::LeaveIfError(pf.Open(aFs, aName, EFileShareAny));
       
    24 	pf.Size(size);
       
    25 	//
       
    26 	// For narrow build, this allocates exactly right amount
       
    27 	// of space, for Unicode build, this allocates the size
       
    28 	// doubled in bytes, which is exactly what we want, if
       
    29 	// we need to widen the narrow Ascii control file (this
       
    30 	// is assumed to be the default case). For UNICODE file
       
    31 	// in UNICODE environment, this allocates too much space.
       
    32 	//
       
    33 	HBufC *data = HBufC::New(size);
       
    34 
       
    35 	if (data == NULL)
       
    36 		{
       
    37 		pf.Close();
       
    38 		User::Leave(KErrNoMemory);
       
    39 		}
       
    40 	//
       
    41 	// buf is "native" pointer, either 16 or 8 wide
       
    42 	//
       
    43 	TPtr buf(data->Des());
       
    44 #ifdef __WINS__
       
    45 	buf.Set(data->Des());		// VC6 kludge
       
    46 #endif
       
    47 	//
       
    48 	// ptr is always 8 wide
       
    49 	//
       
    50 	TPtr8 ptr((TUint8 *)buf.Ptr(), size, size);
       
    51 	pf.Read(ptr);
       
    52 	pf.Close();
       
    53 
       
    54 	if (size >= 2 && ptr[0] == 0xFF && ptr[1] == 0xFE)
       
    55 		{
       
    56 		//
       
    57 		// The file is presented as UNICODE text
       
    58 		// (for now, only one byte order supported)
       
    59 		//
       
    60 		size -= 2;
       
    61 #ifdef _UNICODE
       
    62 		Mem::Copy((TUint8 *)ptr.Ptr(), ptr.Ptr() + 2, size);
       
    63 		buf.SetLength(size / 2);	// True number of Unicode characters.
       
    64 #else
       
    65 		// Trying to load a Unicode file for Narrow build
       
    66 		size = size / 2;
       
    67 		for (TInt i = 1; i < size; ++i)
       
    68 			{
       
    69 			if (ptr[2*i] != 0)
       
    70 				ptr[i] = 0x01;	// Substitute too wide stuff with 0x1
       
    71 			else
       
    72 				ptr[i] = ptr[2*i + 1];
       
    73 			}
       
    74 		buf.SetLength(size); 
       
    75 #endif
       
    76 		}
       
    77 	else
       
    78 		{
       
    79 		//
       
    80 		// The file is narrow, 8 bit characters
       
    81 		//
       
    82 		buf.SetLength(size); 
       
    83 #ifdef _UNICODE
       
    84 		// Must "widen" the characters for unicode.
       
    85 		for (TInt i = size; --i >= 0; )
       
    86 			buf[i] = ptr[i];
       
    87 #endif
       
    88 		}
       
    89 	return data;
       
    90 	}