]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
add file management module
authorMike D. Lowis <mike@mdlowis.com>
Sun, 24 Feb 2013 17:05:20 +0000 (12:05 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Sun, 24 Feb 2013 17:05:20 +0000 (12:05 -0500)
premake4.lua
source/lexer/file.c [new file with mode: 0644]
source/lexer/file.h [new file with mode: 0644]
source/lexer/main.c

index 720f70dcff127a80e6e0819b03a2119da1ab678f..34289b21c4f7f51c1dcfcfdae346492aada5d31c 100644 (file)
@@ -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 (file)
index 0000000..82f462c
--- /dev/null
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#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 : "<stdin>";
+}
+
diff --git a/source/lexer/file.h b/source/lexer/file.h
new file mode 100644 (file)
index 0000000..1e381e3
--- /dev/null
@@ -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 <stdint.h>
+#include <stdbool.h>
+
+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 */
index 07b88a145466db1300951c14d63cefe3357e3723..1b4b269f79299df1185355dab5aeb6ae5b5296be 100644 (file)
@@ -1,16 +1,56 @@
+#include <stdio.h>
 #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;
 }