Skip to content

Commit

Permalink
refactor: changed the behaviour of turn_into_cstr_c, it returns at le…
Browse files Browse the repository at this point in the history
…ast ""
  • Loading branch information
aiq committed Jun 27, 2024
1 parent bbff6c7 commit e7b9efd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
3 changes: 2 additions & 1 deletion doc/clingo/io/cRecorder.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,8 @@ char* turn_into_cstr_c( cRecorder rec[static 1] );
Records if necessary a null character, moves the recorder to the beginning and
returns the recorded memory as C string.
Returns NULL if the function can not finish the memory with a null character.
Returns an empty C string if the function can not finish the memory with a
null character.
.Example
[source,c]
Expand Down
10 changes: 4 additions & 6 deletions inc/clingo/io/cRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,9 @@ CLINGO_API cPen recorder_pen_c( cRecorder rec[static 1] );
mem
*******************************************************************************/

CLINGO_API
bool alloc_recorder_mem_c( cRecorder rec[static 1], int64_t size );
CLINGO_API
bool realloc_recorder_mem_c( cRecorder rec[static 1], int64_t size );
CLINGO_API
void free_recorder_mem_c( cRecorder rec[static 1] );
CLINGO_API bool alloc_recorder_mem_c( cRecorder rec[static 1], int64_t size );
CLINGO_API bool realloc_recorder_mem_c( cRecorder rec[static 1], int64_t size );
CLINGO_API void free_recorder_mem_c( cRecorder rec[static 1] );

CLINGO_API
inline int64_t recorder_cap_c( cRecorder const rec[static 1] )
Expand Down Expand Up @@ -195,6 +192,7 @@ inline void reset_recorder_c( cRecorder rec[static 1] )
/*******************************************************************************
recorded
*******************************************************************************/

CLINGO_API
inline cBytes recorded_bytes_c( cRecorder const rec[static 1] )
{
Expand Down
20 changes: 12 additions & 8 deletions src/clingo/io/cRecorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,20 @@ void println_recorded_c( cRecorder rec[static 1] )
putchar( '\n' );
}

char* turn_into_cstr_c( cRecorder rec[static 1] )
static inline bool finish_cstr( cRecorder rec[static 1] )
{
if ( recorder_cap_c( rec ) == 0 ) return NULL;
cChars recorded = recorded_chars_c( rec );
if ( not is_empty_c_( recorded ) and
last_c_( recorded ) == '\0' )
return true;

if ( not record_char_c( rec, '\0' ) )
{
char const* cstr = rec->mem;
--cstr;
if ( *cstr != '\0' ) return NULL;
}
return record_char_c( rec, '\0' );
}

char* turn_into_cstr_c( cRecorder rec[static 1] )
{
if ( not finish_cstr( rec ) )
return "";

reset_recorder_c( rec );
return rec->mem;
Expand Down
4 changes: 2 additions & 2 deletions test/clingo/io/cRecorder/turn_into_cstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main( void )

rec = &null_recorder_c_();
cstr = turn_into_cstr_c( rec );
expect_at_c_( cstr == NULL );
expect_eq_c_( strcmp( cstr, "" ) );

rec = &recorder_c_( 4 );
cstr = turn_into_cstr_c( rec );
Expand All @@ -30,7 +30,7 @@ int main( void )

record_chars_c_( rec, "abcd" );
cstr = turn_into_cstr_c( rec );
expect_at_c_( cstr == NULL );
expect_eq_c_( strcmp( cstr, "" ) );

return finish_tap_c_();
}

0 comments on commit e7b9efd

Please sign in to comment.