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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Abstraction for "an environment" holding pairs of C strings (name, value)
22 TPtrC16 TEnvVar::ValuePtr(const wchar_t* aValue)
24 // Turn a zero-terminated string into a descriptor which contains the terminator
27 TText16* value = (TText16*)aValue;
28 return TPtrC16(value, User::StringLength(value)+1);
31 void TEnvVar::ConstructL(const TDesC16& aName, const wchar_t* aValue)
33 TPtrC16 valueZ = ValuePtr(aValue);
34 HBufC16* valueCopy = valueZ.AllocLC();
35 iName = aName.AllocL();
40 void TEnvVar::Release()
48 TInt TEnvVar::SetValue(const wchar_t* aValue)
50 // Change the value - if this returns an error then the original value is still intact
53 TPtrC16 valueZ = ValuePtr(aValue);
54 HBufC16* valueCopy=valueZ.Alloc();
63 HBufC16* TEnvVar::Match(const TDesC16& aName)
65 // return value if the name matches
68 if (iName!=0 && iName->Compare(aName)==0)
73 TUint TEnvVar::Length() const
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'
81 return iName->Length()+iValue->Length()+2;
84 TInt TEnvVar::Externalize(const wchar_t* aPair, TDes16& aBuffer)
86 // static function to externalize a "name=value" definition
89 const wchar_t* cp=aPair;
90 for (cp=aPair; *cp; ++cp)
96 return 0; // malformed
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));
107 TInt TEnvVar::Externalize(TDes16& aBuffer)
111 aBuffer.Append(*iName);
112 aBuffer.Append(TChar(0));
113 aBuffer.Append(*iValue);
114 aBuffer.Append(TChar(0));
118 void TEnvVar::ConstructL(const TText16*& aPtr)
121 aPtr+=name.Length()+1;
122 ConstructL(name,(const wchar_t*)aPtr);
123 aPtr+=iValue->Length()+1;
126 CEnvironment::~CEnvironment()
131 iVars[iCount].Release();
137 void CEnvironment::ConstructL(TUint aCount, TDes16& aBuffer)
139 // Set up the environment from a descriptor. If this leaves then
140 // the CEnvironment destructor will be able to clean up properly.
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);
149 const TText16* data=aBuffer.Ptr();
150 for (TUint i=0; i<aCount; ++i)
151 iVars[i].ConstructL(data);
154 HBufC16* CEnvironment::ExternalizeLC(TUint& aCount, wchar_t** envp)
157 return ExternalizeLC(aCount); // get current environment
158 // Externalize supplied environment
161 for (ep=envp; *ep; ++ep)
162 length+=wcslen(*ep)+1;
163 HBufC16* retBuf = HBufC16::NewLC(length);
165 TPtr16 hbuf16=retBuf->Des();
166 for (ep=envp; *ep; ++ep)
167 nvars+=TEnvVar::Externalize(*ep, hbuf16);
172 HBufC16* CEnvironment::ExternalizeLC(TUint& aCount)
176 for (i=0; i<iCount; ++i)
177 length+=iVars[i].Length();
178 HBufC16* retBuf = HBufC16::NewLC(length);
180 TPtr16 hbuf16=retBuf->Des();
181 for (i=0; i<iCount; i++)
182 nvars+=iVars[i].Externalize(hbuf16);
187 // The interface used to support the STDLIB routines
189 wchar_t * CEnvironment::getenv(const wchar_t* aName) const
191 TPtrC16 name((TText16*)aName);
192 for (TUint i=0; i<iCount; ++i)
194 HBufC16* value=iVars[i].Match(name);
197 const wchar_t* vptr = (const wchar_t*)(value->Ptr());
198 return CONST_CAST(wchar_t*,vptr); // I hope nobody modifies this data...
204 int CEnvironment::setenv(const wchar_t* aName, const wchar_t* aValue, int aReplace, int& anErrno)
206 TPtrC16 name((TText16*)aName);
207 TUint freeSlot=iCount;
209 for (TUint i=0; i<iCount; ++i,++ep)
213 const TDesC16* existing=ep->Match(name);
218 ep->SetValue(aValue);
219 return MapError(err,anErrno);
225 if (freeSlot==iCount)
227 // no free slots, time to grow the array
228 ep=(TEnvVar*)User::ReAlloc(iVars,(iCount+1)*sizeof(TEnvVar));
234 TRAPD(ret,iVars[freeSlot].ConstructL(name,aValue));
235 return MapError(ret,anErrno);
238 void CEnvironment::unsetenv(const wchar_t* aName)
240 TPtrC16 name((TText16*)aName);
241 for (TUint i=0; i<iCount; ++i)
243 const TDesC16* value=iVars[i].Match(name);