blob: 9288bb5eeaa862a469564562e19f6d0443c27ec0 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- 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// ---
32// Author: Sanjay Ghemawat
33//
34// A printf() wrapper that writes into a fixed length buffer.
35// Useful in low-level code that does not want to use allocating
36// routines like StringPrintf().
37//
38// The implementation currently uses vsnprintf(). This seems to
39// be fine for use in many low-level contexts, but we may need to
40// rethink this decision if we hit a problem with it calling
41// down into malloc() etc.
42
43#ifndef BASE_RAW_PRINTER_H_
44#define BASE_RAW_PRINTER_H_
45
46#include <config.h>
47#include "base/basictypes.h"
48
49namespace base {
50
51class RawPrinter {
52 public:
53 // REQUIRES: "length > 0"
54 // Will printf any data added to this into "buf[0,length-1]" and
55 // will arrange to always keep buf[] null-terminated.
56 RawPrinter(char* buf, int length);
57
58 // Return the number of bytes that have been appended to the string
59 // so far. Does not count any bytes that were dropped due to overflow.
60 int length() const { return (ptr_ - base_); }
61
62 // Return the number of bytes that can be added to this.
63 int space_left() const { return (limit_ - ptr_); }
64
65 // Format the supplied arguments according to the "format" string
66 // and append to this. Will silently truncate the output if it does
67 // not fit.
68 void Printf(const char* format, ...)
69#ifdef HAVE___ATTRIBUTE__
70 __attribute__ ((__format__ (__printf__, 2, 3)))
71#endif
72;
73
74 private:
75 // We can write into [ptr_ .. limit_-1].
76 // *limit_ is also writable, but reserved for a terminating \0
77 // in case we overflow.
78 //
79 // Invariants: *ptr_ == \0
80 // Invariants: *limit_ == \0
81 char* base_; // Initial pointer
82 char* ptr_; // Where should we write next
83 char* limit_; // One past last non-\0 char we can write
84
85 DISALLOW_COPY_AND_ASSIGN(RawPrinter);
86};
87
88}
89
90#endif // BASE_RAW_PRINTER_H_