annotate ffmpeg/compat/msvcrt/snprintf.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * C99-compatible snprintf() and vsnprintf() implementations
yading@10 3 * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include <stdio.h>
yading@10 23 #include <stdarg.h>
yading@10 24 #include <limits.h>
yading@10 25 #include <string.h>
yading@10 26
yading@10 27 #include "compat/va_copy.h"
yading@10 28 #include "libavutil/error.h"
yading@10 29
yading@10 30 #if defined(__MINGW32__)
yading@10 31 #define EOVERFLOW EFBIG
yading@10 32 #endif
yading@10 33
yading@10 34 int avpriv_snprintf(char *s, size_t n, const char *fmt, ...)
yading@10 35 {
yading@10 36 va_list ap;
yading@10 37 int ret;
yading@10 38
yading@10 39 va_start(ap, fmt);
yading@10 40 ret = avpriv_vsnprintf(s, n, fmt, ap);
yading@10 41 va_end(ap);
yading@10 42
yading@10 43 return ret;
yading@10 44 }
yading@10 45
yading@10 46 int avpriv_vsnprintf(char *s, size_t n, const char *fmt,
yading@10 47 va_list ap)
yading@10 48 {
yading@10 49 int ret;
yading@10 50 va_list ap_copy;
yading@10 51
yading@10 52 if (n == 0)
yading@10 53 return _vscprintf(fmt, ap);
yading@10 54 else if (n > INT_MAX)
yading@10 55 return AVERROR(EOVERFLOW);
yading@10 56
yading@10 57 /* we use n - 1 here because if the buffer is not big enough, the MS
yading@10 58 * runtime libraries don't add a terminating zero at the end. MSDN
yading@10 59 * recommends to provide _snprintf/_vsnprintf() a buffer size that
yading@10 60 * is one less than the actual buffer, and zero it before calling
yading@10 61 * _snprintf/_vsnprintf() to workaround this problem.
yading@10 62 * See http://msdn.microsoft.com/en-us/library/1kt27hek(v=vs.80).aspx */
yading@10 63 memset(s, 0, n);
yading@10 64 va_copy(ap_copy, ap);
yading@10 65 ret = _vsnprintf(s, n - 1, fmt, ap_copy);
yading@10 66 va_end(ap_copy);
yading@10 67 if (ret == -1)
yading@10 68 ret = _vscprintf(fmt, ap);
yading@10 69
yading@10 70 return ret;
yading@10 71 }