rev 356 - in trunk: include/prothon src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-04-14 21:39:21 -0400 (Wed, 14 Apr 2004)
New Revision: 356

Added:
   trunk/include/prothon/pr_arg.h
Modified:
   trunk/src/builtins-string.c
   trunk/src/src.vcproj
Log:
finished string.__mod__() (untested),
uses modified apr_strings.c

Added: trunk/include/prothon/pr_arg.h
===================================================================
--- trunk/include/prothon/pr_arg.h	2004-04-14 22:32:09 UTC (rev 355)
+++ trunk/include/prothon/pr_arg.h	2004-04-15 01:39:21 UTC (rev 356)
@@ -0,0 +1,49 @@
+
+
+// pr_arg.h
+
+// this is used to pass a variable arg list from Prothon to apr_pr_vformatter
+// routine in apr_snprintf.c.  apr_pr_vformatter is a Prothon custom version
+// of apr_vformatter that uses a pr_arg_t list instead of a va_list.
+// a pr_arg_t list includes type information so that a type mismatch will
+// cause an exception instead of a crash.
+
+#ifndef PR_ARG_H
+#define PR_ARG_H
+
+typedef enum {
+	ARG_TYPE_END = 0,
+	ARG_TYPE_INT,
+	ARG_TYPE_FLOAT,
+	ARG_TYPE_STRING,
+	ARG_TYPE_ERROR,
+} arg_type_t;
+
+typedef union {
+	__int64		i64;
+	double		f64;
+	char*		str;
+} arg_arg_t;
+
+typedef struct {
+	arg_arg_t		arg;
+	arg_type_t		type;
+} pr_arg_t;
+
+typedef pr_arg_t* pr_arg_p;
+
+#define pr_arg_err       (ap[0].type  = ARG_TYPE_ERROR)
+#define if_prerr_rtrn  if(ap[0].type == ARG_TYPE_ERROR) return -99
+
+#define pr_arg_start(ap)	int _pr_arg_ind = 0;
+
+#define pr_arg_int(ap)    \
+	((ap)[_pr_arg_ind].type == ARG_TYPE_INT    ? ap[_pr_arg_ind++].arg.i64 : (pr_arg_err,   0) )
+#define pr_arg_float(ap)  \
+	((ap)[_pr_arg_ind].type == ARG_TYPE_FLOAT  ? ap[_pr_arg_ind++].arg.f64 : (pr_arg_err, 0.0) )
+#define pr_arg_string(ap) \
+	((ap)[_pr_arg_ind].type == ARG_TYPE_STRING ? ap[_pr_arg_ind++].arg.str : (pr_arg_err,  "") )
+
+#define pr_arg_end(ap)		((ap)[_pr_arg_ind].type == ARG_TYPE_END)
+
+#endif // #ifndef PR_ARG_H
\ No newline at end of file

Modified: trunk/src/builtins-string.c
===================================================================
--- trunk/src/builtins-string.c	2004-04-14 22:32:09 UTC (rev 355)
+++ trunk/src/builtins-string.c	2004-04-15 01:39:21 UTC (rev 356)
@@ -63,6 +63,7 @@
 #include "parser.h"
 #include "object.h"
 #include <prothon/prothon_dll.h>
+#include <prothon/pr_arg.h>
 
 #define is_String(objid)        (has_proto_QUES(ist, objid, String_OBJ))
 
@@ -232,28 +233,13 @@
 	return obj;
 }
 
-#define ARG_TYPE_INT	0
-#define ARG_TYPE_FLOAT	1
-#define ARG_TYPE_STRING	2
 
-typedef union {
-	i64_t   i64;
-	double	f64;
-	char*	str;
-} arg_types_t;
-
-typedef struct {
-	arg_types_t	arg;
-	u8_t		type;
-} arg_t;
-
-typedef arg_t* arg_p;
-
 DEF(String, __mod__, FORM_RPARAM) {
-	obj_p	argv_obj = parms[1];
-	size_t	i, argc;
-	arg_p	argv;
-	char*   fmt_str = strch(self);
+	obj_p			res, argv_obj = parms[1];
+	size_t			i, argc;
+	pr_arg_p		argv;
+	char			*fmt_str = strch(self), *buf;
+	apr_status_t	aprerr;
 
 	if ( has_proto_QUES(ist, argv_obj, OBJ(STRING_PROTO)) ||
 		!has_proto_QUES(ist, argv_obj,    OBJ(SEQ_PROTO)) ) {
@@ -261,7 +247,7 @@
 		list_append(ist, argv_obj, parms[1]);
 	}
 	argc = list_len(ist, argv_obj);
-	argv = pr_malloc(argc * sizeof(arg_t));
+	argv = pr_malloc((argc+1) * sizeof(pr_arg_t));
 	for (i=0; i < argc; i++) {
 		obj_p arg_obj = list_item(ist, argv_obj, (int) i);
 		if (has_proto_QUES(ist, arg_obj, OBJ(INT_PROTO))) {
@@ -278,8 +264,17 @@
 			return NULL;
 		}
 	}
-	// ben: your argc and argv are ready for use on fmt_str and return resulting string
-	return self;
+	argv[i].type = ARG_TYPE_END;
+	buf = pr_malloc(16384);
+	aprerr = apr_pr_vsnprintf(buf, 16384, fmt_str, argv);
+	if (aprerr = -99) {
+		raise_exception(ist, OBJ(INTERPRETER_EXC), "invalid or wrong number of % params");
+		return NULL;
+	}
+	IF_APR_ERR("error in (str % tuple) operator") return NULL;
+	res = new_string_obj(ist, buf);
+	pr_free(buf);
+	return res;
 }
 
 DEF(String, __gen__, NULL) {

Modified: trunk/src/src.vcproj
===================================================================
--- trunk/src/src.vcproj	2004-04-14 22:32:09 UTC (rev 355)
+++ trunk/src/src.vcproj	2004-04-15 01:39:21 UTC (rev 356)
@@ -36,7 +36,7 @@
 				AdditionalDependencies="apr.lib wsock32.lib"
 				OutputFile="$(OutDir)/prothon.exe"
 				LinkIncremental="2"
-				AdditionalLibraryDirectories="C:\prothon\apr\apr\LibR"
+				AdditionalLibraryDirectories="C:\prothon\apr\apr\LibD"
 				IgnoreAllDefaultLibraries="FALSE"
 				IgnoreDefaultLibraryNames="LIBCMTD.lib"
 				GenerateDebugInformation="TRUE"
@@ -228,6 +228,9 @@
 				RelativePath="parser.h">
 			</File>
 			<File
+				RelativePath="..\include\prothon\pr_arg.h">
+			</File>
+			<File
 				RelativePath=".\prlist.h">
 			</File>
 			<File
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.