Extract parse_size function from ramdisk command.
Change-Id: If7dd36321e1ed2feb20b4c76ddaf303bc997d8b7 Reviewed-on: https://review.haiku-os.org/c/1018 Reviewed-by: Jérôme Duval <jerome.duval@gmail.com> Reviewed-by: Axel Dörfler <axeld@pinc-software.de>
This commit is contained in:
parent
0ca56bdecd
commit
7232789211
@ -12,12 +12,14 @@ namespace BPrivate {
|
||||
|
||||
|
||||
const char* string_for_size(double size, char* string, size_t stringSize);
|
||||
int64 parse_size(const char* sizeString);
|
||||
|
||||
|
||||
} // namespace BPrivate
|
||||
|
||||
|
||||
using BPrivate::string_for_size;
|
||||
using BPrivate::parse_size;
|
||||
|
||||
|
||||
#endif // STRING_FOR_SIZE_H
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <String.h>
|
||||
|
||||
#include <AutoDeleter.h>
|
||||
#include <StringForSize.h>
|
||||
#include <TextTable.h>
|
||||
|
||||
#include <file_systems/ram_disk/ram_disk.h>
|
||||
@ -140,29 +141,7 @@ command_register(int argc, const char* const* argv)
|
||||
case 's':
|
||||
{
|
||||
const char* sizeString = optarg;
|
||||
char* end;
|
||||
deviceSize = strtoll(sizeString, &end, 0);
|
||||
if (end != sizeString && deviceSize > 0) {
|
||||
int64 originalDeviceSize = deviceSize;
|
||||
switch (*end) {
|
||||
case 'g':
|
||||
deviceSize *= 1024;
|
||||
case 'm':
|
||||
deviceSize *= 1024;
|
||||
case 'k':
|
||||
deviceSize *= 1024;
|
||||
end++;
|
||||
break;
|
||||
case '\0':
|
||||
break;
|
||||
default:
|
||||
deviceSize = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (deviceSize > 0 && originalDeviceSize > deviceSize)
|
||||
deviceSize = -1;
|
||||
}
|
||||
deviceSize = parse_size(sizeString);
|
||||
|
||||
if (deviceSize <= 0) {
|
||||
fprintf(stderr, "Error: Invalid size argument: \"%s\"\n",
|
||||
|
@ -1,11 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010 Haiku Inc. All rights reserved.
|
||||
* Copyright 2010-2019 Haiku Inc. All rights reserved.
|
||||
* Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include "StringForSize.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <StringFormat.h>
|
||||
#include <SystemCatalog.h>
|
||||
@ -64,5 +67,40 @@ string_for_size(double size, char* string, size_t stringSize)
|
||||
}
|
||||
|
||||
|
||||
int64
|
||||
parse_size(const char* sizeString)
|
||||
{
|
||||
int64 parsedSize = -1;
|
||||
char* end;
|
||||
parsedSize = strtoll(sizeString, &end, 0);
|
||||
if (end != sizeString && parsedSize > 0) {
|
||||
int64 rawSize = parsedSize;
|
||||
switch (tolower(*end)) {
|
||||
case 't':
|
||||
parsedSize *= 1024;
|
||||
case 'g':
|
||||
parsedSize *= 1024;
|
||||
case 'm':
|
||||
parsedSize *= 1024;
|
||||
case 'k':
|
||||
parsedSize *= 1024;
|
||||
end++;
|
||||
break;
|
||||
case '\0':
|
||||
break;
|
||||
default:
|
||||
parsedSize = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
// Check for overflow
|
||||
if (parsedSize > 0 && rawSize > parsedSize)
|
||||
parsedSize = -1;
|
||||
}
|
||||
|
||||
return parsedSize;
|
||||
}
|
||||
|
||||
|
||||
} // namespace BPrivate
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user