File: table.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 (61 lines) | stat: -rw-r--r-- 1,562 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
52
53
54
55
56
57
58
59
60
61
/* radare - LGPL - Copyright 2009-2012 - pancake */

#include "r_db.h"
#include "r_util.h"

struct r_db_table_t *r_db_table_new(const char *name, const char *fmt, const char *fields) {
	int i;
	int offset = 0;
	struct r_db_table_t *table = R_NEW (RDatabaseTable);
	table->args = strdup (fields);
	table->nelems = r_str_word_set0 (table->args);
	if (table->nelems != strlen (fmt)) {
		eprintf ("r_db_table_new: Invalid arguments\n");
		/* XXX: refactor */
		free (table->args);
		free (table);
		table = NULL;
	} else {
		table->fmt = strdup (fmt);
		table->name = strdup (name);
		table->offset = (int*)malloc (sizeof (int)*table->nelems);
		for (i=0; i<table->nelems; i++) {
			table->offset[i] = offset;
			offset += 4;
		}
	}
	return table;
}

/* Get offset of given named field inside the table */
int r_db_table_key(struct r_db_table_t *table, const char *name) {
	const char *word;
	int i;
	for(i=0;i<table->nelems;i++) {
		word = r_str_word_get0(table->args, i);
		if (!strcmp(name, word))
			break;
	}
	return table->offset[i];
}

/* Get offset of the N field in the table */
int r_db_table_key_i(struct r_db_table_t *table, int elem) {
	int key = -1;
	if (elem>=0 && table->nelems<elem)
		key = table->offset[elem];
	return key;
}

/* Get name of the N field in the table */
const char *r_db_table_field_i(struct r_db_table_t *table, int elem) {
	const char *name = NULL;
	if (elem>=0 && table->nelems<elem)
		name = r_str_word_get0 (table->args, elem);
	return name;
}

void *r_db_table_free(struct r_db_table_t *table) {
	free(table);
	return NULL;
}