Changed fopen r mode to rb
This commit is contained in:
parent
e80b4b6990
commit
e7268771e6
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
L=link
|
||||
OUTPUT=so-cpp.exe
|
||||
OUTPUT=so-cpp
|
||||
SOURCES=expand_line.c hashmap.c line_handler.c pp.c pp_args.c pp_define.c pp_if.c pp_ifdef.c pp_inc.c so-cpp.c str_dyn_arr.c utils.c
|
||||
LIBRARY=none
|
||||
INCPATHS=include
|
||||
|
|
|
@ -103,7 +103,11 @@ char *_compute_expanded_line(void *def_map, char *line)
|
|||
token = strtok(_line, DELIMS);
|
||||
|
||||
if (_is_delim_at_str_start(token, line_start)) {
|
||||
ret = _concat_delim_to_line(line_start, token - line_start, &exp_line);
|
||||
ret = _concat_delim_to_line(
|
||||
line_start,
|
||||
token - line_start,
|
||||
&exp_line
|
||||
);
|
||||
if (ret == PP_FAILED)
|
||||
goto free_and_exit;
|
||||
}
|
||||
|
@ -117,7 +121,11 @@ char *_compute_expanded_line(void *def_map, char *line)
|
|||
token = strtok(NULL, DELIMS);
|
||||
delim_end = _compute_delim_end(token, _line, line_copy);
|
||||
|
||||
ret = _concat_delim_to_line(line_copy + delim_start, delim_end - delim_start, &exp_line);
|
||||
ret = _concat_delim_to_line(
|
||||
line_copy + delim_start,
|
||||
delim_end - delim_start,
|
||||
&exp_line
|
||||
);
|
||||
if (ret == PP_FAILED)
|
||||
goto free_and_exit;
|
||||
|
||||
|
@ -126,7 +134,11 @@ char *_compute_expanded_line(void *def_map, char *line)
|
|||
if (token[token_len - 1] == '\n')
|
||||
token[token_len - 1] = 0x00;
|
||||
|
||||
_concat_expanded_token_to_line(def_map, token, &exp_line);
|
||||
_concat_expanded_token_to_line(
|
||||
def_map,
|
||||
token,
|
||||
&exp_line
|
||||
);
|
||||
|
||||
if (token[token_len - 1] == 0x00) {
|
||||
token[token_len - 1] = '\n';
|
||||
|
@ -188,7 +200,7 @@ free_and_exit:
|
|||
int _concat_delim_to_line(char *delim, int delim_size, char **line)
|
||||
{
|
||||
char *_delim;
|
||||
char ret;
|
||||
int ret;
|
||||
|
||||
_delim = NULL;
|
||||
|
||||
|
|
|
@ -67,7 +67,8 @@ int _get_next_avail_pos(struct hashmap_map *m, char *key)
|
|||
if (!m->data[pos].in_use)
|
||||
return pos;
|
||||
|
||||
if (!strncmp(m->data[pos].key, key, strlen(key)) && m->data[pos].in_use)
|
||||
if (!strncmp(m->data[pos].key, key, strlen(key))
|
||||
&& m->data[pos].in_use)
|
||||
return MAP_ALREADY_USED;
|
||||
|
||||
pos = (pos + 1) % m->max_size;
|
||||
|
@ -138,7 +139,8 @@ int hashmap_put(void *map, char *key, char *value)
|
|||
|
||||
int _is_desired_pos(struct hashmap_map *m, char *key, int pos)
|
||||
{
|
||||
return m->data[pos].key && key && !strcmp(m->data[pos].key, key) && m->data[pos].in_use;
|
||||
return m->data[pos].key && key
|
||||
&& !strcmp(m->data[pos].key, key) && m->data[pos].in_use;
|
||||
}
|
||||
|
||||
int hashmap_get(void *map, char *key, char **ret_val)
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include "hashmap.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int write_expanded_line(void *def_map, char *line, FILE*);
|
||||
extern int write_expanded_line(void *def_map, char *line, FILE *source);
|
||||
extern char *get_expanded_line(void *def_map, char *line);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,6 +6,12 @@
|
|||
#include "str_dyn_arr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int line_handler(char *line, void*, const struct str_dyn_arr *inc_dirs, FILE*, FILE*);
|
||||
extern int line_handler(
|
||||
char *line,
|
||||
void *def_map,
|
||||
const struct str_dyn_arr *inc_dirs,
|
||||
FILE*,
|
||||
FILE*
|
||||
);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include "str_dyn_arr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int pp_compute(void*, const struct str_dyn_arr *inc_dirs, FILE*, FILE*);
|
||||
extern int pp_compute(void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
FILE*in, FILE*);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include "hashmap.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int collect_def(char *line, void *def_map, FILE*);
|
||||
extern int collect_def(char *line, void *def_map, FILE *in);
|
||||
extern int apply_undef(char *line, void *def_map);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "str_dyn_arr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int expand_if(void*, const struct str_dyn_arr *inc_dirs, FILE*, FILE*);
|
||||
extern int expand_if(void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
FILE*, FILE*);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "str_dyn_arr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int expand_ifdef(void*, const struct str_dyn_arr *inc_dirs, FILE*, FILE*);
|
||||
extern int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
FILE*, FILE*);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "str_dyn_arr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern int expand_inc(void*, const struct str_dyn_arr *inc_dirs, FILE*, FILE*);
|
||||
extern int expand_inc(void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
FILE*, FILE*);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -17,7 +17,8 @@
|
|||
|
||||
#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 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)
|
||||
|
@ -25,20 +26,24 @@
|
|||
|
||||
#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 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 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 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))
|
||||
|
||||
#define IS_IF_CURR_BRANCH_END(line) (IS_ELSE_LINE(line) || IS_ELIF_LINE(line) || IS_ENDIF_LINE(line))
|
||||
#define IS_IF_CURR_BRANCH_END(line) \
|
||||
(IS_ELSE_LINE(line) || IS_ELIF_LINE(line) || IS_ENDIF_LINE(line))
|
||||
|
||||
#endif
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
extern int write_line_to_file(FILE*, char *line);
|
||||
extern int write_line_to_file(FILE *out, char *line);
|
||||
extern char *l_strdup(char *str);
|
||||
|
||||
extern void strip_spaces_in_def(char *def);
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
#include "pp_ifdef.h"
|
||||
#include "pp_inc.h"
|
||||
|
||||
int line_handler(char *line, void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
int line_handler(char *line, void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
FILE *source, FILE *out)
|
||||
{
|
||||
int ret;
|
||||
int line_len;
|
||||
|
|
3
pp.c
3
pp.c
|
@ -4,7 +4,8 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
int pp_compute(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
int pp_compute(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source,
|
||||
FILE *out)
|
||||
{
|
||||
char line[PP_LINE_LEN + 1];
|
||||
int read_lines;
|
||||
|
|
16
pp_args.c
16
pp_args.c
|
@ -234,7 +234,11 @@ struct args *pp_parse_args(int argc, char **argv, void *def_map)
|
|||
|
||||
if (_is_split_define_flag(argv[i])) {
|
||||
if (i + 1 < argc) {
|
||||
res = _add_sym_mapp_to_map(def_map, argv[i + 1]);
|
||||
res = _add_sym_mapp_to_map(
|
||||
def_map,
|
||||
argv[i + 1]
|
||||
);
|
||||
|
||||
if (res == PP_FAILED)
|
||||
goto free_and_exit;
|
||||
is_flag = 1;
|
||||
|
@ -282,13 +286,18 @@ struct args *pp_parse_args(int argc, char **argv, void *def_map)
|
|||
continue;
|
||||
}
|
||||
|
||||
if (!is_flag && !in_file_found && i - 1 >= 0 && _is_in_out_file_arg(argv[i - 1])) {
|
||||
if (!is_flag && !in_file_found && i - 1 >= 0
|
||||
&& _is_in_out_file_arg(argv[i - 1])) {
|
||||
|
||||
res = _add_in_file_to_args(args, argv[i]);
|
||||
if (res == PP_FAILED)
|
||||
goto free_and_exit;
|
||||
|
||||
in_file_found = 1;
|
||||
} else if (!is_flag && in_file_found && i - 1 >= 0 && _is_in_out_file_arg(argv[i - 1])) {
|
||||
|
||||
} else if (!is_flag && in_file_found && i - 1 >= 0
|
||||
&& _is_in_out_file_arg(argv[i - 1])) {
|
||||
|
||||
if (out_file_found)
|
||||
goto free_and_exit;
|
||||
|
||||
|
@ -296,6 +305,7 @@ struct args *pp_parse_args(int argc, char **argv, void *def_map)
|
|||
if (res == PP_FAILED)
|
||||
goto free_and_exit;
|
||||
out_file_found = 1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
24
pp_if.c
24
pp_if.c
|
@ -5,7 +5,8 @@
|
|||
#include "expand_line.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int expand_if(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
int expand_if(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source,
|
||||
FILE *out)
|
||||
{
|
||||
char line[PP_LINE_LEN + 1];
|
||||
char *exp_line;
|
||||
|
@ -30,7 +31,14 @@ int expand_if(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, F
|
|||
}
|
||||
|
||||
if (if_en || else_en) {
|
||||
ret = line_handler(line, def_map, inc_dirs, source, out);
|
||||
ret = line_handler(
|
||||
line,
|
||||
def_map,
|
||||
inc_dirs,
|
||||
source,
|
||||
out
|
||||
);
|
||||
|
||||
if (ret == PP_FAILED)
|
||||
return ret;
|
||||
}
|
||||
|
@ -42,7 +50,11 @@ int expand_if(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, F
|
|||
return PP_FAILED;
|
||||
|
||||
if (is_cond_null(exp_line)) {
|
||||
ret = skip_until(source, SKIP_UNTIL_CURR_BRANCH_END);
|
||||
ret = skip_until(
|
||||
source,
|
||||
SKIP_UNTIL_CURR_BRANCH_END
|
||||
);
|
||||
|
||||
if (ret == -1)
|
||||
return PP_FAILED;
|
||||
|
||||
|
@ -51,7 +63,11 @@ int expand_if(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, F
|
|||
else_en = 1;
|
||||
} else {
|
||||
line_len = strlen(line);
|
||||
ret = fseek(source, -line_len, SEEK_CUR);
|
||||
ret = fseek(
|
||||
source,
|
||||
-line_len,
|
||||
SEEK_CUR
|
||||
);
|
||||
|
||||
if (ret == -1)
|
||||
return PP_FAILED;
|
||||
|
|
36
pp_ifdef.c
36
pp_ifdef.c
|
@ -8,7 +8,8 @@
|
|||
|
||||
int _is_sym_defined(void *def_map, char *ifdef_line);
|
||||
|
||||
int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
FILE *source, FILE *out)
|
||||
{
|
||||
char line[PP_LINE_LEN + 1];
|
||||
char *exp_line;
|
||||
|
@ -35,7 +36,14 @@ int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source
|
|||
}
|
||||
|
||||
if (if_en || else_en) {
|
||||
ret = line_handler(line, def_map, inc_dirs, source, out);
|
||||
ret = line_handler(
|
||||
line,
|
||||
def_map,
|
||||
inc_dirs,
|
||||
source,
|
||||
out
|
||||
);
|
||||
|
||||
if (ret == PP_FAILED)
|
||||
return ret;
|
||||
|
||||
|
@ -48,7 +56,11 @@ int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source
|
|||
return PP_FAILED;
|
||||
|
||||
if (!_is_sym_defined(def_map, line)) {
|
||||
ret = skip_until(source, SKIP_UNTIL_CURR_BRANCH_END);
|
||||
ret = skip_until(
|
||||
source,
|
||||
SKIP_UNTIL_CURR_BRANCH_END
|
||||
);
|
||||
|
||||
if (ret == -1)
|
||||
return PP_FAILED;
|
||||
|
||||
|
@ -57,7 +69,11 @@ int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source
|
|||
else_en = 1;
|
||||
} else {
|
||||
line_len = strlen(line);
|
||||
ret = fseek(source, -line_len, SEEK_CUR);
|
||||
ret = fseek(
|
||||
source,
|
||||
-line_len,
|
||||
SEEK_CUR
|
||||
);
|
||||
|
||||
if (ret == -1)
|
||||
return PP_FAILED;
|
||||
|
@ -78,7 +94,11 @@ int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source
|
|||
return PP_FAILED;
|
||||
|
||||
if (_is_sym_defined(def_map, line)) {
|
||||
ret = skip_until(source, SKIP_UNTIL_CURR_BRANCH_END);
|
||||
ret = skip_until(
|
||||
source,
|
||||
SKIP_UNTIL_CURR_BRANCH_END
|
||||
);
|
||||
|
||||
if (ret == -1)
|
||||
return PP_FAILED;
|
||||
|
||||
|
@ -88,7 +108,11 @@ int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source
|
|||
else_en = 1;
|
||||
} else {
|
||||
line_len = strlen(line);
|
||||
ret = fseek(source, -line_len, SEEK_CUR);
|
||||
ret = fseek(
|
||||
source,
|
||||
-line_len,
|
||||
SEEK_CUR
|
||||
);
|
||||
|
||||
if (ret == -1)
|
||||
return PP_FAILED;
|
||||
|
|
18
pp_inc.c
18
pp_inc.c
|
@ -6,9 +6,11 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
char *_get_filename_from_inc(char *inc);
|
||||
int _compute_inc_file(void *def_map, const struct str_dyn_arr *inc_dirs, char *inc_filename, FILE *out);
|
||||
int _compute_inc_file(void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
char *inc_filename, FILE *out);
|
||||
|
||||
int expand_inc(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
||||
int expand_inc(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source,
|
||||
FILE *out)
|
||||
{
|
||||
char line[PP_LINE_LEN + 1];
|
||||
char *fgets_ret;
|
||||
|
@ -44,7 +46,12 @@ int expand_inc(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source,
|
|||
a_strcat(&full_inc_filename, "/");
|
||||
a_strcat(&full_inc_filename, inc_filename);
|
||||
|
||||
ret = _compute_inc_file(def_map, inc_dirs, full_inc_filename, out);
|
||||
ret = _compute_inc_file(
|
||||
def_map,
|
||||
inc_dirs,
|
||||
full_inc_filename,
|
||||
out
|
||||
);
|
||||
free(full_inc_filename);
|
||||
|
||||
if (ret == PP_OK)
|
||||
|
@ -70,13 +77,14 @@ char *_get_filename_from_inc(char *inc)
|
|||
return filename;
|
||||
}
|
||||
|
||||
int _compute_inc_file(void *def_map, const struct str_dyn_arr *inc_dirs, char *inc_filename, FILE *out)
|
||||
int _compute_inc_file(void *def_map, const struct str_dyn_arr *inc_dirs,
|
||||
char *inc_filename, FILE *out)
|
||||
{
|
||||
FILE *inc_file;
|
||||
|
||||
int ret;
|
||||
|
||||
inc_file = fopen(inc_filename, "r");
|
||||
inc_file = fopen(inc_filename, "rb");
|
||||
|
||||
if (inc_file) {
|
||||
ret = pp_compute(def_map, inc_dirs, inc_file, out);
|
||||
|
|
2
so-cpp.c
2
so-cpp.c
|
@ -34,7 +34,7 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
if (args->in_file) {
|
||||
source = fopen(args->in_file, "r");
|
||||
source = fopen(args->in_file, "rb");
|
||||
if (!source) {
|
||||
ret = errno;
|
||||
goto free_and_exit;
|
||||
|
|
14
utils.c
14
utils.c
|
@ -48,10 +48,12 @@ void strip_spaces_in_def(char *def)
|
|||
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)
|
||||
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]))))
|
||||
if (!in_quotes && (!isspace(def[i]) || (i > 0
|
||||
&& !isspace(def[i - 1]))))
|
||||
def[x++] = def[i];
|
||||
else if (in_quotes)
|
||||
x++;
|
||||
|
@ -99,7 +101,10 @@ char *skip_word(char *s)
|
|||
char *_s;
|
||||
|
||||
_s = s;
|
||||
while (!isspace(*_s++));
|
||||
while (!isspace(*_s))
|
||||
_s++;
|
||||
|
||||
_s++;
|
||||
|
||||
return _s;
|
||||
}
|
||||
|
@ -130,7 +135,8 @@ int skip_until(FILE *f, int until)
|
|||
int res;
|
||||
|
||||
while (fgets(line, PP_LINE_LEN, f)) {
|
||||
if (until == SKIP_UNTIL_CURR_BRANCH_END && IS_IF_CURR_BRANCH_END(line))
|
||||
if (until == SKIP_UNTIL_CURR_BRANCH_END
|
||||
&& IS_IF_CURR_BRANCH_END(line))
|
||||
break;
|
||||
|
||||
if (until == SKIP_UNTIL_ENDIF_LINE && IS_ENDIF_LINE(line))
|
||||
|
|
Loading…
Reference in New Issue