File: constr.c

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (51 lines) | stat: -rw-r--r-- 1,061 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <r_util.h>

/* constant string storage */

R_API RConstr* r_constr_new (int size) {
	RConstr *c = R_NEW (RConstr);
	c->l = size>0? size: 1024;
	c->b = malloc (c->l);
	c->i = *c->b = 0;
	return c;
}

R_API void r_constr_free (RConstr *c) {
	free (c->b);
	free (c);
}

R_API const char *r_constr_get (RConstr *c, const char *str) {
	char *e = c->b+c->i, *p = c->b;
	for (p = c->b; p<e; p += strlen (p)+1) {
		if (!strcmp (p, str))
			return p;
	}
	return NULL;
}

R_API const char *r_constr_append (RConstr *c, const char *str) {
	int i = c->i, l = strlen (str)+1;
	if ((c->b + i+l) >= (c->b + c->l))
		return NULL;
	memcpy (c->b + i, str, l);
	c->i += l;
	return c->b+i;
}

R_API const char *r_constr_add (RConstr *c, const char *str) {
	char *p = (char *)r_constr_get (c, str);
	return p? p: r_constr_append (c, str);
}

#if MAIN
main() {
	RConstr *cstr = r_constr_new (7);

	printf ("%s\n", r_constr_add (cstr, "Hello"));
	printf ("%s\n", r_constr_add (cstr, "Hello"));
	printf ("%s\n", r_constr_add (cstr, "World"));

	r_constr_free (cstr);
}
#endif