File: gstring_explode.cc

package info (click to toggle)
sdcc 3.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 99,212 kB
  • sloc: ansic: 918,594; cpp: 69,526; makefile: 56,790; sh: 29,616; asm: 12,364; perl: 12,136; yacc: 7,179; lisp: 1,672; python: 812; lex: 773; awk: 495; sed: 89
file content (34 lines) | stat: -rw-r--r-- 924 bytes parent folder | download | duplicates (2)
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;
}