The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 <utils/TextOutput.h> |
| 18 | |
| 19 | #include <utils/Debug.h> |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | // --------------------------------------------------------------------------- |
| 28 | |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 29 | TextOutput::TextOutput() { |
| 30 | } |
| 31 | |
| 32 | TextOutput::~TextOutput() { |
| 33 | } |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | TextOutput& operator<<(TextOutput& to, bool val) |
| 38 | { |
| 39 | if (val) to.print("true", 4); |
| 40 | else to.print("false", 5); |
| 41 | return to; |
| 42 | } |
| 43 | |
| 44 | TextOutput& operator<<(TextOutput& to, int val) |
| 45 | { |
| 46 | char buf[16]; |
| 47 | sprintf(buf, "%d", val); |
| 48 | to.print(buf, strlen(buf)); |
| 49 | return to; |
| 50 | } |
| 51 | |
| 52 | TextOutput& operator<<(TextOutput& to, long val) |
| 53 | { |
| 54 | char buf[16]; |
| 55 | sprintf(buf, "%ld", val); |
| 56 | to.print(buf, strlen(buf)); |
| 57 | return to; |
| 58 | } |
| 59 | |
| 60 | TextOutput& operator<<(TextOutput& to, unsigned int val) |
| 61 | { |
| 62 | char buf[16]; |
| 63 | sprintf(buf, "%u", val); |
| 64 | to.print(buf, strlen(buf)); |
| 65 | return to; |
| 66 | } |
| 67 | |
| 68 | TextOutput& operator<<(TextOutput& to, unsigned long val) |
| 69 | { |
| 70 | char buf[16]; |
| 71 | sprintf(buf, "%lu", val); |
| 72 | to.print(buf, strlen(buf)); |
| 73 | return to; |
| 74 | } |
| 75 | |
| 76 | TextOutput& operator<<(TextOutput& to, long long val) |
| 77 | { |
| 78 | char buf[32]; |
| 79 | sprintf(buf, "%Ld", val); |
| 80 | to.print(buf, strlen(buf)); |
| 81 | return to; |
| 82 | } |
| 83 | |
| 84 | TextOutput& operator<<(TextOutput& to, unsigned long long val) |
| 85 | { |
| 86 | char buf[32]; |
| 87 | sprintf(buf, "%Lu", val); |
| 88 | to.print(buf, strlen(buf)); |
| 89 | return to; |
| 90 | } |
| 91 | |
| 92 | static TextOutput& print_float(TextOutput& to, double value) |
| 93 | { |
| 94 | char buf[64]; |
| 95 | sprintf(buf, "%g", value); |
| 96 | if( !strchr(buf, '.') && !strchr(buf, 'e') && |
| 97 | !strchr(buf, 'E') ) { |
| 98 | strncat(buf, ".0", sizeof(buf)-1); |
| 99 | } |
| 100 | to.print(buf, strlen(buf)); |
| 101 | return to; |
| 102 | } |
| 103 | |
| 104 | TextOutput& operator<<(TextOutput& to, float val) |
| 105 | { |
| 106 | return print_float(to,val); |
| 107 | } |
| 108 | |
| 109 | TextOutput& operator<<(TextOutput& to, double val) |
| 110 | { |
| 111 | return print_float(to,val); |
| 112 | } |
| 113 | |
| 114 | TextOutput& operator<<(TextOutput& to, const void* val) |
| 115 | { |
| 116 | char buf[16]; |
| 117 | sprintf(buf, "%p", val); |
| 118 | to.print(buf, strlen(buf)); |
| 119 | return to; |
| 120 | } |
| 121 | |
| 122 | static void textOutputPrinter(void* cookie, const char* txt) |
| 123 | { |
| 124 | ((TextOutput*)cookie)->print(txt, strlen(txt)); |
| 125 | } |
| 126 | |
| 127 | TextOutput& operator<<(TextOutput& to, const TypeCode& val) |
| 128 | { |
| 129 | printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to); |
| 130 | return to; |
| 131 | } |
| 132 | |
| 133 | HexDump::HexDump(const void *buf, size_t size, size_t bytesPerLine) |
| 134 | : mBuffer(buf) |
| 135 | , mSize(size) |
| 136 | , mBytesPerLine(bytesPerLine) |
| 137 | , mSingleLineCutoff(16) |
| 138 | , mAlignment(4) |
| 139 | , mCArrayStyle(false) |
| 140 | { |
| 141 | if (bytesPerLine >= 16) mAlignment = 4; |
| 142 | else if (bytesPerLine >= 8) mAlignment = 2; |
| 143 | else mAlignment = 1; |
| 144 | } |
| 145 | |
| 146 | TextOutput& operator<<(TextOutput& to, const HexDump& val) |
| 147 | { |
| 148 | printHexData(0, val.buffer(), val.size(), val.bytesPerLine(), |
| 149 | val.singleLineCutoff(), val.alignment(), val.carrayStyle(), |
| 150 | textOutputPrinter, (void*)&to); |
| 151 | return to; |
| 152 | } |
| 153 | |
| 154 | }; // namespace android |