Exemple #1
0
int wz_vsnprintf(char* str, size_t size, const char* format, va_list ap)
{
	int count;
	va_list aq;

	va_copy(aq, ap);

	// Find out how long the resulting string is
	count = _vscprintf(format, aq);
	va_end(aq);

	if (count >= 0
	 && str != NULL)
	{
		// Perfrom the actual string formatting
		_vsnprintf_s(str, size, _TRUNCATE, format, ap);
	}

	// Return the amount of characters that would be written if _no_ truncation occurred
	return count;
}
Exemple #2
0
    void log_vargs(const char* channel,
                   astra_log_severity_t logLevel,
                   const char* fileName,
                   int lineNo,
                   const char* func,
                   const char* format,
                   va_list args)
    {
#ifdef _WIN32
        int len = _vscprintf(format, args);
#else
        va_list argsCopy;
        va_copy(argsCopy, args);
        int len = vsnprintf(nullptr, 0, format, argsCopy);
        va_end(argsCopy);
#endif
        std::unique_ptr<char[]> buffer(new char[len + 1]);
        vsnprintf(buffer.get(), len + 1, format, args);

        dispatch_log(fileName, lineNo, func, channel, logLevel, buffer.get());
    }
Exemple #3
0
	std::string Format(const char* fmt, va_list args)
	{
		std::string str;

		int buffSize = _vscprintf(fmt, args) + 1;

		if (buffSize <= 1)
			return str;

		char *szBuff = new char[buffSize];
		memset(szBuff, 0, buffSize);

		int len = vsprintf_s(szBuff, buffSize, fmt, args);

		szBuff[buffSize - 1] = 0;

		str = szBuff;
		delete[] szBuff;

		return str;
	}
Exemple #4
0
    static std::string stringf(const char* format, ...)
    {
        va_list args;
        va_start(args, format);
        IGNORE_FORMAT_STRING_ON
        auto size = _vscprintf(format, args) + 1U;
        IGNORE_FORMAT_STRING_OFF
        std::unique_ptr<char[]> buf(new char[size] ); 

#ifndef _MSC_VER
        IGNORE_FORMAT_STRING_ON
        vsnprintf(buf.get(), size, format, args);
        IGNORE_FORMAT_STRING_OFF
#else
        vsnprintf_s(buf.get(), size, _TRUNCATE, format, args);
#endif

        va_end(args);            

        return std::string(buf.get());
    }
Exemple #5
0
bool YL_StringUtil::Format(string& str, const char* szFmt, ...)
{
	va_list argList;
	int len;
	char* buf;

	va_start( argList, szFmt );
	len = _vscprintf(szFmt, argList) + 1;

	buf = new char[len];

	if(vsprintf(buf, szFmt, argList) < 0)
	{
		return false;
	}

	str = string(buf, len - 1);
	delete[] buf;
	va_end( argList );
	return true;
}
Exemple #6
0
// 在主界面中显示提示信息
void MyWinSock::_ShowMessage(const char *szFormat, ...) const
{
	// 根据传入的参数格式化字符串
	char   strMessage[1024] = { 0 };
	va_list   arglist;

	// 处理变长参数
	va_start(arglist, szFormat);
	//strMessage.FormatV(szFormat,arglist);
	int iLen = _vscprintf(szFormat, arglist) + 1;	//得到格式化以后的字符串长度
	if (iLen > 0)
	{
		_vsnprintf_s(strMessage, iLen, szFormat, arglist);
	}
	va_end(arglist);

	// 在主界面中显示
	//printf(strMessage);
	OutputDebugStringA(strMessage);
	OutputDebugStringA("\n");
}
int VPL_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
    int rv = 0;

    if (str != NULL) {
        _vsnprintf_s(str, size, _TRUNCATE, format, ap);
    }

    //rv, the number of characters that would be generated, does NOT include the terminating null character
    rv = _vscprintf(format, ap);

    if (rv < 0) {
        VPL_REPORT_FATAL("_vscprintf(%s):%d", format, rv);
        //When error, always return 0 and set *str to '\0' (To avoid caller crash)
        if(str != NULL && size > 0)
            *str = '\0';
        rv = 0;
    }

    return rv;
}
Exemple #8
0
inline void __cdecl odprintf(PCSTR format, ...) {
	va_list	args;
	va_start(args, format);
	int len = _vscprintf(format, args);
	if (len > 0) {
		len += (1 + 2);
		PSTR buf = (PSTR) malloc(len);
		if (buf) {
			len = vsprintf_s(buf, len, format, args);
			if (len > 0) {
				while (len && isspace(buf[len-1])) len--;
				buf[len++] = '\r';
				buf[len++] = '\n';
				buf[len] = 0;
				OutputDebugStringA(buf);
			}
			free(buf);
		}
		va_end(args);
	}
}
Exemple #9
0
char *
zsys_vprintf (const char *format, va_list argptr)
{
    int size = 256;
    char *string = (char *) malloc (size);
    int required = vsnprintf (string, size, format, argptr);
#if defined (__WINDOWS__)
    if (required < 0 || required >= size)
        required = _vscprintf (format, argptr);
#endif
    //  If formatted string cannot fit into small string, reallocate a
    //  larger buffer for it.
    if (required >= size) {
        size = required + 1;
        string = (char *) realloc (string, size);
        if (!string)
            return NULL;
        vsnprintf (string, size, format, argptr);
    }
    return string;
}
Exemple #10
0
std::string format(const char *fmt, ...)
{
    va_list args;
    std::vector<char> buffer(128);

    va_start(args, fmt);
    int rc = vsnprintf(&buffer[0], buffer.size(), fmt, args);
    va_end(args);
    if (rc >= 0 && rc < static_cast<int>(buffer.size()))
	return std::string(&buffer[0], &buffer[rc]);
#if defined(_MSC_VER) || defined(__MINGW32__) 
    va_start(args, fmt);
    rc = _vscprintf(fmt, args);
    va_end(args);
#endif
    buffer.resize(rc + 1);
    va_start(args, fmt);
    rc = vsnprintf(&buffer[0], buffer.size(), fmt, args);
    va_end(args);
    return std::string(&buffer[0], &buffer[rc]);
}
Exemple #11
0
int vasprintf(char **strp, const char *fmt, va_list ap)
{
  int r = -1, size = _vscprintf(fmt, ap);

  if ((size >= 0) && (size < INT_MAX))
  {
    *strp = (char *)malloc(size+1); //+1 for null
    if (*strp)
    {
      r = vsnprintf(*strp, size+1, fmt, ap);  //+1 for null
      if ((r < 0) || (r > size))
      {
        insane_free(*strp);
        r = -1;
      }
    }
  }
  else { *strp = 0; }

  return(r);
}
Exemple #12
0
	void 
	trace_message(
		__in trace_level level,
		__in const char *format,
		...
		)
	{
		char *message_buf;
		size_t message_len;
		va_list message_args;
		std::stringstream stream;

		if(level <= SHOW_TRACE_LEVEL) {
			va_start(message_args, format);

			message_len = _vscprintf(format, message_args) + 1;
			if(message_len) {
			
				message_buf = new char[message_len];
				if(message_buf) {
					
					//if(vsprintf_s(message_buf, message_len, format, message_args) 
					if(vsprintf(message_buf, format, message_args) 
							== (int)(message_len - 1)) {
						stream << message_buf << std::endl;
					}

					delete[] message_buf;
					message_buf = NULL;
				}
			}

			va_end(message_args);
#ifdef _WIN32
			OutputDebugString(stream.str().c_str());
#else
			std::cout << stream.str();
#endif // _WIN32
		}
	}
Exemple #13
0
tmErrorCode_t tmsysOM3912Print(UInt32 level, const char* format, ...)
{
    tmErrorCode_t   err = TM_OK;

#ifdef NXPFEV2_DBG_MSG
    va_list         args;
    int             len = 0;
    char            *buffer = Null;

    va_start( args, format );
    len = _vscprintf(format, args ) + 1;
    buffer = (char *)malloc(len * sizeof(char));

    (void)vsprintf_s(buffer, len, format, args);

    if(pDbgMsgCallBack != Null)
    {
        err = pDbgMsgCallBack(level, buffer);
    }

    free(buffer);
#else
    static char     buf[PRINT_BUFFER_SIZE] = "\0";
    va_list         ap;

    if(level<DEBUGLVL_VERBOSE)
    {
        va_start(ap, format);
        (void)vsnprintf(buf, PRINT_BUFFER_SIZE, format, ap);
        va_end(ap);

#ifdef CUSTOMER_IMPLEMENTATION
        /* Customer implementation */
        tmdlPrint(buf);
#endif
    }
#endif

    return err;
}
long PdfOutputDevice::PrintVLen( const char* pszFormat, va_list args )
{
    long    lBytes;

    if( !pszFormat )
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }

    if( m_hFile )
    {
        if( (lBytes = vfprintf( m_hFile, pszFormat, args )) < 0 )
        {
            perror( NULL );
            PODOFO_RAISE_ERROR( ePdfError_UnexpectedEOF );
        }
    }
    else
    {
        // OC 17.08.2010: Use new function _vscprintf to get the number of characters:
        // visual c++  8.0 == 1400 (Visual Studio 2005)
        // i am not shure if 1300 is ok here, but who cares this cruel compiler version
#if (defined _MSC_VER && _MSC_VER >= 1400 )
        lBytes = _vscprintf( pszFormat, args );
#elif (defined _MSC_VER || defined __hpux)  // vsnprintf without buffer does not work with MS-VC or HPUX
        int len = 1024;
        do
        {
            char * temp = new char[len+1]; // OC 17.08.2010 BugFix: +1 avoids corrupted heap
            lBytes = vsnprintf( temp, len+1, pszFormat, args );
            delete[] temp;
            len *= 2;
        } while (lBytes < 0 );
#else
        lBytes = vsnprintf( NULL, 0, pszFormat, args );
#endif
    }

    return lBytes;
}
Exemple #15
0
	void Log::PushLog( ELogType::Type type, const CHAR *pFileName, const CHAR *pFuncName, const SIZE_T lineNumber, const CHAR *pError, ... )
	{
		if ( type > m_logLevel )
			return;

		CHAR *tempMsg;
		SIZE_T len = 0;
		va_list arg_ptr;

		va_start( arg_ptr, pError );
		#if defined(PSX_UNICODE)
				len = _vscwprintf( pError, arg_ptr );
				tempMsg = new CHAR[len + 1];
				vswprintf( tempMsg, pError, arg_ptr );
		#else
				len = _vscprintf( pError, arg_ptr );
				tempMsg = new CHAR[len + 1];
				vsprintf( tempMsg, pError, arg_ptr );
		#endif /* defined(PSX_UNICODE) */

		LogBuffer log(tempMsg, pFileName, pFuncName, lineNumber);

		PSX_SafeDeleteArray( tempMsg );

		m_pErrorList->PushFront( log );

		if ( m_pErrorList->GetSize() > m_maxLogSize )
			m_pErrorList->PopBack();

		if ( m_bFileLogging )
		{
			m_pLogFile->WriteString( log.GetBuffer() );
			m_pLogFile->WriteString( PSX_String("\n") );
			m_pLogFile->Flush();
		}

		// Display in output window
		OutputDebugString( log.GetBuffer() );
		OutputDebugString( PSX_String("\n") );
	}
Exemple #16
0
/* hoedown_buffer_printf: formatted printing to a buffer */
void
hoedown_buffer_printf(hoedown_buffer *buf, const char *fmt, ...)
{
	va_list ap;
	int n;

	assert(buf && buf->unit);

	if (buf->size >= buf->asize && hoedown_buffer_grow(buf, buf->size + 1) < 0)
		return;
	
	va_start(ap, fmt);
	n = vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
	va_end(ap);

	if (n < 0) {
#ifndef _MSC_VER
		return;
#else
		va_start(ap, fmt);
		n = _vscprintf(fmt, ap);
		va_end(ap);
#endif
	}

	if ((size_t)n >= buf->asize - buf->size) {
		if (hoedown_buffer_grow(buf, buf->size + n + 1) < 0)
			return;

		va_start(ap, fmt);
		n = vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
		va_end(ap);
	}

	if (n < 0)
		return;

	buf->size += n;
}
Exemple #17
0
void printbuf(printbuf_t* buf, const char* fmt, ...)
{
  size_t avail = buf->size - buf->offset;
  va_list ap;

  va_start(ap, fmt);
  int r = vsnprintf(buf->m + buf->offset, avail, fmt, ap);
  va_end(ap);

  if(r < 0)
  {
#ifdef PLATFORM_IS_WINDOWS
    va_start(ap, fmt);
    r = _vscprintf(fmt, ap);
    va_end(ap);

    if(r < 0)
      return;
#else
    return;
#endif
  }

  if((size_t)r >= avail)
  {
    size_t new_size = buf->size + r + 1;
    buf->m = (char*)ponyint_pool_realloc_size(buf->size, new_size, buf->m);
    buf->size = new_size;
    avail = buf->size - buf->offset;

    va_start(ap, fmt);
    r = vsnprintf(buf->m + buf->offset, avail, fmt, ap);
    va_end(ap);

    pony_assert((r > 0) && ((size_t)r < buf->size));
  }

  buf->offset += r;
}
Exemple #18
0
void Log(const CHAR *pcStr, ...)
{
	va_list var;
	va_start(var, pcStr);
	UINT nCount = _vscprintf(pcStr, var);
	CHAR *pBuffer = new CHAR[nCount + 1];
	if (pBuffer)
	{
		vsprintf_s(pBuffer, nCount + 1, pcStr, var);
		if (g_Handler == null)
		{
			wcout << pBuffer << endl;
			OutputDebugStringA(pBuffer);
			OutputDebugStringA("\n");
		}
		else
			g_Handler->LogHandlerA(pBuffer);

		SAFE_DELETE_ARR(pBuffer);
	}
	va_end(var);
}
//--------------------------------------------------------------------------------------
void DbgMsg(char *lpszFile, int iLine, char *lpszMsg, ...)
{
    va_list mylist;
    va_start(mylist, lpszMsg);

    int len = _vscprintf(lpszMsg, mylist) + 0x100;
    
    char *lpszBuff = (char *)malloc(len);
    if (lpszBuff == NULL)
    {
        va_end(mylist);
        return;
    }

    char *lpszOutBuff = (char *)malloc(len);
    if (lpszOutBuff == NULL)
    {
        free(lpszBuff);
        va_end(mylist);
        return;
    }
    
    vsprintf(lpszBuff, lpszMsg, mylist);
    va_end(mylist);

    sprintf(lpszOutBuff, "[%.5d] %s(%d) : %s", GetCurrentProcessId(), lpszFile, iLine, lpszBuff);	

    OutputDebugString(lpszOutBuff);
    
    HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStd != INVALID_HANDLE_VALUE)
    {
        DWORD dwWritten = 0;
        WriteFile(hStd, lpszBuff, strlen(lpszBuff), &dwWritten, NULL);
    }
        
    free(lpszBuff);
    free(lpszOutBuff);
}
Exemple #20
0
/* bufprintf: formatted printing to a buffer */
void
bufprintf(struct buf *buf, const char *fmt, ...)
{
	va_list ap;
	int n;

	assert(buf && buf->unit);

	if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < 0)
		return;
	
	va_start(ap, fmt);
	n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
	va_end(ap);

	if (n < 0) {
#ifdef _MSC_VER
		va_start(ap, fmt);
		n = _vscprintf(fmt, ap);
		va_end(ap);
#else
		return;
#endif
	}

	if ((size_t)n >= buf->asize - buf->size) {
		if (bufgrow(buf, buf->size + n + 1) < 0)
			return;

		va_start(ap, fmt);
		n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
		va_end(ap);
	}

	if (n < 0)
		return;

	buf->size += n;
}
Exemple #21
0
std::string formatString(const char *fmt, ...) {
	char tmp[512];
	va_list iterator;

#if defined(WIN32)
	va_start(iterator, fmt);
	size_t size = _vscprintf(fmt, iterator) + 1;

	if (size >= sizeof(tmp)) {
		char *dest = new char[size];
		vsnprintf_s(dest, size, size-1, fmt, iterator);
		va_end(iterator);
		std::string result(dest);
		delete[] dest;
		return result;
	}

	vsnprintf_s(tmp, size, size-1, fmt, iterator);
	va_end(iterator);
#else
	va_start(iterator, fmt);
	size_t size = vsnprintf(tmp, sizeof(tmp), fmt, iterator);
	va_end(iterator);

	if (size >= sizeof(tmp)) {
		/* Overflow! -- dynamically allocate memory */
		char *dest = new char[size+1];
		va_start(iterator, fmt);
		vsnprintf(dest, size+1, fmt, iterator);
		va_end(iterator);

		std::string result(dest);
		delete[] dest;
		return result;
	}
#endif

	return std::string(tmp);
}
Exemple #22
0
int x264_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
{
    int length = -1;

    if( n )
    {
        va_list arg2;
        va_copy( arg2, arg );
        length = _vsnprintf( s, n, fmt, arg2 );
        va_end( arg2 );

        /* _(v)snprintf adds a null-terminator only if the length is less than the buffer size. */
        if( length < 0 || length >= n )
            s[n-1] = '\0';
    }

    /* _(v)snprintf returns a negative number if the length is greater than the buffer size. */
    if( length < 0 )
        return _vscprintf( fmt, arg );

    return length;
}
int vasprintf(char **strp, const char *fmt, va_list ap)
{
   auto r = -1, size = _vscprintf(fmt, ap);

	if ((size >= 0) && (size < INT_MAX))
	{
		*strp = static_cast<char *>(malloc(size + 1)); //+1 for null
		if (*strp)
		{
			r = vsnprintf(*strp, size + 1, fmt, ap);  //+1 for null
			if ((r < 0) || (r > size))
			{
				free(*strp); 
				*strp = nullptr;
				r = -1;
			}
		}
	}
	else { *strp = nullptr; }

	return(r);
}
int
basestring_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
  int ret;

  if (size == 0)
  {
    char buf[1];
    return basestring_vsnprintf(buf, 1, format, ap);
  }
  ret = IF_WIN(_vsnprintf,vsnprintf)(str, size, format, ap);
  if (ret >= 0 && ret < (int)size)
    return ret;
#ifdef _WIN32
  if (ret < 0 && errno == EINVAL)
    return ret;
  // otherwise, more than size chars are needed
  return _vscprintf(format, ap);
#else
  return ret;
#endif
}
Exemple #25
0
int u8fprintf(FILE *file, const char *format, ...)
{
	int ret = 0;
	va_list argptr;
	char *buf;
	int buflen;
	wchar_t *wbuf;

	va_start(argptr, format);

	buflen = _vscprintf(format, argptr) + 1;
	if(buflen <= 0) {
		return -1;
	}

	buf = (char *)calloc(buflen, sizeof(char));
	if(buf == NULL) {
		return -1;
	}

	vsnprintf(buf, buflen, format, argptr);

	va_end(argptr);

	if(file == stdout || file == stderr) {
		wbuf = u8stows(buf);
		if(wbuf) {
			ret = fwprintf(file, L"%s", wbuf);
			free(wbuf);
		}
	}
	else {
		ret = fprintf(file, "%s", buf);
	}

	free(buf);

	return ret;
}
void LogGuts(VktraceLogLevel level, const char* fmt, va_list args) {
#if defined(WIN32)
    int requiredLength = _vscprintf(fmt, args) + 1;
#elif defined(PLATFORM_LINUX) || defined(PLATFORM_OSX)
    int requiredLength;
    va_list argcopy;
    va_copy(argcopy, args);
    requiredLength = vsnprintf(NULL, 0, fmt, argcopy) + 1;
    va_end(argcopy);
#endif
    static VKTRACE_THREAD_LOCAL BOOL logging = FALSE;

    // Don't recursively log problems found during logging
    if (logging) {
        return;
    }
    logging = TRUE;

    char* message = (char*)vktrace_malloc(requiredLength);
#if defined(WIN32)
    _vsnprintf_s(message, requiredLength, requiredLength - 1, fmt, args);
#elif defined(PLATFORM_LINUX) || defined(PLATFORM_OSX)
    vsnprintf(message, requiredLength, fmt, args);
#endif

    if (s_reportFunc != NULL) {
        s_reportFunc(level, message);
    } else {
#ifdef ANDROID
#include <android/log.h>
        __android_log_print(ANDROID_LOG_INFO, "vktrace", "%s: %s\n", vktrace_LogLevelToString(level), message);
#else
        printf("%s: %s\n", vktrace_LogLevelToString(level), message);
#endif
    }

    vktrace_free(message);
    logging = FALSE;
}
/**
 * Like sprintf but allocates a buffer large enough to hold the formatted string.
 * The caller is responsible for freeing the memory.
 */
static char* _asprintf(const char* fmt, ...)
{
	size_t size;
	va_list args, tmp_args;
	char* str = 0;

	va_start(args, fmt);
	va_copy(tmp_args, args);
	size = _vscprintf(fmt, tmp_args) + 1;
	va_end(tmp_args);
	if (size > 0)
	{
		str = (char*)malloc(size);
		if (vsnprintf_s(str, size, _TRUNCATE, fmt, args) < 0)
		{
			str[size-1] = 0;
		}
	}
	va_end(args);

	return str;
}
Exemple #28
0
void __cdecl logmsg(const char * format, ...)
{
    va_list args;
    int		len	 ;
    char	buffer[VALUE_LEN+3];
    va_start (args, format);
    len	 =	_vscprintf(format, args);
    if (len > 0 && len < VALUE_LEN && strlen(logfile_buf) > 0)
    {
        FILE	*pFile = NULL;
        len = _vsnprintf(buffer,len,format, args);
        buffer[len++] = '\n';
        buffer[len] = '\0';
        if ( (pFile = fopen(logfile_buf,"a+")) != NULL )
        {
            fprintf(pFile,buffer);
            fclose(pFile);
        }
        va_end(args);
    }
    return;
}
Exemple #29
0
void MCExecPoint::setstringf(const char *p_spec, ...)
{
	va_list t_args;
	int t_count;

#if defined(_HAS_VSCPRINTF)
	va_start(t_args, p_spec);
	t_count = _vscprintf(p_spec, t_args);
	va_end(t_args);
#elif defined(_HAS_VSNPRINTF)
	va_start(t_args, p_spec);
	t_count = vsnprintf(nil, 0, p_spec, t_args);
	va_end(t_args);
#else
#error MCExecPoint::setstringf not implemented
#endif

	va_start(t_args, p_spec);
	vsprintf(getbuffer(t_count + 1), p_spec, t_args);
	va_end(t_args);

	setstrlen();
}
Exemple #30
0
void lxs_sappend_vformat(lua_State* const L,
                         lxs_string* const s,
                         const char* fmt,
                         va_list args)
{
    lxs_assert(L, L);
    lxs_assert(L, s);
    lxs_assert(L, fmt);
    lxs_assert(L, args);

    int len = _vscprintf(fmt, args);
    if (len < 0)
        lxs_error(L, "invalid format string and/or format arguments");
    else if (len == 0)
        return;

    lxs_sensure_space(L, s, static_cast<size_t>(len));

    vsprintf_s(&s->data[s->len], lxs_sfree(s) + 1, fmt, args);
    s->len += len;

    lxs_sterminate(s);
}