2012-12-06 02:11:40 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2012 - pancake */
|
2011-03-17 18:05:39 +00:00
|
|
|
|
2009-07-08 17:14:19 +00:00
|
|
|
#include "r_db.h"
|
2009-09-08 01:08:46 +00:00
|
|
|
#include "r_util.h"
|
2009-07-08 17:14:19 +00:00
|
|
|
|
2010-06-13 09:59:17 +00:00
|
|
|
struct r_db_table_t *r_db_table_new(const char *name, const char *fmt, const char *fields) {
|
2009-07-08 17:14:19 +00:00
|
|
|
int i;
|
|
|
|
int offset = 0;
|
2011-03-17 18:05:39 +00:00
|
|
|
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)) {
|
2012-12-06 02:11:40 +00:00
|
|
|
eprintf ("r_db_table_new: Invalid arguments\n");
|
2009-07-08 17:14:19 +00:00
|
|
|
/* XXX: refactor */
|
2011-03-17 18:05:39 +00:00
|
|
|
free (table->args);
|
|
|
|
free (table);
|
2009-07-08 17:14:19 +00:00
|
|
|
table = NULL;
|
|
|
|
} else {
|
2011-03-17 18:05:39 +00:00
|
|
|
table->fmt = strdup (fmt);
|
|
|
|
table->name = strdup (name);
|
|
|
|
table->offset = (int*)malloc (sizeof (int)*table->nelems);
|
|
|
|
for (i=0; i<table->nelems; i++) {
|
2009-07-08 17:14:19 +00:00
|
|
|
table->offset[i] = offset;
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get offset of given named field inside the table */
|
2010-06-13 09:59:17 +00:00
|
|
|
int r_db_table_key(struct r_db_table_t *table, const char *name) {
|
2012-03-31 23:40:04 +00:00
|
|
|
const char *word;
|
2009-07-08 17:14:19 +00:00
|
|
|
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 */
|
2010-06-13 09:59:17 +00:00
|
|
|
int r_db_table_key_i(struct r_db_table_t *table, int elem) {
|
2009-07-08 17:14:19 +00:00
|
|
|
int key = -1;
|
|
|
|
if (elem>=0 && table->nelems<elem)
|
|
|
|
key = table->offset[elem];
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get name of the N field in the table */
|
2010-06-13 09:59:17 +00:00
|
|
|
const char *r_db_table_field_i(struct r_db_table_t *table, int elem) {
|
2012-03-31 23:40:04 +00:00
|
|
|
const char *name = NULL;
|
2009-07-08 17:14:19 +00:00
|
|
|
if (elem>=0 && table->nelems<elem)
|
2012-03-31 23:40:04 +00:00
|
|
|
name = r_str_word_get0 (table->args, elem);
|
2009-07-08 17:14:19 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2010-06-13 09:59:17 +00:00
|
|
|
void *r_db_table_free(struct r_db_table_t *table) {
|
2009-07-08 17:14:19 +00:00
|
|
|
free(table);
|
|
|
|
return NULL;
|
|
|
|
}
|