commsfwtools/preparedefaultcommsdatabase/Tools/ceddump/src/filedump.cpp
changeset 0 dfb7c4ff071f
child 25 9d7ce34704c8
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2004-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 //
       
    15 
       
    16 /**
       
    17  @file 
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include "filedump.h"
       
    22 
       
    23 #ifndef __TOOLS2__
       
    24 _LIT(DEFAULT_SESSION_PATH, "c:\\");
       
    25 #endif
       
    26 
       
    27 /*static*/ CFileDump* CFileDump::NewL(TPtrC aFilename, TPtrC aAppname, TPtrC aVersion, TBool aDebugOn, CConsoleBase* aConsole, TBool aWriteHeader)
       
    28 	{
       
    29 	CFileDump* fdump = new (ELeave) CFileDump(aDebugOn, aConsole);
       
    30 	CleanupStack::PushL(fdump);
       
    31 	fdump->ConstructL(aFilename, aAppname, aVersion, aWriteHeader);
       
    32 	CleanupStack::Pop(fdump);
       
    33 	return fdump;
       
    34 	}
       
    35 
       
    36 CFileDump::CFileDump(TInt aDebugOn, CConsoleBase* aConsole)
       
    37 	:iDebug(aDebugOn), iConsole(aConsole)
       
    38 	{
       
    39 	}
       
    40 
       
    41 void CFileDump::ConstructL(TPtrC aFilename, TPtrC aAppname, TPtrC aVersion, TBool aWriteHeader)
       
    42 /**
       
    43 Opens the output file
       
    44 */
       
    45 	{
       
    46 	User::LeaveIfError(iFsSession.Connect());
       
    47 	
       
    48 #ifndef __TOOLS2__
       
    49 	// Set up the session path for WINSCW / ARMv5 incase the log file name has no path.
       
    50 	iFsSession.SetSessionPath( DEFAULT_SESSION_PATH );
       
    51 #endif
       
    52 
       
    53 	User::LeaveIfError(iFile.Replace(iFsSession, aFilename, EFileShareAny|EFileStream|EFileWrite));
       
    54 	if (aWriteHeader)
       
    55 		{
       
    56 		iBuffer.Format(_L("############################################################\r\n## AUTO-GENERATED CONFIGURATION FILE\r\n## %S\r\n## %S\r\n############################################################\r\n"),
       
    57 			&aAppname, &aVersion);
       
    58 		WriteBufferToFile(iBuffer, iFile);
       
    59 		}
       
    60 	iInitialised = ETrue;
       
    61 	}
       
    62 
       
    63 CFileDump::~CFileDump()
       
    64 	{
       
    65 	iFile.Close();
       
    66 	iFsSession.Close();
       
    67 	}
       
    68 
       
    69 void CFileDump::WriteTableHeader(TPtrC aTable)
       
    70 /**
       
    71 Writes a table header to the internal buffer
       
    72 
       
    73 @param table A reference to a descriptor containing the name of a table
       
    74 */
       
    75 	{
       
    76 	if (iInitialised && aTable.Length())
       
    77 		{
       
    78 		iBuffer.Format(_L("\r\n############################################################\r\n## %S\r\n## \r\n[%S]\r\n"),
       
    79 			&aTable, &aTable);
       
    80 		WriteBufferToFile(iBuffer, iFile);
       
    81 		}
       
    82 	}
       
    83 
       
    84 
       
    85 void CFileDump::WriteSectionHeader(TInt aCommDbId)
       
    86 /**
       
    87 Writes a section header to the internal buffer
       
    88 
       
    89 @param aCommDbId Unique Id of Communication database
       
    90 */
       
    91 	{
       
    92 	if (iInitialised)
       
    93 		{
       
    94 		switch (aCommDbId)
       
    95 			{
       
    96 			case -1:
       
    97 				iBuffer.Copy(_L("ADD_SECTION\r\n"));
       
    98 				break;
       
    99 			case 0:
       
   100 				iBuffer.Copy(_L("ADD_TEMPLATE\r\n"));
       
   101 				break;
       
   102 			default:
       
   103 				iBuffer.Format(_L("ADD_SECTION\r\n# COMMDB_ID = %d\r\n"),
       
   104 					aCommDbId);
       
   105 				break;
       
   106 			}
       
   107 		WriteBufferToFile(iBuffer, iFile);
       
   108 		}
       
   109 	}
       
   110 
       
   111 
       
   112 void CFileDump::WriteFieldCount(TUint aCount)
       
   113 /**
       
   114 Writes a section header to the internal buffer
       
   115 
       
   116 @param count Field count
       
   117 */
       
   118 	{
       
   119 	if (iInitialised)
       
   120 		{
       
   121 		iBuffer.Format(_L("\tFIELD_COUNT=%d\r\n"), aCount);
       
   122 		WriteBufferToFile(iBuffer, iFile);
       
   123 		}
       
   124 	}
       
   125 
       
   126 
       
   127 TBool CFileDump::WriteColumnValue(TPtrC &aColumn, TPtrC &aSetting)
       
   128 /**
       
   129 Writes a column value to the internal buffer
       
   130 
       
   131 @param column A reference to a descriptor containing the name of a column
       
   132 @param setting The setting
       
   133 @return ETrue if value was written
       
   134 */
       
   135 	{
       
   136 	TBool write=EFalse;
       
   137 	if (iInitialised && aColumn.Length() && aSetting.Length())
       
   138 		{
       
   139 		// Theoretical worst case is longest field is populated with non-ASCII
       
   140 		static TBuf<KMaxExpandedFieldLen> TempValue;	// static to keep it off the stack
       
   141 		TempValue.SetLength(0);
       
   142 		
       
   143 		/* replace any special chars with a portable representation that's re-importable
       
   144 		* Special characters are escaped in the 'C' language fashion with backslash.
       
   145 		* Specials are: 
       
   146 		*	\t = TAB 
       
   147 		*	\n = LF 
       
   148 		*	\[ = [		- Need for escaping is to prevent INI file processing presuming
       
   149 		*	\] = ]		- them to be start of section block
       
   150 		*	\\ = \
       
   151 		*	\xHHHH 		- Hex value for any other character falling outside the normal
       
   152 		*				  ASCII range of 32 - 126. This is because we're most likely 
       
   153 		*				  writing to an ASCII file, so absolutely can't trust that
       
   154 		*				  UniCode will get preserved on a round-trip through an editor
       
   155 		*/
       
   156 		
       
   157 		TInt i = 0;
       
   158 		for (i=0;i<aSetting.Length();i++)
       
   159 			{
       
   160 			TUint16 x = aSetting[i];
       
   161 			switch(x)
       
   162 				{
       
   163 				case '\n':
       
   164 					TempValue.Append(_L("\\n"));
       
   165 					break;
       
   166 				case '\t':
       
   167 					TempValue.Append(_L("\\t"));
       
   168 					break;
       
   169 				case '[':
       
   170 					TempValue.Append(_L("\\["));
       
   171 					break;
       
   172 				case ']':
       
   173 					TempValue.Append(_L("\\]"));
       
   174 					break;
       
   175 				case '\\':
       
   176 					TempValue.Append(_L("\\\\"));
       
   177 					break;
       
   178 				default:
       
   179 					if(x < 32 || x > 126)
       
   180 						{
       
   181 						TempValue.AppendFormat(_L("\\x%04X"), x);
       
   182 						}
       
   183 					else
       
   184 						{
       
   185 						TempValue.Append(x);
       
   186 						}
       
   187 					break;
       
   188 				}
       
   189 			}
       
   190 		
       
   191 		aSetting.Set(TempValue);
       
   192 		iBuffer.Format(_L("\t%S=%S\r\n"), &aColumn, &TempValue);
       
   193 		WriteBufferToFile(iBuffer, iFile);
       
   194 		write=ETrue;;
       
   195 		}
       
   196 	return write;
       
   197 	}
       
   198 
       
   199 
       
   200 void CFileDump::WriteSectionFooter(TInt aCommDbId)
       
   201 /**
       
   202 Writes a section footer to the internal buffer
       
   203 
       
   204 @param aCommDbId Unique Id of Communication database
       
   205 */
       
   206 	{
       
   207 	if (iInitialised)
       
   208 		{
       
   209 		if (aCommDbId == 0)
       
   210 			{
       
   211 			iBuffer.Copy(_L("END_TEMPLATE\r\n\r\n"));
       
   212 			}
       
   213 		else
       
   214 			{
       
   215 			iBuffer.Copy(_L("END_ADD\r\n\r\n"));
       
   216 			}
       
   217 		WriteBufferToFile(iBuffer, iFile);
       
   218 		}
       
   219 	}
       
   220 
       
   221 
       
   222 void CFileDump::WriteErrorMessage(const TPtrC &aMessage)
       
   223 /**
       
   224 Writes a message to the file
       
   225 
       
   226 @param message The error message 
       
   227 */
       
   228 	{
       
   229 	if (iInitialised && aMessage.Length() && aMessage.Length() < (MAX_COL_LONG_VAL_LEN - 140))
       
   230 		{
       
   231 		iBuffer.Format(_L("############################################################\r\n## ERROR\r\n## %S\r\n############################################################\r\n"),
       
   232 			&aMessage);
       
   233 		WriteBufferToFile(iBuffer, iFile);
       
   234 		}
       
   235 	}
       
   236 
       
   237 void CFileDump::Msg(TPtrC aText, ...)
       
   238 	{
       
   239 	if (iInitialised && aText.Length() && aText.Length() < MAX_COL_LONG_VAL_LEN)
       
   240 		{
       
   241 		VA_LIST list;
       
   242 		VA_START(list, aText);
       
   243 		
       
   244 		iBuffer.FormatList(aText, list);
       
   245 		iBuffer.Append(_L("\r\n"));
       
   246 		
       
   247 		if (iConsole)
       
   248 			{
       
   249 			iConsole->Printf(iBuffer);
       
   250 			}
       
   251 		
       
   252 		WriteBufferToFile(iBuffer, iFile);
       
   253 		
       
   254 		VA_END(list);
       
   255 		}
       
   256 	}
       
   257 
       
   258 void CFileDump::Dbg(TPtrC aText, ...)
       
   259 	{
       
   260 	if (iInitialised && iDebug && aText.Length() && aText.Length() < MAX_COL_LONG_VAL_LEN)
       
   261 		{
       
   262 		VA_LIST list;
       
   263 		VA_START(list, aText);
       
   264 		
       
   265 		iBuffer.FormatList(aText, list);
       
   266 		iBuffer.Append(_L("\r\n"));
       
   267 		
       
   268 		if (iConsole)
       
   269 		    {
       
   270 			iConsole->Printf(iBuffer);
       
   271 			}
       
   272 		
       
   273 		WriteBufferToFile(iBuffer, iFile);
       
   274 		
       
   275 		VA_END(list);
       
   276 		}
       
   277 	}
       
   278 
       
   279 TBool CFileDump::WriteBufferToFile(TDesC16 &aBuffer, RFile &aHandle)
       
   280 	{
       
   281 	// keep buffer small to reduce memory usage
       
   282 	static TBuf8<5> outputBuffer;
       
   283 	static TPtrC16 ptr;
       
   284 	static TInt consumed;
       
   285 	static TInt remainder = 0;
       
   286 	
       
   287 	TBool valid = EFalse;
       
   288 	
       
   289 	if (aBuffer.Length())
       
   290 		{
       
   291 		ptr.Set(aBuffer);
       
   292 		do
       
   293 			{
       
   294 			// get something to write
       
   295 			consumed = Min(outputBuffer.MaxLength(), ptr.Length());
       
   296 			
       
   297 			// write it
       
   298 			outputBuffer.Copy(ptr.Left(consumed));
       
   299 			if (aHandle.Write(outputBuffer) != KErrNone)
       
   300 			    {
       
   301 				return EFalse;
       
   302 				}
       
   303 			
       
   304 			// get the next chunk
       
   305 			remainder = ptr.Length() - consumed;
       
   306 			if (remainder > 0)
       
   307 				{
       
   308 				ptr.Set(ptr.Right(remainder));
       
   309 				}
       
   310 			
       
   311 			} 
       
   312 		while (remainder > 0);
       
   313 			
       
   314 		valid = ETrue;
       
   315 		}
       
   316 	
       
   317 	return valid;
       
   318 	}
       
   319 
       
   320 /**
       
   321 Writes attribute section header to the output file
       
   322 */
       
   323 void CFileDump::WriteAttributeSectionHeader()
       
   324 	{
       
   325 	iBuffer.Copy(_L("ADD_ATTRIBUTES\r\n"));
       
   326 	WriteBufferToFile(iBuffer, iFile);
       
   327 	}
       
   328 
       
   329 /**
       
   330 Writes attributes to the output file
       
   331 */
       
   332 void CFileDump::WriteAttributes(TPtrC &aColumn)
       
   333 	{
       
   334 	if (iInitialised && aColumn.Length())
       
   335 		{
       
   336 		iBuffer.Format(_L("\t%S\r\n"), &aColumn);
       
   337 		WriteBufferToFile(iBuffer, iFile);
       
   338 		}	
       
   339 	}
       
   340