2024-12-01 19:35:24 +00:00
|
|
|
//| 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"
|
2024-05-30 13:20:41 +00:00
|
|
|
#include "cwTest.h"
|
2019-12-19 03:24:12 +00:00
|
|
|
#include "cwMem.h"
|
|
|
|
#include "cwTextBuf.h"
|
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
namespace textBuf
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
2019-12-24 15:05:24 +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
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
#define _handleToPtr(h) handleToPtr<handle_t,this_t>(h)
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-24 15:05:24 +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;
|
2019-12-24 15:05:24 +00:00
|
|
|
if((rc = destroy(hRef)) != kOkRC )
|
2019-12-19 03:24:12 +00:00
|
|
|
return rc;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
this_t* p = mem::allocZ<this_t>();
|
|
|
|
p->buf = mem::allocZ<char>(initCharN);
|
2019-12-24 15:05:24 +00:00
|
|
|
p->expandCharN = expandCharN;
|
|
|
|
p->allocCharN = initCharN;
|
2019-12-28 02:51:28 +00:00
|
|
|
p->boolTrueText = mem::duplStr("true");
|
|
|
|
p->boolFalseText = mem::duplStr("false");
|
2019-12-19 03:24:12 +00:00
|
|
|
hRef.set(p);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
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;
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
this_t* p = _handleToPtr(hRef);
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
mem::release(p->buf);
|
|
|
|
mem::release(p->boolTrueText);
|
|
|
|
mem::release(p->boolFalseText);
|
|
|
|
mem::release(p);
|
2019-12-24 15:05:24 +00:00
|
|
|
hRef.clear();
|
2019-12-19 03:24:12 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
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
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
this_t* p = _handleToPtr(h);
|
2019-12-19 03:24:12 +00:00
|
|
|
return p->buf;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
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);
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
this_t* p = _handleToPtr(h);
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
int n = vsnprintf(nullptr,0,fmt,vl1);
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
if( n > 0 )
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
2019-12-24 15:05:24 +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;
|
2019-12-28 02:51:28 +00:00
|
|
|
p->buf = mem::resizeZ<char>( p->buf, p->allocCharN );
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
int m = vsnprintf(p->buf + p->endN, n, fmt, vl );
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-24 15:05:24 +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;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
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);
|
2019-12-24 15:05:24 +00:00
|
|
|
rc_t rc = print(h,fmt,vl);
|
2019-12-19 03:24:12 +00:00
|
|
|
va_end(vl);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
cw::rc_t cw::textBuf::printBool( handle_t h, bool v )
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
this_t* p = _handleToPtr(h);
|
|
|
|
return print(h,"%s", v ? p->boolTrueText : p->boolFalseText);
|
2019-12-19 03:24:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +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
|
|
|
|
2019-12-24 15:05:24 +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
|
|
|
|
2019-12-24 15:05:24 +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
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
cw::rc_t cw::textBuf::setBoolFormat( handle_t h, bool v, const char* s)
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
this_t* p = _handleToPtr(h);
|
2019-12-19 03:24:12 +00:00
|
|
|
|
|
|
|
if( v )
|
2019-12-28 02:51:28 +00:00
|
|
|
p->boolTrueText = mem::reallocStr(p->boolTrueText,s);
|
2019-12-19 03:24:12 +00:00
|
|
|
else
|
2019-12-28 02:51:28 +00:00
|
|
|
p->boolFalseText = mem::reallocStr(p->boolFalseText,s);
|
2019-12-19 03:24:12 +00:00
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
cw::rc_t cw::textBuf::setIntFormat( handle_t h, unsigned width, unsigned flags )
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
this_t* p = _handleToPtr(h);
|
2019-12-19 03:24:12 +00:00
|
|
|
p->intWidth = width;
|
|
|
|
p->intFlags = flags;
|
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
cw::rc_t cw::textBuf::setFloatFormat( handle_t h, unsigned width, unsigned decPlN )
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
this_t* p = _handleToPtr(h);
|
2019-12-19 03:24:12 +00:00
|
|
|
p->floatWidth = width;
|
|
|
|
p->floatDecPlN = decPlN;
|
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2024-05-30 13:20:41 +00:00
|
|
|
cw::rc_t cw::textBuf::test( const test::test_args_t& args )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
handle_t h;
|
|
|
|
rc_t rc;
|
|
|
|
|
|
|
|
if((rc = create(h,8,8)) != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
print(h,"Hello\n");
|
|
|
|
print(h,"foo\n");
|
|
|
|
|
2024-05-30 13:20:41 +00:00
|
|
|
cwLogPrint("%s", text(h) );
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
return destroy(h);
|
|
|
|
}
|