Extract repeated code into trim_trailing_new_line() (!105)

Create function to replace repeated code which optionally removes
trailing new line character from a string.

Closes !105 - Update used btrfs file system commands, new minimum is
              btrfs-progs 4.5
This commit is contained in:
Mike Fleetwood 2022-08-12 08:38:16 +01:00 committed by Curtis Gedak
parent ee823b0be4
commit 1fbc8988ff
6 changed files with 20 additions and 27 deletions

View File

@ -170,6 +170,7 @@ public:
, const Glib::ustring & pattern
) ;
static Glib::ustring trim( const Glib::ustring & src, const Glib::ustring & c = " \t\r\n" ) ;
static Glib::ustring trim_trailing_new_line(const Glib::ustring& src);
static Glib::ustring last_line( const Glib::ustring & src );
static Glib::ustring get_lang() ;
static void tokenize( const Glib::ustring& str,

View File

@ -572,11 +572,8 @@ std::vector<Glib::ustring> DMRaid::lookup_dmraid_members(const Glib::ustring& ar
Glib::ustring error;
Utils::execute_command("udevadm info --query=name " + Glib::shell_quote(array),
output, error, true);
// Strip terminating new line from output.
size_t len = output.length();
if (len > 0 && output[len-1] == '\n')
output.resize(len-1);
output = Utils::trim_trailing_new_line(output);
if (output.empty())
return members; // Empty vector

View File

@ -105,13 +105,7 @@ Dialog_Partition_Info::Dialog_Partition_Info( const Partition & partition ) : pa
if ( concatenated_messages.size() > 0 )
concatenated_messages += "\n\n";
Glib::ustring::size_type take = messages[i].size();
if ( take > 0 )
{
if ( messages[i][take-1] == '\n' )
take -- ; //Skip optional trailing new line
concatenated_messages += "<i>" + messages[i].substr( 0, take ) + "</i>";
}
concatenated_messages += "<i>" + Utils::trim_trailing_new_line(messages[i]) + "</i>";
}
Gtk::Box *vbox = manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
vbox ->set_border_width( 5 ) ;

View File

@ -289,17 +289,12 @@ bool FS_Info::run_blkid_update_cache_one_label( FS_Entry & fs_entry )
if ( ! success )
return false;
size_t len = output.length();
if ( len > 0 && output[len-1] == '\n' )
{
// Output is either the label with a terminating new line or zero bytes
// when the file system has no label. Strip optional trailing new line
// from blkid output.
output.resize( len-1 );
}
// Update cache entry with the read label.
// Output from blkid is either the label with a trailing new line character or
// zero bytes when the file system has no label. Update the cache entry in both
// cases as the label was successfully read even if it didn't exist so is zero
// characters long.
fs_entry.have_label = true;
fs_entry.label = output;
fs_entry.label = Utils::trim_trailing_new_line(output);
return true;
}

View File

@ -809,6 +809,17 @@ Glib::ustring Utils::trim( const Glib::ustring & src, const Glib::ustring & c /*
return src.substr(p1, (p2-p1)+1);
}
// Return string with optional trailing new line character removed.
Glib::ustring Utils::trim_trailing_new_line(const Glib::ustring& src)
{
Glib::ustring::size_type len = src.length();
if (len > 0 && src[len-1] == '\n')
len --;
return src.substr(0, len);
}
// Return portion of string after the last carriage return character or
// the whole string when there is no carriage return character.
Glib::ustring Utils::last_line( const Glib::ustring & src )

View File

@ -357,12 +357,7 @@ void btrfs::read_label(Partition& partition)
return;
}
// Strip terminating new line from output.
size_t len = output.length();
if (len > 0 && output[len-1] == '\n')
output.resize(len-1);
partition.set_filesystem_label(output);
partition.set_filesystem_label(Utils::trim_trailing_new_line(output));
}