2019-12-19 03:24:12 +00:00
|
|
|
#include "cwCommon.h"
|
|
|
|
#include "cwLog.h"
|
|
|
|
#include "cwCommonImpl.h"
|
|
|
|
#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-24 15:05:24 +00:00
|
|
|
this_t* p = memAllocZ<this_t>();
|
|
|
|
p->buf = memAllocZ<char>(initCharN);
|
|
|
|
p->expandCharN = expandCharN;
|
|
|
|
p->allocCharN = initCharN;
|
|
|
|
p->boolTrueText = memDuplStr("true");
|
2019-12-19 03:24:12 +00:00
|
|
|
p->boolFalseText = memDuplStr("false");
|
|
|
|
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
|
|
|
|
|
|
|
memRelease(p->buf);
|
|
|
|
memRelease(p->boolTrueText);
|
|
|
|
memRelease(p->boolFalseText);
|
2019-12-24 15:05:24 +00:00
|
|
|
memRelease(p);
|
|
|
|
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;
|
|
|
|
p->buf = memResizeZ<char>( p->buf, p->allocCharN );
|
|
|
|
}
|
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 )
|
|
|
|
p->boolTrueText = memReallocStr(p->boolTrueText,s);
|
|
|
|
else
|
|
|
|
p->boolFalseText = memReallocStr(p->boolFalseText,s);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
cw::rc_t cw::textBuf::test()
|
|
|
|
{
|
|
|
|
handle_t h;
|
|
|
|
rc_t rc;
|
|
|
|
|
|
|
|
if((rc = create(h,8,8)) != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
print(h,"Hello\n");
|
|
|
|
print(h,"foo\n");
|
|
|
|
|
|
|
|
printf("%s", text(h) );
|
|
|
|
|
|
|
|
return destroy(h);
|
|
|
|
}
|