rev 621 - in trunk: . include/prothon pr/test src

SVN User <[email protected]> Sun, 13 Jun 2004 18:51:34 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-06-13 18:51:31 -0400 (Sun, 13 Jun 2004)
New Revision: 621

Modified:
   trunk/STATUS.txt
   trunk/include/prothon/prothon.h
   trunk/pr/test/test.pr
   trunk/src/builtins-core.c
   trunk/src/builtins-file.c
   trunk/src/sys.c
Log:
working on Dir methods

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-06-13 19:44:50 UTC (rev 620)
+++ trunk/STATUS.txt	2004-06-13 22:51:31 UTC (rev 621)
@@ -4,7 +4,7 @@
 --- check immutable bit for default param values
 --- remove all clr_immutable calls
 
---- add vars_ flag to str.fmt()
+--- add vars_ flag to str.fmt() or implement taint bit
 
 --- Complete File/Dir objects
 		http://www.prothon.org/pipermail/prothon-user/2004-April/000955.html

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-06-13 19:44:50 UTC (rev 620)
+++ trunk/include/prothon/prothon.h	2004-06-13 22:51:31 UTC (rev 621)
@@ -336,7 +336,7 @@
 	DIVIDEZERO_EXC,		//  
 	OVERFLOW_EXC,		//   OverflowError
 	OUTOFMEMORY_EXC,	//  
-	IOEXCEPTION,		//  
+	IO_EXC,		//  
 	FUNCNOTFOUND_EXC,	//  
 	FILENOTFOUND_EXC,	//  
 	STOP_ITERATION_EXC,	//  

Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr	2004-06-13 19:44:50 UTC (rev 620)
+++ trunk/pr/test/test.pr	2004-06-13 22:51:31 UTC (rev 621)
@@ -1,16 +1,5 @@
 #!/usr/bin/env prothon
 
-object Widget:
-    brand = "acme"
-    model = "model A1"
-    serno = 12345
-    def str_(spec = ""):
-        if 'd' in spec:  
-            # detailed spec
-            return brand+'/'+model+'/'+serno
-        return brand+'/'+model
-
-print "{0:d}".fmt(Widget)
-
-print "{0}".fmt(Widget)
-
+d = Dir('.')
+dd = d.dir()
+print d, dd
\ No newline at end of file

Modified: trunk/src/builtins-core.c
===================================================================
--- trunk/src/builtins-core.c	2004-06-13 19:44:50 UTC (rev 620)
+++ trunk/src/builtins-core.c	2004-06-13 22:51:31 UTC (rev 621)
@@ -495,8 +495,8 @@
 EXCEPTION_DECLARE(Exception_OBJ, DIVIDEZERO_EXC,      DivideByZero,      "Divide by zero Error");
 EXCEPTION_DECLARE(Exception_OBJ, OVERFLOW_EXC,        OverflowError,     "Overflow Error");
 EXCEPTION_DECLARE(Exception_OBJ, OUTOFMEMORY_EXC,     OutOfMemoryError,  "Out of memory Error");
-EXCEPTION_DECLARE(Exception_OBJ, IOEXCEPTION,         IOError,           "IO Error");
-EXCEPTION_DECLARE(OBJ(IOEXCEPTION), FILENOTFOUND_EXC, FileNotFound,      "File not found Error");
+EXCEPTION_DECLARE(Exception_OBJ, IO_EXC,         IOError,           "IO Error");
+EXCEPTION_DECLARE(OBJ(IO_EXC), FILENOTFOUND_EXC, FileNotFound,      "File not found Error");
 EXCEPTION_DECLARE(Exception_OBJ, STOP_ITERATION_EXC,  StopIteration,     "Stop iteration (not an error)");
 EXCEPTION_DECLARE(Exception_OBJ, LOCK_EXC,            LockError,         "Locking Error");
 EXCEPTION_DECLARE(Exception_OBJ, PERMISSION_EXC,      PermissionError,   "Permission Error");

Modified: trunk/src/builtins-file.c
===================================================================
--- trunk/src/builtins-file.c	2004-06-13 19:44:50 UTC (rev 620)
+++ trunk/src/builtins-file.c	2004-06-13 22:51:31 UTC (rev 621)
@@ -108,7 +108,6 @@
 	filep->stream  = fp;
 	filep->pool = subpool;
 	filep->flags = flags;
-	SET_TYPE_IF_EXC(File_OBJ, file_obj, DATA_TYPE_DATAPTR) return;
 	file_obj->data.ptr  = filep;
 	file_obj->rd_access = ACC_USER1;
 	file_obj->wr_access = ACC_USER1;
@@ -119,6 +118,7 @@
 						   int bufsize,  apr_int32_t flags ) {
 	obj_p file_obj;
 	file_obj = NEW_OBJ(File_OBJ);
+	SET_TYPE_IF_EXC(File_OBJ, file_obj, DATA_TYPE_DATAPTR) return NULL;
 	set_file_obj_data(ist, file_obj, filename, mode, fp, bufsize, flags);
 	return file_obj;
 }
@@ -127,6 +127,7 @@
 static obj_p new_dir_obj(isp ist, obj_p path) {
 	obj_p dir_obj;
 	dir_obj = NEW_OBJ(Dir_OBJ);
+	SET_TYPE_IF_EXC(Dir_OBJ, dir_obj, DATA_TYPE_DATAPTR) return NULL;
 	set_file_obj_data(ist, dir_obj, path, NULL, NULL, 0, APR_READ);
 	return dir_obj;
 }
@@ -143,13 +144,36 @@
 }
 
 DEF(Dir, init_, FPARM1(path, NEW_STRING("."))) {
+    apr_status_t aprerr; 
+    apr_finfo_t finfo;
+	pr_file_p filep;
+	char *path;
+	const char *newpath;
+
 	BIN_EMPTY_CHK();
-	CHECK_TYPE_EXC(parms[1], OBJ(STRING_PROTO), String);
+	STRING_PARAM(1, path);
 	read_unlock(ist, self);
+	SET_TYPE_IF_EXC(Dir_OBJ, self, DATA_TYPE_DATAPTR) {
+		read_lock(ist, self);  return NULL;
+	}
 	set_file_obj_data(ist, self, parms[1], NULL, NULL, 0, APR_READ);
-	if_rdlock(self) return NULL;
+	filep = self->data.ptr;
+	aprerr = apr_stat(&finfo, path, APR_FINFO_NAME|APR_FINFO_ICASE, filep->pool);
+	if (finfo.valid & (APR_FINFO_NAME|APR_FINFO_ICASE)) {
+		if (finfo.filetype != APR_DIR) {
+			raise_exception( ist, OBJ(IO_EXC), "invalid directory type (%d)", 
+				             finfo.filetype );
+			return NULL;
+		}
+		newpath = finfo.fname;
+		if (!newpath) newpath = finfo.name;
+		if (newpath)
+			set_attr(ist, self, sym(ist, "name"), NEW_STRING((char*)newpath));
+		set_attr(ist, self, sym(ist, "exists"), OBJ(PR_TRUE));
+	}
+	read_lock(ist, self);
 	return OBJ(NONE);
-}	
+}
 
 #define addch3(ch)  buf[buf_idx++].b0 = (u8_t)(ch)
 
@@ -157,7 +181,7 @@
 	char buf[300], *path;
 	BIN_STR_CHK(Dir);
 	path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "name")));
-	apr_snprintf(buf, sizeof(buf), "<Dir:%s>", path);
+	apr_snprintf(buf, sizeof(buf), "<Dir:'%s'>", path);
 	pr_free(path);
 	return NEW_STRING(buf);
 }
@@ -188,6 +212,11 @@
 	return new_dir_obj(ist, NEW_STRING(respath));
 }
 
+DEF(Dir, objList_, FORM_RPARAM) {
+	return parms[1];
+}
+
+
 //*************************** MODULE File *************************************
 
 MODULE_START(File)
@@ -243,7 +272,7 @@
 
 	aprerr = apr_file_open(&f, path, flags, APR_OS_DEFAULT, subpool);
 	if (aprerr != APR_SUCCESS) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "Unable to open path %s: %s",
+		raise_exception(ist, OBJ(IO_EXC), "Unable to open path %s: %s",
 				path, apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 		apr_pool_destroy(subpool);
 		return NULL;
@@ -252,6 +281,7 @@
 	/* set_file_obj_data(ist, ) modified the object, and thus calls write_lock.
 	 * We need to unlock in order to avoid a deadlock */
 	read_unlock(ist, self);
+	SET_TYPE_IF_EXC(File_OBJ, self, DATA_TYPE_DATAPTR) return NULL;
 	set_file_obj_data(ist, self, parms[1], parms[3], f, bufsize, flags);
 	read_lock(ist, self);
 	self->unclonable = TRUE;
@@ -289,11 +319,11 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "flush called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "flush called on closed file");
 		return NULL;
 	}
 	if (apr_file_flush(STREAM(self)) == APR_SUCCESS) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "flushing file");
+		raise_exception(ist, OBJ(IO_EXC), "flushing file");
 		return NULL;
 	}
 	return OBJ(NONE);
@@ -306,7 +336,7 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "fileNo called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "fileNo called on closed file");
 		return NULL;
 	}
 	return NEW_INT(_fileno(STREAM(self)));
@@ -315,7 +345,7 @@
 DEF(File, isATty_QUES, NULL) { 
 	BIN_CONTENT_CHK(File);
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "isatty called on closed stream");
+		raise_exception(ist, OBJ(IO_EXC), "isatty called on closed stream");
 		return NULL;
 	}
 	if (_isatty(_fileno(STREAM(self))))  return OBJ(PR_TRUE); 
@@ -340,11 +370,11 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "read called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "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");
+		raise_exception(ist, OBJ(IO_EXC), "read called on file opened without read perms");
 		return NULL;
 	}
 	if (!size_in) return NEW_BYTES(NULL, 0);
@@ -364,7 +394,7 @@
 	num_read = size;
 	aprerr = apr_file_read(STREAM(self), buf, &num_read);
 	if (aprerr != APR_SUCCESS && aprerr != APR_EOF) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+		raise_exception(ist, OBJ(IO_EXC), "%s",
 				apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 		return NULL;
 	}
@@ -382,7 +412,7 @@
 		num_read = next_read;
 		aprerr = apr_file_read(STREAM(self), buf + total_read, &num_read);
 		if (aprerr != APR_SUCCESS && aprerr != APR_EOF) {
-			raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+			raise_exception(ist, OBJ(IO_EXC), "%s",
 					apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 			return NULL;
 		}
@@ -418,11 +448,11 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "readLine called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "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");
+		raise_exception(ist, OBJ(IO_EXC), "readLine called on file opened without read perms");
 		return NULL;
 	}
 	if (!size_in) return NEW_BYTES(NULL, 0);
@@ -441,7 +471,7 @@
 	}
 	aprerr = apr_file_gets(buf, (int)(size + 1), STREAM(self));
 	if (aprerr != APR_SUCCESS && aprerr != APR_EOF) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+		raise_exception(ist, OBJ(IO_EXC), "%s",
 				apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 		return NULL;
 	}
@@ -458,7 +488,7 @@
 		}
 		aprerr = apr_file_gets(buf + total_read, (int)(next_read + 1), STREAM(self));
 		if (aprerr != APR_SUCCESS && aprerr != APR_EOF) {
-			raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+			raise_exception(ist, OBJ(IO_EXC), "%s",
 					apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 			return NULL;
 		}
@@ -494,11 +524,11 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "readLines called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "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");
+                raise_exception(ist, OBJ(IO_EXC), "readLines called on file opened without read perms");
                 return NULL;
         }
 	if (!size_in) return NEW_BYTES(NULL, 0);
@@ -520,7 +550,7 @@
 		}
 		aprerr = apr_file_gets(buf, (int)(size - total_read + 1), STREAM(self));
 		if (aprerr != APR_SUCCESS && aprerr != APR_EOF) {
-			raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+			raise_exception(ist, OBJ(IO_EXC), "%s",
 					apr_strerror(aprerr,
 					err_buf, sizeof(err_buf)));
 			return NULL;
@@ -539,7 +569,7 @@
 			}
 			aprerr = apr_file_gets(buf + num_read, (int)(next_read + 1), STREAM(self));
 			if (aprerr != APR_SUCCESS && aprerr != APR_EOF) {
-				raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+				raise_exception(ist, OBJ(IO_EXC), "%s",
 						apr_strerror(aprerr,
 						err_buf, sizeof(err_buf)));
 				return NULL;
@@ -577,7 +607,7 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "seek called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "seek called on closed file");
 		return NULL;
 	}
 	pos = (apr_off_t)pos_in;
@@ -592,7 +622,7 @@
 
 	aprerr = apr_file_seek(STREAM(self), origin, &pos);
 	if (aprerr != APR_SUCCESS) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+		raise_exception(ist, OBJ(IO_EXC), "%s",
 				apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 		return NULL;
 	}
@@ -602,7 +632,7 @@
 DEF(File, tell, NULL) {
 	BIN_CONTENT_CHK(File);
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "tell called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "tell called on closed file");
 		return NULL;
 	}
 	return NEW_INT(ftell(STREAM(self)));
@@ -622,7 +652,7 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "truncate called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "truncate called on closed file");
 		return NULL;
 	}
 
@@ -636,7 +666,7 @@
 
 	aprerr = apr_file_trunc(STREAM(self), size);
 	if(aprerr != APR_SUCCESS) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+		raise_exception(ist, OBJ(IO_EXC), "%s",
 				apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 		return NULL;
 	}
@@ -658,17 +688,17 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "write called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "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");
+		raise_exception(ist, OBJ(IO_EXC), "write called on file opened without write perms");
 		return NULL;
 	}
 
 	aprerr = apr_file_write(STREAM(self), str, &size);
 	if (aprerr != APR_SUCCESS) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+		raise_exception(ist, OBJ(IO_EXC), "%s",
 				apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 		return NULL;
 	}
@@ -690,11 +720,11 @@
 		return NULL;
 	}
 	if (!OPEN(self)) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "write called on closed file");
+		raise_exception(ist, OBJ(IO_EXC), "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");
+		raise_exception(ist, OBJ(IO_EXC), "writeLines called on file opened without write perms");
 		return NULL;
 	}
 	if (has_proto(ist, parms[1], OBJ(STRING_PROTO))) {
@@ -702,7 +732,7 @@
 		size = pr_strlen(parms[1]);
 		aprerr = apr_file_write(STREAM(self), str, &size);
 		if (aprerr != APR_SUCCESS) {
-			raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+			raise_exception(ist, OBJ(IO_EXC), "%s",
 					apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 			return NULL;
 		}
@@ -718,7 +748,7 @@
 			size = pr_strlen(item);
 			aprerr = apr_file_write(STREAM(self), str, &size);
 			if (aprerr != APR_SUCCESS) {
-				raise_exception(ist, OBJ(IOEXCEPTION), "%s",
+				raise_exception(ist, OBJ(IO_EXC), "%s",
 						apr_strerror(aprerr, err_buf, sizeof(err_buf)));
 				return NULL;
 			}
@@ -792,6 +822,7 @@
 	MODULE_ADD_SYM(Dir, str_);
 	MODULE_ADD_SYM(Dir, make);
 	MODULE_ADD_SYM(Dir, dir);
+	MODULE_ADD_SYM(Dir, objList_);
 
 	MODULE_SUB_INIT(File);
 	MODULE_ADD_SYM(File, init_);
Modified: trunk/src/sys.c
===================================================================
--- trunk/src/sys.c	2004-06-13 19:44:50 UTC (rev 620)
+++ trunk/src/sys.c	2004-06-13 22:51:31 UTC (rev 621)
@@ -114,7 +114,7 @@
 
 #ifdef WIN32
 	if(!_fullpath( full_path, path, sizeof(full_path))) {
-		raise_exception(ist, OBJ(IOEXCEPTION), "Bad path in PROTHONPATH: %s", path);
+		raise_exception(ist, OBJ(IO_EXC), "Bad path in PROTHONPATH: %s", path);
 		return;
 	}
 #else