Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!julius.cs.uiuc.edu!rpi!uupsi!kepler1!fcaggian From: fcaggian@kepler.com (Frank Caggiano) Newsgroups: comp.lang.eiffel Subject: formated string objects Keywords: sprintf like routine Message-ID: <457@kepler1.kepler.com> Date: 4 Jan 91 21:51:03 GMT Organization: Kepler Financial Mgmt., Setuket, NY Lines: 154 The following c routine is something I've wanted to write for a while now. It makes creating formatted string objects easier. Its an sprintf like routine but it returns a string object. The call and arguments are the same as for the [sf]printf c routines. Declare it as external format_obj: STRING language "C" Usage colud then be something like : local s: STRING do s := format_obj("%2d %-5s%6d%8.3f%5d%7d%6d%4d", n, stock_list.item.ticker.to_c, stock_list.item.amount, price / 256.0, ktotal / 1000, stock_list.item.position, stock_list.item.pending, stock_list.item.form ); This was written and tested on a space running 4.0.3. It should be poratable. Please let me know of any problems especially with the varargs stuff. ********* BEGIN INCLUDED CODE /* -- Copyright (c) 1990 Kepler Financial Mgmt. -- -- Permission is granted to anyone to make or distribute -- copies of this software provided that the -- copyright notice and this permission notice are preserved. -- Ownership remains with Kepler Financial Mgmt. */ #ifndef lint static char *sccsid = "@(#)_objformat.c:1.1 1/4/91"; #endif #include #include <_eiffel.h> #ifndef BUFSIZ #define BUFSIZ 512 #endif BUFSIZ OBJPTR format_obj(fmt, va_alist) char *fmt; va_dcl /* the missing ; IS RIGHT see varargs */ { va_list ap; register int n; register char *cp, c, *buf_end; char buf[BUFSIZ], f[20]; OBJPTR strobj; ROUT_PTR from_c; cp = buf; f[0] = '%'; buf_end = buf+BUFSIZ; va_start (ap); while((c = *fmt++) != '\0' && cp < buf_end) { if(c == '%') { for(c = *fmt++,n = 1; c; n++) { switch(c) { case '%': /* its %% put out the % */ *cp++ = '%'; c = '\0'; break; case 's': /* normal string */ f[n++] = c; f[n] = '\0'; sprintf(cp, f, va_arg(ap,char *)); cp += strlen(cp); c = '\0'; break; case '*': /* variable field width */ { char varwidth[10]; int x = 0; sprintf(varwidth,"%d",va_arg(ap, int)); while((f[n++] = varwidth[x++])) ; } n -= 2; c = *fmt++; break; case 'f': /* floats etc. */ case 'g': case 'e': f[n++] = c; f[n] = '\0'; sprintf(cp, f,va_arg(ap, double)); cp += strlen(cp); c = '\0'; break; case 'd': /* ints and such */ case 'o': case 'c': case 'x': case 'u': f[n++] = c; f[n] = '\0'; sprintf(cp, f,va_arg(ap, int)); cp += strlen(cp); c = '\0'; break; default: /* none of the above */ f[n] = c; c = *fmt++; break; } } } else /* c wasn't '%'; just copy it into buf */ *cp++ = c; } va_end(ap); *cp = '\0'; strobj = (OBJPTR) eif_create("string", 0) from_c = (ROUT_PTR) eif_rout(strobj, "from_c"); (*from_c) (strobj, buf); return (strobj); } By the way has anyone had a chance to tryout the RESOURCE managment stuf I posted some time back? Anyone doing anything with X/GUI's and Eiffel. I would like to hear from anyone doing work along those lines. (if they are free to talk. Those on Double Secret Probation need not apply :-) ). -- Frank Caggiano INTERNET: fcaggian@kepler.com Kepler Financial Management, Ltd. UUCP: ..!uunet!kepler1!fcaggian 100 North Country Rd. fax: (516) 751-8678 Sekauket, NY 11733 voice: (516) 689-6300