libcw/cwTextBuf.cpp

185 lines
4.1 KiB
C++
Raw Permalink Normal View History

//| Copyright: (C) 2020-2024 Kevin Larke <contact AT larke DOT org>
//| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
2019-12-19 03:24:12 +00:00
#include "cwCommon.h"
#include "cwLog.h"
#include "cwCommonImpl.h"
#include "cwTest.h"
2019-12-19 03:24:12 +00:00
#include "cwMem.h"
#include "cwTextBuf.h"
namespace cw
{
namespace textBuf
2019-12-19 03:24:12 +00:00
{
typedef struct textBuf_str
{
char* buf;
unsigned expandCharN; // count of characters to expand buf
unsigned allocCharN; // current allocated size of buf
unsigned endN; // current count of character in buf
char* boolTrueText;
char* boolFalseText;
int intWidth;
unsigned intFlags;
int floatWidth;
int floatDecPlN;
} this_t;
}
2019-12-19 03:24:12 +00:00
}
#define _handleToPtr(h) handleToPtr<handle_t,this_t>(h)
2019-12-19 03:24:12 +00:00
cw::rc_t cw::textBuf::create( handle_t& hRef, unsigned initCharN, unsigned expandCharN )
2019-12-19 03:24:12 +00:00
{
rc_t rc;
if((rc = destroy(hRef)) != kOkRC )
2019-12-19 03:24:12 +00:00
return rc;
this_t* p = mem::allocZ<this_t>();
p->buf = mem::allocZ<char>(initCharN);
p->expandCharN = expandCharN;
p->allocCharN = initCharN;
p->boolTrueText = mem::duplStr("true");
p->boolFalseText = mem::duplStr("false");
2019-12-19 03:24:12 +00:00
hRef.set(p);
return rc;
}
cw::textBuf::handle_t cw::textBuf::create( unsigned initCharN, unsigned expandCharN )
{
handle_t h;
rc_t rc;
if((rc = create(h,initCharN,expandCharN)) != kOkRC )
{
cwLogError(rc,"Log create ailed.");
h.clear();
}
return h;
}
cw::rc_t cw::textBuf::destroy(handle_t& hRef )
2019-12-19 03:24:12 +00:00
{
rc_t rc = kOkRC;
if( !hRef.isValid() )
return rc;
this_t* p = _handleToPtr(hRef);
2019-12-19 03:24:12 +00:00
mem::release(p->buf);
mem::release(p->boolTrueText);
mem::release(p->boolFalseText);
mem::release(p);
hRef.clear();
2019-12-19 03:24:12 +00:00
return rc;
}
void cw::textBuf::clear( handle_t h )
{
this_t* p = _handleToPtr(h);
p->endN = 0;
}
const char* cw::textBuf::text( handle_t h )
2019-12-19 03:24:12 +00:00
{
this_t* p = _handleToPtr(h);
2019-12-19 03:24:12 +00:00
return p->buf;
}
cw::rc_t cw::textBuf::print( handle_t h, const char* fmt, va_list vl )
2019-12-19 03:24:12 +00:00
{
va_list vl1;
va_copy(vl1,vl);
this_t* p = _handleToPtr(h);
2019-12-19 03:24:12 +00:00
int n = vsnprintf(nullptr,0,fmt,vl1);
2019-12-19 03:24:12 +00:00
if( n > 0 )
2019-12-19 03:24:12 +00:00
{
n += 1; // add one to make space for the terminating zero
if( p->endN + n > p->allocCharN )
{
unsigned minExpandCharN = (p->endN + n) - p->allocCharN;
unsigned expandCharN = std::max(minExpandCharN,p->expandCharN);
p->allocCharN += expandCharN;
p->buf = mem::resizeZ<char>( p->buf, p->allocCharN );
}
2019-12-19 03:24:12 +00:00
int m = vsnprintf(p->buf + p->endN, n, fmt, vl );
2019-12-19 03:24:12 +00:00
cwAssert(m==(n-1));
p->endN += n-1; // subtract 1 to no count the terminating zero in the character count
}
2019-12-19 03:24:12 +00:00
va_end(vl1);
return kOkRC;
}
cw::rc_t cw::textBuf::print( handle_t h, const char* fmt, ... )
2019-12-19 03:24:12 +00:00
{
va_list vl;
va_start(vl,fmt);
rc_t rc = print(h,fmt,vl);
2019-12-19 03:24:12 +00:00
va_end(vl);
return rc;
}
cw::rc_t cw::textBuf::printBool( handle_t h, bool v )
2019-12-19 03:24:12 +00:00
{
this_t* p = _handleToPtr(h);
return print(h,"%s", v ? p->boolTrueText : p->boolFalseText);
2019-12-19 03:24:12 +00:00
}
cw::rc_t cw::textBuf::printInt( handle_t h, int v )
{ return print(h,"%i",v); }
2019-12-19 03:24:12 +00:00
cw::rc_t cw::textBuf::printUInt( handle_t h, unsigned v )
{ return print(h,"%i",v); }
2019-12-19 03:24:12 +00:00
cw::rc_t cw::textBuf::printFloat( handle_t h, double v )
{ return print(h,"%f",v); }
2019-12-19 03:24:12 +00:00
cw::rc_t cw::textBuf::setBoolFormat( handle_t h, bool v, const char* s)
2019-12-19 03:24:12 +00:00
{
this_t* p = _handleToPtr(h);
2019-12-19 03:24:12 +00:00
if( v )
p->boolTrueText = mem::reallocStr(p->boolTrueText,s);
2019-12-19 03:24:12 +00:00
else
p->boolFalseText = mem::reallocStr(p->boolFalseText,s);
2019-12-19 03:24:12 +00:00
return kOkRC;
}
cw::rc_t cw::textBuf::setIntFormat( handle_t h, unsigned width, unsigned flags )
2019-12-19 03:24:12 +00:00
{
this_t* p = _handleToPtr(h);
2019-12-19 03:24:12 +00:00
p->intWidth = width;
p->intFlags = flags;
return kOkRC;
}
cw::rc_t cw::textBuf::setFloatFormat( handle_t h, unsigned width, unsigned decPlN )
2019-12-19 03:24:12 +00:00
{
this_t* p = _handleToPtr(h);
2019-12-19 03:24:12 +00:00
p->floatWidth = width;
p->floatDecPlN = decPlN;
return kOkRC;
}
cw::rc_t cw::textBuf::test( const test::test_args_t& args )
{
handle_t h;
rc_t rc;
if((rc = create(h,8,8)) != kOkRC )
return rc;
print(h,"Hello\n");
print(h,"foo\n");
cwLogPrint("%s", text(h) );
return destroy(h);
}