blob: d68af8713e46cf287af11037ac927214da2eec9e [file] [log] [blame]
Elliott Hughes6b3be292015-02-03 14:23:53 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Albertc007bc32015-03-16 10:08:46 -070017#ifndef BASE_STRINGPRINTF_H
18#define BASE_STRINGPRINTF_H
Elliott Hughes6b3be292015-02-03 14:23:53 -080019
20#include <stdarg.h>
21#include <string>
22
23namespace android {
Dan Albertc007bc32015-03-16 10:08:46 -070024namespace base {
Elliott Hughes6b3be292015-02-03 14:23:53 -080025
Dan Albert286bb6d2015-07-09 20:35:09 +000026// These printf-like functions are implemented in terms of vsnprintf, so they
27// use the same attribute for compile-time format string checking. On Windows,
28// if the mingw version of vsnprintf is used, use `gnu_printf' which allows z
29// in %zd and PRIu64 (and related) to be recognized by the compile-time
30// checking.
31#define FORMAT_ARCHETYPE __printf__
32#ifdef __USE_MINGW_ANSI_STDIO
33#if __USE_MINGW_ANSI_STDIO
34#undef FORMAT_ARCHETYPE
35#define FORMAT_ARCHETYPE gnu_printf
36#endif
37#endif
38
Elliott Hughes6b3be292015-02-03 14:23:53 -080039// Returns a string corresponding to printf-like formatting of the arguments.
Dan Albert286bb6d2015-07-09 20:35:09 +000040std::string StringPrintf(const char* fmt, ...)
41 __attribute__((__format__(FORMAT_ARCHETYPE, 1, 2)));
Elliott Hughes6b3be292015-02-03 14:23:53 -080042
43// Appends a printf-like formatting of the arguments to 'dst'.
44void StringAppendF(std::string* dst, const char* fmt, ...)
Dan Albert286bb6d2015-07-09 20:35:09 +000045 __attribute__((__format__(FORMAT_ARCHETYPE, 2, 3)));
Elliott Hughes6b3be292015-02-03 14:23:53 -080046
47// Appends a printf-like formatting of the arguments to 'dst'.
Spencer Low6001c872015-05-13 00:02:55 -070048void StringAppendV(std::string* dst, const char* format, va_list ap)
Dan Albert286bb6d2015-07-09 20:35:09 +000049 __attribute__((__format__(FORMAT_ARCHETYPE, 2, 0)));
50
51#undef FORMAT_ARCHETYPE
Elliott Hughes6b3be292015-02-03 14:23:53 -080052
Dan Albertc007bc32015-03-16 10:08:46 -070053} // namespace base
Elliott Hughes6b3be292015-02-03 14:23:53 -080054} // namespace android
55
Dan Albertc007bc32015-03-16 10:08:46 -070056#endif // BASE_STRINGPRINTF_H