15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2008, Google Inc.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ---
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: Sanjay Ghemawat
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A printf() wrapper that writes into a fixed length buffer.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Useful in low-level code that does not want to use allocating
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// routines like StringPrintf().
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The implementation currently uses vsnprintf().  This seems to
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be fine for use in many low-level contexts, but we may need to
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// rethink this decision if we hit a problem with it calling
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// down into malloc() etc.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_RAW_PRINTER_H_
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_RAW_PRINTER_H_
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <config.h>
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RawPrinter {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // REQUIRES: "length > 0"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Will printf any data added to this into "buf[0,length-1]" and
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will arrange to always keep buf[] null-terminated.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RawPrinter(char* buf, int length);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the number of bytes that have been appended to the string
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so far.  Does not count any bytes that were dropped due to overflow.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int length() const { return (ptr_ - base_); }
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the number of bytes that can be added to this.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int space_left() const { return (limit_ - ptr_); }
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Format the supplied arguments according to the "format" string
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and append to this.  Will silently truncate the output if it does
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not fit.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Printf(const char* format, ...)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE___ATTRIBUTE__
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  __attribute__ ((__format__ (__printf__, 2, 3)))
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We can write into [ptr_ .. limit_-1].
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // *limit_ is also writable, but reserved for a terminating \0
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in case we overflow.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Invariants: *ptr_ == \0
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Invariants: *limit_ == \0
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char* base_;          // Initial pointer
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char* ptr_;           // Where should we write next
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char* limit_;         // One past last non-\0 char we can write
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(RawPrinter);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_RAW_PRINTER_H_
90