bintools/rcomp/src/RESOURCE.CPP
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 1997-2009 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 #include <assert.h>
       
    20 #include <stdlib.h>
       
    21 #include <stdio.h>
       
    22 #include "RESOURCE.H"
       
    23 #include "TOKENS.H"  
       
    24 #include "ERRORHAN.H"
       
    25 
       
    26 // ResourceItem
       
    27 
       
    28 ResourceItem::ResourceItem(const String& aLabelToSet, int aResourceItemType):
       
    29 	iLabel(aLabelToSet),
       
    30 	iLineNumber(0),
       
    31 	iResourceItemType(aResourceItemType)
       
    32 	{}
       
    33 
       
    34 ResourceItem & ResourceItem::operator= ( const ResourceItem &)
       
    35 	{
       
    36 	assert(0);
       
    37 	return * this;
       
    38 	}
       
    39 
       
    40 void ResourceItem::Set(const String* aFileNameToSet, int aLineNumberToSet)
       
    41 	{
       
    42 	iFileName = aFileNameToSet;
       
    43 	iLineNumber = aLineNumberToSet;
       
    44 	}
       
    45 
       
    46 void ResourceItem::RegisterErrorLocation()
       
    47 	{
       
    48 	ErrorHandler::Register(iFileName,iLineNumber);
       
    49 	}
       
    50 
       
    51 int ResourceItem::GetResourceItemType()
       
    52 	{
       
    53 	return iResourceItemType;
       
    54 	}
       
    55 
       
    56 String ResourceItem::GetLabel()
       
    57 	{
       
    58 	return iLabel;
       
    59 	}
       
    60 
       
    61 /***************************************************************/
       
    62 /* SimpleResourceItem                                          */
       
    63 /***************************************************************/
       
    64 SimpleResourceItem::SimpleResourceItem(SimpleStructItem* aItem):
       
    65 	ResourceItem(aItem->iLabel, ESimpleResourceItem),
       
    66 	iStructItem(aItem),
       
    67 	iValueSet(0)
       
    68 	{}	
       
    69 
       
    70 void SimpleResourceItem::Set(const String& aValueToSet)
       
    71 	{
       
    72 	ResourceItem::Set( ErrorHandler::GetFileName(), ErrorHandler::GetLineNumber() );
       
    73 	
       
    74 	unsigned long lengthLimit = 0;	// max length of string, 0 = unchecked
       
    75 	switch ( iStructItem->iItemType)
       
    76 		{
       
    77 		case L_LTEXT8:
       
    78 		case L_LTEXT16:
       
    79 			lengthLimit = 255;	// length encoded as a single byte
       
    80 			// fall through...
       
    81 		case L_TEXT8:
       
    82 		case L_BUF8:
       
    83 		case L_TEXT16:
       
    84 		case L_BUF16:
       
    85 			if ( iStructItem->iLengthLimit.Length() > 0)
       
    86 			{
       
    87 				NumericValue limit(iStructItem->iLengthLimit, L_LONG);
       
    88 				lengthLimit = limit.GetULong();
       
    89 			}
       
    90 			if ( lengthLimit && aValueToSet.ExportLength(TargetCharacterSet,SourceCharacterSet) > lengthLimit )
       
    91 			{
       
    92 				char buf[256];
       
    93 				sprintf(buf, "Text length exceeds specified limit of %lx", lengthLimit);
       
    94 				ErrorHandler::OutputErrorLine( buf);
       
    95 				exit(1);
       
    96 			}
       
    97 		}
       
    98 	
       
    99 	iValue = aValueToSet;
       
   100 	iValueSet = 1;
       
   101 	}
       
   102 
       
   103 void SimpleResourceItem::Set(const StringArray& /*aValues*/)
       
   104 	{
       
   105 	// Can't store array of values
       
   106 	ErrorHandler::OutputErrorLine( "SimpleResourceItem cannot hold array of values");
       
   107 	exit(1);
       
   108 	}
       
   109 
       
   110 ResourceItemArray * SimpleResourceItem::GetRIA()
       
   111 	{
       
   112 	assert(0);	// Don't have RIA in this class.
       
   113 	return NULL;
       
   114 	}
       
   115 
       
   116 void SimpleResourceItem::AddDefault()
       
   117 	{
       
   118 	if(!iValueSet)
       
   119 		iValue = iStructItem->iDefault;
       
   120 	}
       
   121 
       
   122 void SimpleResourceItem::SetSRLink(unsigned long aSRLinkToSet)
       
   123 	{
       
   124 	if(iStructItem->iItemType == L_SRLINK)
       
   125 		iLinkValue = aSRLinkToSet;
       
   126 	}
       
   127 
       
   128 // ArrayResourceItem
       
   129 
       
   130 ArrayResourceItem::ArrayResourceItem( ArrayStructItem * p):
       
   131 	ResourceItem( p->iLabel, EArrayResourceItem),
       
   132 	iStructItem(p)
       
   133 	{}	
       
   134 
       
   135 ArrayResourceItem::~ArrayResourceItem()
       
   136 	{
       
   137 	iValues.DeleteAll();
       
   138 	}
       
   139 
       
   140 void ArrayResourceItem::Set(const String& /*aValue*/)
       
   141 	{
       
   142 	// Can't store single value
       
   143 	ErrorHandler::OutputErrorLine( "ArrayResourceItem requires array of values");
       
   144 	exit(1);
       
   145 	}
       
   146 
       
   147 void ArrayResourceItem::Set(const StringArray& aValuesToSet)
       
   148 	{
       
   149 	ResourceItem::Set(ErrorHandler::GetFileName(),ErrorHandler::GetLineNumber());
       
   150 	iValues = aValuesToSet;
       
   151 	}
       
   152 
       
   153 ResourceItemArray * ArrayResourceItem::GetRIA()
       
   154 	{
       
   155 	assert(0);	// Don't have RIA in this class.
       
   156 	return NULL;
       
   157 	}
       
   158 
       
   159 void ArrayResourceItem::AddDefault()
       
   160 	{
       
   161 	StringArrayIterator NextActual(iValues);
       
   162 	String* actualvalue;
       
   163 	StringArrayIterator NextDefault( iStructItem->iDefaults);
       
   164 	String* defaultvalue;
       
   165 	// Iterate through actual values and default values.
       
   166 	while((actualvalue = NextActual()) != NULL)
       
   167 		{
       
   168 		if((defaultvalue = NextDefault()) == NULL)
       
   169 			return;	// Stop if end of defaults reached.
       
   170 		}
       
   171 	// Having reached the end of the actual values see if there are any
       
   172 	// default values to add.
       
   173 	while((defaultvalue = NextDefault()) != NULL)
       
   174 		iValues.Add(new String (*defaultvalue));
       
   175 	}
       
   176 
       
   177 void ArrayResourceItem::SetSRLink(unsigned long /*aSRLinkToSet*/)
       
   178 	{}
       
   179 
       
   180 // StructTypeResourceItem
       
   181 
       
   182 StructTypeResourceItem::StructTypeResourceItem(StructTypeStructItem* p):
       
   183 	ResourceItem( p->iLabel, EStructTypeResourceItem),
       
   184 	iStructItem(p)
       
   185 	{}	
       
   186 
       
   187 void StructTypeResourceItem::Set(const String& aStructName)
       
   188 	{
       
   189 	ResourceItem::Set(ErrorHandler::GetFileName(),ErrorHandler::GetLineNumber());
       
   190 	iResourceItems.FillFromStruct(aStructName);
       
   191 	}
       
   192 
       
   193 void StructTypeResourceItem::Set(const StringArray& /*aValues*/)
       
   194 	{
       
   195 	// Can't store array of values
       
   196 	ErrorHandler::OutputErrorLine( "StructTypeResourceItem cannot hold array of values");
       
   197 	exit(1);
       
   198 	}
       
   199 
       
   200 ResourceItemArray * StructTypeResourceItem::GetRIA()
       
   201 	{
       
   202 	return& iResourceItems;
       
   203 	}
       
   204 
       
   205 void StructTypeResourceItem::AddDefault()
       
   206 	{
       
   207 	ResourceItemArrayIterator NextActualItem(iResourceItems);
       
   208 	ResourceItem * pActualItem;
       
   209 	
       
   210 	// Add defaults to each of the items already set up.
       
   211 	while( ( pActualItem = NextActualItem() ) != NULL)
       
   212 		pActualItem->AddDefault();
       
   213 	}
       
   214 
       
   215 void StructTypeResourceItem::SetSRLink(unsigned long /*aSRLinkToSet*/)
       
   216 	{}
       
   217 
       
   218 // StructArrayResourceItem
       
   219 
       
   220 StructArrayResourceItem::StructArrayResourceItem(StructArrayStructItem* p):
       
   221 	ResourceItem(p->iLabel, EStructArrayResourceItem),
       
   222 	iStructItem(p),
       
   223 	iLastRIA( NULL)
       
   224 	{}	
       
   225 
       
   226 void StructArrayResourceItem::Set( const String & StructName)
       
   227 	{
       
   228 	ResourceItem::Set( ErrorHandler::GetFileName(), ErrorHandler::GetLineNumber() );
       
   229 	ResourceItemArray* p = new ResourceItemArray;
       
   230 	iArrayOfResourceItemArrays.Add(p);
       
   231 	p->FillFromStruct( StructName);
       
   232 	iLastRIA = p;
       
   233 	}
       
   234 
       
   235 void StructArrayResourceItem::Set(const StringArray& /*aValues*/)
       
   236 	{}
       
   237 
       
   238 ResourceItemArray * StructArrayResourceItem::GetRIA()
       
   239 	{
       
   240 	return iLastRIA;
       
   241 	}
       
   242 
       
   243 void StructArrayResourceItem::AddDefault()
       
   244 	{
       
   245 	ResourceItemArrayArrayIterator next(iArrayOfResourceItemArrays);
       
   246 	ResourceItemArray * p;
       
   247 	while( ( p = next() ) != NULL)
       
   248 		p->AddDefault();
       
   249 	}
       
   250 
       
   251 void StructArrayResourceItem::SetSRLink(unsigned long /*aSRLinkToSet*/)
       
   252 	{}
       
   253 
       
   254 // ResourceItemArray
       
   255 
       
   256 ResourceItemArray::ResourceItemArray():
       
   257 	iLenType(0)
       
   258 	{}
       
   259 	
       
   260 ResourceItemArray::~ResourceItemArray()
       
   261 	{
       
   262 	DeleteAll();
       
   263 	}
       
   264 	
       
   265 void ResourceItemArray::Add( ResourceItem * p)
       
   266 	{
       
   267 	Array::Add( p);
       
   268 	}
       
   269 
       
   270 void ResourceItemArray::FillFromStruct(const String& aStructName)
       
   271 	{
       
   272 	extern StructHeaderArray * pSHA;
       
   273 	StructHeader * pSH = pSHA->Find(aStructName);
       
   274 	StructItemArrayIterator next( pSH->iSIA);
       
   275 	StructItem * pItem;
       
   276 	while ( ( pItem = next() ) != NULL)
       
   277 		Add( pItem->NewResourceItem() );
       
   278 	iLenType = pSH->iLenType;
       
   279 	}
       
   280 
       
   281 void ResourceItemArray::Set( const String & Label, const String & Value)
       
   282 	{
       
   283 	ResourceItem * p = Find( Label);
       
   284 	p->Set( Value);
       
   285 	}
       
   286 
       
   287 void ResourceItemArray::Set( const String & Label, const StringArray & Values)
       
   288 	{
       
   289 	ResourceItem * p = Find( Label);
       
   290 	p->Set( Values);
       
   291 	}
       
   292 
       
   293 ResourceItem * ResourceItemArray::Find( const String & LabelSought)
       
   294 	{
       
   295 	ResourceItemArrayIterator next( * this);
       
   296 	ResourceItem * p;
       
   297 	
       
   298 	while( ( p = next() ) != NULL)
       
   299 		if ( p->iLabel == LabelSought)
       
   300 			return p;
       
   301 	
       
   302 	ErrorHandler::OutputErrorLine( "Label not found");
       
   303 	exit(1);
       
   304 	}
       
   305 
       
   306 void ResourceItemArray::AddDefault()
       
   307 	{
       
   308 	ResourceItemArrayIterator next( * this);
       
   309 	ResourceItem * p;
       
   310 	
       
   311 	while( ( p = next() ) != NULL)
       
   312 		p->AddDefault();
       
   313 	}
       
   314 
       
   315 // ResourceHeader
       
   316 
       
   317 ResourceHeader::ResourceHeader(String aLabelToSet):
       
   318 	iLabel(aLabelToSet),
       
   319 	iLocal(0),
       
   320 	iResourceId(0xdeaddead),
       
   321 	iFormatAsHex(0),
       
   322 	iContainsCompressedUnicode(false)
       
   323 	{
       
   324 	}
       
   325 
       
   326 ResourceHeader::ResourceHeader():
       
   327 	iLocal(0),
       
   328 	iResourceId(0xdeaddead),
       
   329 	iFormatAsHex(0),
       
   330 	iContainsCompressedUnicode(false)
       
   331 	{
       
   332 	}
       
   333 
       
   334 ResourceHeader::~ResourceHeader()
       
   335 	{
       
   336 	}
       
   337 
       
   338 void ResourceHeader::AddDefault()
       
   339 	{
       
   340 	iRIA.AddDefault();
       
   341 	}
       
   342 
       
   343 void ResourceHeader::SetResourceId(NameIdMap& aMap, unsigned long aId, int aFormatAsHex)
       
   344 	{
       
   345 	iResourceId = aId;
       
   346 	iFormatAsHex = aFormatAsHex;
       
   347 	if (iLabel.Length() > 0)
       
   348 		aMap.Add(iLabel, aId);
       
   349 
       
   350 	ResourceItemArrayIterator next(iRIA);
       
   351 	ResourceItem * pItem;
       
   352 	while( ( pItem = next() ) != NULL)
       
   353 		pItem->SetSRLink(aId);
       
   354 	}
       
   355 
       
   356 void ResourceHeader::StreamOut(RCBinaryStream& aStream, int& aSizeOfLargestResourceWhenUncompressed, const char* aDumpFile)
       
   357 	{
       
   358 	ResourceDataStream stream;
       
   359 	iRIA.StreamOut(stream);
       
   360 	if (aDumpFile!=NULL)
       
   361 		{
       
   362 		// dump the resource to aDumpFile in its uncompressed and unpadded state - LINKs, LLINKs, etc will be undefined
       
   363 		stream.Dump(aDumpFile);
       
   364 		}
       
   365 	int sizeWhenUncompressed=0;
       
   366 	iContainsCompressedUnicode=stream.StreamOutReturningWhetherContainsCompressedUnicode(aStream, sizeWhenUncompressed);
       
   367 	// this check is unnecessary, zero size resources are allowed so
       
   368 	// there is no benefit to this assert. 
       
   369 
       
   370 	if (aSizeOfLargestResourceWhenUncompressed<sizeWhenUncompressed)
       
   371 		{
       
   372 		aSizeOfLargestResourceWhenUncompressed=sizeWhenUncompressed;
       
   373 		}
       
   374 	}
       
   375 
       
   376 // ResourceItemArrayArray
       
   377 
       
   378 ResourceItemArrayArray::ResourceItemArrayArray()
       
   379 	{}
       
   380 
       
   381 ResourceItemArrayArray::~ResourceItemArrayArray()
       
   382 	{
       
   383 	DeleteAll();
       
   384 	}
       
   385 	
       
   386 void ResourceItemArrayArray::Add( ResourceItemArray * pNewItem)
       
   387 	{
       
   388 	Array::Add( pNewItem);
       
   389 	}
       
   390 
       
   391 // ResourceItemArrayIterator
       
   392 
       
   393 ResourceItemArrayIterator::ResourceItemArrayIterator( const ResourceItemArray & c):
       
   394 	ArrayIterator( c)
       
   395 	{}
       
   396 
       
   397 ResourceItem * ResourceItemArrayIterator::operator()()
       
   398 	{
       
   399 	return (ResourceItem *) ArrayIterator::operator()();
       
   400 	}
       
   401 
       
   402 // ResourceItemArrayArrayIterator
       
   403 
       
   404 ResourceItemArrayArrayIterator::ResourceItemArrayArrayIterator(const ResourceItemArrayArray & c):
       
   405 	ArrayIterator( c)
       
   406 	{}
       
   407 
       
   408 ResourceItemArray* ResourceItemArrayArrayIterator::operator()()
       
   409 	{
       
   410 	return (ResourceItemArray*) ArrayIterator::operator()();
       
   411 	}