1/* 2 * Copyright (C) 2005 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <binder/TextOutput.h> 18 19#include <binder/Debug.h> 20 21#include <utils/String8.h> 22#include <utils/String16.h> 23 24#include <stdio.h> 25#include <stdlib.h> 26#include <string.h> 27 28namespace android { 29 30// --------------------------------------------------------------------------- 31 32TextOutput::TextOutput() { 33} 34 35TextOutput::~TextOutput() { 36} 37 38// --------------------------------------------------------------------------- 39 40TextOutput& operator<<(TextOutput& to, bool val) 41{ 42 if (val) to.print("true", 4); 43 else to.print("false", 5); 44 return to; 45} 46 47TextOutput& operator<<(TextOutput& to, int val) 48{ 49 char buf[16]; 50 sprintf(buf, "%d", val); 51 to.print(buf, strlen(buf)); 52 return to; 53} 54 55TextOutput& operator<<(TextOutput& to, long val) 56{ 57 char buf[16]; 58 sprintf(buf, "%ld", val); 59 to.print(buf, strlen(buf)); 60 return to; 61} 62 63TextOutput& operator<<(TextOutput& to, unsigned int val) 64{ 65 char buf[16]; 66 sprintf(buf, "%u", val); 67 to.print(buf, strlen(buf)); 68 return to; 69} 70 71TextOutput& operator<<(TextOutput& to, unsigned long val) 72{ 73 char buf[16]; 74 sprintf(buf, "%lu", val); 75 to.print(buf, strlen(buf)); 76 return to; 77} 78 79TextOutput& operator<<(TextOutput& to, long long val) 80{ 81 char buf[32]; 82 sprintf(buf, "%Ld", val); 83 to.print(buf, strlen(buf)); 84 return to; 85} 86 87TextOutput& operator<<(TextOutput& to, unsigned long long val) 88{ 89 char buf[32]; 90 sprintf(buf, "%Lu", val); 91 to.print(buf, strlen(buf)); 92 return to; 93} 94 95static TextOutput& print_float(TextOutput& to, double value) 96{ 97 char buf[64]; 98 sprintf(buf, "%g", value); 99 if( !strchr(buf, '.') && !strchr(buf, 'e') && 100 !strchr(buf, 'E') ) { 101 strncat(buf, ".0", sizeof(buf)-1); 102 } 103 to.print(buf, strlen(buf)); 104 return to; 105} 106 107TextOutput& operator<<(TextOutput& to, float val) 108{ 109 return print_float(to,val); 110} 111 112TextOutput& operator<<(TextOutput& to, double val) 113{ 114 return print_float(to,val); 115} 116 117TextOutput& operator<<(TextOutput& to, const void* val) 118{ 119 char buf[32]; 120 snprintf(buf, sizeof(buf), "%p", val); 121 to.print(buf, strlen(buf)); 122 return to; 123} 124 125TextOutput& operator<<(TextOutput& to, const String8& val) 126{ 127 to << val.string(); 128 return to; 129} 130 131TextOutput& operator<<(TextOutput& to, const String16& val) 132{ 133 to << String8(val).string(); 134 return to; 135} 136 137static void textOutputPrinter(void* cookie, const char* txt) 138{ 139 ((TextOutput*)cookie)->print(txt, strlen(txt)); 140} 141 142TextOutput& operator<<(TextOutput& to, const TypeCode& val) 143{ 144 printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to); 145 return to; 146} 147 148HexDump::HexDump(const void *buf, size_t size, size_t bytesPerLine) 149 : mBuffer(buf) 150 , mSize(size) 151 , mBytesPerLine(bytesPerLine) 152 , mSingleLineCutoff(16) 153 , mAlignment(4) 154 , mCArrayStyle(false) 155{ 156 if (bytesPerLine >= 16) mAlignment = 4; 157 else if (bytesPerLine >= 8) mAlignment = 2; 158 else mAlignment = 1; 159} 160 161TextOutput& operator<<(TextOutput& to, const HexDump& val) 162{ 163 printHexData(0, val.buffer(), val.size(), val.bytesPerLine(), 164 val.singleLineCutoff(), val.alignment(), val.carrayStyle(), 165 textOutputPrinter, (void*)&to); 166 return to; 167} 168 169}; // namespace android 170