summaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2014-12-26 12:02:49 -0500
committerAdrian Thurston <thurston@complang.org>2014-12-26 12:02:49 -0500
commitaa7f56241b5bc4ecc77cff606122ba51654e18a6 (patch)
tree2dbe50c4938abe7e49dab2382043a2aeb67aef5c /src/input.c
parent3954ee391198790f9d6ac8d7dc161f86f85cdfa5 (diff)
downloadcolm-aa7f56241b5bc4ecc77cff606122ba51654e18a6.tar.gz
more refcount fixes for global object, which is now a struct
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/input.c b/src/input.c
index 83eb1e89..daec3ca2 100644
--- a/src/input.c
+++ b/src/input.c
@@ -6,6 +6,9 @@
#include <colm/pdarun.h>
#include <colm/debug.h>
#include <colm/program.h>
+#include <colm/tree.h>
+#include <colm/bytecode.h>
+#include <colm/pool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -1001,3 +1004,51 @@ StreamImpl *newSourceStreamGeneric( const char *name )
return ss;
}
+
+Stream *openStreamFile( Program *prg, char *name, FILE *file )
+{
+ Stream *res = (Stream*)mapElAllocate( prg );
+ res->id = LEL_ID_STREAM;
+ res->in = newSourceStreamFile( name, file );
+ return res;
+}
+
+Stream *openStreamFd( Program *prg, char *name, long fd )
+{
+ Stream *res = (Stream*)mapElAllocate( prg );
+ res->id = LEL_ID_STREAM;
+ res->in = newSourceStreamFd( name, fd );
+ return res;
+}
+
+Stream *openFile( Program *prg, Tree *name, Tree *mode )
+{
+ Head *headName = ((Str*)name)->value;
+ Head *headMode = ((Str*)mode)->value;
+ Stream *stream = 0;
+
+ const char *givenMode = stringData(headMode);
+ const char *fopenMode = 0;
+ if ( memcmp( givenMode, "r", stringLength(headMode) ) == 0 )
+ fopenMode = "rb";
+ else if ( memcmp( givenMode, "w", stringLength(headMode) ) == 0 )
+ fopenMode = "wb";
+ else if ( memcmp( givenMode, "a", stringLength(headMode) ) == 0 )
+ fopenMode = "ab";
+ else {
+ fatal( "unknown file open mode: %s\n", givenMode );
+ }
+
+ /* Need to make a C-string (null terminated). */
+ char *fileName = (char*)malloc(stringLength(headName)+1);
+ memcpy( fileName, stringData(headName), stringLength(headName) );
+ fileName[stringLength(headName)] = 0;
+ FILE *file = fopen( fileName, fopenMode );
+ if ( file != 0 ) {
+ stream = openStreamFile( prg, fileName, file );
+ treeUpref( (Tree*)stream );
+ stream = (Stream*)constructPointer( prg, (Tree*)stream );
+ }
+
+ return stream;
+}