summaryrefslogtreecommitdiffstats
path: root/import/chips/p9/common/pmlib/ppc405lib/sprintf.c
blob: 9e3e23e16af36ce4de91ccfeb289879563a39d89 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: import/chips/p9/common/pmlib/ppc405lib/sprintf.c $            */
/*                                                                        */
/* OpenPOWER HCODE Project                                                */
/*                                                                        */
/* COPYRIGHT 2015,2017                                                    */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed 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                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/// \file sprintf.c \brief Implementations of sprintf() and snprintf() using
/// StringStream.

#include "ssx.h"
#include "ssx_io.h"
#include "string_stream.h"


// sprintf() is easy - just blindly copy in the data.

static int
sprintf_swrite(FILE* stream, const void* buf, size_t size, size_t* written)
{
    StringStream* string = (StringStream*)stream;

    memcpy((void*)(&(string->data[string->next_write])), buf, size);
    string->next_write += size;
    *written = size;
    return 0;
}


// snprintf() requires that bytes that won't fit in the array are simply
// discarded, but still accounted for. Note that vfprintf() is doing the
// high-level accounting.

static int
snprintf_swrite(FILE* stream, const void* buf, size_t size, size_t* written)
{
    StringStream* string = (StringStream*)stream;

    size_t to_write = MIN(size, string->size - string->next_write - 1);

    memcpy((void*)(&(string->data[string->next_write])), buf, to_write);
    string->next_write += to_write;
    *written = size;
    return 0;
}


// We use a StringStream to implement [v]sprintf() and [v]snprintf().  Once
// the formatting is finished the NULL terminator is added.

int
vsprintf(char* str, const char* format, va_list argp)
{
    ssize_t rc;
    StringStream stream;

    _string_stream_create(&stream, str, 1, 0, sprintf_swrite);
    rc = vfprintf((FILE*)(&stream), format, argp);
    stream.data[stream.next_write] = 0;
    return rc;
}



int
sprintf(char* str, const char* format, ...)
{
    va_list argp;
    ssize_t rc;

    va_start(argp, format);
    rc = vsprintf(str, format, argp);
    va_end(argp);

    return rc;
}


int
vsnprintf(char* str, size_t size, const char* format, va_list argp)
{
    ssize_t rc;
    StringStream stream;

    if (size == 0)
    {
        return 0;
    }

    _string_stream_create(&stream, str, size, 0, snprintf_swrite);
    rc = vfprintf((FILE*)(&stream), format, argp);
    stream.data[stream.next_write] = 0;
    return rc;
}


int
snprintf(char* str, size_t size, const char* format, ...)
{
    va_list argp;
    ssize_t rc;

    if (size == 0)
    {
        return 0;
    }

    va_start(argp, format);
    rc = vsnprintf(str, size, format, argp);
    va_end(argp);

    return rc;
}
OpenPOWER on IntegriCloud