fmtmsg(v) | C++ Coding

Andreas Hamm
staatlich geprüfter technischer Assistent für Informatik
Startseite Ι C++ Coding

fmtmsgv


C++
template<class _Elem, class _Traits, class _Ax>
int fmtmsgv(typename std::basic_string<_Elem, _Traits, _Ax>& _Message, const _Elem* _Format, va_list _Ap);


C
int _fmtmsgv(char* _Message, size_t _Size, const char* _Format, va_list _Ap);
int _wfmtmsgv(wchar_t* _Message, size_t _Size, const wchar_t* _Format, va_list _Ap);

C++ Quellcode

fmtmsg.hpp
Quellcode Auszug…
#pragma once

#include "vsxprnf.inl"
#include <stdarg.h>
#include <string>


template<class _Elem, class _Traits, class _Ax>
int fmtmsgv(typename std::basic_string<_Elem, _Traits, _Ax>& _Message, const _Elem* _Format, va_list _Ap);
{
int _Retv = -1;

const int _Cch = vscxprintf(_Format, _Ap);
if( 0 >= _Cch )
{
_Retv = _Cch;
}
else
{
#if !defined(_DEBUG)
_Elem _Buf[256];

#else
_Elem _Buf[16];

#endif
_Elem *_Ptr = (_countof(_Buf) > _Cch) ? (_Buf) : new _Elem[1 + _Cch];
if( !(_Ptr) )
{
/*_Retv = -1;*/
}
else
{
const int _Ccw = vsxprintf_s(_Ptr, (_Cch + 1), _Format, _Ap);
if( 0 >= _Ccw )
{
/*_Retv = _Ccw;*/
}
else if( /*0 > ccw || */(size_t)_Ccw > _Message.max_size() )
// Message buffer size to small ...
{
_Message.assign(_Ptr, _Message.max_size());
}
else
// Anything seems to be alright ...
{
const size_t _Maxcch = (size_t)(_Ccw + 1);
_Message.assign(_Ptr, _Maxcch);
}
if( _Buf != _Ptr )
delete[] _Ptr;
_Retv = _Ccw;
}
}

return _Retv;
}

vsprnf.inl
#pragma once

#include "stdarg.h"
#include "stdio.h"
#include "ctype.h"
#include "wchar.h"


__inline
int vscxprintf(const char* _Format, va_list _Ap)
{
return ::_vscprintf(_Format, _Ap);
}


__inline
int vscxprintf(const wchar_t* _Format, va_list _Ap)
{
return ::_vscwprintf(_Format, _Ap);
}


__inline
int vsxprintf_s(char* _Message, size_t _Size, const char* _Format, va_list _Ap)
{
return ::vsprintf_s(_Message, _Size, _Format, _Ap);
}


__inline
int vsxprintf_s(wchar_t* _Message, size_t _Size, const wchar_t* _Format, va_list _Ap)
{
return ::vswprintf_s(_Message, _Size, _Format, _Ap);
}

C Quellcode

xfmtmsg.c
Quellcode Auszug…
#include "pch.h"

#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <wchar.h>


#if !defined(MAX_FMTMSGV_BUFSIZE)
#if !defined(_DEBUG)
#define MAX_FMTMSGV_BUFSIZE 256

#else
#define MAX_FMTMSGV_BUFSIZE 16

#endif
#endif


int _fmtmsgv(char* _Message, size_t _Size, const char* _Format, va_list _Ap);
{
int _Retv = -1;
if( !(_Message) )
{
_set_errno(EINVAL);
/*_Retv = -1;*/
}
else if( 0 >= (int)(_Count) )
{
_set_errno(EINVAL);
/*_Retv = -1;*/
}
else
{
const int _Cch = _vscprintf(_Format, _Ap);
if( 0 >= _Cch )
{
_Retv = _Cch;
}
else
{
char _Buf[MAX_FMTMSGV_BUFSIZE];
char *_Ptr = (_countof(_Buf) > _Cch) ? (_Buf) : (char*)(malloc((_Cch + 1)/* * sizeof(char)*/));
if( !(_Ptr) )
{
/*_Retv = -1;*/
}
else
{
const int _Ccw = vsprintf_s(_Ptr, (_Cch + 1), _Format, _Ap);
if( 0 >= _Ccw )
{
/*_Retv = _Ccw;*/
}
else if( /*0 < _Ccw && */_Ccw <= (int)(_Count) )
// Anything seems to be alright ...
{
const size_t _Maxcch = min((_Count + 1), (size_t)(_Ccw + 1));
errno_t err;
if( (err = strncpy_s(_Message, _Maxcch, _Ptr, _Maxcch) != 0) )
{
/*_Retv = -1;*/
}
}
else
// Message buffer size (count of characters)
//to small ...
{
errno_t err;
if( (err = strncpy_s(_Message, _Count, _Ptr, _Count) != 0) )
{
/*_Retv = -1;*/
}
else
{
_Message[_Count] = '\0';
_set_errno(EINVAL);
}
}
if( _Buf != _Ptr )
free(_Ptr);
_Retv = _Ccw;
}
}
}
return _Retv;
}


int _wfmtmsgv(wchar_t* _Message, size_t _Size, const wchar_t* _Format, va_list _Ap);
{
int _Retv = -1;
if( !(_Message) )
{
_set_errno(EINVAL);
/*_Retv = -1;*/
}
else if( 0 >= (int)(_Count))
{
_set_errno(EINVAL);
/*_Retv = -1;*/
}
else
{
const int _Cch = _vscwprintf(_Format, _Ap);
if( 0 >= _Cch )
{
_Retv = _Cch;
}
else
{
wchar_t _Buf[MAX_FMTMSGV_BUFSIZE];
wchar_t *_Ptr = (_countof(_Buf) > _Cch) ? (_Buf) : (wchar_t*)(malloc((_Cch + 1) * sizeof(wchar_t)));
if( !(_Ptr) )
{
/*_Retv = -1;*/
}
else
{
const int _Ccw = vswprintf_s(_Ptr, (_Cch + 1), _Format, _Ap);
if( 0 >= _Ccw )
{
/*_Retv = _Ccw;*/
}
else if( /*0 < _Ccw && */_Ccw <= (int)(_Count) )
// Anything seems to be alright ...
{
const size_t _Maxcch = min((_Count + 1), (size_t)(_Ccw + 1));
errno_t err;
if( (err = wcsncpy_s(_Message, _Maxcch, _Ptr, _Maxcch) != 0) )
{
/*_Retv = -1;*/
}
}
else
// Message buffer size (count of characters)
//to small ...
{
errno_t err;
if( (err = wcsncpy_s(_Message, _Count, _Ptr, _Count) != 0) )
{
/*_Retv = -1;*/
}
else
{
_Message[_Count] = '\0';
_set_errno(EINVAL);
}
}
if( _Buf != _Ptr )
free(_Ptr);
_Retv = _Ccw;
}
}
}
return _Retv;
}
Seitenanfang
Impressum Ι Rechtliche Hinweise Ι Haftungsausschluss Ι Nutzungsbedingungen