diff --git a/ANSI.c b/ANSI.c index 8b2b991..a57574e 100644 --- a/ANSI.c +++ b/ANSI.c @@ -69,94 +69,14 @@ #include #include #include -#include #include #include "injdll.h" +#include "debugstr.h" #define lenof(array) (sizeof(array)/sizeof(*(array))) #define isdigit(c) ('0' <= (c) && (c) <= '9') -// ========== Auxiliary debug function - -#define MYDEBUG 0 // no debugging -//#define MYDEBUG 1 // use OutputDebugString -//#define MYDEBUG 2 // use %temp%\ansicon.log - -#if (MYDEBUG > 0) -#if (MYDEBUG > 1) -char tempfile[MAX_PATH]; -#endif -void DEBUGSTR( LPTSTR szFormat, ... ) // sort of OutputDebugStringf -{ - TCHAR szBuffer[1024], szEscape[1024]; - va_list pArgList; - va_start( pArgList, szFormat ); - _vsnwprintf( szBuffer, lenof(szBuffer), szFormat, pArgList ); - va_end( pArgList ); - - szFormat = szBuffer; - if (*szFormat == '\\') - { - BOOL first = TRUE; - LPTSTR pos = szEscape; - while (*++szFormat != '\0' && pos < szEscape + lenof(szEscape) - 4) - { - if (*szFormat < 32) - { - *pos++ = '\\'; - switch (*szFormat) - { - case '\b': *pos++ = 'b'; break; - case '\t': *pos++ = 't'; break; - case '\r': *pos++ = 'r'; break; - case '\n': *pos++ = 'n'; break; - case 27 : *pos++ = 'e'; break; - default: - pos += wprintf( pos, L"%.*o", - (szFormat[1] >= '0' && szFormat[1] <= '7') ? 3 : 1, - *szFormat ); - } - } - else if (*szFormat == '"') - { - if (first) - first = FALSE; - else if (szFormat[1] == '\0') - ; - else - *pos++ = '\\'; - *pos++ = '"'; - } - else - { - *pos++ = *szFormat; - } - } - *pos = '\0'; - szFormat = szEscape; - } -#if (MYDEBUG > 1) - { - FILE* file = fopen( tempfile, "at" ); // _fmode might be binary - if (file != NULL) - { - fwprintf( file, L"%s\n", szFormat ); - fclose( file ); - } - } -#else - OutputDebugString( szFormat ); -#endif -} -#else -#if defined(_MSC_VER) && _MSC_VER <= 1400 -void DEBUGSTR() { } -#else -#define DEBUGSTR(...) -#endif -#endif - // ========== Global variables and constants // Macro for adding pointers/DWORDs together without C arithmetic interfering diff --git a/ansicon.c b/ansicon.c index 105d68a..42da6a2 100644 --- a/ansicon.c +++ b/ansicon.c @@ -64,6 +64,7 @@ #include #include #include "injdll.h" +#include "debugstr.h" #define lenof(array) (sizeof(array)/sizeof(*(array))) diff --git a/debugstr.c b/debugstr.c new file mode 100644 index 0000000..244cbdb --- /dev/null +++ b/debugstr.c @@ -0,0 +1,81 @@ +/* + debugstr.c - Auxiliary debug functionality. +*/ + +#ifndef UNICODE +# define UNICODE +#endif + +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include "debugstr.h" + +#define lenof(array) (sizeof(array)/sizeof(*(array))) + +#if (MYDEBUG > 0) +void DEBUGSTR( LPTSTR szFormat, ... ) // sort of OutputDebugStringf +{ + TCHAR szBuffer[1024], szEscape[1024]; + va_list pArgList; + va_start( pArgList, szFormat ); + _vsnwprintf( szBuffer, lenof(szBuffer), szFormat, pArgList ); + va_end( pArgList ); + + szFormat = szBuffer; + if (*szFormat == '\\') + { + BOOL first = TRUE; + LPTSTR pos = szEscape; + while (*++szFormat != '\0' && pos < szEscape + lenof(szEscape) - 4) + { + if (*szFormat < 32) + { + *pos++ = '\\'; + switch (*szFormat) + { + case '\b': *pos++ = 'b'; break; + case '\t': *pos++ = 't'; break; + case '\r': *pos++ = 'r'; break; + case '\n': *pos++ = 'n'; break; + case 27 : *pos++ = 'e'; break; + default: + pos += wprintf( pos, L"%.*o", + (szFormat[1] >= '0' && szFormat[1] <= '7') ? 3 : 1, + *szFormat ); + } + } + else if (*szFormat == '"') + { + if (first) + first = FALSE; + else if (szFormat[1] == '\0') + ; + else + *pos++ = '\\'; + *pos++ = '"'; + } + else + { + *pos++ = *szFormat; + } + } + *pos = '\0'; + szFormat = szEscape; + } +#if (MYDEBUG > 1) + { + FILE* file = fopen( tempfile, "at" ); // _fmode might be binary + if (file != NULL) + { + fwprintf( file, L"%s\n", szFormat ); + fclose( file ); + } + } +#else + OutputDebugString( szFormat ); +#endif +} +#endif diff --git a/debugstr.h b/debugstr.h new file mode 100644 index 0000000..a366167 --- /dev/null +++ b/debugstr.h @@ -0,0 +1,18 @@ +// ========== Auxiliary debug function + +#define MYDEBUG 0 // no debugging +//#define MYDEBUG 1 // use OutputDebugString +//#define MYDEBUG 2 // use %temp%\ansicon.log + +#if (MYDEBUG > 0) +# if (MYDEBUG > 1) + char tempfile[MAX_PATH]; +# endif + void DEBUGSTR( LPTSTR szFormat, ... ); +#else +# if defined(_MSC_VER) && _MSC_VER <= 1400 + void DEBUGSTR() { } +# else +# define DEBUGSTR(...) +# endif +#endif diff --git a/makefile b/makefile index 605f380..6fbefc2 100644 --- a/makefile +++ b/makefile @@ -32,19 +32,19 @@ ansicon64: x64 x64/ansicon.exe x64/ANSI64.dll x64/ANSI32.dll x64/ANSI-LLW.exe x86: mkdir x86 -x86/ansicon.exe: x86/ansicon.o x86/proctype.o x86/injdll32.o x86/ansiconv.o +x86/ansicon.exe: x86/ansicon.o x86/debugstr.o x86/proctype.o x86/injdll32.o x86/ansiconv.o $(CC) -m32 $+ -s -o $@ -x86/ANSI32.dll: x86/ANSI.o x86/proctype.o x86/injdll32.o x86/ansiv.o +x86/ANSI32.dll: x86/ANSI.o x86/debugstr.o x86/proctype.o x86/injdll32.o x86/ansiv.o $(CC) -m32 $+ -s -o $@ -mdll -Wl,-shared x64: mkdir x64 -x64/ansicon.exe: x64/ansicon.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiconv.o +x64/ansicon.exe: x64/ansicon.o x64/debugstr.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiconv.o $(CC) -m64 $+ -s -o $@ -x64/ANSI64.dll: x64/ANSI.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiv.o +x64/ANSI64.dll: x64/ANSI.o x64/debugstr.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiv.o $(CC) -m64 $+ -s -o $@ -mdll -Wl,-shared x64/ANSI32.dll: x86/ANSI32.dll