Read-only archive of the All About Symbian forum (2001–2013) · About this archive

TBuf8 -> TInt32 and TBuf8 -> TInt16 or SEEK for RFileReadStream

2 replies · 4,117 views · Started 13 September 2005

Are there some conversion functions, which can retrieve from an TBuf8 buffer the TInt32 and TInt16 values?

I am trying to parse an residual SIS file (which was probably created using RFileWriteStream class) and I want to read the whole content of the file to an TBuf8 buffer using RFile class and after retrieve data, which I need. It is usually a length or a pointer to some data inside the file coded as TInt32 or TInt16 value.

Example of one TInt32 value and one TInt16 value:

buffer: 0x03 0x00 0x00 0x00 0x08 0x00

TInt32 value = 3
TInt16 value = 8

To avoid this problem I tried to use the RFileReadStream class, because there are already functions to do that (ReadInt32L(), ReadInt16L(), etc.). But in this case, I was not able to read several times from a specific position of the file. Always I had to close the file, open it again at the specific position, read the data I needed (e.g. another pointer), close it, open it again, etc...

If both are possible, which solution is better?

Thanks a lot for any help...

Because I didn't find any better way, I created simple functions to get the same functionality for my buffer as it is available when you use the streaming functions.

I have created following functions:

TInt readInt(const TDes8& buf, TInt offset, TInt numBytes) {
TInt result = 0;
for (TInt i = numBytes; i > 0; i-- ) {
result *= 0x100;
result += buf[offset + i - 1];
}
return result;
}

TInt readInt32(const TDes8& buf, TInt offset) {
return readInt(buf, offset, 4);
}

TInt readInt16(const TDes8& buf, TInt offset) {
return readInt(buf, offset, 2);
}

Now I am able to read the whole file to a buffer and to parse it after according my needs.
Usage example:

_LIT(KSisFileName, "c:\\system\\install\\Test1.sis"😉;

// Open a RFs
RFs aFs;
User::LeaveIfError(aFs.Connect());
CleanupClosePushL(aFs);

// Open a file
RFile file;
file.Open(aFs, KSisFileName, EFileRead) ;
CleanupClosePushL(file);

// Read the file
TInt iSize ;
file.Size(iSize) ;
HBufC8* buf = HBufC8::NewL(iSize) ;
TPtr8 pBuf = buf->Des() ;
file.Read(pBuf) ;

// Parse SIS file
TInt num;
TBuf<60> numBuffer;

num = readInt32(pBuf, 0);
numBuffer.Num(num);
iEikonEnv->InfoWinL(_L("UID 1:"😉, numBuffer);

num = readInt16(pBuf, 20);
numBuffer.Num(num);
iEikonEnv->InfoWinL(_L("Number of files:"😉, numBuffer);

num = readInt32(pBuf, 52);
numBuffer.Num(num);
iEikonEnv->InfoWinL(_L("Files pointer:"😉, numBuffer);

// Close the file
CleanupStack::PopAndDestroy(&file);

// Close the RFs
CleanupStack::PopAndDestroy(&aFs);

Maybe it can help someone ...

skauting wrote:Because I didn't find any better way, I created simple functions to get the same functionality for my buffer as it is available when you use the streaming functions.

I have created following functions:

TInt readInt(const TDes8& buf, TInt offset, TInt numBytes) {
TInt result = 0;
for (TInt i = numBytes; i > 0; i-- ) {
result *= 0x100;
result += buf[offset + i - 1];
}
return result;
}

TInt readInt32(const TDes8& buf, TInt offset) {
return readInt(buf, offset, 4);
}

TInt readInt16(const TDes8& buf, TInt offset) {
return readInt(buf, offset, 2);
}

Now I am able to read the whole file to a buffer and to parse it after according my needs.
Usage example:

_LIT(KSisFileName, "c:\\system\\install\\Test1.sis"😉;

// Open a RFs
RFs aFs;
User::LeaveIfError(aFs.Connect());
CleanupClosePushL(aFs);

// Open a file
RFile file;
file.Open(aFs, KSisFileName, EFileRead) ;
CleanupClosePushL(file);

// Read the file
TInt iSize ;
file.Size(iSize) ;
HBufC8* buf = HBufC8::NewL(iSize) ;
TPtr8 pBuf = buf->Des() ;
file.Read(pBuf) ;

// Parse SIS file
TInt num;
TBuf<60> numBuffer;

num = readInt32(pBuf, 0);
numBuffer.Num(num);
iEikonEnv->InfoWinL(_L("UID 1:"😉, numBuffer);

num = readInt16(pBuf, 20);
numBuffer.Num(num);
iEikonEnv->InfoWinL(_L("Number of files:"😉, numBuffer);

num = readInt32(pBuf, 52);
numBuffer.Num(num);
iEikonEnv->InfoWinL(_L("Files pointer:"😉, numBuffer);

// Close the file
CleanupStack::PopAndDestroy(&file);

// Close the RFs
CleanupStack::PopAndDestroy(&aFs);

Maybe it can help someone ...

I also struck with the same. If you get the answer can you post it here