rev 641 - in trunk: . pr/test src

SVN User <[email protected]> Sat, 19 Jun 2004 00:49:51 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-06-19 00:49:48 -0400 (Sat, 19 Jun 2004)
New Revision: 641

Modified:
   trunk/STATUS.txt
   trunk/pr/test/file.pr
   trunk/pr/test/test.pr
   trunk/src/builtins-file.c
Log:
updated file.pr, added TextFile generator

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-06-19 04:00:17 UTC (rev 640)
+++ trunk/STATUS.txt	2004-06-19 04:49:48 UTC (rev 641)
@@ -1,17 +1,9 @@
 
 ----------------------- TO-DO (highest priority first) ------------------------
 
---- Complete File/Dir objects
-		http://www.prothon.org/pipermail/prothon-user/2004-April/000955.html
---- TempDir() should create new dir.  del_ should delete it.
---- TextFile, TempFile, and TempTextFile objects
-		http://www.prothon.org/pipermail/prothon-user/2004-June/001863.html
 --- support any object that supports file methods in stdxxx (duck std)
 		http://www.prothon.org/pipermail/prothon-user/2004-June/001769.html	
---- TextFileGen
 
---- range function
-
 --- += --> = a + b,  +! --> .append!()
 
 --- OSThread, Thread, and Gate
@@ -26,16 +18,16 @@
 
 --- evaluate default expressions on every function call
 
---- add vars_ flag to str.fmt() or implement taint bit and unTaint() method
+--- implement taint attribute on strings, unTaint() method, respect in .fmt, eval, exec
 
---- add \u0000 \U00000000 and \N{name} esc sequences
-
 --- unicode module - codecs
 		mask off top 3 bits
 		http://www.prothon.org/pipermail/prothon-user/2004-June/001913.html
 		http://www.prothon.org/pipermail/prothon-user/2004-June/001895.html
 		http://www.prothon.org/pipermail/prothon-user/2004-June/001918.html
 		http://www.unicode.org/versions/Unicode4.0.0/ch05.pdf
+--- Unicode file name support
+--- add \u0000 \U00000000 and \N{name} esc sequences
 
 --- add Object.data_ (builtins-databytes.c)
 

Modified: trunk/pr/test/file.pr
===================================================================
--- trunk/pr/test/file.pr	2004-06-19 04:00:17 UTC (rev 640)
+++ trunk/pr/test/file.pr	2004-06-19 04:49:48 UTC (rev 641)
@@ -1,6 +1,6 @@
 #!/usr/bin/env prothon
 
-f = File("test.txt", 'w+')
+f = TempTextFile()
 for i in 5:
 	f.write("line "+i+'\n')
 f.writeLines( ['line 5\n', 'line 6\n','line 7\n','line 8\n','line 9\n',])
@@ -9,7 +9,7 @@
 ref =  ['line 0\n','line 1\n','line 2\n','line 3\n','line 4\n',]
 ref += ['line 5\n','line 6\n','line 7\n','line 8\n','line 9\n',]
 
-f = File("test.txt")
+f.open('r')
 x = f.read()
 print x
 if (x != ''.join(ref)): 

Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr	2004-06-19 04:00:17 UTC (rev 640)
+++ trunk/pr/test/test.pr	2004-06-19 04:49:48 UTC (rev 641)
@@ -1,9 +1,44 @@
 #!/usr/bin/env prothon
 
+f = TempTextFile()
+for i in 5:
+	f.write("line "+i+'\n')
+f.writeLines( ['line 5\n', 'line 6\n','line 7\n','line 8\n','line 9\n',])
+f.close()
 
-f = TempFile()
-print f, f.open?(), f.exists?()
-f.close()
-print f, f.open?(), f.exists?()
-f.delete()
-print f, f.open?(), f.exists?()
+f.open()
+for line in f:
+	print line
+
+ref =  ['line 0\n','line 1\n','line 2\n','line 3\n','line 4\n',]
+ref += ['line 5\n','line 6\n','line 7\n','line 8\n','line 9\n',]
+
+f.open()
+x = f.read()
+print x
+if (x != ''.join(ref)): 
+	print "test failed: x != f"
+	sys.exit(1)
+
+f.seek(7)
+x = f.read(7)
+print x
+if (x != 'line 1\n'): 
+	print "test failed: x != 'line 1'"
+	sys.exit(1)
+
+f.seek(21)
+x = f.read(14)
+print x
+if (x != 'line 3\nline 4\n'): 
+	print "test failed: x != 'line 3line 4'"
+	sys.exit(1)
+
+x = f.readLines()
+ref = ['line 5\n','line 6\n','line 7\n','line 8\n','line 9\n',]
+print x
+if (''.join(x) != ''.join(ref)): 
+	print "test failed: x != 'line 5','line 6','line 7','line 8','line 9'"
+	sys.exit(1)
+
+print "\nall tests passed"

Modified: trunk/src/builtins-file.c
===================================================================
--- trunk/src/builtins-file.c	2004-06-19 04:00:17 UTC (rev 640)
+++ trunk/src/builtins-file.c	2004-06-19 04:49:48 UTC (rev 641)
@@ -84,6 +84,7 @@
 MODULE_DECLARE(TempDir);
 MODULE_DECLARE(TempFile);
 MODULE_DECLARE(TextFile);
+MODULE_DECLARE(TextFileGen);
 MODULE_DECLARE(TempTextFile);
 
 //*************************** set_file_obj_data *******************************
@@ -893,7 +894,7 @@
 	if (strlen(mode) == 0) {
 		read_unlock(ist, self);
 		SET_TYPE_IF_EXC(File_OBJ, self, DATA_TYPE_DATAPTR) return NULL;
-		set_file_obj_data(ist, self, parms[1], NULL, NULL, 0, 0);
+		set_file_obj_data(ist, self, NEW_STRING(path), NULL, NULL, 0, 0);
 		read_lock(ist, self);
 		self->unclonable = TRUE;
 		return OBJ(NONE);
@@ -1388,7 +1389,7 @@
 	else       del_attr(ist, self, sym(ist, "magic"));
 
 	if (strlen(mode) == 0) {
-		set_file_obj_data(ist, self, parms[1], NULL, NULL, 0, 0);
+		set_file_obj_data(ist, self, NEW_STRING(path), NULL, NULL, 0, 0);
 		read_lock(ist, self);
 		self->unclonable = TRUE;
 		return OBJ(NONE);
@@ -1422,7 +1423,7 @@
 		return NULL;
 	}
 	read_unlock(ist, self);
-	set_file_obj_data(ist, self, parms[1], parms[3], f, bufsize, flags);
+	set_file_obj_data(ist, self, NEW_STRING(path), NEW_STRING(mode), f, bufsize, flags);
 	read_lock(ist, self);
 	self->unclonable = TRUE;
 	return OBJ(NONE);
@@ -1735,7 +1736,7 @@
 	return parms[1];
 }
 
-//*************************** MODULE TempTextFile *************************************
+//*************************** MODULE TEMPTEXTFILE *************************************
 
 MODULE_START(TempTextFile)
 {
@@ -1797,7 +1798,46 @@
 	pr_free(path);
 	return NEW_STRING(buf);
 }
+//*************************** MODULE TEXTFILEGEN *************************************
 
+DEF(TextFile, iter_, NULL) {
+	obj_p new_obj = NEW_OBJ(TextFileGen_OBJ);
+	BIN_CONTENT_CHK(File);
+	if (!OPEN(self)) {
+		raise_exception(ist, OBJ(IO_EXC), "iter_ called on closed file");
+		return NULL;
+	}
+	SET_TYPE_IF_EXC(TextFileGen_OBJ, new_obj, DATA_TYPE_EXTPTR) return NULL;
+	new_obj->data.ptr = self;
+	return new_obj;
+}
+
+MODULE_START(TextFileGen) {
+	TextFileGen_OBJ = NEW_OBJ(OBJ(ITER_PROTO));
+	set_obj_doc(TextFileGen_OBJ, "TextFileGen object prototype");
+
+	MODULE_ADD_TO_BASE(TextFileGen);
+	TextFileGen_OBJ->unclonable = TRUE;
+}
+
+DEF(TextFileGen, next, NULL) {
+	obj_p res, file_obj = self->data.ptr;
+	if (!OPEN(file_obj)) {
+		raise_exception(ist, OBJ(STOP_ITERATION_EXC), NULL);
+		return NULL;
+	}
+	res = call_func0(ist, file_obj, sym(ist, "readLine"));
+	if_exc_return NULL;
+	if (pr_strlen(res) == 0) {
+		del_unlock(res);
+		call_func0(ist, file_obj, sym(ist, "close"));
+		raise_exception(ist, OBJ(STOP_ITERATION_EXC), NULL);
+		return NULL;
+	}
+	return res;
+}
+
+
 //*************************** MAIN_MODULE_INIT *************************************
 
 MAIN_MODULE_INIT(File) {
@@ -1873,11 +1913,15 @@
 	MODULE_ADD_SYM(TextFile, readLines);
 	MODULE_ADD_SYM(TextFile, write);
 	MODULE_ADD_SYM(TextFile, writeLines);
+	MODULE_ADD_SYM(TextFile, iter_);
 
 	MODULE_SUB_INIT(TempTextFile);
 	MODULE_ADD_SYM(TempTextFile, init_);
 	MODULE_ADD_SYM(TempTextFile, str_);
 
+	MODULE_SUB_INIT(TextFileGen);
+	MODULE_ADD_SYM(TextFileGen, next);
+
 	/* 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! */