98 lines
1.8 KiB
C
98 lines
1.8 KiB
C
#include "pp_inc.h"
|
|
#include "pp_line_id.h"
|
|
#include "utils.h"
|
|
#include "pp.h"
|
|
#include <string.h>
|
|
#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 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;
|
|
char *inc_filename;
|
|
char *full_inc_filename;
|
|
|
|
int i;
|
|
int ret;
|
|
|
|
fgets_ret = fgets(line, PP_LINE_LEN, source);
|
|
if (!fgets_ret)
|
|
return PP_FAILED;
|
|
|
|
inc_filename = _get_filename_from_inc(line);
|
|
|
|
full_inc_filename = l_strdup("_test/inputs/");
|
|
if (!full_inc_filename)
|
|
return PP_FAILED;
|
|
|
|
a_strcat(&full_inc_filename, inc_filename);
|
|
|
|
ret = _compute_inc_file(def_map, inc_dirs, full_inc_filename, out);
|
|
free(full_inc_filename);
|
|
|
|
if (ret == PP_OK)
|
|
return PP_OK;
|
|
|
|
for (i = 0; i < inc_dirs->used; i++) {
|
|
full_inc_filename = l_strdup(inc_dirs->data[i]);
|
|
if (!full_inc_filename)
|
|
return PP_FAILED;
|
|
|
|
a_strcat(&full_inc_filename, "/");
|
|
a_strcat(&full_inc_filename, inc_filename);
|
|
|
|
ret = _compute_inc_file(
|
|
def_map,
|
|
inc_dirs,
|
|
full_inc_filename,
|
|
out
|
|
);
|
|
free(full_inc_filename);
|
|
|
|
if (ret == PP_OK)
|
|
return PP_OK;
|
|
}
|
|
|
|
return PP_FAILED;
|
|
}
|
|
|
|
char *_get_filename_from_inc(char *inc)
|
|
{
|
|
char *filename;
|
|
int filename_len;
|
|
|
|
filename = inc;
|
|
|
|
filename += INC_LINE_START_SZ + 1;
|
|
filename += 1;
|
|
|
|
filename_len = strlen(filename);
|
|
filename[filename_len - 2] = 0x00;
|
|
|
|
return filename;
|
|
}
|
|
|
|
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, "rb");
|
|
|
|
if (inc_file) {
|
|
ret = pp_compute(def_map, inc_dirs, inc_file, out);
|
|
fclose(inc_file);
|
|
|
|
return ret == PP_OK ? PP_OK : PP_FAILED;
|
|
}
|
|
|
|
return PP_FAILED;
|
|
}
|