bool ResizeFileSystemJob::run(Report& parent)
{
	Q_ASSERT(partition().fileSystem().firstSector() != -1);
	Q_ASSERT(partition().fileSystem().lastSector() != -1);
	Q_ASSERT(newLength() <= partition().length());

	if (partition().fileSystem().firstSector() == -1 || partition().fileSystem().lastSector() == -1 || newLength() > partition().length())
	{
		kWarning() << "file system first sector: " << partition().fileSystem().firstSector() << ", last sector: " << partition().fileSystem().lastSector() << ", new length: " << newLength() << ", partition length: " << partition().length();
		return false;
	}

	bool rval = false;

	Report* report = jobStarted(parent);

	if (partition().fileSystem().length() == newLength())
	{
		report->line() << i18ncp("@info/plain", "The file system on partition <filename>%2</filename> already has the requested length of 1 sector.", "The file system on partition <filename>%2</filename> already has the requested length of %1 sectors.", newLength(), partition().deviceNode());
		rval = true;
	}
	else
	{
		report->line() << i18nc("@info/plain", "Resizing file system from %1 to %2 sectors.", partition().fileSystem().length(), newLength());

		FileSystem::CommandSupportType support = (newLength() < partition().fileSystem().length()) ? partition().fileSystem().supportShrink() : partition().fileSystem().supportGrow();

		switch(support)
		{
			case FileSystem::cmdSupportBackend:
			{
				Report* childReport = report->newChild();
				childReport->line() << i18nc("@info/plain", "Resizing a %1 file system using internal backend functions.", partition().fileSystem().name());
				rval = resizeFileSystemBackend(*childReport);
				break;
			}

			case FileSystem::cmdSupportFileSystem:
			{
				const qint64 newLengthInByte = Capacity(newLength() * device().logicalSectorSize()).toInt(Capacity::Byte);
				rval = partition().fileSystem().resize(*report, partition().deviceNode(), newLengthInByte);
				break;
			}

			default:
				report->line() << i18nc("@info/plain", "The file system on partition <filename>%1</filename> cannot be resized because there is no support for it.", partition().deviceNode());
				break;
		}

		if (rval)
			partition().fileSystem().setLastSector(partition().fileSystem().firstSector() + newLength() - 1);
	}

	jobFinished(*report, rval);

	return rval;
}
bool ResizeFileSystemJob::resizeFileSystemBackend(Report& report)
{
	bool rval = false;

	CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device().deviceNode());

	if (backendDevice)
	{
		CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable();

		if (backendPartitionTable)
		{
			connect(CoreBackendManager::self()->backend(), SIGNAL(progress(int)), this, SIGNAL(progress(int)));
			rval = backendPartitionTable->resizeFileSystem(report, partition(), newLength());
			disconnect(CoreBackendManager::self()->backend(), SIGNAL(progress(int)), this, SIGNAL(progress(int)));

			if (rval)
			{
				report.line() << i18nc("@info/plain", "Successfully resized file system using internal backend functions.");
				backendPartitionTable->commit();
			}

			delete backendPartitionTable;
		}
		else
			report.line() << i18nc("@info/plain", "Could not open partition <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());

		delete backendDevice;
	}
EXPORT_C TInt AknPhoneNumberTextUtils::CharsThatFitOnRight(
    const TDesC& aText, 
    TInt aLineWidth, 
    const CFont& aFont )
    {
    // Current candidate text for fitting text
    TPtrC currentText(aText); 

    // These two variables are converged by this algorithm until they differ by one
    // Set max chars to be total length + 1, for the sake of the algorithm
    TInt minCharsFromRightThatDoNotFit( aText.Length() + 1 );
    TInt maxCharsFromRightThatFit(0); // must start at 0

    // some incidentally used lengths 
    TInt currentLength(0);      // arbitrary init value
    TInt charsThatFitOnLeft(0); // arbitrary init value
    TInt newLength(0);          // arbitrary init value

    while ( (minCharsFromRightThatDoNotFit - maxCharsFromRightThatFit) != 1 )
        {
        // At the top of the loop, all state is held by currentText itself, and the two 
        // maxCharsXXX variables.
        currentLength = currentText.Length();
        charsThatFitOnLeft = aFont.TextCount( currentText, aLineWidth);  

        if ( charsThatFitOnLeft == currentLength ) // Does this text fit ?
            {
            maxCharsFromRightThatFit = Max( maxCharsFromRightThatFit, charsThatFitOnLeft );

            // If this text cannot be made any bigger, and still fit, then bail out
            if ( (maxCharsFromRightThatFit + 1) >= minCharsFromRightThatDoNotFit )
                {
                // We can exit the loop now. Just fall out and the while test will handle it.
                break;
                }
            else // if this string did fit, and it might have been too small, try making it bigger
                {
                // we know this is still be < minCharsFromRightThatDoNotFit
                newLength = maxCharsFromRightThatFit + 1; 
                }
            }
        else // currentText does not fit. Repeat loop 
            {
            // we know that this many characters did not fit on the line
            minCharsFromRightThatDoNotFit = Min( minCharsFromRightThatDoNotFit, currentLength );

            // Try using the number that fit on the left as the number we will fit on the right.
            newLength = charsThatFitOnLeft;
            }

        currentText.Set(aText.Right(newLength)); 
        }

    return maxCharsFromRightThatFit;
    }
示例#4
0
文件: ACLStr.cpp 项目: Fahrni/ACLLib
VOID ACLStr::_Grow(IN const size_t appendSize)
{
	//	If the string doesn't need growing then
	//	just bail.
	if (appendSize) 
	{
		size_t  strSize(Len());

		//	Check to see if we've run out of room, remember the
		//	string is padded by xxx bytes. Where xxx is the next
		//	eight byte boundry
		if (_realLength <= (_length+appendSize))
		{
            size_t newRealLength(0);
            LPTSTR newString(NULL);
            size_t newLength(__Alloc(strSize+appendSize, &newString, &newRealLength));
            if (newString && newLength && newRealLength)
            {
                MEMSET(newString, 0, newRealLength);
                STRCPY_S(newString, newRealLength, _string);
                ARRAY_DELETE(_string);
                
                _string = newString;
                _length = newLength;
                _realLength = newRealLength;
            }
		}
		else
		{
			//	If the append size is less than the size of
			//	the REAL buffer size then don't worry about
			//	growing the buffer. Just tell this string
			//	that its' size is now larger.
			_length = (strSize+appendSize);
		}
	}
}	//	::grow