sl@0: /* sl@0: * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: template sl@0: inline REncodeWriteStream& operator<<(REncodeWriteStream& aStream,const T& anObject) sl@0: { sl@0: const std::string &name = anObject.Name(); sl@0: sl@0: if(!aStream.Quiet()) sl@0: { sl@0: // Generate human readable progress sl@0: REncodeWriteStream tmp(prog); sl@0: tmp << anObject; sl@0: } sl@0: sl@0: if(aStream.HumanReadable()) sl@0: { sl@0: if(anObject.CommentOnlyInHumanMode() && !aStream.Verbose()) sl@0: { sl@0: return aStream; sl@0: } sl@0: sl@0: aStream.WriteIndent(); sl@0: if(anObject.CommentOnlyInHumanMode()) sl@0: { sl@0: aStream.WriteByte('#'); sl@0: } sl@0: aStream.WriteBin(name.data(), name.size()); sl@0: aStream.WriteSpace(); sl@0: EncodeHuman(aStream, anObject.Value()); sl@0: aStream.WriteLineEnd(); sl@0: } sl@0: else sl@0: { sl@0: // Just write as binary "store" format sl@0: aStream.StoreWriteStream() << anObject.Value(); sl@0: } sl@0: return aStream; sl@0: } sl@0: sl@0: template sl@0: REncodeWriteStream& operator<<(REncodeWriteStream& aStream, const EncDecEnum &aEncDecEnum) sl@0: { sl@0: const std::string &name = aEncDecEnum.Name(); sl@0: sl@0: if(!aStream.Quiet()) sl@0: { sl@0: // Generate human readable progress sl@0: REncodeWriteStream tmp(prog); sl@0: tmp << aEncDecEnum; sl@0: } sl@0: sl@0: if(aStream.HumanReadable()) sl@0: { sl@0: if(aEncDecEnum.CommentOnlyInHumanMode() && !aStream.Verbose()) sl@0: { sl@0: return aStream; sl@0: } sl@0: aStream.WriteIndent(); sl@0: if(aEncDecEnum.CommentOnlyInHumanMode()) sl@0: { sl@0: aStream.WriteByte('#'); sl@0: } sl@0: aStream.WriteBin(name.data(), name.size()); sl@0: aStream.WriteSpace(); sl@0: const char *enumName = aEncDecEnum.ValueName(); sl@0: if(enumName) sl@0: { sl@0: aStream.WriteByte('"'); sl@0: aStream.WriteBin(enumName, strlen(enumName)); sl@0: aStream.WriteByte('"'); sl@0: if(aStream.Verbose()) sl@0: { sl@0: // In verbose mode so include numeric value as a comment sl@0: aStream.WriteCStr(" # "); sl@0: EncodeHuman(aStream, TUint32(aEncDecEnum.Value())); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: // Symbolic value not available, so write as numeric sl@0: EncodeHuman(aStream, TUint32(aEncDecEnum.Value())); sl@0: } sl@0: aStream.WriteLineEnd(); sl@0: } sl@0: else sl@0: { sl@0: // Just write as binary "store" format sl@0: aStream.StoreWriteStream() << aEncDecEnum.Value(); sl@0: } sl@0: return aStream; sl@0: } sl@0: sl@0: sl@0: template sl@0: inline RDecodeReadStream& operator>>(RDecodeReadStream& aStream,T& anObject) sl@0: { sl@0: const std::string &name = anObject.Name(); sl@0: if(aStream.HumanReadable()) sl@0: { sl@0: if(anObject.CommentOnlyInHumanMode()) sl@0: { sl@0: return aStream; // Not accepted in human mode, so just return... sl@0: } sl@0: // Need to convert to ascii sl@0: aStream.CheckName(name); sl@0: DecodeHuman(aStream, anObject.Value()); sl@0: } sl@0: else sl@0: { sl@0: // Just read binary sl@0: aStream.iReadStream >> anObject.Value(); sl@0: } sl@0: sl@0: // Generate human readable progress sl@0: // prog.WriteIndent(); sl@0: REncodeWriteStream tmp(prog); sl@0: tmp << anObject; sl@0: sl@0: return aStream; sl@0: } sl@0: sl@0: template sl@0: RDecodeReadStream& operator>>(RDecodeReadStream& aStream, EncDecEnum &aEncDecEnum) sl@0: { sl@0: const std::string &name = aEncDecEnum.Name(); sl@0: if(aStream.HumanReadable()) sl@0: { sl@0: if(aEncDecEnum.CommentOnlyInHumanMode()) sl@0: { sl@0: return aStream; // Not accepted in human mode, so just return... sl@0: } sl@0: // Need to convert to ascii sl@0: aStream.CheckName(name); sl@0: sl@0: aStream.ReadNextToken(); sl@0: aEncDecEnum.SetValue(aStream.Token().c_str()); sl@0: } sl@0: else sl@0: { sl@0: // Just read binary sl@0: aStream.iReadStream >> aEncDecEnum.Value(); sl@0: } sl@0: sl@0: // Generate human readable progress sl@0: // prog.WriteIndent(); sl@0: REncodeWriteStream tmp(prog); sl@0: tmp << aEncDecEnum; sl@0: sl@0: return aStream; sl@0: } sl@0: sl@0: sl@0: sl@0: template sl@0: EncDecEnum::EncDecEnum(const char *aName, const EnumEntry *aEnumEntries, bool aCommentOnlyInHumanMode) sl@0: : iName(aName), iEnumEntries(aEnumEntries), iCommentOnlyInHumanMode(aCommentOnlyInHumanMode), iValue(0) sl@0: { sl@0: if(iEnumEntries == 0) sl@0: { sl@0: FatalError(); // programing error sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: template sl@0: void EncDecEnum::SetValue(const T &aValue) sl@0: { sl@0: // Search enum entires which one which matches sl@0: if(ValueToName(aValue) == 0) sl@0: { sl@0: dbg << Log::Endl(); sl@0: dbg << Log::Indent() << "Unknown value for enum " << iName << " value " << TUint32(aValue) << Log::Endl(); sl@0: // FatalError(); sl@0: } sl@0: sl@0: iValue = aValue; sl@0: return; sl@0: } sl@0: sl@0: template sl@0: void EncDecEnum::SetValue(const char *aName) sl@0: { sl@0: // Search enum entires which one which matches sl@0: const EnumEntry *p = iEnumEntries; sl@0: while(p && p->iName) sl@0: { sl@0: // We ignore case when matching enums sl@0: if(strcasecmp(aName, p->iName) == 0) sl@0: { sl@0: // Found it sl@0: iValue = p->iValue; sl@0: return; sl@0: } sl@0: ++p; sl@0: } sl@0: sl@0: dbg << Log::Endl(); sl@0: dbg << Log::Indent() << "Unknown value for enum " << iName << " value '" << aName << "'" << Log::Endl(); sl@0: std::string str(aName); sl@0: iValue = ReadUnsignedNumber(str,sizeof(T)); sl@0: } sl@0: sl@0: template sl@0: const char *EncDecEnum::ValueToName(const T &aValue) const sl@0: { sl@0: if(iEnumEntries == 0) FatalError(); sl@0: const EnumEntry *p = iEnumEntries; sl@0: while(p && p->iName) sl@0: { sl@0: if(p->iValue == aValue) sl@0: { sl@0: return p->iName; sl@0: } sl@0: ++p; sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: // end of file sl@0: