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