Added collect defines
This commit is contained in:
parent
3124f56f8c
commit
adde72c74d
|
@ -4,7 +4,7 @@ LIBRARY=none
|
|||
INCPATHS=include
|
||||
LIBPATHS=
|
||||
LDFLAGS=
|
||||
CCFLAGS=-c -Wall -g -Wextra -Werror -pedantic-errors
|
||||
CCFLAGS=-c -Wall -g -Wextra -Werror -pedantic-errors -std=c89
|
||||
CC=gcc
|
||||
CODING_STYLE_CHECKER=checkpatch_wrapper.sh
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// SPDX-License-Identifier: BSD-2
|
||||
/* SPDX-License-Identifier: BSD-2 */
|
||||
|
||||
#include <hashmap.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -127,7 +127,10 @@ int hashmap_put(string_map_t map, char *key, char *value)
|
|||
pos = _get_next_avail_pos(m, key);
|
||||
}
|
||||
|
||||
m->data[pos] = (struct hashmap_elem) {key, 1, value};
|
||||
m->data[pos].key = key;
|
||||
m->data[pos].in_use = 1;
|
||||
m->data[pos].val = value;
|
||||
|
||||
m->curr_size++;
|
||||
|
||||
return MAP_OK;
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
/* SPDX-License-Identifier: BSD-2 */
|
||||
|
||||
#ifndef _char_dyn_arr
|
||||
#define _char_dyn_arr
|
||||
|
||||
#define CHAR_DYN_ARR_OK 0
|
||||
#define CHAR_DYN_ARR_FAILED -1
|
||||
|
||||
struct char_dyn_arr {
|
||||
char **data;
|
||||
int used;
|
||||
int size;
|
||||
};
|
||||
|
||||
extern struct char_dyn_arr *init_char_dyn_arr(int size);
|
||||
extern int insert_char_dyn_arr(struct char_dyn_arr *a, char *e);
|
||||
extern void free_char_dyn_arr(struct char_dyn_arr *a);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef _line_handler_h
|
||||
#define _line_handler_h
|
||||
|
||||
#include "pp_defines.h"
|
||||
#include "hashmap.h"
|
||||
#include "str_dyn_arr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int line_handler(char *line, string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out);
|
||||
|
||||
#endif
|
|
@ -2,9 +2,9 @@
|
|||
#define _pp_h
|
||||
|
||||
#include "hashmap.h"
|
||||
#include "char_dyn_arr.h"
|
||||
#include "str_dyn_arr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int pp_compute(string_map_t def_map, struct char_dyn_arr *inc_dirs, FILE *source, FILE *out);
|
||||
extern int pp_compute(string_map_t def_map, struct str_dyn_arr *inc_dirs, FILE *source, FILE *out);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
#define _pp_args
|
||||
|
||||
#include "hashmap.h"
|
||||
#include "char_dyn_arr.h"
|
||||
#include "str_dyn_arr.h"
|
||||
|
||||
struct args {
|
||||
struct char_dyn_arr *inc_dirs;
|
||||
struct str_dyn_arr *inc_dirs;
|
||||
char *in_file;
|
||||
char *out_file;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef _pp_define
|
||||
#define _pp_define
|
||||
|
||||
#include "pp_defines.h"
|
||||
#include "str_dyn_arr.h"
|
||||
#include "hashmap.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int collect_def(char *line, string_map_t def_map, FILE *source);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
#ifndef _pp_line_id_h
|
||||
#define _pp_line_id_h
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define IF_LINE_START "#if "
|
||||
#define IF_LINE_START_SZ (sizeof(IF_LINE_START) - 1)
|
||||
#define IS_IF_LINE(line) (!strncmp(line, IF_LINE_START, IF_LINE_START_SZ))
|
||||
|
||||
#define ELSE_LINE_START "#else"
|
||||
#define ELSE_LINE_START_SZ (sizeof(ELSE_LINE_START) - 1)
|
||||
#define IS_ELSE_LINE(line) (!strncmp(line, ELSE_LINE_START, ELSE_LINE_START_SZ))
|
||||
|
||||
#define ELIF_LINE_START "#elif "
|
||||
#define ELIF_LINE_START_SZ (sizeof(ELIF_LINE_START) - 1)
|
||||
#define IS_ELIF_LINE(line) (!strncmp(line, ELIF_LINE_START, ELIF_LINE_START_SZ))
|
||||
|
||||
#define ENDIF_LINE_START "#endif"
|
||||
#define ENDIF_LINE_START_SZ (sizeof(ENDIF_LINE_START) - 1)
|
||||
#define IS_ENDIF_LINE(line) (!strncmp(line, ENDIF_LINE_START, ENDIF_LINE_START_SZ))
|
||||
|
||||
#define DEF_LINE_START "#define"
|
||||
#define DEF_LINE_START_SZ (sizeof(DEF_LINE_START) - 1)
|
||||
#define IS_DEF_LINE(line) (!strncmp(line, DEF_LINE_START, DEF_LINE_START_SZ))
|
||||
|
||||
#define UNDEF_LINE_START "#undef"
|
||||
#define UNDEF_LINE_START_SZ (sizeof(UNDEF_LINE_START) - 1)
|
||||
#define IS_UNDEF_LINE(line) (!strncmp(line, UNDEF_LINE_START, UNDEF_LINE_START_SZ))
|
||||
|
||||
#define IFDEF_LINE_START "#ifdef "
|
||||
#define IFDEF_LINE_START_SZ (sizeof(IFDEF_LINE_START) - 1)
|
||||
#define IS_IFDEF_LINE(line) (!strncmp(line, IFDEF_LINE_START, IFDEF_LINE_START_SZ))
|
||||
|
||||
#define IFNDEF_LINE_START "#ifndef "
|
||||
#define IFNDEF_LINE_START_SZ (sizeof(IFNDEF_LINE_START) - 1)
|
||||
#define IS_IFNDEF_LINE(line) (!strncmp(line, IFNDEF_LINE_START, IFNDEF_LINE_START_SZ))
|
||||
|
||||
#define INC_LINE_START "#include"
|
||||
#define INC_LINE_START_SZ (sizeof(INC_LINE_START) - 1)
|
||||
#define IS_INC_LINE(line) (!strncmp(line, INC_LINE_START, INC_LINE_START_SZ))
|
||||
|
||||
#endif
|
|
@ -0,0 +1,20 @@
|
|||
/* SPDX-License-Identifier: BSD-2 */
|
||||
|
||||
#ifndef _str_dyn_arr
|
||||
#define _str_dyn_arr
|
||||
|
||||
#define CHAR_DYN_ARR_OK 0
|
||||
#define CHAR_DYN_ARR_FAILED -1
|
||||
|
||||
struct str_dyn_arr {
|
||||
char **data;
|
||||
int used;
|
||||
int size;
|
||||
};
|
||||
|
||||
extern struct str_dyn_arr *init_str_dyn_arr(int size);
|
||||
extern int insert_str_dyn_arr(struct str_dyn_arr *a, char *e);
|
||||
extern void free_str_dyn_arr(struct str_dyn_arr *a);
|
||||
|
||||
#endif
|
||||
|
|
@ -3,7 +3,14 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
int write_line_to_file(FILE *f, char *line);
|
||||
extern int write_line_to_file(FILE *f, char *line);
|
||||
extern char *strdup(char *str);
|
||||
|
||||
extern void strip_spaces_in_def(char *def);
|
||||
extern int a_strcat(char **dest, char *src);
|
||||
|
||||
extern int get_next_whitespace_idx(char *s);
|
||||
extern char *skip_word(char *s);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
#include "line_handler.h"
|
||||
#include "pp_define.h"
|
||||
#include "pp_line_id.h"
|
||||
|
||||
int line_handler(char *line, string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
{
|
||||
int written;
|
||||
int line_len;
|
||||
|
||||
(void) inc_dirs;
|
||||
(void) out;
|
||||
|
||||
if (IS_DEF_LINE(line))
|
||||
return collect_def(line, def_map, source);
|
||||
|
||||
if (IS_UNDEF_LINE(line))
|
||||
return PP_OK;
|
||||
|
||||
if (IS_IF_LINE(line)) {
|
||||
return PP_OK;
|
||||
}
|
||||
|
||||
if (IS_IFDEF_LINE(line)) {
|
||||
return PP_OK;
|
||||
}
|
||||
|
||||
if (IS_IFNDEF_LINE(line)) {
|
||||
return PP_OK;
|
||||
}
|
||||
|
||||
if (IS_INC_LINE(line)) {
|
||||
return PP_OK;
|
||||
}
|
||||
|
||||
line_len = strlen(line);
|
||||
written = fprintf(out, "%s", line);
|
||||
|
||||
if (written < 0 || written != line_len)
|
||||
return PP_FAILED;
|
||||
|
||||
return PP_OK;
|
||||
}
|
17
pp.c
17
pp.c
|
@ -1,7 +1,22 @@
|
|||
#include "pp.h"
|
||||
#include "pp_defines.h"
|
||||
#include "line_handler.h"
|
||||
|
||||
int pp_compute(string_map_t def_map, struct char_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
#include <stdio.h>
|
||||
|
||||
int pp_compute(string_map_t def_map, struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
{
|
||||
char line[PP_LINE_LEN + 1];
|
||||
int ret;
|
||||
|
||||
while (fgets(line, PP_LINE_LEN, source)) {
|
||||
ret = line_handler(line, def_map, inc_dirs, source, out);
|
||||
if (ret == PP_FAILED)
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ferror(source))
|
||||
return PP_FAILED;
|
||||
|
||||
return PP_OK;
|
||||
}
|
||||
|
|
10
pp_args.c
10
pp_args.c
|
@ -1,4 +1,4 @@
|
|||
// SPDX-License-Identifier: BSD-2
|
||||
/* SPDX-License-Identifier: BSD-2 */
|
||||
|
||||
#include "pp_args.h"
|
||||
#include "pp_defines.h"
|
||||
|
@ -140,14 +140,14 @@ struct args *_create_struct_args(void)
|
|||
if (!ret)
|
||||
goto free_and_exit;
|
||||
|
||||
ret->inc_dirs = init_char_dyn_arr(2);
|
||||
ret->inc_dirs = init_str_dyn_arr(2);
|
||||
if (!(ret->inc_dirs))
|
||||
goto free_and_exit;
|
||||
|
||||
return ret;
|
||||
|
||||
free_and_exit:
|
||||
free_char_dyn_arr(ret->inc_dirs);
|
||||
free_str_dyn_arr(ret->inc_dirs);
|
||||
free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -156,7 +156,7 @@ int _collect_inc_dirs(struct args *args, char *inc_dir)
|
|||
{
|
||||
int res;
|
||||
|
||||
res = insert_char_dyn_arr(args->inc_dirs, inc_dir);
|
||||
res = insert_str_dyn_arr(args->inc_dirs, inc_dir);
|
||||
|
||||
if (res == CHAR_DYN_ARR_OK)
|
||||
return PP_OK;
|
||||
|
@ -304,7 +304,7 @@ void pp_free_args(struct args *args)
|
|||
if (!args)
|
||||
return;
|
||||
|
||||
free_char_dyn_arr(args->inc_dirs);
|
||||
free_str_dyn_arr(args->inc_dirs);
|
||||
free(args->in_file);
|
||||
free(args->out_file);
|
||||
free(args);
|
||||
|
|
|
@ -0,0 +1,140 @@
|
|||
#include "pp_define.h"
|
||||
#include "pp_defines.h"
|
||||
#include "line_handler.h"
|
||||
#include "utils.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
int _collect_sym_mapp(string_map_t def_map, char *def_line);
|
||||
char *_get_sym(char *def_line);
|
||||
char *_get_mapp(char *def_line);
|
||||
|
||||
int collect_def(char *line, string_map_t def_map, FILE *source)
|
||||
{
|
||||
char *def_multiline;
|
||||
char read_line[PP_LINE_LEN + 1];
|
||||
|
||||
int len;
|
||||
int ret;
|
||||
|
||||
def_multiline = NULL;
|
||||
len = strlen(line);
|
||||
|
||||
strip_spaces_in_def(line);
|
||||
|
||||
if (line[len - 2] != '\\') {
|
||||
ret = _collect_sym_mapp(def_map, line);
|
||||
if (ret == PP_FAILED) {
|
||||
ret = PP_FAILED;
|
||||
goto free_and_exit;
|
||||
}
|
||||
} else {
|
||||
def_multiline = strdup(line);
|
||||
if (!def_multiline)
|
||||
return PP_FAILED;
|
||||
|
||||
while (fgets(read_line, PP_LINE_LEN, source)) {
|
||||
strip_spaces_in_def(read_line);
|
||||
|
||||
len = strlen(def_multiline);
|
||||
def_multiline[len - 2] = 0x00;
|
||||
|
||||
ret = a_strcat(&def_multiline, line);
|
||||
if (ret == -1) {
|
||||
ret = PP_FAILED;
|
||||
goto free_and_exit;
|
||||
}
|
||||
|
||||
|
||||
len = strlen(read_line);
|
||||
if (line[len - 2] != '\\')
|
||||
break;
|
||||
}
|
||||
|
||||
ret = _collect_sym_mapp(def_map, def_multiline);
|
||||
if (ret == PP_FAILED) {
|
||||
ret = PP_FAILED;
|
||||
goto free_and_exit;
|
||||
}
|
||||
}
|
||||
|
||||
ret = PP_OK;
|
||||
|
||||
free_and_exit:
|
||||
free(def_multiline);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int _collect_sym_mapp(string_map_t def_map, char *def_line)
|
||||
{
|
||||
int ret;
|
||||
|
||||
char *sym;
|
||||
char *mapp;
|
||||
|
||||
sym = NULL;
|
||||
mapp = NULL;
|
||||
|
||||
sym = _get_sym(def_line);
|
||||
if (!sym)
|
||||
goto free_and_exit;
|
||||
|
||||
mapp = _get_mapp(def_line);
|
||||
if (!mapp)
|
||||
goto free_and_exit;
|
||||
|
||||
ret = hashmap_put(def_map, sym, mapp);
|
||||
|
||||
if (ret == MAP_OMEM)
|
||||
goto free_and_exit;
|
||||
|
||||
return PP_OK;
|
||||
|
||||
free_and_exit:
|
||||
free(sym);
|
||||
free(mapp);
|
||||
return PP_FAILED;
|
||||
}
|
||||
|
||||
char *_get_sym(char *def_line)
|
||||
{
|
||||
char *sym;
|
||||
int sym_end_index;
|
||||
|
||||
def_line = skip_word(def_line);
|
||||
if (!(*def_line))
|
||||
return NULL;
|
||||
|
||||
sym_end_index = get_next_whitespace_idx(def_line);
|
||||
|
||||
sym = calloc(sym_end_index + 1, sizeof(char));
|
||||
if (!sym)
|
||||
return NULL;
|
||||
|
||||
strncpy(sym, def_line, sym_end_index);
|
||||
return sym;
|
||||
}
|
||||
|
||||
char *_get_mapp(char *def_line)
|
||||
{
|
||||
char *mapp;
|
||||
int mapp_len;
|
||||
|
||||
def_line = skip_word(def_line);
|
||||
if (!(*def_line))
|
||||
return NULL;
|
||||
|
||||
def_line = skip_word(def_line);
|
||||
if (!(*def_line))
|
||||
return NULL;
|
||||
|
||||
mapp_len = strlen(def_line) - 1;
|
||||
|
||||
mapp = calloc(strlen(def_line) + 1, sizeof(char));
|
||||
if (!mapp)
|
||||
return NULL;
|
||||
|
||||
strncpy(mapp, def_line, mapp_len);
|
||||
return mapp;
|
||||
}
|
2
so-cpp.c
2
so-cpp.c
|
@ -1,4 +1,4 @@
|
|||
// SPDX-License-Identifier: BSD-2
|
||||
/* SPDX-License-Identifier: BSD-2 */
|
||||
|
||||
#include "hashmap.h"
|
||||
#include "pp.h"
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
// SPDX-License-Identifier: BSD-2
|
||||
/* SPDX-License-Identifier: BSD-2 */
|
||||
|
||||
#include "char_dyn_arr.h"
|
||||
#include "str_dyn_arr.h"
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct char_dyn_arr *init_char_dyn_arr(int size)
|
||||
struct str_dyn_arr *init_str_dyn_arr(int size)
|
||||
{
|
||||
struct char_dyn_arr *a;
|
||||
struct str_dyn_arr *a;
|
||||
|
||||
a = NULL;
|
||||
|
||||
a = calloc(1, sizeof(struct char_dyn_arr));
|
||||
a = calloc(1, sizeof(struct str_dyn_arr));
|
||||
if (!a)
|
||||
goto free_and_exit;
|
||||
|
||||
|
@ -30,7 +30,7 @@ free_and_exit:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int insert_char_dyn_arr(struct char_dyn_arr *a, char *e)
|
||||
int insert_str_dyn_arr(struct str_dyn_arr *a, char *e)
|
||||
{
|
||||
if (a->used == a->size) {
|
||||
a->size *= 2;
|
||||
|
@ -50,7 +50,7 @@ free_and_exit:
|
|||
return CHAR_DYN_ARR_FAILED;
|
||||
}
|
||||
|
||||
extern void free_char_dyn_arr(struct char_dyn_arr *a)
|
||||
extern void free_str_dyn_arr(struct str_dyn_arr *a)
|
||||
{
|
||||
int i;
|
||||
|
84
utils.c
84
utils.c
|
@ -1,6 +1,8 @@
|
|||
#include "utils.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
int write_line_to_file(FILE *f, char *line)
|
||||
{
|
||||
|
@ -16,3 +18,85 @@ int write_line_to_file(FILE *f, char *line)
|
|||
return 0;
|
||||
}
|
||||
|
||||
char *strdup(char *s)
|
||||
{
|
||||
int size;
|
||||
char *p;
|
||||
|
||||
size = strlen(s) + 1;
|
||||
p = calloc(size, sizeof(char));
|
||||
|
||||
if (p)
|
||||
memcpy(p, s, size);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void strip_spaces_in_def(char *def)
|
||||
{
|
||||
int i;
|
||||
int x;
|
||||
int in_quotes;
|
||||
|
||||
in_quotes = 0;
|
||||
|
||||
for (i = x = 0; def[i]; i++) {
|
||||
if (i > 0 && def[i] == '\"' && def[i - 1] != '\\' && !in_quotes)
|
||||
in_quotes = 1;
|
||||
else if (i > 0 && def[i] == '\"' && def[i - 1] != '\\' && in_quotes)
|
||||
in_quotes = 0;
|
||||
|
||||
if (!in_quotes && (!isspace(def[i]) || (i > 0 && !isspace(def[i - 1]))))
|
||||
def[x++] = def[i];
|
||||
else if (in_quotes)
|
||||
x++;
|
||||
}
|
||||
|
||||
def[x] = 0x00;
|
||||
}
|
||||
|
||||
int a_strcat(char **dest, char *src)
|
||||
{
|
||||
char *result;
|
||||
char *temp;
|
||||
int needed;
|
||||
|
||||
needed = strlen(*dest) + strlen(src) + 1;
|
||||
result = calloc(needed, sizeof(char));
|
||||
if (!result)
|
||||
return -1;
|
||||
|
||||
sprintf(result, "%s%s", *dest, src);
|
||||
|
||||
temp = *dest;
|
||||
*dest = result;
|
||||
|
||||
free(temp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_next_whitespace_idx(char *s)
|
||||
{
|
||||
int c;
|
||||
|
||||
c = 0;
|
||||
while (!isspace(*s)) {
|
||||
s++;
|
||||
c++;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
char *skip_word(char *s)
|
||||
{
|
||||
char *_s;
|
||||
|
||||
_s = s;
|
||||
while (!isspace(*_s++));
|
||||
|
||||
return _s;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue