summaryrefslogtreecommitdiff
path: root/sal/systools/win32/uwinapi/sntprintf.c
blob: 70980305c4ae9ada39a4677e60e375150ea252fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#define _SNPRINTF_DLLIMPORT __declspec( dllexport )

#include <stdarg.h>
#include <stdio.h>

#ifdef __MINGW32__
/* MinGW-w64 doesn't have a _tcsinc() inline or library function */
#define _MB_MAP_DIRECT
#endif
#include <tchar.h>
#include <systools/win32/snprintf.h>

#ifdef _MSC_VER
#pragma warning(disable:4273)       // inconsistent dll linkage
#endif

#if defined(__MINGW32_VERSION) && ((__MINGW32_MAJOR_VERSION < 3)||((__MINGW32_MAJOR_VERSION == 3)&&(__MINGW32_MINOR_VERSION < 18)))

/*  The non-debug versions of _vscprintf/_scprintf are just calls
    to _vsprintf/_sprintf with string buffer pointer set to NULL,
    requires MSVCRT version 7.0 */
#ifdef __MINGW32__
static int __cdecl _vsctprintf( const TCHAR *format, va_list ap )
#else
static int __cdecl _vsctprintf( const _TXCHAR *format, va_list ap )
#endif
{
    FILE    *fp = _tfopen( _T("NUL"), _T("wb") );

    if ( fp )
    {
        int retval = _vftprintf( fp, format, ap );
        fclose( fp );

        return retval;
    }

    return -1;
}
#endif

#if !defined(__MINGW32__) || defined (__NO_ISOCEXT)

/*  This function retrieves the pointer to the last character of a buffer.
    That is the pointer to the last character of the buffer that fits
    completely into that buffer or the position of the terminating zero.

    buffer  Pointer to a _TXCHAR buffer to be examined
    count   size of the buffer to be examined

    return  The pointer to the last character that fits into the buffer or
            NULL if count is zero or count is one and the first byte was a
            leading DBCS character
*/

static _TCHAR *GetLastBufferChar( _TCHAR *buffer, size_t count )
{
    _TCHAR  *last = NULL;
    _TCHAR  *cur = buffer;

    while ( (size_t)(cur - buffer) < count )
    {
        last = cur;

        if ( !*last )
            break;

        cur = _tcsinc(last);
    }

    return last;
}

/* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */

_SNPRINTF_DLLIMPORT int __cdecl vsntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, va_list list )
{
    int     retval;

    /*  First of all call the existing non POSIX standard function assuming
        the buffer size will be large enough */

    retval = _vsntprintf( buffer, count, format, list );

    if ( retval < 0 )
    {
        /*  If the buffer wasn't large enough ensure that the buffer will be
            zero terminated */

        _TCHAR  *last = GetLastBufferChar( buffer, count );
        if (last )
            *last = 0;

        /*  Retrieve the count of characters that would have been written
            if the buffer were large enough */

        retval = _vsctprintf( format, list );
    }
    else if ( (size_t)retval == count && count )
    {
        /*  If the buffer was large enough but not large enough for the trailing
            zero make the buffer zero terminated */

        _TCHAR  *last = GetLastBufferChar( buffer, count );
        if (last )
            *last = 0;
    }

    return retval;
}

/* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */

_SNPRINTF_DLLIMPORT int __cdecl sntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, ... )
{
    va_list list;
    int     retval;

    va_start( list, format );
    retval = vsntprintf( buffer, count, format, list );
    va_end( list );

    return retval;
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */