reszip/src/rcmp.cpp
changeset 0 f58d6ec98e88
equal deleted inserted replaced
-1:000000000000 0:f58d6ec98e88
       
     1 /*
       
     2 * Copyright (c) 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 "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 // RCMP.CPP
       
    18 //
       
    19 //
       
    20 
       
    21 #include <e32svr.h>
       
    22 #include <e32cons.h>
       
    23 #include <s32file.h>
       
    24 #include <s32mem.h>
       
    25 
       
    26 
       
    27 #include "rescomp.h"
       
    28 #include "resdict.h"
       
    29 #include "resentry.h"
       
    30 
       
    31 const TInt KConsWidth = 20;
       
    32 const TInt KConsHeight = 15;
       
    33 
       
    34 // TOOL version string (will be printed on command line so please keep upto date)
       
    35 _LIT(KReszipVersionString, "reszip version 2.0.0");
       
    36 
       
    37 LOCAL_D CConsoleBase* console;
       
    38 LOCAL_D RFs TheFs;
       
    39 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    40 
       
    41 LOCAL_D CArrayFixFlat<TFileName>* iFileList;
       
    42 
       
    43 TBuf<256> Arg;
       
    44 
       
    45 
       
    46 _LIT(KUncompressedExt,".orig");
       
    47 
       
    48 LOCAL_C void setupCleanup()
       
    49 //
       
    50 // Initialise the cleanup stack.
       
    51 //
       
    52     {
       
    53 	TheTrapCleanup=CTrapCleanup::New();
       
    54 	}
       
    55 
       
    56 void AddToFileListL(const TDesC& aName);
       
    57 
       
    58 
       
    59 void AddFileListL(const TDesC& aName)
       
    60 	{
       
    61 	RDebug::Print(_L("Adding files from "));
       
    62 	RDebug::Print(aName);
       
    63 	
       
    64 	RFileReadStream stream;
       
    65 	User::LeaveIfError( stream.Open(TheFs, aName, EFileRead) );
       
    66 	CleanupClosePushL(stream);
       
    67 
       
    68 	TBuf<512> buf;
       
    69 	TInt ch = 0;
       
    70 	TInt err = KErrNone;
       
    71 	while(err == KErrNone)
       
    72 		{
       
    73 		TRAP(err, ch = stream.ReadUint8L());
       
    74 		if (err == KErrNone)
       
    75 			{
       
    76 			if (ch != 13 && ch != 10)
       
    77 				{
       
    78 				buf.Append(ch);
       
    79 				}
       
    80 			else
       
    81 				{
       
    82 				AddToFileListL(buf);
       
    83 				buf.Zero();
       
    84 				}
       
    85 			}
       
    86 		}
       
    87 
       
    88 	CleanupStack::PopAndDestroy();	// stream
       
    89 	}
       
    90 
       
    91 void AddToFileListL(const TDesC& aName)
       
    92 	{
       
    93 	if (aName.Length() == 0)
       
    94 		{
       
    95 		return;
       
    96 		}
       
    97 	RDebug::Print(aName);
       
    98 
       
    99 	if (aName[0] == '@')
       
   100 		{
       
   101 		AddFileListL(aName.Right(aName.Length()-1));
       
   102 		return;
       
   103 		}
       
   104 
       
   105 	TParse parse;
       
   106 	if (parse.Set(aName, NULL, NULL) != KErrNone)
       
   107 		{
       
   108 		return;
       
   109 		}
       
   110 	TFileName name(parse.NameAndExt());
       
   111 	TPtrC driveAndPath(parse.DriveAndPath());
       
   112 
       
   113 	CDir* fileList = NULL; 
       
   114 	CDir* dirList = NULL; 
       
   115 
       
   116 
       
   117 	// Report any missing files (not fatal, as we don't want to stop the list)
       
   118 	TEntry entry;
       
   119 	if((!parse.IsWild()) && TheFs.Entry(aName,entry) != KErrNone)
       
   120 		console->Printf(_L("Error: Cannot find %S\n"), &aName);
       
   121 			
       
   122 
       
   123 	TInt err = TheFs.GetDir(aName, KEntryAttNormal,ESortByName,fileList,dirList);	
       
   124 	if (err != KErrNone)
       
   125 		{
       
   126 		delete fileList;
       
   127 		delete dirList;
       
   128 		return;
       
   129 		}
       
   130 
       
   131 	TInt count = fileList->Count();
       
   132 
       
   133 	_LIT(KString,"%S\n"); 
       
   134 
       
   135 	TInt appendErr = KErrNone;
       
   136 	for (TInt i=0;i<count;i++) 
       
   137 		{
       
   138 		TFileName fullName(driveAndPath);
       
   139 		fullName.Append((*fileList)[i].iName);
       
   140 		TRAPD(appendErr, iFileList->AppendL(fullName) );
       
   141 		if(appendErr!=KErrNone)
       
   142 			break;
       
   143 		}
       
   144 
       
   145 	delete fileList;
       
   146 	delete dirList;
       
   147 	User::LeaveIfError(appendErr);
       
   148 	}
       
   149 
       
   150 
       
   151 
       
   152 void ProcessAllFilesL()
       
   153 	{
       
   154 	TInt totalSize = 0;
       
   155 	TInt totalCompressed = 0;
       
   156 
       
   157 	if (Arg.Length() == 0)
       
   158 		{
       
   159 		// Write help information
       
   160 		console->Printf(_L("ResZip ROM Resource Compressor (%S)\n------------------------------\n"), &KReszipVersionString);
       
   161 		console->Printf(_L("Syntax: ResZip [full path to resource files]\n"));
       
   162 		console->Printf(_L("        ResZip [@full path to text file containing resource filenames]\n"));
       
   163 		console->Printf(_L("e.g: ResZip r:\\epoc32\\release\\wins\\udeb\\z\\system\\data\\*.rsc\n"));
       
   164 		console->Printf(_L("     ResZip @r:\\files.txt\n"));
       
   165 		console->Printf(_L("         where r:\\files.txt contains list of filenames (wildcards allowed)\n"));
       
   166 		console->Printf(_L("Full Path may include wildcards for compressing all resources in a directory\n"));
       
   167 		console->Printf(_L("May be used to compress non-ROM resources, with lower run-time performance\n"));
       
   168 		return;
       
   169 		}
       
   170 
       
   171 	iFileList = new(ELeave)CArrayFixFlat<TFileName>(32);
       
   172 
       
   173 	AddToFileListL(Arg);
       
   174 
       
   175 	TInt filecount = iFileList->Count();
       
   176 
       
   177 	if (filecount == 0)
       
   178 		{
       
   179 		console->Printf(_L("No files to process\n"));
       
   180 		}
       
   181 	else
       
   182 		{
       
   183 		TBuf<80> buf;
       
   184 		buf.Format(_L("Processing %d files\n"), filecount);
       
   185 		console->Printf(buf);
       
   186 		console->Printf(_L("Name\t\t\t Resource\tOrig\tUncompressed\tNew\t%%\tDictSize\n"));
       
   187 		console->Printf(_L("====\t\t\t ========\t====\t============\t===\t==\t========\n"));
       
   188 		}
       
   189 
       
   190 	TTime startTime;
       
   191 	startTime.UniversalTime();
       
   192 
       
   193 	for (TInt ii=0; ii<filecount; ii++)
       
   194 		{
       
   195 
       
   196 		CResComp* compressor = new(ELeave)CResComp(console);
       
   197 		CleanupStack::PushL(compressor);
       
   198 
       
   199 		TFileName fullName(iFileList->At(ii));
       
   200 		compressor->LoadFileL(TheFs, fullName);
       
   201 		compressor->CompressL();
       
   202 
       
   203 		if (!compressor->IsValid())
       
   204 			{
       
   205 			console->Printf(_L("\r"));
       
   206 			console->Printf(compressor->iConOutput);
       
   207 			console->Printf(_L("Not compressed\n"));
       
   208 			}
       
   209 		else
       
   210 			{
       
   211 			compressor->DisplayStats();
       
   212 			totalSize += compressor->OriginalFileSize();
       
   213 			totalCompressed += compressor->CompressedSize();
       
   214 			TFileName newName(fullName);
       
   215 			newName.Append(KUncompressedExt);
       
   216 			TheFs.Delete(newName);
       
   217 			User::LeaveIfError(TheFs.Rename(fullName, newName));
       
   218 			TRAPD(err, compressor->WriteFileL(TheFs,fullName));
       
   219 			if(err != KErrNone)
       
   220 				{
       
   221 				TheFs.Delete(fullName);
       
   222 				TheFs.Rename(newName, fullName);
       
   223 				User::Leave(err);
       
   224 				}
       
   225 			}
       
   226 
       
   227 		CleanupStack::PopAndDestroy(compressor);	
       
   228 		}
       
   229 
       
   230 	console->Printf(_L("------------------------------\n"));
       
   231 	console->Printf(_L("Total File Size: %d bytes\n"), totalSize);
       
   232 	console->Printf(_L("Total Compressed Size: %d bytes\n"), totalCompressed);
       
   233 
       
   234 	if (totalSize > 0)
       
   235 		{
       
   236 		TInt compression = ((totalCompressed) * 100) / totalSize;
       
   237 		console->Printf(_L("Total Compression = %d %%\n"), compression);
       
   238 		}
       
   239 
       
   240 	TTime endTime;
       
   241 	endTime.UniversalTime();
       
   242 
       
   243 	TTimeIntervalSeconds seconds;
       
   244 	endTime.SecondsFrom(startTime, seconds);
       
   245 
       
   246 	TInt secs = seconds.Int();
       
   247 	
       
   248 	TInt mins = secs / 60;
       
   249 	TInt hour = mins / 60;
       
   250 
       
   251 	secs -= (mins * 60);
       
   252 	mins -= (hour * 60);
       
   253 
       
   254 	TBuf<80> time;
       
   255 	time.Format(_L("Total time: %02d:%02d:%02d"), hour, mins, secs);
       
   256 	console->Printf(time);
       
   257 
       
   258 	delete iFileList;
       
   259 	}
       
   260 
       
   261 
       
   262 void DoConsoleL()
       
   263 	{
       
   264 	console=Console::NewL(_L("EPOC32 - reszip"),
       
   265 		TSize(KConsWidth,KConsHeight));
       
   266 	CleanupStack::PushL(console);
       
   267 	//
       
   268 	User::CommandLine(Arg);
       
   269 	//
       
   270 	User::LeaveIfError( TheFs.Connect() );
       
   271 	//
       
   272 	TRAPD(r,ProcessAllFilesL());
       
   273 
       
   274 	if (r!=KErrNone)
       
   275 		console->Printf(_L("failed with code %d\n"), r);
       
   276 	CleanupStack::PopAndDestroy(); // console
       
   277 	TheFs.Close();
       
   278 	}
       
   279 
       
   280 GLDEF_C TInt E32Main()
       
   281 	{
       
   282 	setupCleanup();
       
   283 	TRAPD(err,DoConsoleL());
       
   284 	delete TheTrapCleanup;
       
   285 	return KErrNone;
       
   286 	}