#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},
};
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;
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;
}
struct Source_Location_Delta* get_source_location_delta(struct Source_Location *begining,struct Source_Location *ending)
{
struct Source_Location_Delta *ret;
wonky_assert(begining->line <= ending->line);
wonky_assert(begining->starting_byte_index <= ending->starting_byte_index);
ret=wonky_malloc(sizeof(struct Source_Location_Delta));
ret->line_offset=ending->line - begining->line;
ret->column=ending->column;
ret->location=ending;
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