From 268a53de823a6750d6256ee1fb1e7707b4b45740 Mon Sep 17 00:00:00 2001 From: Xavier ASUS Date: Fri, 18 Oct 2019 00:31:54 +0200 Subject: sdcc-3.9.0 fork implementing GNU assembler syntax This fork aims to provide better support for stm8-binutils --- sim/ucsim/gstring_explode.cc | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 sim/ucsim/gstring_explode.cc (limited to 'sim/ucsim/gstring_explode.cc') diff --git a/sim/ucsim/gstring_explode.cc b/sim/ucsim/gstring_explode.cc new file mode 100644 index 0000000..d1e8df1 --- /dev/null +++ b/sim/ucsim/gstring_explode.cc @@ -0,0 +1,34 @@ +#include +#include +#include + +/** + * Uses of explode and implode. Note: there are no memory leaks here. + */ +int main(void) +{ + gstring a = "drewpc:x:38241:29:Drew Philip C:/home/cia/drewpc:/usr/local/bin/tcsh"; + gstring token = ":"; + gstring bar; + gstring* foo; // Array of gstrings. + int nfields = a.nfields(token); + + cout << "Variable is initialized to a string: " << a << endl; + cout << "Variable is seperated by token: '" << token << "'" << endl; + + // explode() allocates memory for each array index automatically. + foo = a.explode(token); + + for(int i = 0; i < nfields; i++) { + cout << "Array[" << i << "]: " << foo[i] << endl; + } + + // implode puts foo back together, separated by token. + bar = implode(foo, token, nfields); + cout << "Variable is set to implosion of array: " << bar << endl; + + // you do have to delete foo, or you'll have memory leaks. + delete [] foo; + + return 0; +} -- cgit v1.2.3