rev 618 - in trunk: . pr/test src

SVN User <[email protected]> Sun, 13 Jun 2004 04:05:13 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-06-13 04:05:10 -0400 (Sun, 13 Jun 2004)
New Revision: 618

Modified:
   trunk/STATUS.txt
   trunk/pr/test/test.pr
   trunk/src/builtins-file.c
Log:
Dir init_, str_, make, dir methods added

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-06-13 04:56:52 UTC (rev 617)
+++ trunk/STATUS.txt	2004-06-13 08:05:10 UTC (rev 618)
@@ -1,6 +1,13 @@
 
 ----------------------- TO-DO (highest priority first) ------------------------
 
+--- Complete File/Dir objects
+		http://www.prothon.org/pipermail/prothon-user/2004-April/000955.html
+--- TextFile object
+		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	
+
 --- add \u0000 \U00000000 and \N{name} esc sequences
 
 --- unicode module - codecs
@@ -12,12 +19,6 @@
 
 --- add Object.data_ (builtins-databytes.c)
 
---- Complete File/Dir objects
---- TextFile object
-		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
-		
 --- allow tracebacks in the constructors to exception objects or raise command
 --- Traceback objects (stack trace of an exception), exceptions should show function names
 --- Fix error reporting mess, hide fake file names

Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr	2004-06-13 04:56:52 UTC (rev 617)
+++ trunk/pr/test/test.pr	2004-06-13 08:05:10 UTC (rev 618)
@@ -1,10 +1,16 @@
 #!/usr/bin/env prothon
 
-x = 123
+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 "{x}".fmt()	# 123
+print "{0:d}".fmt(Widget)
 
-print "{x} {0} {y}".fmt("abc", y=1.23) # 123 abc 1.23
+print "{0}".fmt(Widget)
 
-print "{x} {0} {y}".fmt("abc", y=1.23, x='xyz') # xyz abc 1.23
-

Modified: trunk/src/builtins-file.c
===================================================================
--- trunk/src/builtins-file.c	2004-06-13 04:56:52 UTC (rev 617)
+++ trunk/src/builtins-file.c	2004-06-13 08:05:10 UTC (rev 618)
@@ -66,7 +66,7 @@
 
 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 */
 	apr_int32_t	flags;		/* APR flags */
 } pr_file_t;
@@ -79,19 +79,27 @@
 #define FLAGS(obj)   (((pr_file_p)((obj)->data.ptr))->flags)
 #define OPEN(obj)    (((obj) != File_OBJ) && STREAM(obj))
 
+MODULE_DECLARE(Dir);
 MODULE_DECLARE(File);
 
-static void set_file_obj(obj_p file_obj, obj_p filename, obj_p mode,
-			     apr_file_t *fp, int bufsize,
-			     apr_int32_t flags)
-{
-	apr_pool_t *subpool = apr_file_pool_get(fp);
+//*************************** set_file_obj_data *******************************
+static void set_file_obj_data( isp ist, obj_p file_obj, obj_p filename, obj_p mode,
+						  	   apr_file_t *fp, int bufsize, apr_int32_t flags) {
+    apr_status_t aprerr;
+	apr_pool_t *subpool = NULL;
 	pr_file_p filep;
 
+	if (fp) 
+		subpool = apr_file_pool_get(fp);
+	else {
+		aprerr = apr_pool_create(&subpool, get_pr_head_pool());
+		IF_APR_ERR("out of memory in set_file_obj_data()") return;
+	}
 	set_attr(ist, file_obj, sym(ist, "name"), filename);
-	set_attr(ist, file_obj, sym(ist, "mode"), mode);
+	if (mode)
+		set_attr(ist, file_obj, sym(ist, "mode"), mode);
 
-	filep = pr_malloc(sizeof(*filep));
+	filep = pr_malloc(sizeof(pr_file_t));
 
 	if (bufsize < MIN_BUFSIZE)
 		bufsize = MIN_BUFSIZE;
@@ -106,18 +114,82 @@
 	file_obj->wr_access = ACC_USER1;
 }
 
-
-static obj_p new_file_object(obj_p filename, obj_p mode,
-			     apr_file_t *fp, int bufsize,
-			     apr_int32_t flags)
-{
+//*************************** new_file_obj ************************************
+static obj_p new_file_obj( isp ist,  obj_p filename, obj_p mode, apr_file_t *fp, 
+						   int bufsize,  apr_int32_t flags ) {
 	obj_p file_obj;
-
 	file_obj = NEW_OBJ(File_OBJ);
-	set_file_obj(file_obj, filename, mode, fp, bufsize, flags);
+	set_file_obj_data(ist, file_obj, filename, mode, fp, bufsize, flags);
 	return file_obj;
 }
 
+//*************************** new_dir_obj *************************************
+static obj_p new_dir_obj(isp ist, obj_p path) {
+	obj_p dir_obj;
+	dir_obj = NEW_OBJ(Dir_OBJ);
+	set_file_obj_data(ist, dir_obj, path, NULL, NULL, 0, APR_READ);
+	return dir_obj;
+}
+
+//*************************** MODULE Dir **************************************
+
+MODULE_START(Dir)
+{
+	Dir_OBJ = NEW_OBJ(NULL);
+	set_obj_doc(Dir_OBJ, "Directory object prototype");
+
+	MODULE_ADD_TO_BASE(Dir);
+	Dir_OBJ->unclonable = TRUE;
+}
+
+DEF(Dir, init_, FPARM1(path, NEW_STRING("."))) {
+	BIN_EMPTY_CHK();
+	CHECK_TYPE_EXC(parms[1], OBJ(STRING_PROTO), String);
+	read_unlock(ist, self);
+	set_file_obj_data(ist, self, parms[1], NULL, NULL, 0, APR_READ);
+	if_rdlock(self) return NULL;
+	return OBJ(NONE);
+}	
+
+#define addch3(ch)  buf[buf_idx++].b0 = (u8_t)(ch)
+
+DEF(Dir, str_, NULL) {
+	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);
+	pr_free(path);
+	return NEW_STRING(buf);
+}
+
+DEF(Dir, make, FPARM1(uperm, NEW_INT(APR_UREAD|APR_UWRITE|APR_UEXECUTE))) {
+    apr_status_t aprerr; 
+	char* path;
+	i32_t uperm;
+	pr_file_p filep;
+	BIN_CONTENT_CHK(Dir);
+	INT_32_PARAM(1, uperm);
+	filep = self->data.ptr;
+	path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "name")));
+	aprerr = apr_dir_make(path, uperm, filep->pool);
+	IF_APR_ERR("Unable to make directory") return NULL;
+	return OBJ(NONE);
+}
+
+DEF(Dir, dir, NULL) {
+    apr_status_t aprerr; 
+	char *path, *respath;
+	pr_file_p filep;
+	BIN_CONTENT_CHK(Dir);
+	filep = self->data.ptr;
+	path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "name")));
+	aprerr = apr_filepath_merge(&respath, path, "..", 0, filep->pool);
+	IF_APR_ERR("Unable to calculate parent of directory") return NULL;
+	return new_dir_obj(ist, NEW_STRING(respath));
+}
+
+//*************************** MODULE File *************************************
+
 MODULE_START(File)
 {
 	File_OBJ = NEW_OBJ(NULL);
@@ -177,10 +249,10 @@
 		return NULL;
 	}
 
-	/* set_file_obj() modified the object, and thus calls write_lock.
+	/* 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_file_obj(self, parms[1], parms[3], f, bufsize, flags);
+	set_file_obj_data(ist, self, parms[1], parms[3], f, bufsize, flags);
 	read_lock(ist, self);
 	self->unclonable = TRUE;
 	return OBJ(NONE);
@@ -707,7 +779,7 @@
 	if (aprerr != APR_SUCCESS)
 		return NULL;
 
-	return new_file_object(file, mode, fp, MIN_BUFSIZE, flags);
+	return new_file_obj(ist, file, mode, fp, MIN_BUFSIZE, flags);
 }
 
 MAIN_MODULE_INIT(File) {
@@ -715,6 +787,12 @@
 	apr_status_t aprerr;
 	obj_p file_obj;
 
+	MODULE_SUB_INIT(Dir);
+	MODULE_ADD_SYM(Dir, init_);
+	MODULE_ADD_SYM(Dir, str_);
+	MODULE_ADD_SYM(Dir, make);
+	MODULE_ADD_SYM(Dir, dir);
+
 	MODULE_SUB_INIT(File);
 	MODULE_ADD_SYM(File, init_);
 	MODULE_ADD_SYM(File, read);