summaryrefslogtreecommitdiff
path: root/sim/ucsim/gstring_explode.cc
blob: d1e8df1bdafd9b5a6a3f82e4d7457114bdc85692 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}