[Gridflow-cvs] [svn] commit: r6046 - /trunk/src/gridflow.cxx

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Thu Aug 12 00:26:55 EDT 2010


Author: matju
Date: Thu Aug 12 00:26:53 2010
New Revision: 6046

Log:
much better oprintf() for use by [gf/sprintf] and [gf/error]

Modified:
    trunk/src/gridflow.cxx

Modified: trunk/src/gridflow.cxx
==============================================================================
--- trunk/src/gridflow.cxx (original)
+++ trunk/src/gridflow.cxx Thu Aug 12 00:26:53 2010
@@ -130,38 +130,56 @@
 }
 
 static float eatfloat (int argc, t_atom *argv, int &i) {
-	if (!argc) RAISE("not enough args"); if (argv[i].a_type != A_FLOAT) RAISE("expected float");
+	if (!argc) RAISE("not enough args"); if (argv[i].a_type != A_FLOAT) RAISE("expected float in $%d",i+1);
 	return argv[i++].a_float;}
 static t_symbol *eatsymbol (int &argc, t_atom *&argv, int &i) {
-	if (!argc) RAISE("not enough args"); if (argv[i].a_type != A_SYMBOL) RAISE("expected symbol");
+	if (!argc) RAISE("not enough args"); if (argv[i].a_type != A_SYMBOL) RAISE("expected symbol in $%d",i+1);
 	return argv[i++].a_symbol;}
 #define EATFLOAT   eatfloat(argc,argv,i)
 #define EATSYMBOL eatsymbol(argc,argv,i)
 void pd_oprintf (std::ostream &o, const char *s, int argc, t_atom *argv) {
+	// currently not supporting *m$ ... see man page
+	// currently not supporting %_ (a former extension standing for any atom... merge this functionality with %s or whatever)
+	// have our own proper atom_string
 	int i=0;
 	const char *t;
 	for (; *s; s++) {
 		if (*s!='%') {o << (char)*s; continue;}
-		t=s; // future use
+		t=s;
+		int n=0; // number of args to eat before the actual value
 		s++; // skip the %
-		switch (*s) {
-		  case 'd': case 'i': o << long(EATFLOAT); break;
-		  case 'f': o << EATFLOAT; break;
-		  case 's': o << EATSYMBOL->s_name; break;
-		  case '_':
-			if (!argc) RAISE("not enough args");
-			char buf[MAXPDSTRING];
-			atom_string(&argv[i++],buf,MAXPDSTRING);
-			o << buf;
-		  break;
-		  case '%':
-			o << "%";
-		  break;
-		  default:
-			RAISE("sorry, the format character '%c' is not supported yet",*s);
+		while (strchr("#0- +'I",*s)) s++; // skip flags
+		if (*s>='1' && *s<='9') {do {s++;} while (*s>='0' && *s<='9');} // skip field width
+		else if (*s=='*') {s++; n++;}
+		//post("precision debug %c @ %d",*s,s-t);
+		if (*s=='.') { // precision
+			s++;
+			if (*s>='1' && *s<='9') {do {s++;} while (*s>='0' && *s<='9');} // skip field width
+			else if (*s=='*') {s++; n++;}
 		}
-	}
-	return;
+		if (strchr("hlLqjzt",*s)) RAISE("can't use length modifier '%c' in the context of pd",*s);
+		char form[t-s+2]; sprintf(form,"%.*s",s-t+1,t);
+		//o << "[" << form << "]";
+		int k[2];
+		if (n>0) k[0]=int(EATFLOAT);
+		if (n>1) k[1]=int(EATFLOAT);
+		if (strchr("cdiouxX",*s)) {switch (n) {
+			case 0: oprintf(o,form,          int(EATFLOAT)); break;
+			case 1: oprintf(o,form,k[0],     int(EATFLOAT)); break;
+			case 2: oprintf(o,form,k[0],k[1],int(EATFLOAT)); break;
+		}} else if (strchr("fg",*s)) {switch (n) {
+			case 0: oprintf(o,form,          EATFLOAT); break;
+			case 1: oprintf(o,form,k[0],     EATFLOAT); break;
+			case 2: oprintf(o,form,k[0],k[1],EATFLOAT); break;
+		}} else if (strchr("s",*s)) {switch (n) {
+			case 0: oprintf(o,form,          EATSYMBOL->s_name); break;
+			case 1: oprintf(o,form,k[0],     EATSYMBOL->s_name); break;
+			case 2: oprintf(o,form,k[0],k[1],EATSYMBOL->s_name); break;
+		}} else if (strchr("%",*s)) o << "%";
+		else RAISE("sorry, the format character '%c' is not supported yet, in \"%.*s\"",*s,s+1-t,t);
+		//case '_': if (!argc) RAISE("not enough args");
+		//	char buf[MAXPDSTRING]; atom_string(&argv[i++],buf,MAXPDSTRING); o << buf; break;
+	}
 }
 
 std::ostream &operator << (std::ostream &self, const t_atom &a) {



More information about the Gridflow-cvs mailing list