summaryrefslogtreecommitdiff
path: root/sim/ucsim/gstring_explode.cc
diff options
context:
space:
mode:
authorXavier ASUS <xavi92psx@gmail.com>2019-10-18 00:31:54 +0200
committerXavier ASUS <xavi92psx@gmail.com>2019-10-18 00:31:54 +0200
commit268a53de823a6750d6256ee1fb1e7707b4b45740 (patch)
tree42c1799a9a82b2f7d9790ee9fe181d72a7274751 /sim/ucsim/gstring_explode.cc
downloadsdcc-gas-268a53de823a6750d6256ee1fb1e7707b4b45740.tar.gz
sdcc-3.9.0 fork implementing GNU assembler syntax
This fork aims to provide better support for stm8-binutils
Diffstat (limited to 'sim/ucsim/gstring_explode.cc')
-rw-r--r--sim/ucsim/gstring_explode.cc34
1 files changed, 34 insertions, 0 deletions
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 <gstring.h>
+#include <iostream.h>
+#include <fstream.h>
+
+/**
+ * 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;
+}