Added units to block sizes
This commit is contained in:
parent
cf604bbc62
commit
6c3c070d8b
37
shared_functions/parse_block_units.cpp
Normal file
37
shared_functions/parse_block_units.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
inline int parse_block_units(const std::string &user_input) {
|
||||
/***
|
||||
inline int parse_block_units(std::string user_input):
|
||||
Parses "block_size" strings such as '10K', '16M', etc
|
||||
|
||||
Returns the equivalent size measured in bytes
|
||||
|
||||
Returns -1 if user_input string is invalid
|
||||
***/
|
||||
std::string last_character = user_input.substr(user_input.length()-1, user_input.length()-1);
|
||||
|
||||
std::string all_but_last_character = user_input.substr(0, user_input.length()-1);
|
||||
|
||||
if (!is_number(all_but_last_character)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (last_character == "K" || last_character == "k") {
|
||||
int blocksize = (int)stol(all_but_last_character);
|
||||
return (blocksize * 1024);
|
||||
}
|
||||
|
||||
if (last_character == "M" || last_character == "m") {
|
||||
int blocksize = (int)stol(all_but_last_character);
|
||||
return (blocksize * 1024 * 1024);
|
||||
}
|
||||
|
||||
/*
|
||||
If we've made it this far,
|
||||
verify user_input is all digits and just pass the original value through
|
||||
*/
|
||||
|
||||
if (!is_number(user_input)) {
|
||||
return -1;
|
||||
}
|
||||
return (int)stol(user_input);
|
||||
}
|
Loading…
Reference in a new issue