c++ioswinapiunicodetchar

How to port Windows C++ that handles unicode with tchar.h to iOS app


I have some c++ code that I need to integrate into an iOS app. The windows c++ code handles unicode using tchar.h. I have made the following defines for iOS:

#include <wchar.h>
#define _T(x)     x
#define TCHAR       char
#define _tremove    unlink
#define _stprintf   sprintf
#define _sntprintf vsnprintf
#define _tcsncpy wcsncpy
#define _tcscpy wcscpy
#define _tcscmp wcscmp
#define _tcsrchr wcsrchr
#define _tfopen fopen

When trying to build the app many of these are either missing (ex. wcscpy) or have the wrong arguments. The coder responsible for the c++ code said I should use char instead of wchar so I defined TCHAR as char. Does anyone have a clue as to how this should be done?


Solution

  • The purpose of TCHAR (and FYI, it is _TCHAR in the C runtime, TCHAR is for the Win32 API) is to allow code to switch between either char or wchar_t APIs at compile time, but your defines are mixing them together. The wcs functions are for wchar_t, so you need to change those defines to the char counterparts, to match your other char-based defines:

    #define _tcsncpy strncpy
    #define _tcscpy strcpy
    #define _tcscmp strcmp
    #define _tcsrchr strrchr
    

    Also, you are mapping _sntprintf to the wrong C function. It needs to be mapped to snprintf() instead of vsnprintf():

    #define _sntprintf snprintf
    

    snprintf() and vsnprintf() are declared very differently:

    int snprintf ( char * s, size_t n, const char * format, ... );
    int vsnprintf (char * s, size_t n, const char * format, va_list arg );
    

    Which is likely why you are getting "wrong arguments" errors.