1/*
2 * Copyright 2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * 		Hugo Santos <hugosantos@gmail.com>
7 * 		Ingo Weinhold <bonefish@cs.tu-berlin.de>
8 */
9
10#include "Context.h"
11
12#include <stdio.h>
13#include <string.h>
14
15string
16Context::FormatSigned(int64 value, int bytes) const
17{
18	char tmp[32];
19
20	// decimal
21
22	if (fDecimal) {
23		snprintf(tmp, sizeof(tmp), "%" B_PRId64, value);
24		return tmp;
25	}
26
27	// hex
28
29	snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
30
31	// Negative numbers are expanded when being converted to int64. Hence
32	// we skip all but the last 2 * bytes hex digits to retain the original
33	// type's width.
34	int len = strlen(tmp);
35	int offset = len - min_c(len, bytes * 2);
36
37	// use the existing "0x" prefix or prepend it again
38	if (offset <= 2) {
39		offset = 0;
40	} else {
41		tmp[--offset] = 'x';
42		tmp[--offset] = '0';
43	}
44
45	return tmp + offset;
46}
47
48string
49Context::FormatUnsigned(uint64 value) const
50{
51	char tmp[32];
52	snprintf(tmp, sizeof(tmp), fDecimal ? "%" B_PRIu64 : "0x%" B_PRIx64, value);
53	return tmp;
54}
55
56string
57Context::FormatFlags(uint64 value) const
58{
59	char tmp[32];
60	snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
61	return tmp;
62}
63
64string
65Context::FormatPointer(const void *address) const
66{
67	char buffer[32];
68	sprintf(buffer, "%p", address);
69	return buffer;
70}
71