cwObject.h/cpp : Added to_string() and objectToFile()
This commit is contained in:
parent
2171d8a0a6
commit
b5f7e76983
41
cwObject.cpp
41
cwObject.cpp
@ -676,6 +676,25 @@ unsigned cw::object_t::to_string( char* buf, unsigned bufByteN ) const
|
||||
return type->to_string(this,buf,bufByteN );
|
||||
}
|
||||
|
||||
char* cw::object_t::to_string() const
|
||||
{
|
||||
unsigned n = 1024;
|
||||
char* buf = mem::alloc<char>(n);
|
||||
do
|
||||
{
|
||||
unsigned actual_bytes = to_string(buf,n);
|
||||
if( actual_bytes < n )
|
||||
break;
|
||||
|
||||
n *= 2;
|
||||
buf = mem::resize(buf,n);
|
||||
|
||||
}while(1);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
void cw::object_t::print(const print_ctx_t* c) const
|
||||
{
|
||||
print_ctx_t ctx;
|
||||
@ -965,5 +984,27 @@ void cw::objectPrintTypes( object_t* o0 )
|
||||
|
||||
|
||||
|
||||
cw::rc_t cw::objectToFile( const char* fn, const object_t* obj )
|
||||
{
|
||||
rc_t rc = kOkRC;
|
||||
|
||||
char* buf;
|
||||
if((buf = obj->to_string()) == nullptr )
|
||||
{
|
||||
rc = cwLogError(kOpFailRC,"Unable to convert object to string.");
|
||||
goto errLabel;
|
||||
}
|
||||
|
||||
if((rc = file::fnWrite(fn,buf,textLength(buf))) != kOkRC )
|
||||
{
|
||||
rc = cwLogError(rc,"Object write failed on write to '%s'.",cwStringNullGuard(fn));
|
||||
goto errLabel;
|
||||
}
|
||||
errLabel:
|
||||
|
||||
mem::release(buf);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
@ -259,6 +259,7 @@ namespace cw
|
||||
|
||||
// convert this object to a string
|
||||
unsigned to_string( char* buf, unsigned bufByteN ) const;
|
||||
char* to_string() const;
|
||||
|
||||
// print this object
|
||||
void print(const print_ctx_t* c=NULL) const;
|
||||
@ -304,6 +305,8 @@ namespace cw
|
||||
rc_t objectFromFile( const char* fn, object_t*& objRef );
|
||||
void objectPrintTypes( object_t* o );
|
||||
|
||||
rc_t objectToFile( const char* fn, const object_t* obj );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user