rev 195 - in trunk: modules/File pr

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: bcollins
Date: 2004-03-29 11:28:38 -0500 (Mon, 29 Mar 2004)
New Revision: 195

Added:
   trunk/pr/stdio_test.pr
Modified:
   trunk/modules/File/File.c
   trunk/pr/test.pr
Log:
Add stdin/stdout/stderr default File objects. These can be rerouted.


Modified: trunk/modules/File/File.c
===================================================================
--- trunk/modules/File/File.c	2004-03-28 23:41:54 UTC (rev 194)
+++ trunk/modules/File/File.c	2004-03-29 16:28:38 UTC (rev 195)
@@ -64,27 +64,54 @@
 
 typedef struct {
 	apr_file_t	*stream;	/* APR file reference */
-	int			bufsize;	/* Size of our buffer */
+	int		bufsize;	/* Size of our buffer */
 	apr_pool_t	*pool;		/* Pool associated with this file */
-} file_t;
+	apr_int32_t	flags;		/* APR flags */
+} pr_file_t;
 
-typedef file_t* file_p;
+typedef pr_file_t* pr_file_p;
 
-#define STREAM(obj)  (((file_p)((obj)->data.ptr))->stream)
-#define BUFSIZE(obj) (((file_p)((obj)->data.ptr))->bufsize)
-#define POOL(obj)    (((file_p)((obj)->data.ptr))->pool)
+#define STREAM(obj)  (((pr_file_p)((obj)->data.ptr))->stream)
+#define BUFSIZE(obj) (((pr_file_p)((obj)->data.ptr))->bufsize)
+#define POOL(obj)    (((pr_file_p)((obj)->data.ptr))->pool)
+#define FLAGS(obj)   (((pr_file_p)((obj)->data.ptr))->flags)
 #define OPEN(obj)    (((obj) != FILE_PROTO) && STREAM(obj))
 
-obj_p FILE_PROTO, DIR_PROTO;
+static obj_p FILE_PROTO, DIR_PROTO;
 
+static obj_p new_file_object(obj_p filename, obj_p mode,
+			     apr_file_t *fp, int bufsize,
+			     apr_int32_t flags)
+{
+	obj_p file_obj = new_object(FILE_PROTO);
+	apr_pool_t *subpool = apr_file_pool_get(fp);
+	pr_file_p filep;
+
+	file_obj = new_object(FILE_PROTO);
+	set_attr(ist, file_obj, sym(ist, "name"), filename);
+	set_attr(ist, file_obj, sym(ist, "mode"), mode);
+
+	filep = pr_malloc(sizeof(*filep));
+
+	if (bufsize < MIN_BUFSIZE)
+		bufsize = MIN_BUFSIZE;
+
+	filep->bufsize = bufsize;
+	filep->stream  = fp;
+	filep->pool = subpool;
+	filep->flags = flags;
+	file_obj->data_type = OBJ_TYPE_DATAPTR;
+	file_obj->data.ptr  = filep;
+
+	return file_obj;
+}
+
 DEF( FILE_PROTO, __init__, list6( sym(ist, "path"),    NULL, 
                                   sym(ist, "mode"),    new_string_obj("r"),
                                   sym(ist, "bufsize"), new_int_obj(-1) ) ) {
-	obj_p file_obj;
-	file_p filep;
 	apr_file_t *f;
+	apr_status_t aprerr;
 	apr_pool_t *subpool;
-	apr_status_t aprerr;
 	apr_int32_t flags;
 	char *file, *mode;
 	char err_buf[1024];
@@ -140,17 +167,7 @@
 		return NULL;
 	}
 
-	file_obj = new_object(FILE_PROTO);
-	set_attr(ist, file_obj, sym(ist, "name"), parms[1]);
-	set_attr(ist, file_obj, sym(ist, "mode"), parms[3]);
-	filep = pr_malloc(sizeof(file_t));
-	filep->bufsize = (int)(parms[5]->data.i64);
-	if (filep->bufsize < MIN_BUFSIZE) filep->bufsize = MIN_BUFSIZE;
-	filep->stream  = f;
-	filep->pool = subpool;
-	file_obj->data_type = OBJ_TYPE_DATAPTR;
-	file_obj->data.ptr  = filep;
-	return file_obj;
+	return new_file_object(parms[1], parms[3], f, (int)(parms[5]->data.i64, flags));
 }
 
 DEF(FILE_PROTO, __objlist__, FORM_RPARAM) {
@@ -218,6 +235,10 @@
 		raise_exception(ist, OBJ(IOEXCEPTION), "read called on closed file");
 		return NULL;
 	}
+	if (!(FLAGS(self) & APR_READ)) {
+		raise_exception(ist, OBJ(IOEXCEPTION), "read called on file opened without read perms");
+		return NULL;
+	}
 	size_in = parms[1]->data.i64;
 	if (!size_in) return new_string_obj("");
 	if (size_in < 0) {
@@ -290,6 +311,10 @@
 		raise_exception(ist, OBJ(IOEXCEPTION), "readline called on closed file");
 		return NULL;
 	}
+	if (!(FLAGS(self) & APR_READ)) {
+		raise_exception(ist, OBJ(IOEXCEPTION), "readline called on file opened without read perms");
+		return NULL;
+	}
 	size_in = parms[1]->data.i64;
 	if (!size_in) return new_string_obj("");
 	if (size_in < 0) {
@@ -360,6 +385,10 @@
 		raise_exception(ist, OBJ(IOEXCEPTION), "readlines called on closed file");
 		return NULL;
 	}
+        if (!(FLAGS(self) & APR_READ)) {
+                raise_exception(ist, OBJ(IOEXCEPTION), "readlines called on file opened without read perms");
+                return NULL;
+        }
 	size_in = parms[1]->data.i64;
 	if (!size_in) return new_string_obj("");
 	if (size_in < 0) {
@@ -511,7 +540,10 @@
 		raise_exception(ist, OBJ(IOEXCEPTION), "write called on closed file");
 		return NULL;
 	}
-
+	if (!(FLAGS(self) & APR_WRITE)) {
+		raise_exception(ist, OBJ(IOEXCEPTION), "write called on file opened without write perms");
+		return NULL;
+	}
 	str  = strch(parms[1]);
 	size = pr_strlen(parms[1]);
 
@@ -538,7 +570,10 @@
 		raise_exception(ist, OBJ(IOEXCEPTION), "write called on closed file");
 		return NULL;
 	}
-
+	if (!(FLAGS(self) & APR_WRITE)) {
+		raise_exception(ist, OBJ(IOEXCEPTION), "writelines called on file opened without write perms");
+		return NULL;
+	}
 	if (has_proto(ist, parms[1], OBJ(STRING_PROTO))) {
 		str  = strch(parms[1]);
 		size = pr_strlen(parms[1]);
@@ -579,12 +614,60 @@
 	return parms[1];
 }
 
+#define __STDIN		0
+#define __STDOUT	1
+#define __STDERR	2
+
+static obj_p __open_std_file(int type, apr_pool_t *subpool)
+{
+	obj_p mode, file;
+	apr_file_t *fp;
+	apr_status_t aprerr;
+	apr_int32_t flags;
+
+	switch (type) {
+		case __STDIN:
+			aprerr = apr_file_open_stdin(&fp, subpool);
+			mode = new_string_obj("r");
+			file = new_string_obj("<stdin>");
+			flags = APR_READ;
+			break;
+
+		case __STDOUT:
+			aprerr = apr_file_open_stdout(&fp, subpool);
+			file = new_string_obj("<stdout>");
+			mode = new_string_obj("w");
+			flags = APR_WRITE;
+			break;
+
+		case __STDERR:
+			aprerr = apr_file_open_stderr(&fp, subpool);
+			file = new_string_obj("<stderr>");
+			mode = new_string_obj("w");
+			flags = APR_WRITE;
+			break;
+
+		default:
+			return NULL; /* XXX something stupid, do an error */
+	}
+
+	if (aprerr != APR_SUCCESS)
+		return NULL;
+
+	return new_file_object(file, mode, fp, MIN_BUFSIZE, flags);
+}
+
 install_module(File) {
+	apr_pool_t *std_pool;
+	apr_status_t aprerr;
+	obj_p file_obj;
+
 	FILE_PROTO = new_object(NULL);
 	DIR_PROTO  = new_object(NULL);
+
 	set_attr(ist, OBJ(OBJECT),	sym(ist, "File"), FILE_PROTO);
 	set_attr(ist, OBJ(OBJECT),	sym(ist, "Dir"),  DIR_PROTO);
-	
+
 	FILE_PROTO__init__init();
 	FILE_PROTO__objlist__init();
 	FILE_PROTOcloseinit();
@@ -602,5 +685,17 @@
 	FILE_PROTOwritelinesinit();
 	DIR_PROTO__init__init();
 	DIR_PROTO__objlist__init();
+
+	/* Create base file objects for std{in,out,err} */
+	aprerr = apr_pool_create(&std_pool, get_pr_head_pool());
+	/* Need some kind of error return! */
+	if (aprerr != APR_SUCCESS)
+		return;
+
+	if ((file_obj = __open_std_file(__STDIN, std_pool)))
+		set_attr(ist, FILE_PROTO, sym(ist, "stdin"), file_obj);
+	if ((file_obj = __open_std_file(__STDOUT, std_pool)))
+		set_attr(ist, FILE_PROTO, sym(ist, "stdout"), file_obj);
+	if ((file_obj = __open_std_file(__STDERR, std_pool)))
+		set_attr(ist, FILE_PROTO, sym(ist, "stderr"), file_obj);
 }
-

Added: trunk/pr/stdio_test.pr
===================================================================
--- trunk/pr/stdio_test.pr	2004-03-28 23:41:54 UTC (rev 194)
+++ trunk/pr/stdio_test.pr	2004-03-29 16:28:38 UTC (rev 195)
@@ -0,0 +1,15 @@
+#!/usr/local/bin/prothon
+
+import File, String
+
+stdout = File.stdout
+stdin = File.stdin
+stderr = File.stderr
+
+stdout.write("Enter your name: ")
+
+name = stdin.readline()
+
+stdout.write("Hello " + name)
+
+stderr.write("This is written on stderr\n")


Property changes on: trunk/pr/stdio_test.pr
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native

Modified: trunk/pr/test.pr
===================================================================
--- trunk/pr/test.pr	2004-03-28 23:41:54 UTC (rev 194)
+++ trunk/pr/test.pr	2004-03-29 16:28:38 UTC (rev 195)
@@ -3,19 +3,3 @@
 print Sys.version
 
 print Sys.platform
-
-/*
-f = File("test.txt", "w")
-
-f.write("abc")
-
-f.close()
-
-f = File("test.txt", "a")
-
-f.write("def")
-
-f.close()
-f = File("test.txt", "r")
-
-*/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.