[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"
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
ResizeFSJob::RelativeSize::RelativeSize()
|
ResizeFSJob::RelativeSize::RelativeSize()
|
||||||
: m_unit( None )
|
: m_value( 0 )
|
||||||
, m_value( 0 )
|
, m_unit( None )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ResizeFSJob::RelativeSize::RelativeSize( const QString& s)
|
template<int N>
|
||||||
: m_unit( None )
|
void matchUnitSuffix(
|
||||||
, m_value( 0 )
|
const QString& s,
|
||||||
|
const char (&suffix)[N],
|
||||||
|
ResizeFSJob::RelativeSize::Unit matchedUnit,
|
||||||
|
int& value,
|
||||||
|
ResizeFSJob::RelativeSize::Unit& unit
|
||||||
|
)
|
||||||
{
|
{
|
||||||
QString valuePart;
|
if ( s.endsWith( suffix ) )
|
||||||
|
|
||||||
if ( s.endsWith( '%' ) )
|
|
||||||
{
|
{
|
||||||
valuePart = s.left( s.length() - 1 );
|
value = s.left( s.length() - N + 1 ).toInt();
|
||||||
m_unit = Percent;
|
unit = matchedUnit;
|
||||||
}
|
}
|
||||||
if ( s.endsWith( "MiB" ) )
|
}
|
||||||
{
|
|
||||||
valuePart = s.left( s.length() - 3 );
|
|
||||||
m_unit = Absolute;
|
ResizeFSJob::RelativeSize::RelativeSize( const QString& s)
|
||||||
}
|
: m_value( 0 )
|
||||||
|
, m_unit( None )
|
||||||
if ( ( m_unit != None ) && !valuePart.isEmpty() )
|
{
|
||||||
m_value = valuePart.toInt();
|
matchUnitSuffix( s, "%", Percent, m_value, m_unit );
|
||||||
|
matchUnitSuffix( s, "MiB", Absolute, m_value, m_unit );
|
||||||
|
|
||||||
if ( !m_value )
|
if ( !m_value )
|
||||||
m_unit = None;
|
m_unit = None;
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
class RelativeSize
|
class RelativeSize
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -45,15 +46,14 @@ class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob
|
|||||||
Absolute
|
Absolute
|
||||||
};
|
};
|
||||||
|
|
||||||
quint64 value() const { return m_value; }
|
int value() const { return m_value; }
|
||||||
Unit unit() const { return m_unit; }
|
Unit unit() const { return m_unit; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
quint64 m_value;
|
int m_value;
|
||||||
Unit m_unit;
|
Unit m_unit;
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
public:
|
|
||||||
explicit ResizeFSJob( QObject* parent = nullptr );
|
explicit ResizeFSJob( QObject* parent = nullptr );
|
||||||
virtual ~ResizeFSJob() override;
|
virtual ~ResizeFSJob() override;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user