[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
BOUNCE rtl@rtlinux.org: Approval required: Non-member submission from [Tony Denault <denault@hawaii.edu>] (fwd)
- To: rtl@rtlinux.org
- Subject: BOUNCE rtl@rtlinux.org: Approval required: Non-member submission from [Tony Denault <denault@hawaii.edu>] (fwd)
- From: root <root>
- Date: Mon, 7 May 2001 03:26:56 -0600 (MDT)
>From owner-rtl Fri May 4 22:13:14 2001
Received: from m1.hawaii.edu (pmdf@m1.hawaii.edu [128.171.94.13])
by hq.fsmlabs.com (8.11.2/8.11.2) with ESMTP id f454DDJ25855
for <rtl@rtlinux.org>; Fri, 4 May 2001 22:13:14 -0600
Received: from CONVERSION-DAEMON.m1.hawaii.edu by m1.hawaii.edu
(PMDF V6.0-24 #38433) id <0GCU00401GUW5N@m1.hawaii.edu>; Fri,
04 May 2001 18:08:56 -1000 (HST)
Received: from uhunix1.its.hawaii.edu (uhunix1.its.hawaii.edu [128.171.44.6])
by m1.hawaii.edu (PMDF V6.0-24 #38433)
with ESMTP id <0GCU0031KGUVTM@m1.hawaii.edu>; Fri,
04 May 2001 18:08:55 -1000 (HST)
Received: from localhost (denault@localhost) by uhunix1.its.hawaii.edu
(8.8.8+Sun/8.8.8) with ESMTP id SAA02629 for <rtl@rtlinux.org>; Fri,
04 May 2001 18:08:55 -1000 (HST)
Date: Fri, 04 May 2001 18:08:54 -1000 (HST)
From: Tony Denault <denault@hawaii.edu>
Subject: Re: [rtl] printing float
In-reply-to: <3AE81F24.B199DBAE@radar.mcgill.ca>
X-X-Sender: <denault@uhunix1>
To: rtl@rtlinux.org
Message-id: <Pine.GSO.4.33.0105041801500.223-100000@uhunix1>
MIME-version: 1.0
Content-type: TEXT/PLAIN; charset=US-ASCII
X-Authentication-warning: uhunix1.its.hawaii.edu: denault owned process doing
-bs
On Thu, 26 Apr 2001, Frederic Cazenave wrote:
> Hi,
>
> What can I do to print float inside a fp thread ???
> I tryed to do
> char f_print[128];
>
After my signature, I'll post a very simple sprintf-like function - Its
very limited, but does floating point values. One of its problems
is that it uses the va_start() family of function. I think these are
non-reentrance macros so you can not do something like this:
int mrt_msg( const char * fmt, ...)
{
int l;
char buf[256];
va_list argptr;
va_start( argptr, fmt );
l = mrt_sprintf( buf, sizeof(buf), argptr );
va_end( argptr );
rtf_put( AO_FIFO_OUT_INX, buf, l);
return l;
}
I ended up having to duplicate the code for mrt_sprint() inside mrt_msg().
Anyone else have a better solution they are willing to post?
Tony
/-----------------------------------------------------------------------------\
| Tony Denault | Internet: denault@irtf.ifa.hawaii.edu |
| NASA IRTF, Institute of Astronomy | Phone: (808) 974-4206 |
| 1175 Manono St., Bldg 393 | Fax: (808) 974-4207 |
| Hilo, Hawaii 96720 | |
\-----------------------------------------------------------------------------/
There is my mrt_sprint():
#include <rtl_fifo.h>
#include <stdarg.h>
#include <string.h>
static char dig[10] = {'0','1','2','3','4','5','6','7','8','9' };
/*--------------------------------------------------------------------
** mrt_sprintf() - very simple sprintf funciton to format output.
** returns strlen(message).
*/
int mrt_sprintf( char * message, int msg_len, const char *format,...)
{
char buf[128];
int pos = 0;
va_list args;
va_start(args,format);
while (*format && (pos < msg_len-2)) {
if (*format == '%'){
char *b;
++format;
switch(*format) {
case 's':{
char *s;
s = va_arg(args,char *);
sprintf(buf,"%s",s);
}break;
case 'd':{
int d;
d = va_arg(args,int);
sprintf(buf,"%d",d);
}break;
case 'x':{
int d;
d = va_arg(args,int);
sprintf(buf,"%x",d);
}break;
case 'g':{
double f;
int fi, f0, f1, f2;
char c;
f = va_arg(args,double);
if (f <0){
c = '-';
f = -f;
}else{
c = ' ';
}
fi = (int)f; // integer portion.
// decmial portion...get index for upto 3 decmial places.
f = f - fi;
f0 = f*10; f0 %= 10;
f1 = f*100; f1 %= 10;
f2 = f*1000; f2 %= 10;
sprintf(buf,"%c%d.%c%c%c", c, fi, dig[f0], dig[f1], dig[f2]);
}break;
default:
sprintf(buf,"(Unsupported format %c)",*format);
}
++format;
b = buf;
while (*b && pos < msg_len-2){
message[pos] = *b;
++b;
++pos;
}
}else{
message[pos] = *format;
++pos;
++format;
}
}
message[pos] = 0;
va_end(args);
return pos;
}
/** done **/