os/ossrv/genericopenlibs/cstdlib/USTLIB/GETENV.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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 // Abstraction for "an environment" holding pairs of C strings (name, value)
    15 // 
    16 //
    17 
    18 #include "ENVIRON.H"
    19 #include "LPOSIX.H"
    20 #include <string.h>
    21 
    22 TPtrC16 TEnvVar::ValuePtr(const wchar_t* aValue)
    23 //
    24 // Turn a zero-terminated string into a descriptor which contains the terminator
    25 //
    26 	{
    27 	TText16* value = (TText16*)aValue;
    28 	return TPtrC16(value, User::StringLength(value)+1);
    29 	}
    30 
    31 void TEnvVar::ConstructL(const TDesC16& aName, const wchar_t* aValue)
    32 	{
    33 	TPtrC16 valueZ = ValuePtr(aValue);
    34 	HBufC16* valueCopy = valueZ.AllocLC();
    35 	iName = aName.AllocL();
    36 	iValue = valueCopy;
    37 	CleanupStack::Pop();
    38 	}
    39 
    40 void TEnvVar::Release()
    41 	{
    42 	delete iName;
    43 	iName=0;
    44 	delete iValue;
    45 	iValue=0;
    46 	}
    47 
    48 TInt TEnvVar::SetValue(const wchar_t* aValue)
    49 //
    50 // Change the value - if this returns an error then the original value is still intact
    51 //
    52 	{
    53 	TPtrC16 valueZ = ValuePtr(aValue);
    54 	HBufC16* valueCopy=valueZ.Alloc();
    55 	if (valueCopy==0)
    56 		return KErrNoMemory;
    57 	delete iValue;
    58 	iValue = valueCopy;
    59 	return KErrNone;
    60 	}
    61 
    62 
    63 HBufC16* TEnvVar::Match(const TDesC16& aName)
    64 //
    65 // return value if the name matches
    66 //
    67 	{
    68 	if (iName!=0 && iName->Compare(aName)==0)
    69 		return iValue;
    70 	return 0;
    71 	}
    72 
    73 TUint TEnvVar::Length() const
    74 //
    75 // How much space do we need to externalize this pair?
    76 // The format is name followed by '\0' followed by value followed by '\0'
    77 //
    78 	{
    79 	if (iName==0) 
    80 		return 0;
    81 	return iName->Length()+iValue->Length()+2;
    82 	}
    83 
    84 TInt TEnvVar::Externalize(const wchar_t* aPair, TDes16& aBuffer)
    85 //
    86 // static function to externalize a "name=value" definition
    87 //
    88 	{
    89 	const wchar_t* cp=aPair;
    90 	for (cp=aPair; *cp; ++cp)
    91 		{
    92 		if (*cp==L'=')
    93 			break;
    94 		}
    95 	if (*cp!=L'=')
    96 		return 0;	// malformed
    97 
    98 	TPtrC16 name((TText16*)aPair,cp-aPair);
    99 	TPtrC16 value((TText16*)(cp+1));
   100 	aBuffer.Append(name);
   101 	aBuffer.Append(TChar(0));
   102 	aBuffer.Append(value);
   103 	aBuffer.Append(TChar(0));
   104 	return 1;
   105 	}
   106 
   107 TInt TEnvVar::Externalize(TDes16& aBuffer)
   108 	{
   109 	if (iName==0) 
   110 		return 0;
   111 	aBuffer.Append(*iName);
   112 	aBuffer.Append(TChar(0));
   113 	aBuffer.Append(*iValue);
   114 	aBuffer.Append(TChar(0));
   115 	return 1;
   116 	}
   117 
   118 void TEnvVar::ConstructL(const TText16*& aPtr)
   119 	{
   120 	TPtrC16 name(aPtr);
   121 	aPtr+=name.Length()+1;
   122 	ConstructL(name,(const wchar_t*)aPtr);
   123 	aPtr+=iValue->Length()+1;
   124 	}
   125 
   126 CEnvironment::~CEnvironment()
   127 	{
   128 	while (iCount>0)
   129 		{
   130 		--iCount;
   131 		iVars[iCount].Release();
   132 		}
   133 	User::Free(iVars);
   134 	}
   135 
   136 
   137 void CEnvironment::ConstructL(TUint aCount, TDes16& aBuffer)
   138 //
   139 // Set up the environment from a descriptor. If this leaves then
   140 // the CEnvironment destructor will be able to clean up properly.
   141 //
   142 	{
   143 	// always allocate at least one slot - makes life easier elsewhere
   144 	TInt bytes = (aCount+1)*sizeof(TEnvVar);
   145 	iVars=(TEnvVar*) User::AllocL(bytes);
   146 	Mem::FillZ(iVars,bytes);
   147 	iCount=aCount+1;
   148 
   149 	const TText16* data=aBuffer.Ptr();
   150 	for (TUint i=0; i<aCount; ++i)
   151 		iVars[i].ConstructL(data);
   152 	}
   153 
   154 HBufC16* CEnvironment::ExternalizeLC(TUint& aCount, wchar_t** envp)
   155 	{
   156 	if (envp==0)
   157 		return ExternalizeLC(aCount);	// get current environment
   158 	// Externalize supplied environment
   159 	TInt length=0;
   160 	wchar_t** ep=envp;
   161 	for (ep=envp; *ep; ++ep)
   162 		length+=wcslen(*ep)+1;
   163 	HBufC16* retBuf = HBufC16::NewLC(length);
   164 	TInt nvars=0;
   165 	TPtr16 hbuf16=retBuf->Des();
   166 	for (ep=envp; *ep; ++ep)
   167 		nvars+=TEnvVar::Externalize(*ep, hbuf16);
   168 	aCount=nvars;
   169 	return retBuf;
   170 	}
   171 
   172 HBufC16* CEnvironment::ExternalizeLC(TUint& aCount)
   173 	{
   174 	TInt length=0;
   175 	TUint i=0;
   176 	for (i=0; i<iCount; ++i)
   177 		length+=iVars[i].Length();
   178 	HBufC16* retBuf = HBufC16::NewLC(length);
   179 	TInt nvars=0;
   180 	TPtr16 hbuf16=retBuf->Des();
   181 	for (i=0; i<iCount; i++)
   182 		nvars+=iVars[i].Externalize(hbuf16);
   183 	aCount=nvars;
   184 	return retBuf;
   185 	}
   186 
   187 // The interface used to support the STDLIB routines
   188 
   189 wchar_t * CEnvironment::getenv(const wchar_t* aName) const
   190 	{
   191 	TPtrC16 name((TText16*)aName);
   192 	for (TUint i=0; i<iCount; ++i)
   193 		{
   194 		HBufC16* value=iVars[i].Match(name);
   195 		if (value)
   196 			{
   197 			const wchar_t* vptr = (const wchar_t*)(value->Ptr());
   198 			return CONST_CAST(wchar_t*,vptr);	// I hope nobody modifies this data...
   199 			}
   200 		}
   201 	return 0;
   202 	}
   203 
   204 int CEnvironment::setenv(const wchar_t* aName, const wchar_t* aValue, int aReplace, int& anErrno)
   205 	{
   206 	TPtrC16 name((TText16*)aName);
   207 	TUint freeSlot=iCount;
   208 	TEnvVar* ep=iVars;
   209 	for (TUint i=0; i<iCount; ++i,++ep)
   210 		{
   211 		if (ep->NotEmpty())
   212 			{
   213 			const TDesC16* existing=ep->Match(name);
   214 			if (existing)
   215 				{
   216 				TInt err=KErrNone;
   217 				if (aReplace)
   218 					ep->SetValue(aValue);
   219 				return MapError(err,anErrno);
   220 				}
   221 			}
   222 		else
   223 			freeSlot=i;
   224 		}
   225 	if (freeSlot==iCount)	
   226 		{
   227 		// no free slots, time to grow the array
   228 		ep=(TEnvVar*)User::ReAlloc(iVars,(iCount+1)*sizeof(TEnvVar));
   229 		if (ep==0)
   230 			return KErrNoMemory;
   231 		iVars=ep;
   232 		++iCount;
   233 		}
   234 	TRAPD(ret,iVars[freeSlot].ConstructL(name,aValue));
   235 	return MapError(ret,anErrno);
   236 	}
   237 
   238 void CEnvironment::unsetenv(const wchar_t* aName)
   239 	{
   240 	TPtrC16 name((TText16*)aName);
   241 	for (TUint i=0; i<iCount; ++i)
   242 		{
   243 		const TDesC16* value=iVars[i].Match(name);
   244 		if (value)
   245 			{
   246 			iVars[i].Release();
   247 			return;
   248 			}
   249 		}
   250 	}
   251