|
Packit |
18d29c |
/* Copyright (c) 2008, Google Inc.
|
|
Packit |
18d29c |
* All rights reserved.
|
|
Packit |
18d29c |
*
|
|
Packit |
18d29c |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
18d29c |
* modification, are permitted provided that the following conditions are
|
|
Packit |
18d29c |
* met:
|
|
Packit |
18d29c |
*
|
|
Packit |
18d29c |
* * Redistributions of source code must retain the above copyright
|
|
Packit |
18d29c |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
18d29c |
* * Redistributions in binary form must reproduce the above
|
|
Packit |
18d29c |
* copyright notice, this list of conditions and the following disclaimer
|
|
Packit |
18d29c |
* in the documentation and/or other materials provided with the
|
|
Packit |
18d29c |
* distribution.
|
|
Packit |
18d29c |
* * Neither the name of Google Inc. nor the names of its
|
|
Packit |
18d29c |
* contributors may be used to endorse or promote products derived from
|
|
Packit |
18d29c |
* this software without specific prior written permission.
|
|
Packit |
18d29c |
*
|
|
Packit |
18d29c |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
18d29c |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
18d29c |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
18d29c |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
18d29c |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
18d29c |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
18d29c |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
18d29c |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
18d29c |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
18d29c |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
18d29c |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
18d29c |
*
|
|
Packit |
18d29c |
* ---
|
|
Packit |
18d29c |
* Author: Craig Silverstein
|
|
Packit |
18d29c |
* Copied from google-perftools and modified by Shinichiro Hamaji
|
|
Packit |
18d29c |
*/
|
|
Packit |
18d29c |
|
|
Packit |
18d29c |
#ifndef _WIN32
|
|
Packit |
18d29c |
# error You should only be including windows/port.cc in a windows environment!
|
|
Packit |
18d29c |
#endif
|
|
Packit |
18d29c |
|
|
Packit |
18d29c |
#include "config.h"
|
|
Packit |
18d29c |
#include <stdarg.h> // for va_list, va_start, va_end
|
|
Packit |
18d29c |
#include <string.h> // for strstr()
|
|
Packit |
18d29c |
#include <assert.h>
|
|
Packit |
18d29c |
#include <string>
|
|
Packit |
18d29c |
#include <vector>
|
|
Packit |
18d29c |
#include "port.h"
|
|
Packit |
18d29c |
|
|
Packit |
18d29c |
using std::string;
|
|
Packit |
18d29c |
using std::vector;
|
|
Packit |
18d29c |
|
|
Packit |
18d29c |
// These call the windows _vsnprintf, but always NUL-terminate.
|
|
Packit |
18d29c |
int safe_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
|
|
Packit |
18d29c |
if (size == 0) // not even room for a \0?
|
|
Packit |
18d29c |
return -1; // not what C99 says to do, but what windows does
|
|
Packit |
18d29c |
str[size-1] = '\0';
|
|
Packit |
18d29c |
return _vsnprintf(str, size-1, format, ap);
|
|
Packit |
18d29c |
}
|
|
Packit |
18d29c |
|
|
Packit |
18d29c |
#ifndef HAVE_SNPRINTF
|
|
Packit |
18d29c |
int snprintf(char *str, size_t size, const char *format, ...) {
|
|
Packit |
18d29c |
va_list ap;
|
|
Packit |
18d29c |
va_start(ap, format);
|
|
Packit |
18d29c |
const int r = vsnprintf(str, size, format, ap);
|
|
Packit |
18d29c |
va_end(ap);
|
|
Packit |
18d29c |
return r;
|
|
Packit |
18d29c |
}
|
|
Packit |
18d29c |
#endif
|