[fsresizer] Simplify and make safer
- Make RelativeSize public so we can use it in non-member functions - Make a template out of matching the string suffixes; this is safer because the length of the suffix can be computed at compile-time (+1 for the trailing NUL) rather than writing it out in boilerplate.
This commit is contained in:
parent
cdfb55e5cf
commit
a81588190a
@ -29,32 +29,36 @@
|
||||
#include "utils/Logger.h"
|
||||
|
||||
ResizeFSJob::RelativeSize::RelativeSize()
|
||||
: m_unit( None )
|
||||
, m_value( 0 )
|
||||
: m_value( 0 )
|
||||
, m_unit( None )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ResizeFSJob::RelativeSize::RelativeSize( const QString& s)
|
||||
: m_unit( None )
|
||||
, m_value( 0 )
|
||||
template<int N>
|
||||
void matchUnitSuffix(
|
||||
const QString& s,
|
||||
const char (&suffix)[N],
|
||||
ResizeFSJob::RelativeSize::Unit matchedUnit,
|
||||
int& value,
|
||||
ResizeFSJob::RelativeSize::Unit& unit
|
||||
)
|
||||
{
|
||||
QString valuePart;
|
||||
|
||||
if ( s.endsWith( '%' ) )
|
||||
if ( s.endsWith( suffix ) )
|
||||
{
|
||||
valuePart = s.left( s.length() - 1 );
|
||||
m_unit = Percent;
|
||||
value = s.left( s.length() - N + 1 ).toInt();
|
||||
unit = matchedUnit;
|
||||
}
|
||||
if ( s.endsWith( "MiB" ) )
|
||||
{
|
||||
valuePart = s.left( s.length() - 3 );
|
||||
m_unit = Absolute;
|
||||
}
|
||||
|
||||
if ( ( m_unit != None ) && !valuePart.isEmpty() )
|
||||
m_value = valuePart.toInt();
|
||||
|
||||
}
|
||||
|
||||
|
||||
ResizeFSJob::RelativeSize::RelativeSize( const QString& s)
|
||||
: m_value( 0 )
|
||||
, m_unit( None )
|
||||
{
|
||||
matchUnitSuffix( s, "%", Percent, m_value, m_unit );
|
||||
matchUnitSuffix( s, "MiB", Absolute, m_value, m_unit );
|
||||
|
||||
if ( !m_value )
|
||||
m_unit = None;
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
class RelativeSize
|
||||
{
|
||||
public:
|
||||
@ -45,15 +46,14 @@ class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob
|
||||
Absolute
|
||||
};
|
||||
|
||||
quint64 value() const { return m_value; }
|
||||
int value() const { return m_value; }
|
||||
Unit unit() const { return m_unit; }
|
||||
|
||||
private:
|
||||
quint64 m_value;
|
||||
int m_value;
|
||||
Unit m_unit;
|
||||
} ;
|
||||
|
||||
public:
|
||||
explicit ResizeFSJob( QObject* parent = nullptr );
|
||||
virtual ~ResizeFSJob() override;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user