forked from github/server
converting output to stdio for windows CP1252
This commit is contained in:
parent
81de21fcb8
commit
c2c276e0d3
8 changed files with 240 additions and 8 deletions
|
@ -1228,7 +1228,7 @@ write_reports(faction * f, time_t ltime)
|
|||
report_type * rtype = report_types;
|
||||
|
||||
errno = 0;
|
||||
printf("Reports for %s:", factionname(f));
|
||||
log_stdio(stdout, "Reports for %s:", factionname(f));
|
||||
fflush(stdout);
|
||||
|
||||
for (;rtype!=NULL;rtype=rtype->next) {
|
||||
|
|
|
@ -511,7 +511,7 @@ factionorders(void)
|
|||
if (f!=NULL) {
|
||||
const char * pass = getstrtoken();
|
||||
if (quiet==0) {
|
||||
printf(" %4s;", factionid(f));
|
||||
log_stdio(stdout, " %4s;", factionid(f));
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
|
@ -1363,7 +1363,7 @@ readfaction(FILE * F, int encoding)
|
|||
xrds(F, &f->name, encoding);
|
||||
xrds(F, &f->banner, encoding);
|
||||
|
||||
if (quiet==0) printf(" - Lese Partei %s (%s)\n", f->name, factionid(f));
|
||||
if (quiet==0) log_stdio(stdout, " - Lese Partei %s (%s)\n", f->name, factionid(f));
|
||||
|
||||
rds(F, &email);
|
||||
if (set_email(&f->email, email)!=0) {
|
||||
|
|
|
@ -11,6 +11,7 @@ without prior permission by the authors of Eressea.
|
|||
*/
|
||||
#include <config.h>
|
||||
#include "log.h"
|
||||
#include "unicode.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -20,8 +21,14 @@ without prior permission by the authors of Eressea.
|
|||
|
||||
/* TODO: set from external function */
|
||||
int log_flags = LOG_FLUSH|LOG_CPERROR|LOG_CPWARNING;
|
||||
#ifdef STDIO_CP
|
||||
static int stdio_codepage = STDIO_CP;
|
||||
#else
|
||||
static int stdio_codepage = 0;
|
||||
#endif
|
||||
static FILE * logfile;
|
||||
|
||||
#define MAXLENGTH 4096 /* because I am lazy, CP437 output is limited to this many chars */
|
||||
void
|
||||
log_flush(void)
|
||||
{
|
||||
|
@ -36,6 +43,32 @@ log_puts(const char * str)
|
|||
fputs(str, logfile);
|
||||
}
|
||||
|
||||
static int
|
||||
cp_convert(const char * format, char * buffer, size_t length, int codepage)
|
||||
{
|
||||
/* when console output on MSDOS, convert to codepage */
|
||||
const char * input = format;
|
||||
char * pos = buffer;
|
||||
|
||||
while (pos+1<buffer+length && *input) {
|
||||
size_t length = 0;
|
||||
int result = 0;
|
||||
if (codepage==437) {
|
||||
result = unicode_utf8_to_cp437(pos, input, &length);
|
||||
} else if (codepage==1252) {
|
||||
result = unicode_utf8_to_cp1252(pos, input, &length);
|
||||
}
|
||||
if (result!=0) {
|
||||
*pos = 0; /* just in case caller ignores our return value */
|
||||
return result;
|
||||
}
|
||||
++pos;
|
||||
input+=length;
|
||||
}
|
||||
*pos = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
log_printf(const char * format, ...)
|
||||
{
|
||||
|
@ -49,6 +82,34 @@ log_printf(const char * format, ...)
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
log_stdio(FILE * io, const char * format, ...)
|
||||
{
|
||||
va_list marker;
|
||||
if (!logfile) logfile = stderr;
|
||||
va_start(marker, format);
|
||||
if (stdio_codepage) {
|
||||
char buffer[MAXLENGTH];
|
||||
char converted[MAXLENGTH];
|
||||
|
||||
vsnprintf(buffer, sizeof(buffer), format, marker);
|
||||
if (cp_convert(buffer, converted, MAXLENGTH, stdio_codepage)==0) {
|
||||
fputs(converted, stderr);
|
||||
} else {
|
||||
/* fall back to non-converted output */
|
||||
va_end(marker);
|
||||
va_start(marker, format);
|
||||
vfprintf(io, format, marker);
|
||||
}
|
||||
} else {
|
||||
vfprintf(io, format, marker);
|
||||
}
|
||||
va_end(marker);
|
||||
if (log_flags & LOG_FLUSH) {
|
||||
log_flush();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
log_open(const char * filename)
|
||||
{
|
||||
|
@ -116,7 +177,22 @@ _log_warn(const char * format, ...)
|
|||
va_list marker;
|
||||
fputs("WARNING: ", stderr);
|
||||
va_start(marker, format);
|
||||
vfprintf(stderr, format, marker);
|
||||
if (stdio_codepage) {
|
||||
char buffer[MAXLENGTH];
|
||||
char converted[MAXLENGTH];
|
||||
|
||||
vsnprintf(buffer, sizeof(buffer), format, marker);
|
||||
if (cp_convert(buffer, converted, MAXLENGTH, stdio_codepage)==0) {
|
||||
fputs(converted, stderr);
|
||||
} else {
|
||||
/* fall back to non-converted output */
|
||||
va_end(marker);
|
||||
va_start(marker, format);
|
||||
vfprintf(stderr, format, marker);
|
||||
}
|
||||
} else {
|
||||
vfprintf(stderr, format, marker);
|
||||
}
|
||||
va_end(marker);
|
||||
}
|
||||
if (log_flags & LOG_FLUSH) {
|
||||
|
@ -143,9 +219,25 @@ _log_error(const char * format, ...)
|
|||
if (logfile!=stderr) {
|
||||
if (log_flags & LOG_CPERROR) {
|
||||
va_list marker;
|
||||
|
||||
fputs("ERROR: ", stderr);
|
||||
va_start(marker, format);
|
||||
vfprintf(stderr, format, marker);
|
||||
if (stdio_codepage) {
|
||||
char buffer[MAXLENGTH];
|
||||
char converted[MAXLENGTH];
|
||||
|
||||
vsnprintf(buffer, sizeof(buffer), format, marker);
|
||||
if (cp_convert(buffer, converted, MAXLENGTH, stdio_codepage)==0) {
|
||||
fputs(converted, stderr);
|
||||
} else {
|
||||
/* fall back to non-converted output */
|
||||
va_end(marker);
|
||||
va_start(marker, format);
|
||||
vfprintf(stderr, format, marker);
|
||||
}
|
||||
} else {
|
||||
vfprintf(stderr, format, marker);
|
||||
}
|
||||
va_end(marker);
|
||||
}
|
||||
log_flush();
|
||||
|
@ -168,7 +260,22 @@ _log_info(unsigned int flag, const char * format, ...)
|
|||
if (log_flags & flag) {
|
||||
fprintf(stderr, "INFO[%u]: ", flag);
|
||||
va_start(marker, format);
|
||||
vfprintf(stderr, format, marker);
|
||||
if (stdio_codepage) {
|
||||
char buffer[MAXLENGTH];
|
||||
char converted[MAXLENGTH];
|
||||
|
||||
vsnprintf(buffer, sizeof(buffer), format, marker);
|
||||
if (cp_convert(buffer, converted, MAXLENGTH, stdio_codepage)==0) {
|
||||
fputs(converted, stderr);
|
||||
} else {
|
||||
/* fall back to non-converted output */
|
||||
va_end(marker);
|
||||
va_start(marker, format);
|
||||
vfprintf(stderr, format, marker);
|
||||
}
|
||||
} else {
|
||||
vfprintf(stderr, format, marker);
|
||||
}
|
||||
va_end(marker);
|
||||
}
|
||||
if (log_flags & LOG_FLUSH) {
|
||||
|
|
|
@ -20,6 +20,7 @@ extern "C" {
|
|||
extern void log_puts(const char * str);
|
||||
extern void log_close(void);
|
||||
extern void log_flush(void);
|
||||
extern void log_stdio(FILE * io, const char * format, ...);
|
||||
|
||||
#define log_warning(x) _log_warn x
|
||||
#define log_error(x) _log_error x
|
||||
|
|
|
@ -199,3 +199,123 @@ unicode_utf8_to_ucs4(wint_t *ucs4_character, const char *utf8_string,
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Convert a UTF-8 encoded character to CP437. */
|
||||
int
|
||||
unicode_utf8_to_cp437(char *cp_character, const char *utf8_string,
|
||||
size_t *length)
|
||||
{
|
||||
wint_t ucs4_character;
|
||||
int result;
|
||||
|
||||
result = unicode_utf8_to_ucs4(&ucs4_character, utf8_string, length);
|
||||
if (result!=0) {
|
||||
/* pass decoding characters upstream */
|
||||
return result;
|
||||
}
|
||||
|
||||
if (ucs4_character<0x7F) {
|
||||
*cp_character = (char)ucs4_character;
|
||||
} else {
|
||||
struct { wint_t ucs4; unsigned char cp437; } xref[160] = {
|
||||
{0x00A0, 255}, {0x00A1, 173}, {0x00A2, 155}, {0x00A3, 156},
|
||||
{0x00A5, 157}, {0x00A7, 21}, {0x00AA, 166}, {0x00AB, 174},
|
||||
{0x00AC, 170}, {0x00B0, 248}, {0x00B1, 241}, {0x00B2, 253},
|
||||
{0x00B5, 230}, {0x00B6, 20}, {0x00B7, 250}, {0x00BA, 167},
|
||||
{0x00BB, 175}, {0x00BC, 172}, {0x00BD, 171}, {0x00BF, 168},
|
||||
{0x00C4, 142}, {0x00C5, 143}, {0x00C6, 146}, {0x00C7, 128},
|
||||
{0x00C9, 144}, {0x00D1, 165}, {0x00D6, 153}, {0x00DC, 154},
|
||||
{0x00DF, 225}, {0x00E0, 133}, {0x00E1, 160}, {0x00E2, 131},
|
||||
{0x00E4, 132}, {0x00E5, 134}, {0x00E6, 145}, {0x00E7, 135},
|
||||
{0x00E8, 138}, {0x00E9, 130}, {0x00EA, 136}, {0x00EB, 137},
|
||||
{0x00EC, 141}, {0x00ED, 161}, {0x00EE, 140}, {0x00EF, 139},
|
||||
{0x00F1, 164}, {0x00F2, 149}, {0x00F3, 162}, {0x00F4, 147},
|
||||
{0x00F6, 148}, {0x00F7, 246}, {0x00F9, 151}, {0x00FA, 163},
|
||||
{0x00FB, 150}, {0x00FC, 129}, {0x00FF, 152}, {0x0192, 159},
|
||||
{0x0393, 226}, {0x0398, 233}, {0x03A3, 228}, {0x03A6, 232},
|
||||
{0x03A9, 234}, {0x03B1, 224}, {0x03B4, 235}, {0x03B5, 238},
|
||||
{0x03C0, 227}, {0x03C3, 229}, {0x03C4, 231}, {0x03C6, 237},
|
||||
{0x2022, 7}, {0x203C, 19}, {0x207F, 252}, {0x20A7, 158},
|
||||
{0x2190, 27}, {0x2191, 24}, {0x2192, 26}, {0x2193, 25},
|
||||
{0x2194, 29}, {0x2195, 18}, {0x21A8, 23}, {0x2219, 249},
|
||||
{0x221A, 251}, {0x221E, 236}, {0x221F, 28}, {0x2229, 239},
|
||||
{0x2248, 247}, {0x2261, 240}, {0x2264, 243}, {0x2265, 242},
|
||||
{0x2302, 127}, {0x2310, 169}, {0x2320, 244}, {0x2321, 245},
|
||||
{0x2500, 196}, {0x2502, 179}, {0x250C, 218}, {0x2510, 191},
|
||||
{0x2514, 192}, {0x2518, 217}, {0x251C, 195}, {0x2524, 180},
|
||||
{0x252C, 194}, {0x2534, 193}, {0x253C, 197}, {0x2550, 205},
|
||||
{0x2551, 186}, {0x2552, 213}, {0x2553, 214}, {0x2554, 201},
|
||||
{0x2555, 184}, {0x2556, 183}, {0x2557, 187}, {0x2558, 212},
|
||||
{0x2559, 211}, {0x255A, 200}, {0x255B, 190}, {0x255C, 189},
|
||||
{0x255D, 188}, {0x255E, 198}, {0x255F, 199}, {0x2560, 204},
|
||||
{0x2561, 181}, {0x2562, 182}, {0x2563, 185}, {0x2564, 209},
|
||||
{0x2565, 210}, {0x2566, 203}, {0x2567, 207}, {0x2568, 208},
|
||||
{0x2569, 202}, {0x256A, 216}, {0x256B, 215}, {0x256C, 206},
|
||||
{0x2580, 223}, {0x2584, 220}, {0x2588, 219}, {0x258C, 221},
|
||||
{0x2590, 222}, {0x2591, 176}, {0x2592, 177}, {0x2593, 178},
|
||||
{0x25A0, 254}, {0x25AC, 22}, {0x25B2, 30}, {0x25BA, 16},
|
||||
{0x25BC, 31}, {0x25C4, 17}, {0x25CB, 9}, {0x25D8, 8},
|
||||
{0x25D9, 10}, {0x263A, 1}, {0x263B, 2}, {0x263C, 15},
|
||||
{0x2640, 12}, {0x2642, 11}, {0x2660, 6}, {0x2663, 5},
|
||||
{0x2665, 3}, {0x2666, 4}, {0x266A, 13}, {0x266B, 14}
|
||||
};
|
||||
int l=0, r=160;
|
||||
while (l!=r) {
|
||||
int m = (l+r)/2;
|
||||
if (xref[m].ucs4==ucs4_character) {
|
||||
*cp_character = (char)xref[m].cp437;
|
||||
break;
|
||||
}
|
||||
else if (xref[m].ucs4<ucs4_character) l = m;
|
||||
else r = m;
|
||||
}
|
||||
if (l==r) {
|
||||
*cp_character = '?';
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Convert a UTF-8 encoded character to CP1252. */
|
||||
int
|
||||
unicode_utf8_to_cp1252(char *cp_character, const char *utf8_string,
|
||||
size_t *length)
|
||||
{
|
||||
wint_t ucs4_character;
|
||||
int result;
|
||||
|
||||
result = unicode_utf8_to_ucs4(&ucs4_character, utf8_string, length);
|
||||
if (result!=0) {
|
||||
/* pass decoding characters upstream */
|
||||
return result;
|
||||
}
|
||||
|
||||
if (ucs4_character<=0x7F || ucs4_character>=0xA0) {
|
||||
*cp_character = (char)ucs4_character;
|
||||
} else {
|
||||
struct { wint_t ucs4; unsigned char cp; } xref[] = {
|
||||
{0x20ac, 0x80}, {0x0081, 0x81}, {0x201a, 0x82}, {0x0192, 0x83},
|
||||
{0x201e, 0x84}, {0x2026, 0x85}, {0x2020, 0x86}, {0x2021, 0x87},
|
||||
{0x02c6, 0x88}, {0x2030, 0x89}, {0x0160, 0x8a}, {0x2039, 0x8b},
|
||||
{0x0152, 0x8c}, {0x008d, 0x8d}, {0x017d, 0x8e}, {0x008f, 0x8f},
|
||||
{0x0090, 0x90}, {0x2018, 0x91}, {0x2019, 0x92}, {0x201c, 0x93},
|
||||
{0x201d, 0x94}, {0x2022, 0x95}, {0x2013, 0x96}, {0x2014, 0x97},
|
||||
{0x02dc, 0x98}, {0x2122, 0x99}, {0x0161, 0x9a}, {0x203a, 0x9b},
|
||||
{0x0153, 0x9c}, {0x009d, 0x9d}, {0x017e, 0x9e}, {0x0178, 0x9f}
|
||||
};
|
||||
int l=0, r=sizeof(xref)/sizeof(xref[0]);
|
||||
while (l!=r) {
|
||||
int m = (l+r)/2;
|
||||
if (xref[m].ucs4==ucs4_character) {
|
||||
*cp_character = (char)xref[m].cp;
|
||||
break;
|
||||
}
|
||||
else if (xref[m].ucs4<ucs4_character) l = m;
|
||||
else r = m;
|
||||
}
|
||||
if (l==r) {
|
||||
*cp_character = '?';
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ extern "C" {
|
|||
|
||||
#include <wchar.h>
|
||||
#define USE_UNICODE
|
||||
extern int unicode_utf8_to_cp437(char *ucs4_character, const char *utf8_string, size_t *length);
|
||||
extern int unicode_utf8_to_cp1252(char *ucs4_character, const char *utf8_string, size_t *length);
|
||||
extern int unicode_utf8_to_ucs4(wint_t *ucs4_character, const char *utf8_string, size_t *length);
|
||||
extern int unicode_utf8_strcasecmp(const char * a, const char * b);
|
||||
extern int unicode_latin1_to_utf8(unsigned char *out, size_t *outlen, const unsigned char *in, size_t *inlen);
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#define CONFIG_H
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define STDIO_CP 1252 /* log.c, convert to console character set */
|
||||
# pragma warning (disable: 4201 4214 4514 4115 4711)
|
||||
# pragma warning(disable: 4056)
|
||||
/* warning C4056: overflow in floating point constant arithmetic */
|
||||
|
|
|
@ -9,8 +9,8 @@ function run_scripts()
|
|||
"hse/portals.lua",
|
||||
"hse/stats.lua"
|
||||
}
|
||||
for index in scripts do
|
||||
local script = scriptpath .. "/" .. scripts[index]
|
||||
for index, value in pairs(scripts) do
|
||||
local script = scriptpath .. "/" .. value
|
||||
print("- loading " .. script)
|
||||
if pcall(dofile, script)==0 then
|
||||
print("Could not load " .. script)
|
||||
|
@ -30,6 +30,7 @@ function process(orders)
|
|||
print("could not read game")
|
||||
return -1
|
||||
end
|
||||
init_summary()
|
||||
|
||||
-- run the turn:
|
||||
read_orders(orders)
|
||||
|
|
Loading…
Reference in a new issue