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
28 namespace android {
29
30 // ---------------------------------------------------------------------------
31
TextOutput()32 TextOutput::TextOutput() {
33 }
34
~TextOutput()35 TextOutput::~TextOutput() {
36 }
37
38 // ---------------------------------------------------------------------------
39
operator <<(TextOutput & to,bool val)40 TextOutput& operator<<(TextOutput& to, bool val)
41 {
42 if (val) to.print("true", 4);
43 else to.print("false", 5);
44 return to;
45 }
46
operator <<(TextOutput & to,int val)47 TextOutput& 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
operator <<(TextOutput & to,long val)55 TextOutput& 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
operator <<(TextOutput & to,unsigned int val)63 TextOutput& 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
operator <<(TextOutput & to,unsigned long val)71 TextOutput& 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
operator <<(TextOutput & to,long long val)79 TextOutput& 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
operator <<(TextOutput & to,unsigned long long val)87 TextOutput& 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
print_float(TextOutput & to,double value)95 static 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
operator <<(TextOutput & to,float val)107 TextOutput& operator<<(TextOutput& to, float val)
108 {
109 return print_float(to,val);
110 }
111
operator <<(TextOutput & to,double val)112 TextOutput& operator<<(TextOutput& to, double val)
113 {
114 return print_float(to,val);
115 }
116
operator <<(TextOutput & to,const void * val)117 TextOutput& operator<<(TextOutput& to, const void* val)
118 {
119 char buf[16];
120 sprintf(buf, "%p", val);
121 to.print(buf, strlen(buf));
122 return to;
123 }
124
operator <<(TextOutput & to,const String8 & val)125 TextOutput& operator<<(TextOutput& to, const String8& val)
126 {
127 to << val.string();
128 return to;
129 }
130
operator <<(TextOutput & to,const String16 & val)131 TextOutput& operator<<(TextOutput& to, const String16& val)
132 {
133 to << String8(val).string();
134 return to;
135 }
136
textOutputPrinter(void * cookie,const char * txt)137 static void textOutputPrinter(void* cookie, const char* txt)
138 {
139 ((TextOutput*)cookie)->print(txt, strlen(txt));
140 }
141
operator <<(TextOutput & to,const TypeCode & val)142 TextOutput& operator<<(TextOutput& to, const TypeCode& val)
143 {
144 printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to);
145 return to;
146 }
147
HexDump(const void * buf,size_t size,size_t bytesPerLine)148 HexDump::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
operator <<(TextOutput & to,const HexDump & val)161 TextOutput& 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