On Tue, 2003-06-17 at 08:49, Kostas Oikonomou wrote:
> Here's my solution to this problem. (I've used this solution for a while.)
>
> Icon code:
> ----------
>
> # Dynamically-loaded procedure to do C's "sprintf(format,x)".
> procedure sprintf1(a[])
> return (sprintf1 := pathload("libCutils.so","isprintf"))!a
> end
>
>
> C code (really, RTL, has to be run through "rtt")
> --------------------
> #include "Icon2C.h"
> int isprintf(int argc, struct descrip argv[]) {
> char cs[32];
> ArgString(1); ArgReal(2);
> sprintf(cs, StringVal(argv[1]), RealVal(argv[2]));
> /* RTL hiding inside this macro: */
> RetString(cs);
> }
Thanks, Kostas!
However, it looks to me as though 'isprintf()' is very specific:
it takes a single format descriptor embedded in argv[1] and
uses it to format a single real value from argv[2] - right?
That's certainly useful, but not as general as Michael's
improved version of printf.icn. In fact, I think it points
out the difficulty with trying to implement the general
case using C's sprintf() - one would have to examine the
arguments to determine the type of Unicon-to-C conversion
required before calling C's sprintf (i.e., StringVal(),
RealVal(), etc.) - not to mention handling of Icon's automatic
type coercions.
Of course, Icon's variable-size integer support and
arbitrary-length strings also makes the size of of any temporary
buffer difficult to compute, making buffer overflows possible.
My suspicion is that a general solution based on C's sprintf
is going to be just as complicated (and perhaps more error
prone!) than working from Michael's pure Icon/Unicon adaptation
of William Mitchell's code.
-Steve
--
Steve Wampler -- [EMAIL PROTECTED]
Quantum materiae materietur marmota monax si marmota
monax materiam possit materiari?
-------------------------------------------------------
This SF.Net email is sponsored by: INetU
Attention Web Developers & Consultants: Become An INetU Hosting Partner.
Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group