raw_printer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2008, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Sanjay Ghemawat
  32. //
  33. // A printf() wrapper that writes into a fixed length buffer.
  34. // Useful in low-level code that does not want to use allocating
  35. // routines like StringPrintf().
  36. //
  37. // The implementation currently uses vsnprintf(). This seems to
  38. // be fine for use in many low-level contexts, but we may need to
  39. // rethink this decision if we hit a problem with it calling
  40. // down into malloc() etc.
  41. #ifndef BASE_RAW_PRINTER_H_
  42. #define BASE_RAW_PRINTER_H_
  43. #include <config.h>
  44. #include "base/basictypes.h"
  45. namespace base {
  46. class RawPrinter {
  47. public:
  48. // REQUIRES: "length > 0"
  49. // Will printf any data added to this into "buf[0,length-1]" and
  50. // will arrange to always keep buf[] null-terminated.
  51. RawPrinter(char* buf, int length);
  52. // Return the number of bytes that have been appended to the string
  53. // so far. Does not count any bytes that were dropped due to overflow.
  54. int length() const { return (ptr_ - base_); }
  55. // Return the number of bytes that can be added to this.
  56. int space_left() const { return (limit_ - ptr_); }
  57. // Format the supplied arguments according to the "format" string
  58. // and append to this. Will silently truncate the output if it does
  59. // not fit.
  60. void Printf(const char* format, ...)
  61. #ifdef HAVE___ATTRIBUTE__
  62. __attribute__ ((__format__ (__printf__, 2, 3)))
  63. #endif
  64. ;
  65. private:
  66. // We can write into [ptr_ .. limit_-1].
  67. // *limit_ is also writable, but reserved for a terminating \0
  68. // in case we overflow.
  69. //
  70. // Invariants: *ptr_ == \0
  71. // Invariants: *limit_ == \0
  72. char* base_; // Initial pointer
  73. char* ptr_; // Where should we write next
  74. char* limit_; // One past last non-\0 char we can write
  75. DISALLOW_COPY_AND_ASSIGN(RawPrinter);
  76. };
  77. }
  78. #endif // BASE_RAW_PRINTER_H_