[libcalamares] Tidy PartitionSize
- Use unitsComparable where applicable - Use SizeUnit instead of unit_t -- since this is a template specialization, we have the more meaningful type name to use, instead of the generic one.
This commit is contained in:
parent
72e1a36752
commit
90975b62bf
@ -1,6 +1,7 @@
|
|||||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
*
|
*
|
||||||
* Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
|
* Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
|
||||||
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||||
*
|
*
|
||||||
* Calamares is free software: you can redistribute it and/or modify
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -42,23 +43,23 @@ unitSuffixes()
|
|||||||
PartitionSize::PartitionSize( const QString& s )
|
PartitionSize::PartitionSize( const QString& s )
|
||||||
: NamedSuffix( unitSuffixes(), s )
|
: NamedSuffix( unitSuffixes(), s )
|
||||||
{
|
{
|
||||||
if ( ( unit() == unit_t::Percent ) && ( value() > 100 || value() < 0 ) )
|
if ( ( unit() == SizeUnit::Percent ) && ( value() > 100 || value() < 0 ) )
|
||||||
{
|
{
|
||||||
cDebug() << "Percent value" << value() << "is not valid.";
|
cDebug() << "Percent value" << value() << "is not valid.";
|
||||||
m_value = 0;
|
m_value = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( m_unit == unit_t::None )
|
if ( m_unit == SizeUnit::None )
|
||||||
{
|
{
|
||||||
m_value = s.toInt();
|
m_value = s.toInt();
|
||||||
if ( m_value > 0 )
|
if ( m_value > 0 )
|
||||||
m_unit = unit_t::Byte;
|
m_unit = SizeUnit::Byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( m_value <= 0 )
|
if ( m_value <= 0 )
|
||||||
{
|
{
|
||||||
m_value = 0;
|
m_value = 0;
|
||||||
m_unit = unit_t::None;
|
m_unit = SizeUnit::None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,17 +73,17 @@ PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
|
|||||||
|
|
||||||
switch ( m_unit )
|
switch ( m_unit )
|
||||||
{
|
{
|
||||||
case unit_t::None:
|
case SizeUnit::None:
|
||||||
return -1;
|
return -1;
|
||||||
case unit_t::Percent:
|
case SizeUnit::Percent:
|
||||||
if ( value() == 100 )
|
if ( value() == 100 )
|
||||||
return totalSectors; // Common-case, avoid futzing around
|
return totalSectors; // Common-case, avoid futzing around
|
||||||
else
|
else
|
||||||
return totalSectors * value() / 100;
|
return totalSectors * value() / 100;
|
||||||
case unit_t::Byte:
|
case SizeUnit::Byte:
|
||||||
case unit_t::KiB:
|
case SizeUnit::KiB:
|
||||||
case unit_t::MiB:
|
case SizeUnit::MiB:
|
||||||
case unit_t::GiB:
|
case SizeUnit::GiB:
|
||||||
return CalamaresUtils::bytesToSectors ( toBytes(), sectorSize );
|
return CalamaresUtils::bytesToSectors ( toBytes(), sectorSize );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,19 +98,19 @@ PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
|
|||||||
|
|
||||||
switch ( m_unit )
|
switch ( m_unit )
|
||||||
{
|
{
|
||||||
case unit_t::None:
|
case SizeUnit::None:
|
||||||
return -1;
|
return -1;
|
||||||
case unit_t::Percent:
|
case SizeUnit::Percent:
|
||||||
if ( totalSectors < 1 || sectorSize < 1 )
|
if ( totalSectors < 1 || sectorSize < 1 )
|
||||||
return -1;
|
return -1;
|
||||||
if ( value() == 100 )
|
if ( value() == 100 )
|
||||||
return totalSectors * sectorSize; // Common-case, avoid futzing around
|
return totalSectors * sectorSize; // Common-case, avoid futzing around
|
||||||
else
|
else
|
||||||
return totalSectors * value() / 100;
|
return totalSectors * value() / 100;
|
||||||
case unit_t::Byte:
|
case SizeUnit::Byte:
|
||||||
case unit_t::KiB:
|
case SizeUnit::KiB:
|
||||||
case unit_t::MiB:
|
case SizeUnit::MiB:
|
||||||
case unit_t::GiB:
|
case SizeUnit::GiB:
|
||||||
return toBytes();
|
return toBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,19 +126,19 @@ PartitionSize::toBytes( qint64 totalBytes ) const
|
|||||||
|
|
||||||
switch ( m_unit )
|
switch ( m_unit )
|
||||||
{
|
{
|
||||||
case unit_t::None:
|
case SizeUnit::None:
|
||||||
return -1;
|
return -1;
|
||||||
case unit_t::Percent:
|
case SizeUnit::Percent:
|
||||||
if ( totalBytes < 1 )
|
if ( totalBytes < 1 )
|
||||||
return -1;
|
return -1;
|
||||||
if ( value() == 100 )
|
if ( value() == 100 )
|
||||||
return totalBytes; // Common-case, avoid futzing around
|
return totalBytes; // Common-case, avoid futzing around
|
||||||
else
|
else
|
||||||
return totalBytes * value() / 100;
|
return totalBytes * value() / 100;
|
||||||
case unit_t::Byte:
|
case SizeUnit::Byte:
|
||||||
case unit_t::KiB:
|
case SizeUnit::KiB:
|
||||||
case unit_t::MiB:
|
case SizeUnit::MiB:
|
||||||
case unit_t::GiB:
|
case SizeUnit::GiB:
|
||||||
return toBytes();
|
return toBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,16 +154,16 @@ PartitionSize::toBytes() const
|
|||||||
|
|
||||||
switch ( m_unit )
|
switch ( m_unit )
|
||||||
{
|
{
|
||||||
case unit_t::None:
|
case SizeUnit::None:
|
||||||
case unit_t::Percent:
|
case SizeUnit::Percent:
|
||||||
return -1;
|
return -1;
|
||||||
case unit_t::Byte:
|
case SizeUnit::Byte:
|
||||||
return value();
|
return value();
|
||||||
case unit_t::KiB:
|
case SizeUnit::KiB:
|
||||||
return CalamaresUtils::KiBtoBytes( static_cast<unsigned long long>( value() ) );
|
return CalamaresUtils::KiBtoBytes( static_cast<unsigned long long>( value() ) );
|
||||||
case unit_t::MiB:
|
case SizeUnit::MiB:
|
||||||
return CalamaresUtils::MiBtoBytes( static_cast<unsigned long long>( value() ) );
|
return CalamaresUtils::MiBtoBytes( static_cast<unsigned long long>( value() ) );
|
||||||
case unit_t::GiB:
|
case SizeUnit::GiB:
|
||||||
return CalamaresUtils::GiBtoBytes( static_cast<unsigned long long>( value() ) );
|
return CalamaresUtils::GiBtoBytes( static_cast<unsigned long long>( value() ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,19 +171,17 @@ PartitionSize::toBytes() const
|
|||||||
bool
|
bool
|
||||||
PartitionSize::operator< ( const PartitionSize& other ) const
|
PartitionSize::operator< ( const PartitionSize& other ) const
|
||||||
{
|
{
|
||||||
if ( ( m_unit == unit_t::None || other.m_unit == unit_t::None ) ||
|
if ( !unitsComparable( m_unit, other.m_unit ) )
|
||||||
( m_unit == unit_t::Percent && other.m_unit != unit_t::Percent ) ||
|
|
||||||
( m_unit != unit_t::Percent && other.m_unit == unit_t::Percent ) )
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch ( m_unit )
|
switch ( m_unit )
|
||||||
{
|
{
|
||||||
case unit_t::Percent:
|
case SizeUnit::Percent:
|
||||||
return ( m_value < other.m_value );
|
return ( m_value < other.m_value );
|
||||||
case unit_t::Byte:
|
case SizeUnit::Byte:
|
||||||
case unit_t::KiB:
|
case SizeUnit::KiB:
|
||||||
case unit_t::MiB:
|
case SizeUnit::MiB:
|
||||||
case unit_t::GiB:
|
case SizeUnit::GiB:
|
||||||
return ( toBytes() < other.toBytes () );
|
return ( toBytes() < other.toBytes () );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,19 +191,17 @@ PartitionSize::operator< ( const PartitionSize& other ) const
|
|||||||
bool
|
bool
|
||||||
PartitionSize::operator> ( const PartitionSize& other ) const
|
PartitionSize::operator> ( const PartitionSize& other ) const
|
||||||
{
|
{
|
||||||
if ( ( m_unit == unit_t::None || other.m_unit == unit_t::None ) ||
|
if ( !unitsComparable( m_unit, other.m_unit ) )
|
||||||
( m_unit == unit_t::Percent && other.m_unit != unit_t::Percent ) ||
|
|
||||||
( m_unit != unit_t::Percent && other.m_unit == unit_t::Percent ) )
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch ( m_unit )
|
switch ( m_unit )
|
||||||
{
|
{
|
||||||
case unit_t::Percent:
|
case SizeUnit::Percent:
|
||||||
return ( m_value > other.m_value );
|
return ( m_value > other.m_value );
|
||||||
case unit_t::Byte:
|
case SizeUnit::Byte:
|
||||||
case unit_t::KiB:
|
case SizeUnit::KiB:
|
||||||
case unit_t::MiB:
|
case SizeUnit::MiB:
|
||||||
case unit_t::GiB:
|
case SizeUnit::GiB:
|
||||||
return ( toBytes() > other.toBytes () );
|
return ( toBytes() > other.toBytes () );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,19 +211,17 @@ PartitionSize::operator> ( const PartitionSize& other ) const
|
|||||||
bool
|
bool
|
||||||
PartitionSize::operator== ( const PartitionSize& other ) const
|
PartitionSize::operator== ( const PartitionSize& other ) const
|
||||||
{
|
{
|
||||||
if ( ( m_unit == unit_t::None || other.m_unit == unit_t::None ) ||
|
if ( !unitsComparable( m_unit, other.m_unit ) )
|
||||||
( m_unit == unit_t::Percent && other.m_unit != unit_t::Percent ) ||
|
|
||||||
( m_unit != unit_t::Percent && other.m_unit == unit_t::Percent ) )
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch ( m_unit )
|
switch ( m_unit )
|
||||||
{
|
{
|
||||||
case unit_t::Percent:
|
case SizeUnit::Percent:
|
||||||
return ( m_value == other.m_value );
|
return ( m_value == other.m_value );
|
||||||
case unit_t::Byte:
|
case SizeUnit::Byte:
|
||||||
case unit_t::KiB:
|
case SizeUnit::KiB:
|
||||||
case unit_t::MiB:
|
case SizeUnit::MiB:
|
||||||
case unit_t::GiB:
|
case SizeUnit::GiB:
|
||||||
return ( toBytes() == other.toBytes () );
|
return ( toBytes() == other.toBytes () );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ class PartitionSize : public NamedSuffix<SizeUnit, SizeUnit::None>
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PartitionSize() : NamedSuffix() { }
|
PartitionSize() : NamedSuffix() { }
|
||||||
PartitionSize( int v, unit_t u ) : NamedSuffix( v, u ) { }
|
PartitionSize( int v, SizeUnit u ) : NamedSuffix( v, u ) { }
|
||||||
PartitionSize( const QString& );
|
PartitionSize( const QString& );
|
||||||
|
|
||||||
bool isValid() const
|
bool isValid() const
|
||||||
|
Loading…
Reference in New Issue
Block a user