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