1351 char *load_message_from_file(const char *filename) |
1352 char *load_message_from_file(const char *filename) |
1352 { |
1353 { |
1353 FILE *fd; |
1354 FILE *fd; |
1354 struct stat buf; |
1355 struct stat buf; |
1355 char *msgbuf, *msgbuf_utf8; |
1356 char *msgbuf, *msgbuf_utf8; |
1356 char *eol; |
1357 char *p; |
|
1358 char *next_utf8_char; |
1357 |
1359 |
1358 fd = fopen(filename, "r"); |
1360 fd = fopen(filename, "r"); |
1359 |
1361 |
1360 if (!fd || fstat(fileno(fd), &buf)) { |
1362 if (!fd || fstat(fileno(fd), &buf)) { |
1361 scr_LogPrint(LPRINT_LOGNORM, "Cannot open message file (%s)", filename); |
1363 scr_LogPrint(LPRINT_LOGNORM, "Cannot open message file (%s)", filename); |
1372 |
1374 |
1373 msgbuf = g_new0(char, HBB_BLOCKSIZE); |
1375 msgbuf = g_new0(char, HBB_BLOCKSIZE); |
1374 fread(msgbuf, HBB_BLOCKSIZE-1, 1, fd); |
1376 fread(msgbuf, HBB_BLOCKSIZE-1, 1, fd); |
1375 fclose(fd); |
1377 fclose(fd); |
1376 |
1378 |
|
1379 next_utf8_char = msgbuf; |
|
1380 |
1377 // Strip trailing newlines |
1381 // Strip trailing newlines |
1378 for (eol = msgbuf ; *eol ; eol++) |
1382 for (p = msgbuf ; *p ; p++) { |
1379 ; |
1383 // Check there is no binary data. It must be a *message* file! |
1380 if (eol > msgbuf) |
1384 if (utf8_mode) { |
1381 eol--; |
1385 if (p == next_utf8_char) { |
1382 while (eol > msgbuf && *eol == '\n') |
1386 if (!iswprint(get_char(p)) && *p != '\n') |
1383 *eol-- = 0; |
1387 break; |
|
1388 next_utf8_char = next_char(p); |
|
1389 } |
|
1390 } else { |
|
1391 unsigned char sc = *p; |
|
1392 if (!iswprint(sc) && sc != '\n') |
|
1393 break; |
|
1394 } |
|
1395 } |
|
1396 |
|
1397 if (*p) { // We're not at the End Of Line... |
|
1398 scr_LogPrint(LPRINT_LOGNORM, "Message file contains " |
|
1399 "invalid characters (%s)", filename); |
|
1400 g_free(msgbuf); |
|
1401 return NULL; |
|
1402 } |
|
1403 |
|
1404 // p is now at the EOL |
|
1405 if (p > msgbuf) |
|
1406 p--; |
|
1407 while (p > msgbuf && *p == '\n') |
|
1408 *p-- = 0; |
1384 |
1409 |
1385 // It could be empty, once the trailing newlines are gone |
1410 // It could be empty, once the trailing newlines are gone |
1386 if (eol == msgbuf && *eol == '\n') { |
1411 if (p == msgbuf && *p == '\n') { |
1387 scr_LogPrint(LPRINT_LOGNORM, "Message file is empty (%s)", filename); |
1412 scr_LogPrint(LPRINT_LOGNORM, "Message file is empty (%s)", filename); |
1388 g_free(msgbuf); |
1413 g_free(msgbuf); |
1389 return NULL; |
1414 return NULL; |
1390 } |
1415 } |
1391 |
1416 |