S
Stefan Ram
I would know how to write a plain and simple function
to search and replace a substring within a string. It might
take several hours including testing. But for something so
common, I thought it should be easier to find some code
for this already existing. This turned out to be more
difficult than I thought.
Does anyone know of a function or a string library that
can be used as follows:
#include <lib.h>
...
{ char * const p = malloc( 1024 );
if( p )
{ strncpy( p, "exemple", 1023 ); p[ 1023 ] = 0;
char * const r = srp( p, "e", "ABC", 1024 );
if( r )
{ printf( "%s\n", r );
if( r != p )free( r ); }
free( p ); }}
This should print "ABCxABCmplABC". The function "srp"
might replace within the buffer given (it knows the size
from the fourth argument). If this is not possible, it
tries to realloc or malloc a larger buffer. The buffer
with the result is returned (or 0 in case of an error).
Has anyone already written something like this?
The interface might differ - it just should search and
replace.
If not:
Does there exist a simple text editor without a UI, but a
C-API (written in plain ISO C)?
#include <editor.h>
...
struct editor * const e = editor_load_from_file( "alpha.txt" );
if( e )
{ editor_search_and_replace( e, "e", "ABC" );
char * s = editor_string( e ); /* this function is less important */
if( s ) { printf( "buffer = \"%s\".\n", s ); editor_string_dispose( s ); }
editor_save_to_file( e, "alpha.txt", EDITOR_OVERWRITE_OK );
editor_quit( e ); }
TIA
to search and replace a substring within a string. It might
take several hours including testing. But for something so
common, I thought it should be easier to find some code
for this already existing. This turned out to be more
difficult than I thought.
Does anyone know of a function or a string library that
can be used as follows:
#include <lib.h>
...
{ char * const p = malloc( 1024 );
if( p )
{ strncpy( p, "exemple", 1023 ); p[ 1023 ] = 0;
char * const r = srp( p, "e", "ABC", 1024 );
if( r )
{ printf( "%s\n", r );
if( r != p )free( r ); }
free( p ); }}
This should print "ABCxABCmplABC". The function "srp"
might replace within the buffer given (it knows the size
from the fourth argument). If this is not possible, it
tries to realloc or malloc a larger buffer. The buffer
with the result is returned (or 0 in case of an error).
Has anyone already written something like this?
The interface might differ - it just should search and
replace.
If not:
Does there exist a simple text editor without a UI, but a
C-API (written in plain ISO C)?
#include <editor.h>
...
struct editor * const e = editor_load_from_file( "alpha.txt" );
if( e )
{ editor_search_and_replace( e, "e", "ABC" );
char * s = editor_string( e ); /* this function is less important */
if( s ) { printf( "buffer = \"%s\".\n", s ); editor_string_dispose( s ); }
editor_save_to_file( e, "alpha.txt", EDITOR_OVERWRITE_OK );
editor_quit( e ); }
TIA