質問

This question is specific to Visual C++ (you may assume Visual C++ 2005 and later).

I would like to create glue code for a program from unixoid systems (FreeBSD in particular) in order build and run on Win32 with a minimum of changes to the original .c file. Now, most of this is straightforward, but now I ran into an issue. I am using the tchar.h header and TCHAR/_TCHAR and need to create glue code for the err and errx calls (see err(3)) in the original code. Bear with me, even if you don't agree that code using tchar.h should still be written.

The calls to err and errx take roughly two forms, and this is where the problem occurs:

err(1, "some string with %d format specifiers and %s such", ...)
/* or */
err(1, NULL, ...)

The latter would output the error stored in errno (using strerror).

Now, the question is, is there any way to write a generic macro that can take both NULL and a string literal? I do not have to (and will not) care about variables getting passed as the second parameter, only NULL and literal strings.

Of course my naive initial approach didn't account for fmt passed as NULL (using variadic macros!):

#define err(eval, fmt, ...) my_err(eval, _T(fmt), __VA_ARGS__)

Right now I don't have any ideas how to achieve this with macros, because it would require a kind of mix of compile-time and runtime conditionals that I cannot imagine at the moment. So I am looking for an authoritative answer whether this is conceivable at all or not.

The method I am resorting to right now - lacking a better approach - is to write a wrapper function that accepts, just like err and errx, a (const) char * and then converting that to wchar_t * if compiled with _UNICODE defined. This should work, assuming that the caller passes a _TCHAR* string for the variable arguments after the fmt (which is a sane assumption in my context). Otherwise I'd also have to convert %s to %hs inside the format string, to handle "ANSI" strings, as MS calls them.

役に立ちましたか?

解決

Here's one solution:

#define _WIN32_WINNT 0x0502

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#ifdef _UNICODE

#define LNULL NULL
#define fn(x) myfn(L ## x)

#else

#define fn(x) myfn(x)

#endif

void myfn(TCHAR * str)
{
    if (str == NULL) _tprintf(_T("<NULL>")); else _tprintf(str);
    _tprintf(_T("\n"));
}

int main(int argc, char ** argv)
{
    fn("hello");
    fn(NULL);
    return 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top