[En-Nut-Discussion] unix port & header files

Harald Kipp harald.kipp at egnite.de
Thu Mar 4 11:04:57 CET 2004


Hi Matthias,

At 09:30 04.03.2004 +0100, you wrote:

>how should the compile distinguish between the nut/os version
>of fopen and the native fopen call.

Mmh...Deep Thought would say "Tricky".

One idea: Try a stdio wrapper.

1. All Nut/OS modules will use an include file, which defines
#define fopen(blah) PREFIX_fopen(blah)
#define fprint(blah) PREFIX_fprint(blah)
and so on, plus
#define FILE PREFIX_FILE

2. All application code will use an include file, which defines

#define fopen(blah) WRAPPED_fopen(blah)
#define fprint(blah) WRAPPED_fprint(blah)
and so on, plus
#define FILE WRAPPED_FILE

and

typedef struct {
   PREFIX_FILE *emul;
   FILE *native;
} WRAPPED_FILE;


3. The wrapper module will not use any of those defines and
thus use the native calls, but implement the wrapped ones.

WRAPPED_FILE *WRAPPED_fopen(char *name, etc)
{
    WRAPPED_FILE *fp;
    if(strncmp(name, "bt0", 3) || strncmp(name, "ppp", 3).... {
       fp->emul = PREFIX_fopen(name, etc);
       fp->native = (FILE *)-1;
    }
    else {
       fp->native = fopen(name, etc);
    }
    return fp;
}

int WRAPPED_fprintf(WRPFILE *fp, etc)
{
   if(fp->native == (FILE *)-1)
     return PREFIX_fprint(fp->emul, etc);
   return fprint(fp->native, etc);
}

The wrapper module must, of course, know all wrapped and prefixed
prototypes and data types. Use #define and #undef.

I may have overlooked something, but generally it should work.

Another option may be to rewrite the Nut/OS stdio. But
the advantage with the procedure above is, that you can
take over any new Nut/OS version without modification.

Your application code and the Nut/OS bluetooth extensions
will not notice the wrapping and look like normal, portable
C programming, at least on Linux. The drag with Winsock is,
that it doesn't support stdio on sockets.

Harald




More information about the En-Nut-Discussion mailing list