rev 401 - in trunk: include/prothon modules/Prosist src

SVN User <[email protected]> Thu, 22 Apr 2004 19:30:00 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-04-22 19:29:54 -0400 (Thu, 22 Apr 2004)
New Revision: 401

Modified:
   trunk/include/prothon/prothon.h
   trunk/include/prothon/prothon_dll.h
   trunk/modules/Prosist/Prosist.c
   trunk/src/builtins-list.c
   trunk/src/builtins-string.c
   trunk/src/object.c
Log:
Prosist saving tree and restoring tree now works on simple objects

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-04-22 20:53:38 UTC (rev 400)
+++ trunk/include/prothon/prothon.h	2004-04-22 23:29:54 UTC (rev 401)
@@ -576,6 +576,10 @@
 obj_p new_string_n_obj(isp ist, char* string, size_t n);
 #define NEW_STRINGN(string, n)  new_string_n_obj(ist, string, n)
 
+// SET_STRING_DATA: Change an existing object's data to string data
+// clobbers any esisting data pointer
+void set_string_data(isp ist, obj_p obj, char* p, size_t len);
+
 // PR_STRPTR: macro to retreive string object as a C string
 // warning, string objects may contain null chars so C string result may be short
 // This corresponds to dptr member of apr_datum_t.
@@ -593,8 +597,12 @@
 
 // NEW_LIST_OBJ: Create a new list object with a given initial size
 obj_p new_list_obj(isp ist, size_t initial_size);
-#define NEW_LIST(size)  new_list_obj(ist, size)
+#define NEW_LIST(initial_size) new_list_obj(ist, initial_size)
 
+// INIT_LIST_DATA: Change an existing object's data to a list object's data
+// clobbers any existing data pointer
+void init_list_data(isp ist, obj_p obj, int initial_size);
+
 // NEW_TUPLE_OBJ: Create a new immutable tuple object
 // A tuple is identical to a list except that at the Prothon level it is
 // immutable.  At the C level this means that you should not modify it

Modified: trunk/include/prothon/prothon_dll.h
===================================================================
--- trunk/include/prothon/prothon_dll.h	2004-04-22 20:53:38 UTC (rev 400)
+++ trunk/include/prothon/prothon_dll.h	2004-04-22 23:29:54 UTC (rev 401)
@@ -92,8 +92,10 @@
 	obj_p		(*new_float_obj)(isp ist, double num);
 	obj_p		(*new_string_obj)(isp ist, const char* string);
 	obj_p		(*new_string_n_obj)(isp ist, char* string, size_t n);
+	void		(*set_string_data)(isp ist, obj_p obj, char* p, size_t len);
 	obj_p		(*new_tuple_obj)(isp ist, int fixed_size);
 	obj_p		(*new_list_obj)(isp ist, size_t initial_size);
+	void		(*init_list_data)(isp ist, obj_p obj, int initial_size);
 	obj_p		(*copy_list_obj)(isp ist, obj_p list_obj);
 	obj_p		(*new_C_func_obj)(isp ist, pr_func* func_ptr, obj_p form_lbl_val_list);
 	obj_p		(*call_func)( isp ist, obj_p self, obj_p func_sym, 
@@ -245,8 +247,10 @@
 #define new_float_obj		(*services->new_float_obj)
 #define new_string_obj		(*services->new_string_obj)
 #define new_string_n_obj	(*services->new_string_n_obj)
+#define set_string_data		(*services->set_string_data)
 #define new_tuple_obj		(*services->new_tuple_obj)
 #define new_list_obj		(*services->new_list_obj)
+#define init_list_data		(*services->init_list_data)
 #define copy_list_obj		(*services->copy_list_obj)
 #define sym					(*services->sym)
 #define list1				(*services->list1)

Modified: trunk/modules/Prosist/Prosist.c
===================================================================
--- trunk/modules/Prosist/Prosist.c	2004-04-22 20:53:38 UTC (rev 400)
+++ trunk/modules/Prosist/Prosist.c	2004-04-22 23:29:54 UTC (rev 401)
@@ -173,8 +173,6 @@
 
 	aprerr = apr_dbm_store(psrecp->db, key_d, value);
 	IF_APR_DBM_ERR(psrecp->db, "storing int value ", key_d.dptr);
-printf("key: %s\n", key_d.dptr);
-printf("val: %s\n\n", value.dptr);
 	ADD_BUF_END();
 }
 
@@ -237,18 +235,18 @@
 }
 
 //******************************** load_obj ***********************************
-void load_obj(isp ist, psrec_p psrecp, obj_p id_obj) {
+obj_p load_obj(isp ist, psrec_p psrecp, obj_p id_obj) {
 	apr_datum_t value;
 	psid_t n, id;
 	char *p, *end;
-	obj_p obj;
+	obj_p res, obj;
 	id = (psid_t) id_obj->data.i64;
 
-	value = load_obj_by_id(psrecp, id);  if_exc_return;
+	value = load_obj_by_id(psrecp, id);  if_exc_return NULL;
 	p = value.dptr;
 	end = p + value.dsize;
 
-	obj = dict_item(ist, psrecp->id_to_obj, id_obj);  if_exc_return;
+	obj = dict_item(ist, psrecp->id_to_obj, id_obj);  if_exc_return NULL;
 
 	while (p < end) {
 		switch (*p) {
@@ -260,7 +258,7 @@
 						n = n*10 + *p - '0';
 					ref_idobj = NEW_INT(n);
 					proto = dict_item( ist, psrecp->id_to_obj, 
-						               ref_idobj);  if_exc_return;
+						               ref_idobj);  if_exc_return NULL;
 					del_unlock(ref_idobj);
 					if (first_proto)
 						switch_proto(ist, obj, proto);
@@ -270,45 +268,58 @@
 				}
 				break;
 			}
-					  /*
 			case 'a': {
-				empty_obj(ist, psrecp, id_obj, &new_flg);
 				while(*p++ != ';') {
-					obj_p ref_id_obj;
-					while(*p++ != ':');
-					for(n=0; *p != ',' && *p != ';'; p++)
+					obj_p ref_id_obj, aval;
+					char *q, *aname = pr_malloc(value.dsize);
+					for (q=aname; *p != ':';) *q++ = *p++;
+					*q = 0;
+					for(n=0, p++; *p != ',' && *p != ';'; p++)
 						n = n*10 + *p - '0';
 					ref_id_obj = NEW_INT(n);
-					if (!dict_item(ist, psrecp->id_to_obj, ref_id_obj))
-						list_append(ist, idlist, ref_id_obj);
-					else
-						del_unlock(ref_id_obj);
+					aval = dict_item( ist, psrecp->id_to_obj, ref_id_obj);  
+					if_exc_return NULL;
+					set_attr(ist, obj, sym(ist, aname), aval);  if_exc_return NULL;
+					del_unlock(ref_id_obj);
 				}
 				break;
 			}
 			case 'l': {
-				empty_obj(ist, psrecp, id_obj, &new_flg);
+				init_list_data(ist, obj, 0);
 				while(++p < end) {
-					obj_p ref_id_obj;
+					obj_p ref_id_obj, item;
 					for(n=0; *p != ',' && p < end; p++)
 						n = n*10 + *p - '0';
 					ref_id_obj = NEW_INT(n);
-					if (!dict_item(ist, psrecp->id_to_obj, ref_id_obj))
-						list_append(ist, idlist, ref_id_obj);
-					else
-						del_unlock(ref_id_obj);
+					item = dict_item( ist, psrecp->id_to_obj, ref_id_obj);  
+					if_exc_return NULL;
+					list_append(ist, obj, item);
+					del_unlock(ref_id_obj);
 				}
-				return;
+				goto done;
 			}
-			*/
 			case 'i':
+				obj->data_type = DATA_TYPE_IMMDATA;
+				for(n=0,p++; *p != ':'; p++)
+					n = n*10 + *p - '0';
+				obj->imm_data_len = n;
+				memcpy(&(obj->data), p+1, sizeof(obj->data));
+				goto done;
 			case 's':
-				return;
+				set_string_data(ist, obj, p+1, end-(p+1));
+				goto done;
+			case '`':
+				del_unlock(obj);
+				return NULL;
 			default:
 				raise_exception(ist, ProsistExc_OBJ, "invalid record from db: %d", id);
-				return;
+				return NULL;
 		}
 	}
+done:
+	res = call_func0(ist, obj, SYM(__FROMSTORPROXY__));
+	del_unlock(obj);
+	return res;
 }
 
 //******************************** append_ids *********************************
@@ -422,19 +433,21 @@
 //******************************** load_db ************************************
 obj_p load_db(isp ist, psrec_p psrecp) {
 	int llen, idx = 0;
-	obj_p idlist = new_list_obj(ist, 20);
+	obj_p idlist = new_list_obj(ist, 20), root = NULL;
 		
 	append_ids(ist, psrecp, idlist, 0); if_exc_return NULL;
 	for (; idx < (int) list_len(ist, idlist); idx++) {
 		psid_t id = (psid_t) (list_item(ist, idlist, idx)->data.i64);
 		append_ids(ist, psrecp, idlist, id); if_exc_return NULL;
 	}
+	root = load_obj(ist, psrecp, NEW_INT(0)); if_exc_return NULL;
 	llen = (int) list_len(ist, idlist);
 	for(idx=0; idx < llen; idx++) {
 		obj_p id_obj = list_item(ist, idlist, idx);
-		load_obj(ist, psrecp, id_obj);
+		load_obj(ist, psrecp, id_obj); if_exc_return NULL;
+		del_unlock(id_obj);
 	}
-	return NULL;
+	return root;
 }
 
 //******************************** store_obj **********************************
@@ -466,10 +479,6 @@
 		ADD_BUF_TO_DATUM(value);
 		aprerr = apr_dbm_store(psrecp->db, key, value);
 		IF_APR_DBM_ERR(psrecp->db, "storing id object ", value.dptr);
-
-printf("key: %s\n", key.dptr);
-printf("val: %s\n\n", value.dptr);
-
 		ADD_BUF_END();
 		return;
 	}
@@ -520,10 +529,6 @@
 	ADD_BUF_TO_DATUM(value);
 	aprerr = apr_dbm_store(psrecp->db, key, value);
 	IF_APR_DBM_ERR(psrecp->db, "storing proxy object ", key.dptr);
-
-printf("key: %s\n", key.dptr);
-printf("val: %s\n\n", value.dptr);
-
 	ADD_BUF_END();
 }
 
@@ -700,13 +705,6 @@
 	aprerr = apr_dbm_open_ex(&db, "SDBM", name, mode, uperm, cntxt);  
 	IF_APR_ERR("opening Prosist database") return NULL;
 
-	read_unlock(ist, self);
-	set_obj_rdacc(self, ACC_USER1);
-	set_obj_doc(self, "Prosist DataBase");
-	set_attr(ist, self, sym(ist, "filename"), parms[1]);
-	set_attr(ist, self, sym(ist, "root"),     root_obj);
-	read_lock(ist, self);
-
 	self->data_type		= DATA_TYPE_DATAPTR;
 	self->data.ptr		= psrecp = pr_malloc(sizeof(psrec_t));
 	psrecp->db			= db;
@@ -719,13 +717,19 @@
 
 	if (root_obj == OBJ(NONE)) {
 		psrecp->nextid = load_dbm_int(psrecp, "!nextid");
-		psrecp->root   = load_db(ist, psrecp);
+		root_obj = psrecp->root = load_db(ist, psrecp);
 	} else {
 		psrecp->root   = root_obj;
 		psrecp->nextid = 0;
 		store_new_db(ist, psrecp);  if_exc_return NULL; 
 		store_dbm_int(psrecp, "!nextid", psrecp->nextid);
 	}
+	read_unlock(ist, self);
+	set_obj_rdacc(self, ACC_USER1);
+	set_obj_doc(self, "Prosist DataBase");
+	set_attr(ist, self, sym(ist, "filename"), parms[1]);
+	set_attr(ist, self, sym(ist, "root"),     root_obj);
+	read_lock(ist, self);
 	return OBJ(NONE);
 }
 /*

Modified: trunk/src/builtins-list.c
===================================================================
--- trunk/src/builtins-list.c	2004-04-22 20:53:38 UTC (rev 400)
+++ trunk/src/builtins-list.c	2004-04-22 23:29:54 UTC (rev 401)
@@ -93,6 +93,16 @@
 	return list_obj;
 }
 
+//********************************* init_list_data ****************************
+void init_list_data(isp ist, obj_p obj, int initial_size) {
+	list_p lstp;
+	lstp = obj->data.ptr = 
+			pr_malloc(list_sizeof(max(initial_size, 2)));
+	obj->data_type = DATA_TYPE_DATAPTR;
+	lstpsize(lstp) = max(initial_size, 2);
+	lstplen(lstp)  = 0;
+}
+
 //********************************* copy_list_obj ****************************
 obj_p copy_list_obj(isp ist, obj_p list_obj) {
 	obj_p res;

Modified: trunk/src/builtins-string.c
===================================================================
--- trunk/src/builtins-string.c	2004-04-22 20:53:38 UTC (rev 400)
+++ trunk/src/builtins-string.c	2004-04-22 23:29:54 UTC (rev 401)
@@ -112,6 +112,16 @@
 	return obj;
 }
 
+//********************************* set_string_data ***************************
+void set_string_data(isp ist, obj_p obj, char* p, size_t len) {
+	pr_str_p obj_str;
+	obj_str = obj->data.ptr = obj_malloc(obj, sizeof(pr_str_t)+len+1);
+	obj_str->len = len;
+	memcpy(&(obj_str->str[0]), p, len);
+	obj_str->str[len] = 0;
+	obj->immutable = TRUE;
+}
+
 //********************************* STRING MODULE *****************************
 
 MODULE_DECLARE(String);

Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c	2004-04-22 20:53:38 UTC (rev 400)
+++ trunk/src/object.c	2004-04-22 23:29:54 UTC (rev 401)
@@ -141,8 +141,10 @@
 	dll_services.new_float_obj	  = new_float_obj;	
 	dll_services.new_string_obj	  = new_string_obj;	
 	dll_services.new_string_n_obj = new_string_n_obj;	
+	dll_services.set_string_data  = set_string_data;	
 	dll_services.new_tuple_obj	  = new_tuple_obj;	
 	dll_services.new_list_obj	  = new_list_obj;
+	dll_services.init_list_data	  = init_list_data;
 	dll_services.copy_list_obj	  = copy_list_obj;
 	dll_services.new_C_func_obj	  = new_C_func_obj;	
 	dll_services.call_func		  = call_func;