Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=150f3135782be366f9d3df3c1a700bd5b54af5e0
commit 150f3135782be366f9d3df3c1a700bd5b54af5e0 Author: James Buren <[email protected]> Date: Sat Aug 18 23:43:23 2012 -0500 convert function for converting string to size to C++ diff --git a/Utility.cc b/Utility.cc index 6b5f7db..e745af2 100644 --- a/Utility.cc +++ b/Utility.cc @@ -1,6 +1,8 @@ #include <fcntl.h> #include <unistd.h> #include <string.h> +#include <errno.h> +#include <stdlib.h> #include "Utility.hh" #define LOGFILE "fwsetup.log" @@ -32,6 +34,46 @@ pid_t execute(const string &cmd) return pid; } +unsigned long long string_to_size(const string &text) +{ + unsigned long long n = 0; + char *p = 0; + string suffix; + + errno = 0; + + n = strtoull(text.c_str(),&p,10); + + if(errno != 0) + return 0; + + if(n == 0) + { + errno = EINVAL; + return 0; + } + + suffix = p; + + if(suffix.empty() || suffix == "B" || suffix == "BiB") + n *= 1; + else if(suffix == "K" || suffix == "KiB") + n *= KIBIBYTE; + else if(suffix == "M" || suffix == "MiB") + n *= MEBIBYTE; + else if(suffix == "G" || suffix == "GiB") + n *= GIBIBYTE; + else if(suffix == "T" || suffix == "TiB") + n *= TEBIBYTE; + else + { + errno = EINVAL; + return 0; + } + + return n; +} + #ifdef NEWT bool get_text_size(const string &text,int &width,int &height) { diff --git a/Utility.hh b/Utility.hh index 7e47a85..ee6dca0 100644 --- a/Utility.hh +++ b/Utility.hh @@ -3,9 +3,15 @@ #include <sys/types.h> #include <string> +#define KIBIBYTE (2LLU << 9LLU) +#define MEBIBYTE (2LLU << 19LLU) +#define GIBIBYTE (2LLU << 29LLU) +#define TEBIBYTE (2LLU << 39LLU) + using std::string; pid_t execute(const string &cmd); +unsigned long long string_to_size(const string &text); #ifdef NEWT bool get_text_size(const string &text,int &width,int &height); _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
