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