2015-05-26 21:33:55 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-08-15 09:43:21 +03:00
|
|
|
/* Provides a safer sprintf for printing to fixed-size character arrays. */
|
2015-05-26 21:33:55 +03:00
|
|
|
|
2016-08-15 09:43:21 +03:00
|
|
|
#ifndef mozilla_Sprintf_h_
|
|
|
|
#define mozilla_Sprintf_h_
|
2015-05-26 21:33:55 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2016-09-14 21:48:45 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2016-10-12 19:03:53 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2016-09-14 21:48:45 +03:00
|
|
|
|
2015-05-26 21:33:55 +03:00
|
|
|
#ifdef __cplusplus
|
2016-09-14 21:48:45 +03:00
|
|
|
|
|
|
|
template <size_t N>
|
2017-05-04 21:10:19 +03:00
|
|
|
MOZ_FORMAT_PRINTF(2, 0)
|
2016-09-14 21:48:45 +03:00
|
|
|
int VsprintfLiteral(char (&buffer)[N], const char* format, va_list args)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(format != buffer);
|
|
|
|
int result = vsnprintf(buffer, N, format, args);
|
|
|
|
buffer[N - 1] = '\0';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-05-26 21:33:55 +03:00
|
|
|
template <size_t N>
|
2016-10-12 19:03:53 +03:00
|
|
|
MOZ_FORMAT_PRINTF(2, 3)
|
2016-08-15 09:44:00 +03:00
|
|
|
int SprintfLiteral(char (&buffer)[N], const char* format, ...)
|
2015-05-26 21:33:55 +03:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2016-09-14 21:48:45 +03:00
|
|
|
int result = VsprintfLiteral(buffer, format, args);
|
2015-05-26 21:33:55 +03:00
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:48:45 +03:00
|
|
|
#endif
|
2016-08-15 09:43:21 +03:00
|
|
|
#endif /* mozilla_Sprintf_h_ */
|