Elliott Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 1 | /* |
| 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 Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 17 | #ifndef BASE_STRINGPRINTF_H |
| 18 | #define BASE_STRINGPRINTF_H |
Elliott Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdarg.h> |
| 21 | #include <string> |
| 22 | |
| 23 | namespace android { |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 24 | namespace base { |
Elliott Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 25 | |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 26 | // 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 Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 39 | // Returns a string corresponding to printf-like formatting of the arguments. |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 40 | std::string StringPrintf(const char* fmt, ...) |
| 41 | __attribute__((__format__(FORMAT_ARCHETYPE, 1, 2))); |
Elliott Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 42 | |
| 43 | // Appends a printf-like formatting of the arguments to 'dst'. |
| 44 | void StringAppendF(std::string* dst, const char* fmt, ...) |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 45 | __attribute__((__format__(FORMAT_ARCHETYPE, 2, 3))); |
Elliott Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 46 | |
| 47 | // Appends a printf-like formatting of the arguments to 'dst'. |
Spencer Low | 6001c87 | 2015-05-13 00:02:55 -0700 | [diff] [blame] | 48 | void StringAppendV(std::string* dst, const char* format, va_list ap) |
Dan Albert | 286bb6d | 2015-07-09 20:35:09 +0000 | [diff] [blame] | 49 | __attribute__((__format__(FORMAT_ARCHETYPE, 2, 0))); |
| 50 | |
| 51 | #undef FORMAT_ARCHETYPE |
Elliott Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 52 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 53 | } // namespace base |
Elliott Hughes | 6b3be29 | 2015-02-03 14:23:53 -0800 | [diff] [blame] | 54 | } // namespace android |
| 55 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 56 | #endif // BASE_STRINGPRINTF_H |