TextOutput.cpp 3.7 KB

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