From: Mike D. Lowis Date: Sun, 24 Feb 2013 17:05:20 +0000 (-0500) Subject: add file management module X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=a95f9300f0076b8f34ea961b7214d10736f69dce;p=proto%2Fsclpl.git add file management module --- diff --git a/premake4.lua b/premake4.lua index 720f70d..34289b2 100644 --- a/premake4.lua +++ b/premake4.lua @@ -1,7 +1,7 @@ ------------------------------------------------------------------------------- -- SCLPL Distribution Build Configuration ------------------------------------------------------------------------------- -solution "SCLPL Distribtution" +solution "SCLPL Distribution" configurations { "Release" } targetdir "build" diff --git a/source/lexer/file.c b/source/lexer/file.c new file mode 100644 index 0000000..82f462c --- /dev/null +++ b/source/lexer/file.c @@ -0,0 +1,74 @@ +#include +#include "file.h" + +int Line = 1; +int Column = 1; +char* Name = NULL; +FILE* Handle = NULL; + +bool file_open(char* fname) +{ + Line = 0; + Column = 0; + Name = fname; + if (NULL == Name) + { + Handle = stdin; + } + else + { + printf("%s\n",fname); + Handle = fopen(fname,"r"); + } + return (NULL != Handle); +} + +void file_close(void) +{ + fclose(Handle); +} + +bool file_eof(void) +{ + bool ret = true; + if (NULL != Handle) + { + ret = feof( Handle ); + } + return ret; +} + +char file_get(void) +{ + char ret = EOF; + if (NULL != Handle) + { + ret = fgetc(Handle); + if ('\n' == ret) + { + Line++; + Column = 1; + } + else + { + Column++; + } + } + return ret; +} + +int file_line(void) +{ + return Line; +} + +int file_column(void) +{ + return Column; +} + +char* file_name(void) +{ + return (NULL != Name) ? Name : ""; +} + diff --git a/source/lexer/file.h b/source/lexer/file.h new file mode 100644 index 0000000..1e381e3 --- /dev/null +++ b/source/lexer/file.h @@ -0,0 +1,22 @@ +/** + @file file.h + @brief Describes a simple module for tracking input from a file including + line and column info. + $Revision$ + $HeadURL$ +*/ +#ifndef FILE_H +#define FILE_H + +#include +#include + +bool file_open(char* fname); +void file_close(void); +bool file_eof(void); +char file_get(void); +int file_line(void); +int file_column(void); +char* file_name(void); + +#endif /* FILE_H */ diff --git a/source/lexer/main.c b/source/lexer/main.c index 07b88a1..1b4b269 100644 --- a/source/lexer/main.c +++ b/source/lexer/main.c @@ -1,16 +1,56 @@ +#include #include "gc.h" +#include "file.h" + +int lex_files(int argc, char** argv); +int lex_input(FILE* outfile); int main(int argc, char** argv) { - /* init the collector */ - int foo; - gc_set_stack_base(&foo); - - /* main program */ + int ret; + if (argc > 1) + { + ret = lex_files(argc,argv); + } + else + { + file_open(NULL); + ret = lex_input(stdout); + file_close(); + } + return ret; +} +int lex_files(int argc, char** argv) +{ + int ret = 0; + int i; + for (i = 1; i < argc; i++) + { + if (file_open(argv[i])) + { + fprintf(stdout, "@file %s\n", file_name()); + ret = lex_input(stdout); + file_close(); + } + else + { + fprintf(stderr, "@error File not found: %s\n", argv[i]); + ret = 1; + break; + } + } + return ret; +} - /* shutdown the collector */ - gc_shutdown(); - return 0; +int lex_input(FILE* outfile) +{ + int ret = 0; + while (!file_eof()) + { + char ch = file_get(); + fprintf(stdout,"%s %d %d %c\n","char",file_line(),file_column(),ch); + } + return ret; }