📄 bs_snprintf.c
字号:
/*******************************************************************************
** Copyright (c) 2002,Utstarcom Inc.
** All rights reserved.
**
** Group : WBG ROM
** Creater : RCT
** Data : 2006-1-15
** Description : wrapper function for snprintf and vsnprintf
** Version : 1.1
**-----------------------------------------------------------------------------
** <complement:Update history>
********************************************************************************/
#include "global.h"
#include "xCommon.h"
#include "bs_snprintf.h"
#include "bs_log.h"
#if defined(VXWORKS)
#include <fioLib.h>
/*
* A container of the following type is allocated from the stack
* on entry to gw_vsnprintf(), initialized with details about the
* caller's output buffer, and passed to fioFormatV() for use by
* bs_sn_output_fn().
*/
typedef struct {
char *s; /* The user's output buffer */
int nmax; /* The length of the user's output buffer */
int length; /* The current length of the string in s[] */
} BsPrintData;
static STATUS bs_sn_output_fn(char *buffer, int nc, int arg);
int bs_vsnprintf(char *s, int n, const char *fmt, va_list ap)
{
BsPrintData data; /* A container for details about the output buffer */
/*
* Record details about the output buffer in 'data'. This can
* then be passed to the fioFormatV() output function.
*/
data.s = s;
data.nmax = n;
data.length = 0;
/*
* Format output into data.s[] and record the accumulated length
* of the string in data.length.
*/
if(fioFormatV(fmt, ap, bs_sn_output_fn, (int) &data) == ERROR)
return ERROR;
/*
* Terminate the accumulated string.
* Note that gw_sn_output_fn() guarantees that data.length is < data.nmax.
*/
data.s[data.length] = '\0';
/*
* Return a count of the number of characters in s[], excluding the
* terminating '\0'. Note that this will be less than the number
* expected, if the output had to be truncated.
*/
return data.length;
}
/*.......................................................................
* This is the function that fioFormatV() calls to render output into
* the gw_snprintf() caller's output buffer.
*/
static STATUS bs_sn_output_fn(char *buffer, int nc, int arg)
{
BsPrintData *data = (BsPrintData *) arg;
/*
* Work out how many of the nc characters in buffer[] can be appended
* to data->s[]. Leave room for a '\0' terminator at the end of
* data->s[].
*/
int nnew = nc;
if(data->length + nnew + 1 >= data->nmax)
nnew = data->nmax - data->length - 1;
/*
* Append nnew characters from buffer[] to data.s[].
*/
if(nnew > 0) {
memcpy(data->s + data->length, buffer, nnew);
data->length += nnew;
}; return OK;
}
#elif defined(WIN32)
int bs_vsnprintf(char *s, int n, const char *fmt, va_list ap)
{
return _vsnprintf(s,n,fmt,ap);
}
#else
int bs_vsnprintf(char *s, int n, const char *fmt, va_list ap)
{
return vsnprintf(s,n,fmt,ap);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -