Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reading of line numbers of tables, arrays and keys. #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions line_number_sample.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[fruit]]
name = "Banana"
price = 1.1

[[fruit]]
name = "Orange"

[[fruit]]
name = "Carrot"
price = 1.2
40 changes: 39 additions & 1 deletion toml.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ typedef struct toml_keyval_t toml_keyval_t;
struct toml_keyval_t {
const char *key; /* key to this value */
const char *val; /* the raw value */
int lineno;
};

typedef struct toml_arritem_t toml_arritem_t;
Expand All @@ -301,6 +302,7 @@ struct toml_array_t {

int nitem; /* number of elements */
toml_arritem_t *item;
int lineno;
};

struct toml_table_t {
Expand All @@ -319,6 +321,7 @@ struct toml_table_t {
/* tables in the table */
int ntab;
toml_table_t **tab;
int lineno;
};

static inline void xfree(const void *x) {
Expand Down Expand Up @@ -764,6 +767,7 @@ static toml_keyval_t *create_keyval_in_table(context_t *ctx, toml_table_t *tab,

/* save the key in the new value struct */
dest->key = newkey;
dest->lineno = keytok.lineno;
return dest;
}

Expand Down Expand Up @@ -812,6 +816,7 @@ static toml_table_t *create_keytable_in_table(context_t *ctx, toml_table_t *tab,

/* save the key in the new table struct */
dest->key = newkey;
dest->lineno = ctx->tok.lineno;
return dest;
}

Expand Down Expand Up @@ -905,6 +910,7 @@ static toml_table_t *create_table_in_array(context_t *ctx,
e_outofmemory(ctx, FLINE);
return 0;
}
ret->lineno = ctx->tok.lineno;
base[n].tab = ret;
parent->item = base;
parent->nitem++;
Expand Down Expand Up @@ -1292,7 +1298,8 @@ static int walk_tabpath(context_t *ctx) {
nexttab->implicit = true;
} break;
}


nexttab->lineno = ctx->tok.lineno;
/* switch to next tab */
curtab = nexttab;
}
Expand Down Expand Up @@ -1889,6 +1896,37 @@ int toml_key_exists(const toml_table_t *tab, const char *key) {
return 0;
}

int toml_key_lineno(const toml_table_t *tab, const char *key) {
int i;
for (i = 0; i < tab->nkval; i++) {
if (0 == strcmp(key, tab->kval[i]->key)) {
return tab->kval[i]->lineno;
}
}
for (i = 0; i < tab->narr; i++) {
if (0 == strcmp(key, tab->arr[i]->key)) {
return tab->arr[i]->lineno;
}
}
for (i = 0; i < tab->ntab; i++) {
if (0 == strcmp(key, tab->tab[i]->key)) {
toml_table_t * value = tab->tab[i];
return value->lineno;
}
}

return -1;
}

int toml_table_lineno(const toml_table_t *tab) {
return tab->lineno;
}


int toml_array_lineno(const toml_array_t *arr) {
return arr->lineno;
}

toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key) {
int i;
for (i = 0; i < tab->nkval; i++) {
Expand Down
6 changes: 6 additions & 0 deletions toml.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ TOML_EXTERN char toml_array_type(const toml_array_t *arr);
/* Return the key of an array */
TOML_EXTERN const char *toml_array_key(const toml_array_t *arr);

TOML_EXTERN int toml_array_lineno(const toml_array_t *arr);

/* Return the number of key-values in a table */
TOML_EXTERN int toml_table_nkval(const toml_table_t *tab);

Expand All @@ -150,6 +152,10 @@ TOML_EXTERN int toml_table_ntab(const toml_table_t *tab);
/* Return the key of a table*/
TOML_EXTERN const char *toml_table_key(const toml_table_t *tab);

TOML_EXTERN int toml_key_lineno(const toml_table_t *tab, const char *key);

TOML_EXTERN int toml_table_lineno(const toml_table_t *tab);

/*--------------------------------------------------------------
* misc
*/
Expand Down
57 changes: 57 additions & 0 deletions toml_line_number_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "toml.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void fatal(const char *msg, const char *msg1) {
fprintf(stderr, "ERROR: %s%s\n", msg, msg1 ? msg1 : "");
exit(1);
}


int main() {
FILE *fp;
char errbuf[200];

// 1. Read and parse toml file
fp = fopen("line_number_sample.toml", "r");
if (!fp) {
fatal("cannot open line_number_sample.toml - ", strerror(errno));
}

toml_table_t *conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);

if (!conf) {
fatal("cannot parse - ", errbuf);
}

// 2. Traverse to an array of fruit.
toml_array_t *fruits = toml_array_in(conf, "fruit");
if (!fruits) {
fatal("missing [fruit] table", "");
}
printf("Found fruit table starting at line: %d\n",toml_key_lineno(conf,"fruit"));

toml_table_t * second_fruit = toml_table_at(fruits,1);

toml_datum_t name = toml_string_in(second_fruit, "price");
if (!name.ok) {
fprintf(stderr, "Fruit at line: %d did not specify a price.\n",toml_table_lineno(second_fruit));
}

toml_table_t * third_fruit = toml_table_at(fruits,2);
toml_datum_t third_name = toml_string_in(third_fruit,"name");
if (!third_name.ok) {
fputs("Problem with example toml file",stderr);
exit(EXIT_FAILURE);
}

if (strcmp(third_name.u.s,"carrot")) {
fprintf(stderr, "Fruit name at line: %d was not a permitted value.\n",toml_key_lineno(third_fruit,"name"));
}
// I do not free stuff because it exits immediately

return 0;
}