// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
TInt TMmsFileText::Read( TDes& aDes )
/**
Reads single line text record into the specified descriptor.

The read operation begins at the current file position, and ends when
a line delimiter character is read or the caller's buffer is full or
the file ends;

If the line is longer than fits into user's buffer, of if the file does
not end with a terminator, KErrTooBig is returned.
The purpose is to inform the caller that a terminator should not be added
to the line when it is written elsewhere.

Next time the reading continues from the current position so that a long
line may be read in chunks and terminator added when the end of the line
has been reached.

If Read() is called when the current position is the end of the file (that 
is, after the last line delimiter in the file), KErrEof is returned, and the 
length of the buffer is set to zero.

@param aDes On return, contains the single record read from the file. Any 
            previous contents are overwritten.

@return KErrNone if successful, otherwise one of the other system-wide error 
        codes. KErrTooBig indicates that the line does not end with a
        terminator. Buffer is too short to hold the whole line or the line
        is the last line in the file and the file does not end with a 
        terminator character.
*/
	{
	TText* pD = ( TText* )aDes.Ptr();
	TInt len = aDes.MaxLength();
	TInt newLen = 0;
	TInt r = KErrNone;
    TBool terminate = EFalse;
	while ( newLen < len )
		{
		if ( iNext >= iEnd )
			{
			r = FillBuffer();
			if ( r != KErrNone && r != KErrEof )
			    {
				return r;
			    }
			if ( r == KErrEof )
				{
				aDes.SetLength( newLen );
				return ( newLen ? KErrTooBig : KErrEof );
				}
			continue;
			}
		terminate = newLen;
		r = CheckForTerminator( terminate );
		if ( r != KErrNone || terminate)
			{
			aDes.SetLength( newLen );
			return r;
			}
		*pD++ = ( *iNext++ );
		newLen++;
		}
	aDes.SetLength( newLen );
	terminate = newLen;
	r=CheckForTerminator( terminate );
	if ( r != KErrNone || terminate )
	    {
		return r;
	    }
// don't skip the rest of the line - return the rest the next time.
	return KErrTooBig;
	}
/**
Finds the keystring from the source string and replaces it with the
replacement string. The formated string is stored in the destination
descriptor.
*/
TInt CResourceLoader::Formater(TDes& aDest, const TDesC& aSource, const TDesC& aKey, const TDesC& aSubs, TBidiText::TDirectionality aDirectionality)    
    {
    // substitute string must not contain KSubStringSeparator, 
    // or results will be unpredictable 
    __ASSERT_DEBUG(aSubs.Locate(KSubStringSeparator) == KErrNotFound, 
        User::Panic(KPanicCategory, EInvalidSubstitute));

    TInt keylength(aKey.Length());

    //aDest must be empty.
    aDest.Zero();

    // offset indicates end of last handled key in source
    TInt offset(0);

    // offset in destination string
    TInt desOffset(0);

    // Substring directionalities are adjusted after all changes are done.
    TBool checkSubstringDirectionalities(EFalse);

    // count is the position in the source from which the substring starts
    TInt count(0);

    // Replaced parameters count
    TInt replaceCount(0);

    while (count != KErrNotFound)
        {
        // desCount is the position of the substring starts in destination.
        TInt desCount(0);

        TPtrC remainder = aSource.Right(aSource.Length() - offset);
        count = remainder.Find(aKey);

        TInt maxSubLength = -1;
        if (count != KErrNotFound)
            {
            replaceCount++;
            desOffset += count;
            offset += count;
            count = offset;
            desCount = desOffset;

            // copy source to destination if first time
            if (aDest.Length() == 0)
                aDest.Append(aSource);

            // delete found key from destination
            aDest.Delete(desCount, keylength);

            offset += keylength; // increase offset by key length

            if (count + keylength < (aSource.Length()-1)) // aKey is not at the end of string
                {
                if (aSource[count+keylength] == '[') // Key includes max datalength
                    {
                    maxSubLength = 10*(aSource[count+keylength+1]-'0') 
                                   + (aSource[count+keylength+2]-'0');
                    aDest.Delete(desCount,4); // Length information stored->delete from descriptor
                    offset += 4; // increase offset by max sub length indicator
                    }
                }
         
            aDest.Insert(desCount, aSubs);
        
            desOffset = desCount + aSubs.Length();

            if (maxSubLength > 0 && aSubs.Length() > maxSubLength)
                {
                aDest.Delete(desCount+maxSubLength-1, aSubs.Length()+1-maxSubLength);     
                TText ellipsis(KEllipsis);
                aDest.Insert(desCount+maxSubLength-1, TPtrC(&ellipsis,1));
                desOffset = desCount + maxSubLength;
                }

            TBidiText::TDirectionality subsDir =
                TBidiText::TextDirectionality(aDest.Mid(desCount, desOffset - desCount));

            // If inserted string has different directionality,
            // insert directionality markers so that bidi algorithm works in a desired way.
            if (aDirectionality != subsDir)
                {
                checkSubstringDirectionalities = ETrue;

                TInt freeSpace = aDest.MaxLength() - aDest.Length();

                // Protect the directionality of the inserted string.
                if (freeSpace >= KExtraSpaceForSubStringDirMarkers)
                    {
                    TBuf<1> subsMarker;
                    subsMarker.Append(subsDir == TBidiText::ELeftToRight ?
                        KLRMarker : KRLMarker);

                    aDest.Insert(desOffset, subsMarker);
                    aDest.Insert(desCount, subsMarker);
                    desOffset += KExtraSpaceForSubStringDirMarkers;
                    }
                }
            }
        }

    // Adjust substring directionality markers if necessary
    // and if there is enough room in destination string
    if (checkSubstringDirectionalities)
        {
        TText mainMarker = (aDirectionality == TBidiText::ELeftToRight ? 
            KLRMarker : KRLMarker);

        TInt freeSpace = aDest.MaxLength() - aDest.Length();

        // If not already done, protect the directionality of the original string
        // and all of the KSubStringSeparator separated substrings.
        if (freeSpace > 0 
            && aDest.Length()
            && aDest[0] != mainMarker 
            && aDest[0] != KSubStringSeparator
            && aDest[0] != KDirNotFound)  
            {
            aDest.Insert(0, TPtrC(&mainMarker, 1));
            freeSpace--;
            }

        // Find and protect KSubStringSeparator separated substrings.
        // Go through string backwards so that any changes will not affect indexes 
        // that are not yet checked.
        TInt j(aDest.Length()-1);
        while (freeSpace > 0 && j >= 0) 
            {
            if (aDest[j] == KSubStringSeparator && j < (aDest.Length() - 1) 
                && aDest[j+1] != mainMarker && aDest[j+1] != KDirNotFound)
                {
                aDest.Insert(j+1, TPtrC(&mainMarker, 1));
                freeSpace--;
                }
            j--;
            }
        }

    return replaceCount;
    }
EXPORT_C void CheckStringSpaceL(const TDes& aString, TInt aSpace)
{
	if (aString.Length()+aSpace > aString.MaxLength()) User::Leave(KErrOverflow);
}
Ejemplo n.º 4
0
// ==========================================================================
// METHOD:  Constructor
//
// DESIGN:  
// ==========================================================================
CLogFileHandler::CLogFileHandler( RFs& aFs,
                                  TDes& aFormatBuffer, 
                                  TDes8& aOutputBuffer,
                                  const TDesC& aDirectoryName,
                                  const TDesC& aFileName ) : 
    iFs( aFs ),                                  
    iFormatBuffer( aFormatBuffer ),
    iFormatBuffer8( const_cast<TUint8*>(reinterpret_cast<const TUint8*>(aFormatBuffer.Ptr())), 0, aFormatBuffer.MaxLength()*2 ),
    iOutputBuffer( aOutputBuffer ),
    iDirectoryName( aDirectoryName ),
    iFileName( aFileName ),
    iTryToOpenFile( ETrue ),
    iFileIsOpen( EFalse ),
    iFirstTime( ETrue )
    {
    } // END CLogFileHandler
Ejemplo n.º 5
0
TInt CIpuTestHarness::ReadLineFromScript(TDes& aBuffer)
//
// Reads the next line from the script file, and sets the passed-in descriptor with its contents.
// Returns KErrNone if reading succeeded; KErrNotFound if the EOF was reached. When EOF is reached,
// the file is closed.
	{
	// *********************************
	// Assume script is 8-bit text file
	// *********************************
	TBool isAComment = ETrue;
	TInt err = KErrNone;
	TBuf<512> line;
	while (isAComment && !err)
		{
		TFileText text;
		text.Set(*iScriptFile);
		line.SetLength(0);
		for(;;)
			{
			TBuf8<2> c;
			err = iScriptFile->Read(c,1);
			if (err && err != KErrEof)
				{
				iTest.Printf(_L("Error reading file: %d\n"), err);
				break;
				}
			if (c.Length() == 0)
				{
				err = KErrEof;
				break;
				}
			else
				{
				if (c[0] == '\n') // break out if it is CR
					break;
				else if (c[0] != (TUint8)(0x0d)) // otherwise append the char, _unless_ it is a LF
					line.Append(c[0]);
				}
			}
		if (err == KErrNone && line.Locate('/') != 0) // comment (only works if it's the first character)
			{
			isAComment = EFalse;
			}
		}

	// The line read is not a comment, or have hit end of file
	if (!err)
		{
		// copy to passed in descriptor, but do not allow an overflow
		aBuffer.Copy(line.Left(aBuffer.MaxLength()));
		LogIt(_L("***SCRIPT : read command '%S' ***\n"), &aBuffer);
		}
	else
		{
		iScriptFile->Close();
		err = KErrNotFound;
		iScriptRunning = EFalse;
		LogIt(_L("***SCRIPT ENDED***\n"));
		}
	return err;
	}
/**
Retrieves the database system settings from the settings table.

@param aDbName Logical database name: "main" for the main database or attached database name,
@param aCollationDllName Output parameter, will contain the stored collation dll name,
@param aDbConfigFileVersion Output parameter, will contain the stored database config file version,
@param aSettingsVersion Output parameter, will contain the version of the settings table.
@param aCompactionMode Output parameter. Database compaction mode (one of TSqlCompactionMode enum item values except ESqlCompactionNotSet).

@see TSqlCompactionMode

@leave KErrGeneral, either unable to retrieve the data from the settings table or the 
					stored table version or config file version is invalid or the stored compaction mode is invalid.
	   KErrOverflow, aCollationDllName is not large enough to store the name of the 
	   				 collation dll that is stored in the settings table.
       KErrNoMemory, an out of memory condition has occurred.
	   Note that the function may also leave with other system-wide error codes or SQL
	   errors of ESqlDbError type
@panic SqlDb 2 In _DEBUG mode if iDbHandle is NULL (uninitialized TSqlDbSysSettings object).
*/
void TSqlDbSysSettings::GetSettingsL(const TDesC& aDbName, TDes& aCollationDllName, TInt& aDbConfigFileVersion, 
									 TInt& aSettingsVersion, TSqlCompactionMode& aCompactionMode)
	{
	__ASSERT_DEBUG(iDbHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));

	HBufC* buf = HBufC::NewLC(sizeof(KGetSettingsSql) + aDbName.Length());
	TPtr sql = buf->Des();
			
	//Prepare statement handle
	sql.Format(KGetSettingsSql(), &aDbName);
	sqlite3_stmt* stmtHandle = ::StmtPrepare16L(iDbHandle, sql);
	CleanupStack::PushL(TCleanupItem(&FinalizeStatementHandle, stmtHandle));
	//Move to the first record
	TInt err = ::StmtNext(stmtHandle);
	__SQLLEAVE_IF_ERROR(err);
	//Check that it is a valid row. The error is checked on the previous line. 
	//The "if" bellow will check whether there is a valid record or not.
	if(err != KSqlAtRow)
		{
		__SQLLEAVE(KErrGeneral);
		}
	//Get the system settings version 
	aSettingsVersion = sqlite3_column_int(stmtHandle, KSysVersionColIdx);
	if(aSettingsVersion < ESqlSystemVersion1)
		{
		__SQLLEAVE(KErrGeneral);
		}
	if(aSettingsVersion > ESqlSystemVersion2)
		{
		//The "ConfigFileVersion" column exists and is used to store the most recent
		//version of the database's config file (if there is one) that has 
		//been successfully processed
		aDbConfigFileVersion = sqlite3_column_int(stmtHandle, KConfigFileVersionColIdx);
		if(aDbConfigFileVersion < KSqlNullDbConfigFileVersion)
			{
			__SQLLEAVE(KErrGeneral);
			}
			
		//The "CollationDllName" column exists and its value can be read.
        //The column type might be different than SQLITE_TEXT - malformed database.
        if(sqlite3_column_type(stmtHandle, KCollationDllNameColIdx) != SQLITE_TEXT)
            {
            __SQLLEAVE(KErrGeneral);   
            }
		const void* ptr = sqlite3_column_text16(stmtHandle, KCollationDllNameColIdx);
        //Null column value - this might be an indication of an "out of memory" problem, if the column text  
        //is in UTF8 format. (sqlite3_column_text16() may allocate memory for UTF8->UTF16 conversion)
		__SQLLEAVE_IF_NULL(const_cast<void*>(ptr));
        TPtrC16 src(reinterpret_cast <const TUint16*> (ptr));
        if(src.Length() > aCollationDllName.MaxLength())
            {
            __SQLLEAVE(KErrOverflow);	
            }
        aCollationDllName.Copy(src);
		}
	if(aSettingsVersion > ESqlSystemVersion3)
		{
		aCompactionMode = static_cast <TSqlCompactionMode> (sqlite3_column_int(stmtHandle, KCompactionModeColIdx));
		if(aCompactionMode != ESqlCompactionManual && aCompactionMode != ESqlCompactionBackground && aCompactionMode != ESqlCompactionAuto)
			{
			__SQLLEAVE(KErrGeneral);
			}
		}
	CleanupStack::PopAndDestroy();//TCleanupItem(&FinalizeStatementHandle, stmtHandle)	
	CleanupStack::PopAndDestroy(buf);
	}