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 17 #ifndef ANDROID_BASE_STRINGPRINTF_H 18 #define ANDROID_BASE_STRINGPRINTF_H 19 20 #include <stdarg.h> 21 #include <string> 22 23 namespace android { 24 namespace base { 25 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 39 // Returns a string corresponding to printf-like formatting of the arguments. 40 std::string StringPrintf(const char* fmt, ...) 41 __attribute__((__format__(FORMAT_ARCHETYPE, 1, 2))); 42 43 // Appends a printf-like formatting of the arguments to 'dst'. 44 void StringAppendF(std::string* dst, const char* fmt, ...) 45 __attribute__((__format__(FORMAT_ARCHETYPE, 2, 3))); 46 47 // Appends a printf-like formatting of the arguments to 'dst'. 48 void StringAppendV(std::string* dst, const char* format, va_list ap) 49 __attribute__((__format__(FORMAT_ARCHETYPE, 2, 0))); 50 51 #undef FORMAT_ARCHETYPE 52 53 } // namespace base 54 } // namespace android 55 56 #endif // ANDROID_BASE_STRINGPRINTF_H 57