slang-users mailing list

[2019 Date Index] [2019 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Limitations of eval()?


Bernd Eggink <bernd.eggink@xxxxxxxxxx> wrote:
> The real problem I was trying to solve is the following. I need a
> wrapper function which works exactly like 'printf', except that it first
> makes some changes to the format parameter. My first attempt was to
> change the corresponding stack element and then call printf() - but then
> printf thinks it has zero parameters and generates a stack underflow
> error. And as _NARGS can't be set from outside, I'm stuck.
>
> It's not _that_ important, but I'd be interested to know if this could 
> be solved in an elegant way, without using eval.

Here is an example of a function that modifies to format parameter to
replace commas to '|' and writes the result using fprintf:

define fprintf_wrap ()
{
   variable args = __pop_list (_NARGS);
   if (length (args) < 2)
     throw UsageError, "Usage: ret=fprintf_wrap(fp, format, ...);";
   variable format = args[1];
   format = strreplace (format, ",", "|");
   args[1] = format;
   return fprintf (__push_list (args));
}

define slsh_main ()
{
   variable format = "%d,%d\n";
   () = fprintf (stdout, format, 123, 456);
   () = fprintf_wrap (stdout, format, 123, 456);
}

--John
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2019 date index] [2019 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]