diff -r 000000000000 -r 3da2a79470a7 testtoolsconn/stat/desktop/source/lib/src/crepmisc.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testtoolsconn/stat/desktop/source/lib/src/crepmisc.cpp Mon Mar 08 15:04:18 2010 +0800 @@ -0,0 +1,623 @@ +/* +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ + + + + +#include "stdafx.h" +#include +#include +#include +#include +#include "cReporter.h" + + +/***************************************************************************** + * Reporter preparation + * + * Sets member strings to a known state + *****************************************************************************/ + +void +Reporter::prepare ( void ) +{ + int i; + +#ifdef RPT_DEBUG_PROG + // set filename contents to garbage value + for (i=0;i= 0 && + code < RPT_MAX_LIST_ENTRIES ) + { + if ( messageList [ code ] ) + { + buildMessage ( messageList [ code ], messType ); + } + else + { + char tmpMsg [ 128 ]; + (void) sprintf ( tmpMsg, "[ No message text for code %d ]", code ); + buildMessage ( tmpMsg, messType ); + } + } +} + +/***************************************************************************** + * Generate a filename using the application name and current date. + *****************************************************************************/ + +void +Reporter::generateFilename ( char * validName, const char * appName ) +{ + /* Construct a filename for the logging to be written to. + * Filename is of the format x[xxx]9988.rpt where : + * + * xxxx = up to first 4 chars of calling program's name + * 99 = 2 digit day of month + * 88 = 2 digit month of year + */ + + const int PROG_LEN = 4; + time_t curtime; + struct tm *tminfo; + + // get filename from path + extractName ( buffer, appName, PROG_LEN ); + + (void) time ( &curtime ); + tminfo = localtime ( &curtime ); + + (void) sprintf ( validName, + "%s%02d%02d.RPT", + buffer, + tminfo->tm_mday, + tminfo->tm_mon + 1); + +#ifdef RPT_DEBUG_PROG + (void) fprintf ( stdout, "filename [%s] generated from [%s]\n", + validName, appName ); + (void) fflush ( stdout ); +#endif +} + +/***************************************************************************** + * Extract 'length' characters of the actual filename from the path. + *****************************************************************************/ + +void +Reporter::extractName ( char * file, const char * path, const int length ) +{ + char *ptr; + + // find the last backslash + ptr = strrchr ( path, '\\' ); + + if ( !ptr ) + { + strncpy ( file, path, length ); + } + else + { + strncpy ( file, (++ptr), length ); + } + + *( file + length ) = '\0'; +} + +/**************************************************************************** + * Open the file or print a message if we can't + *****************************************************************************/ + +int +Reporter::openFile ( unsigned long ulOpenAttributes ) +{ +#ifdef UNICODE + TCHAR uFilename[CPO_MAX_FILENAME_LEN + 1]; + + // Convert to UNICODE. + if (!MultiByteToWideChar(CP_ACP, // conversion type + 0, // flags + filename, // source + -1, // length + uFilename, // dest + CPO_MAX_FILENAME_LEN)) // length + { + return ( CPO_FALSE ); + } + + hFile = CreateFile(uFilename, + GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + ulOpenAttributes, + FILE_ATTRIBUTE_NORMAL, + NULL); +#else + hFile = CreateFile(filename, + GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + ulOpenAttributes, + FILE_ATTRIBUTE_NORMAL, + NULL); +#endif + + if (hFile == INVALID_HANDLE_VALUE) + { +// char szTemp[500] = {0}; +// sprintf(szTemp, "WARNING : [%s] could not be opened. File output disabled", filename); +// MessageBox(NULL, szTemp, NULL, MB_OK); + return ( CPO_FALSE ); + } + + // move to the end if a file already exists + SetFilePointer(hFile, 0, 0, FILE_END); + +#ifdef RPT_DEBUG_PROG + (void) fprintf ( stdout, "file [%s] opened\n", filename ); + (void) fflush ( stdout ); +#endif + + return ( CPO_TRUE ); +} + +/***************************************************************************** + * Construct the parts of the complete message. + *****************************************************************************/ + +void +Reporter::buildMessage ( const char * message, const char messType ) +{ + char prefixInfo [ RPT_MAX_MSG_LEN + 1 ]; + char finalStr [ RPT_OPEN_MSG_LEN + RPT_MAX_MSG_LEN + 1 ]; + int maxlen; + + // get prefix information + switch ( messType ) + { + case RPT_TEXT: + case RPT_MSG: + *( prefixInfo ) = '\0'; + break; + case RPT_HEADER: + addHeaderInfo ( prefixInfo ); + break; + default: + addLogInfo ( prefixInfo, messType ); + break; + } + + // parse arguments passed with message + (void) vsprintf ( buffer, message, pCurrent ); + + // get max length of output string + if ( messType == RPT_DEBUG || messType == RPT_MSG || + messType == RPT_ERROR || messType == RPT_TEXT ) + { + maxlen = RPT_OPEN_MSG_LEN - (int) strlen ( prefixInfo ) - 1; + } + else + { + maxlen = RPT_MAX_MSG_LEN - (int) strlen ( prefixInfo ) - 1; + } + + // make sure we are dealing with a valid message of correct length + validateText ( buffer, maxlen ); + + // generate the output string + (void) sprintf ( finalStr, + "%s%s", + prefixInfo, + buffer ); + + if ( messType == RPT_TEXT ) + { + (void) strcpy ( buffer, finalStr ); + } + else + { + writeToStream ( finalStr ); + } +} + +/***************************************************************************** + * Validate the supplied string for zero length and unprintable chars up to + * length bytes. + * + * If unprintable chars are found embedded in the text, the text will be + * truncated at that point. + * + * If the text is longer than the specified length, it will be truncated at + * that length. + * + * If the text is empty or the 1st char is unprintable, the function will + * insert replacement 'invalid' text. + *****************************************************************************/ + +void +Reporter::validateText ( char * text, const int length ) +{ + int len = (int) strlen ( text ); + if ( len > length ) + { + len = length; + } + + // check for empty message or unprintable 1st char + if ( len && isprint ( text [ 0 ] ) ) + { + // check message for unprintable characters + for (int i=1;i RPT_MAX_LIST_ENTRIES || messageList [ code ] ) + { + return; + } + + // jump any spaces in front of text + while ( *( pText ) == ' ' || *( pText ) == '\t' ) + { + pText++; + } + + // copy string + // messageList [ code ] = new char ( strlen ( pText ) + 1 ); + messageList [ code ] = + (char *) malloc ( sizeof ( char ) * ( strlen ( pText ) + 1 ) ); + + (void) strcpy ( messageList [ code ], pText ); + +#ifdef RPT_DEBUG_PROG +// (void) fprintf ( stdout, "code [%s] text [%s] entry [%s] len %d\n", +// pCode, pText, messageList [ code ], strlen ( pText ) ); +// (void) fflush ( stdout ); +#endif + + // let object know there is at least one message entry + fileRead = 1; +} + +/****************************************************************************/ +