Skip to content

Commit

Permalink
Add initial implementation for register map parser
Browse files Browse the repository at this point in the history
  • Loading branch information
matwey committed May 9, 2017
1 parent 223963d commit bd3e340
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/reg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef _REG_H
#define _REG_H

#include <stdint.h>

struct reg {
const char* error_str;
uint16_t leds_out;
};

int reg_init(struct reg* reg);
int reg_from_map(struct reg* reg, char* map);
int reg_validate(struct reg* reg);

#endif // _REG_H
68 changes: 68 additions & 0 deletions src/reg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <reg.h>

#include <stdlib.h>
#include <string.h>
#include <assert.h>

static char* x_strchr(const char *s, int c) {
char* n = NULL;

if ((n = strchr(s, c))) {
*(n++) = '\0';
return n;
}

return n;
}

static int reg_from_pair(struct reg* reg, char* key, unsigned long int value) {

assert(value > 0);
assert(value <= 0xFFFF);

reg->leds_out = value;

return 0;
}

static int reg_from_line(struct reg* reg, char* line) {
char* key = line;
char* value = NULL;

if (line[0] == '\0' || line[0] == '#')
return 0;

if (!(value = strchr(line, '=')))
return -1;
*(value++) = '\0';

return reg_from_pair(reg, key, strtoul(value, NULL, 16));
}

int reg_init(struct reg* reg) {
memset(reg, 0, sizeof(struct reg));

return 0;
}

int reg_from_map(struct reg* reg, char* map) {
char* c = map;
char* n = NULL;

do {
n = x_strchr(c, '\n');
if (reg_from_line(reg, c)) {
reg->error_str = "Can not parse register map";

return -1;
}
c = n;
} while (n);

return 0;
}

int reg_validate(struct reg* reg) {

return 0;
}
48 changes: 48 additions & 0 deletions test/reg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <check.h>
#include <stdlib.h>

#include <reg.h>

START_TEST (test_reg_init1) {
struct reg reg;
ck_assert_int_eq(reg_init(&reg), 0);
}
END_TEST
START_TEST (test_reg_from_map1) {
char x[] = "LEDS_OUT = 0x42";
struct reg reg;
ck_assert_int_eq(reg_init(&reg), 0);
ck_assert_int_eq(reg_from_map(&reg, x), 0);
ck_assert_int_eq(reg.leds_out, 0x42);
}
END_TEST

Suite* range_suite(void) {
Suite *s;
TCase *tc_core;

s = suite_create("reg");

tc_core = tcase_create("Core");

tcase_add_test(tc_core, test_reg_init1);
tcase_add_test(tc_core, test_reg_from_map1);
suite_add_tcase(s, tc_core);

return s;
}

int main(void) {
int number_failed;
Suite *s;
SRunner *sr;

s = range_suite();
sr = srunner_create(s);

srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

0 comments on commit bd3e340

Please sign in to comment.