diff options
| author | Yue Du <daviddu@us.ibm.com> | 2015-10-08 13:40:33 -0500 |
|---|---|---|
| committer | Joshua Hunsberger <jahunsbe@us.ibm.com> | 2017-10-23 15:56:18 -0500 |
| commit | a1268463544386716f41f17da0e7e35ee9375343 (patch) | |
| tree | 6ed1fd767335b79422c9605c1b5bc90e4af8330e /import/chips/p9/common/pmlib/ppc405lib/sprintf.c | |
| parent | 286b12fc048eae37ea3ac04ac4aba8778d02d2a4 (diff) | |
| download | talos-hcode-a1268463544386716f41f17da0e7e35ee9375343.tar.gz talos-hcode-a1268463544386716f41f17da0e7e35ee9375343.zip | |
first draft of ppe_closed/cme & sgpe, and common/pmlib/
Change-Id: I08440fadfeff7e4777a776167d4fdcffe5fec82f
Reviewed-on: http://gfw160.aus.stglabs.ibm.com:8080/gerrit/21058
Tested-by: Jenkins Server
Reviewed-by: Gregory S. Still <stillgs@us.ibm.com>
Diffstat (limited to 'import/chips/p9/common/pmlib/ppc405lib/sprintf.c')
| -rw-r--r-- | import/chips/p9/common/pmlib/ppc405lib/sprintf.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/import/chips/p9/common/pmlib/ppc405lib/sprintf.c b/import/chips/p9/common/pmlib/ppc405lib/sprintf.c new file mode 100644 index 00000000..6011ad9c --- /dev/null +++ b/import/chips/p9/common/pmlib/ppc405lib/sprintf.c @@ -0,0 +1,138 @@ +/* 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 */ +// $Id: sprintf.c,v 1.1.1.1 2013/12/11 20:49:20 bcbrock Exp $ +// $Source: /afs/awd/projects/eclipz/KnowledgeBase/.cvsroot/eclipz/chips/p8/working/procedures/lib/sprintf.c,v $ +//----------------------------------------------------------------------------- +// *! (C) Copyright International Business Machines Corp. 2013 +// *! All Rights Reserved -- Property of IBM +// *! *** IBM Confidential *** +//----------------------------------------------------------------------------- + +/// \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; +} |

