Skip to content

Commit

Permalink
Dynamically allocate the parsers array
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Engqvist Johansson committed Oct 27, 2015
1 parent 95bc717 commit c0dd2bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/nmea/nmea.c
Expand Up @@ -93,7 +93,7 @@ _split_string(char *string, char **values)
void __attribute__ ((constructor)) nmea_init(void);
void nmea_init()
{
nmea_load_parsers();
(void) nmea_load_parsers();
}

/**
Expand Down
31 changes: 17 additions & 14 deletions src/nmea/parser.c
Expand Up @@ -98,13 +98,11 @@ nmea_init_parser(const char *filename)
int
nmea_load_parsers()
{
int n_files, i;
int i;
char *files[255];
char *parser_path;
nmea_parser_module_s *parser;

memset(parsers, 0, sizeof parsers);

/* Get list of so files */
parser_path = getenv("NMEA_PARSER_PATH");
if (NULL == parser_path) {
Expand All @@ -114,27 +112,32 @@ nmea_load_parsers()
parser_path = strdup(parser_path);
}

n_files = _get_so_files(parser_path, files);
if (1 > n_files) {
free(parser_path);
n_parsers = _get_so_files(parser_path, files);
free(parser_path);
if (1 > n_parsers) {
return -1;
}

i = n_files;
/* Allocate parsers array */
parsers = malloc((sizeof (nmea_parser_module_s *)) * n_parsers);
if (NULL == parsers) {
return (nmea_parser_module_s *) NULL;
}
memset(parsers, 0, (sizeof (nmea_parser_module_s *)) * n_parsers);

i = n_parsers;
while (i-- > 0) {
parser = nmea_init_parser(files[i]);
free(files[i]);

if (NULL == parser) {
free(parser_path);
return -1;
}

parsers[(int) parser->parser.type] = parser;
parsers[(int) parser->parser.type - 1] = parser;
}

free(parser_path);
return n_files;
return n_parsers;
}

void
Expand All @@ -143,7 +146,7 @@ nmea_unload_parsers()
int i = 0;
nmea_parser_module_s *parser;

while (i < NMEA_NUM_PARSERS) {
while (i < n_parsers) {
parser = parsers[i++];
if (NULL == parser) {
continue;
Expand All @@ -157,7 +160,7 @@ nmea_unload_parsers()
nmea_parser_module_s *
nmea_get_parser_by_type(nmea_t type)
{
return parsers[(int) type];
return parsers[(int) type - 1];
}

nmea_parser_module_s *
Expand All @@ -166,7 +169,7 @@ nmea_get_parser_by_sentence(const char *sentence)
int i = 0;
nmea_parser_module_s *parser;

while (i < NMEA_NUM_PARSERS) {
while (i < n_parsers) {
parser = parsers[i++];
if (NULL == parser) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/nmea/parser.h
Expand Up @@ -28,8 +28,8 @@ typedef struct {

typedef int (*init_f) (nmea_parser_s *);

#define NMEA_NUM_PARSERS 12
nmea_parser_module_s *parsers[NMEA_NUM_PARSERS];
nmea_parser_module_s **parsers;
int n_parsers;

/**
* Load the parser libs into array.
Expand Down

0 comments on commit c0dd2bb

Please sign in to comment.