📄 stringloader.cpp
字号:
/*
* StringLoader.cpp
*
* Copyright 2005 - 2007, Antony Pranata
* http://www.antonypranata.com
*
* Project: Screenshot for Symbian OS.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "StringLoader.h"
void StringLoader::Load(TDes& aDest, TInt aResourceId)
{
CEikonEnv::Static()->ReadResource(aDest, aResourceId);
}
HBufC* StringLoader::LoadLC(TInt aResourceId)
{
CEikonEnv* eikonEnv = CEikonEnv::Static();
return eikonEnv->AllocReadResourceLC(aResourceId);
}
HBufC* StringLoader::LoadLC(TInt aResourceId, TInt aInt)
{
_LIT(KFormatNumber, "%N");
// Gets the resource string and then reallocate the buffer so that aString
// can be inserted in the middle.
TBuf<5> number;
number.AppendNum(aInt);
HBufC* resource = CEikonEnv::Static()->AllocReadResourceLC(aResourceId);
HBufC* buffer = HBufC::NewL(
resource->Length() + number.Length() - KFormatNumber().Length());
// Removes the %N and then inserts aInt.
TPtr bufferPtr(buffer->Des());
bufferPtr.Append(*resource);
CleanupStack::PopAndDestroy(); // resource
TInt index = bufferPtr.Find(KFormatNumber);
if (index >= 0)
{
bufferPtr.Delete(index, KFormatNumber().Length());
bufferPtr.Insert(index, number);
}
CleanupStack::PushL(buffer); // have to push it because "C" at LoadLC
return buffer;
}
HBufC* StringLoader::LoadLC(TInt aResourceId, const TDesC& aString)
{
_LIT(KFormatUnicode, "%U");
// Gets the resource string and then reallocate the buffer so that aString
// can be inserted in the middle.
HBufC* resource = CEikonEnv::Static()->AllocReadResourceLC(aResourceId);
HBufC* buffer = HBufC::NewL(
resource->Length() + aString.Length() - KFormatUnicode().Length());
// Removes the %U and then inserts aString.
TPtr bufferPtr(buffer->Des());
bufferPtr.Append(*resource);
CleanupStack::PopAndDestroy(); // resource
TInt index = bufferPtr.Find(KFormatUnicode);
if (index >= 0)
{
bufferPtr.Delete(index, KFormatUnicode().Length());
bufferPtr.Insert(index, aString);
}
CleanupStack::PushL(buffer); // have to push it because "C" at LoadLC
return buffer;
}
// This function will replace all %XU with aStrings.
HBufC* StringLoader::LoadLC(TInt aResourceId, const MDesCArray& aStrings)
{
HBufC* resource = CEikonEnv::Static()->AllocReadResourceLC(aResourceId);
// Gets the total length of aStrings.
TInt length = 0;
for (TInt i = 0; i < aStrings.MdcaCount(); i++)
{
length += aStrings.MdcaPoint(i).Length();
}
// Allocates a new descriptor. We don't know the maximum length,
// so we just use the maximum value.
HBufC* buffer = HBufC::NewL(resource->Length() + length);
TPtr bufferPtr(buffer->Des());
bufferPtr.Append(*resource);
CleanupStack::PopAndDestroy(); // resource
TInt j = 0;
TInt index = -1; // index of %
while (j < bufferPtr.Length())
{
// If this is % sign, then save this index.
if (bufferPtr[j] == '%')
{
index = j;
}
// If we found U and index is greater or equal than 0, it means
// we have to insert aStrings[X].
else if ((bufferPtr[j] == 'U') && (index >= 0))
{
TLex lex(bufferPtr.Mid(index + 1, j - index));
TInt number;
lex.Val(number);
bufferPtr.Delete(index, j - index + 1);
if (number < aStrings.MdcaCount())
{
bufferPtr.Insert(index, aStrings.MdcaPoint(number));
j = index + aStrings.MdcaPoint(number).Length() - 1;
}
index = 0;
}
j++;
}
CleanupStack::PushL(buffer);
return buffer;
}
// This function will replace all %XN with aInts and all %XU with aStrings.
// Example: "Analysing\n%0N/%1N\nTime left\n%0U:%1U"
HBufC* StringLoader::LoadLC(TInt aResourceId, const MDesCArray& aStrings,
const CArrayFix<TInt>& aInts)
{
HBufC* resource = CEikonEnv::Static()->AllocReadResourceLC(aResourceId);
// Gets the total length of aStrings and aInts.
TInt length = 0;
TInt i = 0;
for (i = 0; i < aStrings.MdcaCount(); i++)
{
length += aStrings.MdcaPoint(i).Length();
}
for (i = 0; i < aInts.Count(); i++)
{
TInt number = aInts.At(i);
TBuf<6> numberStr;
numberStr.AppendNum(number);
length += numberStr.Length();
}
// Allocates a new descriptor. We don't know the maximum length,
// so we just use the maximum value.
HBufC* buffer = HBufC::NewL(resource->Length() + length);
TPtr bufferPtr(buffer->Des());
bufferPtr.Append(*resource);
CleanupStack::PopAndDestroy(); // resource
TInt j = 0;
TInt index = -1; // index of %
while (j < bufferPtr.Length())
{
// If this is % sign, then save this index.
if (bufferPtr[j] == '%')
{
index = j;
}
// If we found U and index is greater or equal than 0, it means
// we have to insert aStrings[X].
else if ((bufferPtr[j] == 'U') && (index >= 0))
{
TLex lex(bufferPtr.Mid(index + 1, j - index));
TInt number;
lex.Val(number);
if (number < aStrings.MdcaCount())
{
bufferPtr.Delete(index, j - index + 1);
bufferPtr.Insert(index, aStrings.MdcaPoint(number));
j = index + aStrings.MdcaPoint(number).Length() - 1;
}
index = 0;
}
// If we found N and index is greater or equal than 0, it means
// we have to insert aInts[X].
else if ((bufferPtr[j] == 'N') && (index >= 0))
{
TLex lex(bufferPtr.Mid(index + 1, j - index));
TInt number;
lex.Val(number);
if (number < aInts.Count())
{
bufferPtr.Delete(index, j - index + 1);
number = aInts.At(number);
TBuf<6> numberStr;
numberStr.AppendNum(number);
bufferPtr.Insert(index, numberStr);
j = index + numberStr.Length() - 1;
}
index = 0;
}
j++;
}
CleanupStack::PushL(buffer);
return buffer;
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -