#ifndef WONKY_SOURCE_FILE_C
#define WONKY_SOURCE_FILE_C WONKY_SOURCE_FILE_C
#include <source_file.h>
static struct Source_Name start_of_file_name
=
{
.name="TODO startoffile",
.name_size=sizeof("TODO startoffile")
};
struct Source_Location start_of_file
=
{
.line=0,
.column=0,
.starting_byte_index=0,
.length=0,
.src=&(struct Source_File){.type=SOURCE_TEXT_PARTIAL_TEXT,.src_name=&start_of_file_name,.src="",.src_name=0},
};
const char stdargs_builtin[] =
"#ifndef __WONKY_INTERNAL_STDARGS_H\n"
"#define __WONKY_INTERNAL_STDARGS_H __WONKY_INTERNAL_STDARGS_H\n"
"\n"
"#define va_start(v,l) \n"
"#define va_end(v) \n"
"#define va_arg(v,l) \n"
"#define va_copy(d,s) \n"
"#define __builtin_va_list void *\n"
"typedef __builtin_va_list __gnuc_va_list;\n"
"\n"
"#endif\n";
const char stddef_builtin[] =
"#ifndef __WONKY_INTERNAL_STDDEF_H\n"
"#define __WONKY_INTERNAL_STDDEF_H __WONKY_INTERNAL_STDDEF_H\n"
"\n"
"typedef long long unsigned int ptrdiff_t; \n"
"typedef long long unsigned int size_t; \n"
"typedef int wchar_t; \n"
"#define NULL ((void*)0) \n"
"#define offsetof(type,member_designator) ((size_t)0) \n"
"#define __attribute__(x) \n"
"\n"
"#endif\n";
struct Source_File* get_source_file_from_string(char *filename,size_t filename_size,struct Program *program)
{
struct Source_File *ret;
FILE *file;
size_t selected_base_directory;
size_t file_size;
char *hold_constructed_filename;
ret=wonky_malloc(sizeof(struct Source_File));
ret->type=SOURCE_TEXT_FULL_TEXT;
ret->src="";
ret->src_size=0;
ret->src_name=NULL;
#warning TODO: refactor this when it get's out of control
if(gstrn_cmp(filename,"stdarg.h",filename_size))
{
ret->src=gstrncpy(stdargs_builtin,sizeof(stdargs_builtin));
ret->src_size=sizeof(stdargs_builtin)-1;
ret->src_name=get_source_name(filename);
return ret;
}
if(gstrn_cmp(filename,"stddef.h",filename_size))
{
ret->src=gstrncpy(stddef_builtin,sizeof(stddef_builtin));
ret->src_size=sizeof(stddef_builtin)-1;
ret->src_name=get_source_name(filename);
return ret;
}
for(selected_base_directory=0;selected_base_directory<wonky_default_include_paths_size;++selected_base_directory)
{
hold_constructed_filename=gstr_append(
wonky_default_include_paths[selected_base_directory],
filename
);
file=fopen(hold_constructed_filename,"r");
if(file!=NULL)
break;
else
wonky_free(hold_constructed_filename);
}
if(file==NULL)
{
file=fopen(filename,"r");
if(file==NULL)
{
push_generic_error(program,"Could not open file %s",filename);
wonky_free(ret);
return NULL;
}
}
fseek(file,0,SEEK_END);
file_size=ftell(file);
rewind(file);
ret->src=wonky_malloc(file_size);
ret->src_size=file_size;
ret->src_name=get_source_name(hold_constructed_filename);
fread(ret->src,file_size,sizeof(char),file);
fclose(file);
return ret;
}
struct Source_File* get_temp_source_file()
{
struct Source_File *ret;
ret=wonky_malloc(sizeof(struct Source_File));
ret->type=SOURCE_TEXT_FULL_TEXT;
ret->src_name=get_source_name("scratch pad");
ret->src=NULL;
ret->src_size=0;
return ret;
}
struct Source_Location* get_source_location(size_t line,size_t column,size_t on_which_byte,size_t length,struct Source_File *src)
{
struct Source_Location *ret;
ret=wonky_malloc(sizeof(struct Source_Location));
ret->line=line;
ret->column=column;
ret->starting_byte_index=on_which_byte;
ret->length=length;
ret->src=src;
return ret;
}
void source_file_expand(struct Source_File *src,size_t expand_byte_count)
{
if(src->src)
{
char *hold_new_src;
size_t i;
hold_new_src=wonky_malloc(src->src_size+expand_byte_count);
gmemmove(hold_new_src,src->src,src->src_size);
wonky_free(src->src);
for(i=0;i<expand_byte_count;++i)
src->src[src->src_size+i]='\0';
src->src_size+=expand_byte_count;
}else
{
src->src=wonky_calloc(expand_byte_count,1);
src->src_size=expand_byte_count;
}
}
void delete_source_file(struct Source_File *src)
{
wonky_free(src);
}
struct Source_Name* get_source_name(char *constructed_name)
{
struct Source_Name *ret;
ret=wonky_malloc(sizeof(struct Source_Name));
ret->name=constructed_name;
ret->name_size=gstrnlen(constructed_name,100);
return ret;
}
#endif