From 7d4508b161ec50413f03b4ced29f35df8e2de455 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Thu, 5 Nov 2009 17:39:09 +0000 Subject: [PATCH] Utility class to format integer values. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33905 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/debugger/util/IntegerFormatter.cpp | 72 +++++++++++++++++++++ src/apps/debugger/util/IntegerFormatter.h | 32 +++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/apps/debugger/util/IntegerFormatter.cpp create mode 100644 src/apps/debugger/util/IntegerFormatter.h diff --git a/src/apps/debugger/util/IntegerFormatter.cpp b/src/apps/debugger/util/IntegerFormatter.cpp new file mode 100644 index 0000000000..818b4f308c --- /dev/null +++ b/src/apps/debugger/util/IntegerFormatter.cpp @@ -0,0 +1,72 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include "IntegerFormatter.h" + +#include + +#include + + +/*static*/ bool +IntegerFormatter::FormatValue(const BVariant& value, integer_format format, + char* buffer, size_t bufferSize) +{ + bool isSigned; + if (!value.IsInteger(&isSigned)) + return false; + + if (format == INTEGER_FORMAT_DEFAULT) + format = isSigned ? INTEGER_FORMAT_SIGNED : INTEGER_FORMAT_UNSIGNED; + + if (format == INTEGER_FORMAT_HEX_DEFAULT) { + switch (value.Type()) { + case B_INT8_TYPE: + case B_UINT8_TYPE: + format = INTEGER_FORMAT_HEX_8; + break; + case B_INT16_TYPE: + case B_UINT16_TYPE: + format = INTEGER_FORMAT_HEX_16; + break; + case B_INT32_TYPE: + case B_UINT32_TYPE: + format = INTEGER_FORMAT_HEX_32; + break; + case B_INT64_TYPE: + case B_UINT64_TYPE: + default: + format = INTEGER_FORMAT_HEX_64; + break; + } + } + + // format the value + switch (format) { + case INTEGER_FORMAT_SIGNED: + snprintf(buffer, bufferSize, "%lld", value.ToInt64()); + break; + case INTEGER_FORMAT_UNSIGNED: + snprintf(buffer, bufferSize, "%llu", value.ToUInt64()); + break; + case INTEGER_FORMAT_HEX_8: + snprintf(buffer, bufferSize, "%#x", (uint8)value.ToUInt64()); + break; + case INTEGER_FORMAT_HEX_16: + snprintf(buffer, bufferSize, "%#x", (uint16)value.ToUInt64()); + break; + case INTEGER_FORMAT_HEX_32: + snprintf(buffer, bufferSize, "%#lx", (uint32)value.ToUInt64()); + break; + case INTEGER_FORMAT_HEX_64: + case INTEGER_FORMAT_HEX_DEFAULT: + default: + snprintf(buffer, bufferSize, "%#llx", value.ToUInt64()); + break; + } + + return true; +} diff --git a/src/apps/debugger/util/IntegerFormatter.h b/src/apps/debugger/util/IntegerFormatter.h new file mode 100644 index 0000000000..1bf0a1b15e --- /dev/null +++ b/src/apps/debugger/util/IntegerFormatter.h @@ -0,0 +1,32 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef INTEGER_FORMATTER_H +#define INTEGER_FORMATTER_H + + +#include + + +enum integer_format { + INTEGER_FORMAT_DEFAULT = 0, + INTEGER_FORMAT_SIGNED = 1, + INTEGER_FORMAT_UNSIGNED = 2, + INTEGER_FORMAT_HEX_DEFAULT = 3, + INTEGER_FORMAT_HEX_8 = 8, + INTEGER_FORMAT_HEX_16 = 16, + INTEGER_FORMAT_HEX_32 = 32, + INTEGER_FORMAT_HEX_64 = 64 +}; + + +class IntegerFormatter { +public: + static bool FormatValue(const BVariant& value, + integer_format format, char* buffer, + size_t bufferSize); +}; + + +#endif // INTEGER_FORMATTER_H