From f69004ab25faf63da3e33ce50485ecf8702c5b0d Mon Sep 17 00:00:00 2001 From: dashodanger Date: Mon, 10 Jun 2024 21:23:59 -0600 Subject: [PATCH] Stop usin' streams --- CMakeLists.txt | 1 - source/bsp.cc | 1 - source/bsp_level.cc | 1 - source/bsp_misc.cc | 1 - source/bsp_node.cc | 1 - source/bsp_utility.cc | 111 ------------------------- source/bsp_utility.h | 37 --------- source/bsp_wad.cc | 1 - source/dm_extra.cc | 3 + source/g_doom.cc | 4 +- source/lib_util.cc | 83 ++++++++++++++++++- source/lib_util.h | 10 +++ source/lib_wad.cc | 106 ++++++++++++------------ source/lib_wad.h | 24 +----- source/m_addons.cc | 8 +- source/m_addons.h | 3 +- source/m_cookie.cc | 59 ++++++++----- source/m_cookie.h | 4 +- source/m_dialog.cc | 43 ++++++---- source/m_lua.cc | 11 ++- source/m_manage.cc | 2 +- source/m_options.cc | 100 +++++++++++----------- source/m_theme.cc | 105 +++++++++++++----------- source/m_trans.cc | 54 ++++++++---- source/main.cc | 54 ++++++------ source/main.h | 16 ++-- source/poly.cc | 1 + source/poly_util.cc | 21 ----- source/poly_util.h | 3 - source/sys_debug.cc | 187 +++++++++++++++++++++++++++++++++++++----- source/sys_debug.h | 60 +++----------- source/ui_game.cc | 2 - source/ui_module.cc | 10 +-- 33 files changed, 602 insertions(+), 525 deletions(-) delete mode 100644 source/bsp_utility.cc delete mode 100644 source/bsp_utility.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e90061db..41a813f76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,6 @@ set(OBSIDIAN_SOURCE_FILES source/bsp_level.cc source/bsp_misc.cc source/bsp_node.cc - source/bsp_utility.cc source/bsp_wad.cc source/bsp.cc source/csg_bsp.cc diff --git a/source/bsp.cc b/source/bsp.cc index a4e34bba3..555c70103 100644 --- a/source/bsp.cc +++ b/source/bsp.cc @@ -21,7 +21,6 @@ #include -#include "bsp_utility.h" #include "bsp_wad.h" #include "lib_util.h" #include "raw_def.h" diff --git a/source/bsp_level.cc b/source/bsp_level.cc index a915f9727..6ed4cec0f 100644 --- a/source/bsp_level.cc +++ b/source/bsp_level.cc @@ -24,7 +24,6 @@ #include #include "bsp_local.h" -#include "bsp_utility.h" #include "bsp_wad.h" #include "lib_parse.h" #include "miniz.h" diff --git a/source/bsp_misc.cc b/source/bsp_misc.cc index 5214dd2b2..85682613c 100644 --- a/source/bsp_misc.cc +++ b/source/bsp_misc.cc @@ -23,7 +23,6 @@ #include #include "bsp_local.h" -#include "bsp_utility.h" #include "bsp_wad.h" #include "raw_def.h" #include "sys_macro.h" diff --git a/source/bsp_node.cc b/source/bsp_node.cc index 8166fbaa3..a6f9a9adb 100644 --- a/source/bsp_node.cc +++ b/source/bsp_node.cc @@ -21,7 +21,6 @@ #include #include "bsp_local.h" -#include "bsp_utility.h" #include "bsp_wad.h" #include "sys_macro.h" diff --git a/source/bsp_utility.cc b/source/bsp_utility.cc deleted file mode 100644 index de79ca4ef..000000000 --- a/source/bsp_utility.cc +++ /dev/null @@ -1,111 +0,0 @@ -//------------------------------------------------------------------------ -// UTILITIES -//------------------------------------------------------------------------ -// -// Copyright (C) 2001-2018 Andrew Apted -// Copyright (C) 1997-2003 Andr� Majorel et al -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -//------------------------------------------------------------------------ - -#include "bsp_utility.h" - -#include "bsp_local.h" -#include "sys_macro.h" - -namespace ajbsp -{ - -//------------------------------------------------------------------------ -// MEMORY ALLOCATION -//------------------------------------------------------------------------ - -// -// Allocate memory with error checking. Zeros the memory. -// -void *UtilCalloc(int size) -{ - void *ret = calloc(1, size); - - if (!ret) - cur_info->FatalError("Out of memory (cannot allocate %d bytes)\n", size); - - return ret; -} - -// -// Reallocate memory with error checking. -// -void *UtilRealloc(void *old, int size) -{ - void *ret = realloc(old, size); - - if (!ret) - cur_info->FatalError("Out of memory (cannot reallocate %d bytes)\n", size); - - return ret; -} - -// -// Free the memory with error checking. -// -void UtilFree(void *data) -{ - if (data == NULL) - BugError("Trying to free a NULL pointer\n"); - - free(data); -} - -//------------------------------------------------------------------------ -// MATH STUFF -//------------------------------------------------------------------------ - -// -// rounds the value _up_ to the nearest power of two. -// -int RoundPOW2(int x) -{ - if (x <= 2) - return x; - - x--; - - for (int tmp = x >> 1; tmp; tmp >>= 1) - x |= tmp; - - return x + 1; -} - -// -// Compute angle of line from (0,0) to (dx,dy). -// Result is degrees, where 0 is east and 90 is north. -// -double ComputeAngle(double dx, double dy) -{ - double angle; - - if (dx == 0) - return (dy > 0) ? 90.0 : 270.0; - - angle = atan2((double)dy, (double)dx) * 180.0 / M_PI; - - if (angle < 0) - angle += 360.0; - - return angle; -} - -} // namespace ajbsp - -//--- editor settings --- -// vi:ts=4:sw=4:noexpandtab diff --git a/source/bsp_utility.h b/source/bsp_utility.h deleted file mode 100644 index 5e70f9dd4..000000000 --- a/source/bsp_utility.h +++ /dev/null @@ -1,37 +0,0 @@ -//------------------------------------------------------------------------ -// UTILITIES -//------------------------------------------------------------------------ -// -// Copyright (C) 2001-2013 Andrew Apted -// Copyright (C) 1997-2003 André Majorel et al -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -//------------------------------------------------------------------------ - -#pragma once - -namespace ajbsp -{ - -// memory allocation, guaranteed to not return NULL. -void *UtilCalloc(int size); -void *UtilRealloc(void *old, int size); -void UtilFree(void *data); - -// math stuff -int RoundPOW2(int x); -double ComputeAngle(double dx, double dy); - -} // namespace ajbsp - -//--- editor settings --- -// vi:ts=4:sw=4:noexpandtab diff --git a/source/bsp_wad.cc b/source/bsp_wad.cc index 060819499..172e02c0b 100644 --- a/source/bsp_wad.cc +++ b/source/bsp_wad.cc @@ -24,7 +24,6 @@ #include #include "bsp_local.h" -#include "bsp_utility.h" #include "lib_util.h" #include "sys_endian.h" #include "sys_macro.h" diff --git a/source/dm_extra.cc b/source/dm_extra.cc index 336f1e796..8656ffffa 100644 --- a/source/dm_extra.cc +++ b/source/dm_extra.cc @@ -19,6 +19,9 @@ // //------------------------------------------------------------------------ +#include +#include + #include "csg_main.h" #include "g_doom.h" #ifndef CONSOLE_ONLY diff --git a/source/g_doom.cc b/source/g_doom.cc index be8f3b687..bdb8d8da8 100644 --- a/source/g_doom.cc +++ b/source/g_doom.cc @@ -21,6 +21,8 @@ #include "g_doom.h" +#include + #include #include @@ -532,7 +534,7 @@ bool Doom::StartWAD(std::string filename) #ifndef CONSOLE_ONLY DLG_ShowError(_("Unable to create wad file:\n\n%s"), strerror(errno)); #else - StdOutPrintf(_("Unable to create wad file:\n\n%s"), strerror(errno)); + printf(_("Unable to create wad file:\n\n%s"), strerror(errno)); #endif return false; } diff --git a/source/lib_util.cc b/source/lib_util.cc index c072157f9..72a3fe2e5 100644 --- a/source/lib_util.cc +++ b/source/lib_util.cc @@ -568,7 +568,7 @@ void StringRemoveCRLF(std::string *str) { str->pop_back(); } - if (str->back() == '\r') + if (!str->empty() && str->back() == '\r') { str->pop_back(); } @@ -912,5 +912,86 @@ uint32_t TimeGetMillies() .count(); } +//------------------------------------------------------------------------ +// MEMORY ALLOCATION +//------------------------------------------------------------------------ + +// +// Allocate memory with error checking. Zeros the memory. +// +void *UtilCalloc(int size) +{ + void *ret = calloc(1, size); + + if (!ret) + Main::FatalError("Out of memory (cannot allocate %d bytes)\n", size); + + return ret; +} + +// +// Reallocate memory with error checking. +// +void *UtilRealloc(void *old, int size) +{ + void *ret = realloc(old, size); + + if (!ret) + Main::FatalError("Out of memory (cannot reallocate %d bytes)\n", size); + + return ret; +} + +// +// Free the memory with error checking. +// +void UtilFree(void *data) +{ + if (data == NULL) + Main::FatalError("Trying to free a NULL pointer\n"); + + free(data); +} + +//------------------------------------------------------------------------ +// MATH STUFF +//------------------------------------------------------------------------ + +// +// rounds the value _up_ to the nearest power of two. +// +int RoundPOW2(int x) +{ + if (x <= 2) + return x; + + x--; + + for (int tmp = x >> 1; tmp; tmp >>= 1) + x |= tmp; + + return x + 1; +} + +// +// Compute angle of line from (0,0) to (dx,dy). +// Result is degrees, where 0 is east and 90 is north. +// +double ComputeAngle(double dx, double dy) +{ + double angle; + + if (dx == 0) + return (dy > 0) ? 90.0 : 270.0; + + angle = atan2((double)dy, (double)dx) * 180.0 / M_PI; + + if (angle < 0) + angle += 360.0; + + return angle; +} + + //--- editor settings --- // vi:ts=4:sw=4:noexpandtab diff --git a/source/lib_util.h b/source/lib_util.h index 864fdd270..31543c0e7 100644 --- a/source/lib_util.h +++ b/source/lib_util.h @@ -129,6 +129,13 @@ char *mem_gets(char *buf, int size, const char **str_ptr); uint32_t TimeGetMillies(); +/* memory utilities */ + +void *UtilCalloc(int size); +void *UtilRealloc(void *old, int size); +void UtilFree(void *data); + + /* math utilities */ uint32_t IntHash(uint32_t key); @@ -155,5 +162,8 @@ std::pair AlongCoord(double along, double px1, double py1, doubl bool VectorSameDir(double dx1, double dy1, double dx2, double dy2); +int RoundPOW2(int x); +double ComputeAngle(double dx, double dy); + //--- editor settings --- // vi:ts=4:sw=4:noexpandtab diff --git a/source/lib_wad.cc b/source/lib_wad.cc index c3d7c9c3c..621ba9196 100644 --- a/source/lib_wad.cc +++ b/source/lib_wad.cc @@ -26,6 +26,7 @@ #include "lib_util.h" #include "main.h" #include "physfs.h" +#include "raw_def.h" #include "sys_assert.h" #include "sys_endian.h" @@ -35,9 +36,9 @@ // WAD READING //------------------------------------------------------------------------ -static PHYSFS_File *wad_R_fp; -static raw_wad_header_t wad_R_header; -static raw_wad_lump_t *wad_R_dir; +static PHYSFS_File *wad_R_fp; +static raw_wad_header_t wad_R_header; +static raw_wad_entry_t *wad_R_dir; bool WAD_OpenRead(std::string filename) { @@ -59,21 +60,21 @@ bool WAD_OpenRead(std::string filename) return false; } - if (0 != memcmp(wad_R_header.magic + 1, "WAD", 3)) + if (0 != memcmp(wad_R_header.ident + 1, "WAD", 3)) { LogPrintf("WAD_OpenRead: not a WAD file!\n"); PHYSFS_close(wad_R_fp); return false; } - wad_R_header.num_lumps = LE_U32(wad_R_header.num_lumps); + wad_R_header.num_entries = LE_U32(wad_R_header.num_entries); wad_R_header.dir_start = LE_U32(wad_R_header.dir_start); /* read directory */ - if (wad_R_header.num_lumps >= 5000) // sanity check + if (wad_R_header.num_entries >= 5000) // sanity check { - LogPrintf("WAD_OpenRead: bad header (%u entries?)\n", static_cast(wad_R_header.num_lumps)); + LogPrintf("WAD_OpenRead: bad header (%u entries?)\n", static_cast(wad_R_header.num_entries)); PHYSFS_close(wad_R_fp); return false; } @@ -86,13 +87,13 @@ bool WAD_OpenRead(std::string filename) return false; } - wad_R_dir = new raw_wad_lump_t[wad_R_header.num_lumps + 1]; + wad_R_dir = new raw_wad_entry_t[wad_R_header.num_entries + 1]; - for (int i = 0; i < (int)wad_R_header.num_lumps; i++) + for (int i = 0; i < (int)wad_R_header.num_entries; i++) { - raw_wad_lump_t *L = &wad_R_dir[i]; + raw_wad_entry_t *L = &wad_R_dir[i]; - size_t res = (PHYSFS_readBytes(wad_R_fp, L, sizeof(raw_wad_lump_t)) / sizeof(raw_wad_lump_t)); + size_t res = (PHYSFS_readBytes(wad_R_fp, L, sizeof(raw_wad_entry_t)) / sizeof(raw_wad_entry_t)); if (res != 1) { if (i == 0) @@ -105,12 +106,12 @@ bool WAD_OpenRead(std::string filename) LogPrintf("WAD_OpenRead: hit EOF reading dir-entry %d\n", i); // truncate directory - wad_R_header.num_lumps = i; + wad_R_header.num_entries = i; break; } - L->start = LE_U32(L->start); - L->length = LE_U32(L->length); + L->pos = LE_U32(L->pos); + L->size = LE_U32(L->size); } return true; // OK @@ -128,12 +129,12 @@ void WAD_CloseRead(void) int WAD_NumEntries(void) { - return (int)wad_R_header.num_lumps; + return (int)wad_R_header.num_entries; } int WAD_FindEntry(const char *name) { - for (unsigned int i = 0; i < wad_R_header.num_lumps; i++) + for (unsigned int i = 0; i < wad_R_header.num_entries; i++) { char buffer[16]; strncpy(buffer, wad_R_dir[i].name, 8); @@ -150,16 +151,16 @@ int WAD_FindEntry(const char *name) int WAD_EntryLen(int entry) { - SYS_ASSERT(entry >= 0 && entry < (int)wad_R_header.num_lumps); + SYS_ASSERT(entry >= 0 && entry < (int)wad_R_header.num_entries); - return wad_R_dir[entry].length; + return wad_R_dir[entry].size; } const char *WAD_EntryName(int entry) { static char name_buf[16]; - SYS_ASSERT(entry >= 0 && entry < (int)wad_R_header.num_lumps); + SYS_ASSERT(entry >= 0 && entry < (int)wad_R_header.num_entries); // entries are often not NUL terminated, hence return a static copy strncpy(name_buf, wad_R_dir[entry].name, 8); @@ -170,18 +171,18 @@ const char *WAD_EntryName(int entry) bool WAD_ReadData(int entry, int offset, int length, void *buffer) { - SYS_ASSERT(entry >= 0 && entry < (int)wad_R_header.num_lumps); + SYS_ASSERT(entry >= 0 && entry < (int)wad_R_header.num_entries); SYS_ASSERT(offset >= 0); SYS_ASSERT(length > 0); - raw_wad_lump_t *L = &wad_R_dir[entry]; + raw_wad_entry_t *L = &wad_R_dir[entry]; - if ((uint32_t)offset + (uint32_t)length > L->length) + if ((uint32_t)offset + (uint32_t)length > L->size) { // EOF return false; } - if (!PHYSFS_seek(wad_R_fp, L->start + offset)) + if (!PHYSFS_seek(wad_R_fp, L->pos + offset)) { return false; } @@ -193,17 +194,17 @@ bool WAD_ReadData(int entry, int offset, int length, void *buffer) // WAD WRITING //------------------------------------------------------------------------ -static std::ofstream wad_W_fp; +static FILE *wad_W_fp; -static std::list wad_W_directory; +static std::list wad_W_directory; -static raw_wad_lump_t wad_W_lump; +static raw_wad_entry_t wad_W_lump; bool WAD_OpenWrite(std::string filename) { - wad_W_fp.open(filename, std::ios::out | std::ios::binary); + wad_W_fp = FileOpen(filename, "wb"); - if (!wad_W_fp.is_open()) + if (!wad_W_fp) { LogPrintf("WAD_OpenWrite: cannot create file: %s\n", filename.c_str()); return false; @@ -215,15 +216,15 @@ bool WAD_OpenWrite(std::string filename) raw_wad_header_t header; memset(&header, 0, sizeof(header)); - wad_W_fp.write((const char *)&header, sizeof(raw_wad_header_t)); - wad_W_fp << std::flush; + fwrite(&header, sizeof(raw_wad_header_t), 1, wad_W_fp); + fflush(wad_W_fp); return true; } void WAD_CloseWrite(void) { - wad_W_fp << std::flush; + fflush(wad_W_fp); // write the directory @@ -231,36 +232,37 @@ void WAD_CloseWrite(void) raw_wad_header_t header; - memcpy(header.magic, "PWAD", sizeof(header.magic)); + memcpy(header.ident, "PWAD", sizeof(header.ident)); - header.dir_start = wad_W_fp.tellp(); - header.num_lumps = 0; + header.dir_start = ftell(wad_W_fp); + header.num_entries = 0; - std::list::iterator WDI; + std::list::iterator WDI; for (WDI = wad_W_directory.begin(); WDI != wad_W_directory.end(); ++WDI) { - raw_wad_lump_t *L = &(*WDI); + raw_wad_entry_t *L = &(*WDI); - wad_W_fp.write((const char *)L, sizeof(raw_wad_lump_t)); - wad_W_fp << std::flush; + fwrite(L, sizeof(raw_wad_entry_t), 1, wad_W_fp); + fflush(wad_W_fp); - header.num_lumps++; + header.num_entries++; } - wad_W_fp << std::flush; + fflush(wad_W_fp); // finally write the _real_ WAD header header.dir_start = LE_U32(header.dir_start); - header.num_lumps = LE_U32(header.num_lumps); + header.num_entries = LE_U32(header.num_entries); - wad_W_fp.seekp(0, std::ios::beg); + fseek(wad_W_fp, 0, SEEK_SET); - wad_W_fp.write((const char *)&header, sizeof(header)); + fwrite(&header, sizeof(header), 1, wad_W_fp); - wad_W_fp << std::flush; - wad_W_fp.close(); + fflush(wad_W_fp); + fclose(wad_W_fp); + wad_W_fp = nullptr; LogPrintf("Closed WAD file\n"); @@ -278,7 +280,7 @@ void WAD_NewLump(std::string name) memcpy(wad_W_lump.name, name.data(), name.size()); - wad_W_lump.start = wad_W_fp.tellp(); + wad_W_lump.pos = ftell(wad_W_fp); } bool WAD_AppendData(const void *data, int length) @@ -290,12 +292,12 @@ bool WAD_AppendData(const void *data, int length) SYS_ASSERT(length > 0); - return static_cast(wad_W_fp.write(static_cast(data), length)); + return fwrite(data, length, 1, wad_W_fp); } void WAD_FinishLump(void) { - const int len = static_cast(wad_W_fp.tellp()) - static_cast(wad_W_lump.start); + const int len = ftell(wad_W_fp) - wad_W_lump.pos; // pad lumps to a multiple of four bytes int padding = ALIGN_LEN(len) - len; @@ -304,13 +306,13 @@ void WAD_FinishLump(void) { static uint8_t zeros[4] = {0, 0, 0, 0}; - wad_W_fp.write((const char *)zeros, padding); - wad_W_fp << std::flush; + fwrite(zeros, padding, 1, wad_W_fp); + fflush(wad_W_fp); } // fix endianness - wad_W_lump.start = LE_U32(wad_W_lump.start); - wad_W_lump.length = LE_U32(len); + wad_W_lump.pos = LE_U32(wad_W_lump.pos); + wad_W_lump.size = LE_U32(len); wad_W_directory.push_back(wad_W_lump); } diff --git a/source/lib_wad.h b/source/lib_wad.h index db0d576ba..8ba0c5b05 100644 --- a/source/lib_wad.h +++ b/source/lib_wad.h @@ -25,7 +25,7 @@ #include -#include +#include bool WAD_OpenRead(std::string filename); void WAD_CloseRead(); @@ -46,27 +46,5 @@ void WAD_NewLump(std::string name); bool WAD_AppendData(const void *data, int length); void WAD_FinishLump(); -/* ----- WAD structure (Doom) ---------------------- */ - -#pragma pack(push, 1) -struct raw_wad_header_t -{ - char magic[4]; - - uint32_t num_lumps; - uint32_t dir_start; -}; -#pragma pack(pop) - -#pragma pack(push, 1) -struct raw_wad_lump_t -{ - uint32_t start; - uint32_t length; - - char name[8]; -}; -#pragma pack(pop) - //--- editor settings --- // vi:ts=4:sw=4:noexpandtab diff --git a/source/m_addons.cc b/source/m_addons.cc index 7a783dbc5..d19f6f51b 100644 --- a/source/m_addons.cc +++ b/source/m_addons.cc @@ -159,9 +159,9 @@ void VFS_OptParse(std::string name) } } -void VFS_OptWrite(std::ofstream &fp) +void VFS_OptWrite(FILE *fp) { - fp << "---- Enabled Addons ----\n\n"; + fprintf(fp, "---- Enabled Addons ----\n\n"); for (unsigned int i = 0; i < all_addons.size(); i++) { @@ -169,11 +169,11 @@ void VFS_OptWrite(std::ofstream &fp) if (info->enabled) { - fp << "addon = " << info->name << "\n"; + fprintf(fp, "addon = %s\n", info->name.c_str()); } } - fp << "\n"; + fprintf(fp, "\n"); } void VFS_ScanForPresets() diff --git a/source/m_addons.h b/source/m_addons.h index 848247eac..d7484ed9f 100644 --- a/source/m_addons.h +++ b/source/m_addons.h @@ -24,7 +24,6 @@ #include #include -#include #include #include #include @@ -35,7 +34,7 @@ void VFS_ScanForAddons(); void VFS_ScanForPresets(); void VFS_OptParse(std::string name); -void VFS_OptWrite(std::ofstream &fp); +void VFS_OptWrite(FILE *fp); // util functions bool VFS_CopyFile(const char *src_name, const char *dest_name); diff --git a/source/m_cookie.cc b/source/m_cookie.cc index 3b6015884..a96799f8e 100644 --- a/source/m_cookie.cc +++ b/source/m_cookie.cc @@ -21,7 +21,9 @@ #include "m_cookie.h" -#include +#include + +#include #ifndef CONSOLE_ONLY #include "hdr_fltk.h" @@ -175,9 +177,10 @@ bool Cookie_Load(std::string filename) active_module.clear(); setlocale(LC_NUMERIC, "C"); - std::ifstream cookie_fp(filename, std::ios::in); - if (!cookie_fp.is_open()) + FILE *cookie_fp = FileOpen(filename, "r"); + + if (!cookie_fp) { return false; } @@ -189,14 +192,29 @@ bool Cookie_Load(std::string filename) int error_count = 0; - for (std::string line; std::getline(cookie_fp, line);) + std::string buffer; + int c = EOF; + for (;;) { - if (!Cookie_ParseLine(line)) + buffer.clear(); + while ((c = fgetc(cookie_fp)) != EOF) + { + buffer.push_back(c); + if (c == '\n') + break; + } + + if (!Cookie_ParseLine(buffer)) { error_count += 1; } + + if (feof(cookie_fp) || ferror(cookie_fp)) + break; } + fclose(cookie_fp); + if (main_action != MAIN_SOFT_RESTART) { if (error_count > 0) @@ -241,9 +259,10 @@ bool Cookie_Save(std::string filename) { context = cookie_context_e::Save; setlocale(LC_NUMERIC, "C"); - std::ofstream cookie_fp(filename, std::ios::out); - if (!cookie_fp.is_open()) + FILE *cookie_fp = FileOpen(filename, "w"); + + if (!cookie_fp) { LogPrintf("Error: unable to create file: %s\n(%s)\n\n", filename.c_str(), strerror(errno)); return false; @@ -255,10 +274,10 @@ bool Cookie_Save(std::string filename) } // header... - cookie_fp << "-- CONFIG FILE : OBSIDIAN " << OBSIDIAN_SHORT_VERSION << " \"" << OBSIDIAN_CODE_NAME << "\"\n"; - cookie_fp << "-- Build " << OBSIDIAN_VERSION << "\n"; - cookie_fp << "-- Based on OBLIGE Level Maker (C) 2006-2017 Andrew Apted\n"; - cookie_fp << "-- " << OBSIDIAN_WEBSITE << "\n\n"; + fprintf(cookie_fp, "-- CONFIG FILE : OBSIDIAN %s \"%s\"\n", OBSIDIAN_SHORT_VERSION, OBSIDIAN_CODE_NAME.c_str()); + fprintf(cookie_fp, "-- Build %s\n", OBSIDIAN_VERSION); + fprintf(cookie_fp, "-- Based on OBLIGE Level Maker (C) 2006-2017 Andrew Apted\n"); + fprintf(cookie_fp, "-- %s\n\n", OBSIDIAN_WEBSITE); // settings... std::vector lines; @@ -267,7 +286,7 @@ bool Cookie_Save(std::string filename) for (unsigned int i = 0; i < lines.size(); i++) { - cookie_fp << lines[i] << "\n"; + fprintf(cookie_fp, "%s\n", lines[i].c_str()); } if (main_action == MAIN_HARD_RESTART || main_action == MAIN_QUIT) @@ -275,7 +294,7 @@ bool Cookie_Save(std::string filename) LogPrintf("DONE.\n\n"); } - cookie_fp.close(); + fclose(cookie_fp); setlocale(LC_NUMERIC, numeric_locale.c_str()); return true; } @@ -454,7 +473,7 @@ class RecentFiles_c } } - void write_all(std::ofstream &fp, std::string keyword) const + void write_all(FILE *fp, std::string keyword) const { // Files are written in opposite order, newest at the end. // This allows the parser to merely insert() items in the @@ -462,13 +481,12 @@ class RecentFiles_c for (int k = size - 1; k >= 0; k--) { - std::string fn = StringFormat("%s = %s\n", keyword.c_str(), filenames[k].c_str()); - fp.write(fn.c_str(), fn.size()); + fprintf(fp, "%s = %s\n", keyword.c_str(), filenames[k].c_str()); } if (size > 0) { - fp << "\n"; + fprintf(fp, "\n"); } } @@ -497,7 +515,7 @@ class RecentFiles_c static RecentFiles_c recent_wads; static RecentFiles_c recent_configs; -void Recent_Parse(std::string name, std::string value) +void Recent_Parse(const std::string &name, const std::string &value) { if (StringCompare(name, "recent_wad") == 0) { @@ -509,10 +527,9 @@ void Recent_Parse(std::string name, std::string value) } } -void Recent_Write(std::ofstream &fp) +void Recent_Write(FILE *fp) { - fp << "---- Recent Files ----\n\n"; - + fprintf(fp, "---- Recent Files ----\n\n"); recent_wads.write_all(fp, "recent_wad"); recent_configs.write_all(fp, "recent_config"); } diff --git a/source/m_cookie.h b/source/m_cookie.h index 49ee841c3..eebe316c4 100644 --- a/source/m_cookie.h +++ b/source/m_cookie.h @@ -42,8 +42,8 @@ bool Theme_Options_Save(std::string filename); /* recent file stuff */ -void Recent_Parse(std::string name, std::string value); -void Recent_Write(std::ofstream &fp); +void Recent_Parse(const std::string &name, const std::string &value); +void Recent_Write(FILE *fp); typedef enum { diff --git a/source/m_dialog.cc b/source/m_dialog.cc index c5fb924c6..97b121fe0 100644 --- a/source/m_dialog.cc +++ b/source/m_dialog.cc @@ -21,8 +21,8 @@ #include -#include #include +#include #include #include "hdr_fltk.h" @@ -358,18 +358,18 @@ void DLG_EditSeed(void) catch (std::invalid_argument &e) { (void)e; - std::cout << _("Invalid argument. Will process as string.\n"); + printf(_("Invalid argument. Will process as string.\n")); } catch (std::out_of_range &e) { (void)e; // clang-format off - std::cout << _("Resulting number would be out of range. Will process as string.\n"); + printf(_("Resulting number would be out of range. Will process as string.\n")); // clang-format on } catch (std::exception &e) { - std::cout << e.what(); + printf(e.what()); } string_seed = word; ob_set_config("string_seed", word.c_str()); @@ -405,7 +405,7 @@ class UI_LogViewer : public Fl_Double_Window void ReadLogs(); - void WriteLogs(std::ofstream &fp); + void WriteLogs(FILE *fp); private: int CountSelectedLines() const; @@ -571,7 +571,7 @@ void UI_LogViewer::ReadLogs() JumpEnd(); } -void UI_LogViewer::WriteLogs(std::ofstream &fp) +void UI_LogViewer::WriteLogs(FILE *fp) { for (int n = 1; n <= browser->size(); n++) { @@ -579,7 +579,7 @@ void UI_LogViewer::WriteLogs(std::ofstream &fp) if (str) { - StreamPrintf(fp, "%s\n", str); + fprintf(fp, "%s\n", str); } } } @@ -687,7 +687,7 @@ tryagain:; goto tryagain; } - std::ofstream fp{filename}; + FILE *fp = FileOpen(filename, "w"); if (!fp) { @@ -699,7 +699,7 @@ tryagain:; that->WriteLogs(fp); - fp.close(); + fclose(fp); } //---------------------------------------------------------------------- @@ -794,26 +794,41 @@ void UI_GlossaryViewer::ReadGlossary() return; } - std::fstream gloss_stream; + FILE *gloss_file = FileOpen(glossary, "r"); - gloss_stream.open(glossary, std::ios::in); + if (!gloss_file) + { + return; + } std::string buffer; - while (std::getline(gloss_stream, buffer)) + int c = EOF; + for (;;) { + buffer.clear(); + while ((c = fgetc(gloss_file)) != EOF) + { + buffer.push_back(c); + if (c == '\n') + break; + } + // remove any newline at the end (LF or CR/LF) StringRemoveCRLF(&buffer); // remove any DEL characters (mainly to workaround an FLTK bug) StringReplaceChar(&buffer, 0x7f, 0); - std::cout << buffer << std::endl; + buffer.push_back('\n'); browser->add(buffer.data()); + + if (feof(gloss_file) || ferror(gloss_file)) + break; } // close the log file after current contents are read - gloss_stream.close(); + fclose(gloss_file); } void UI_GlossaryViewer::quit_callback(Fl_Widget *w, void *data) diff --git a/source/m_lua.cc b/source/m_lua.cc index 0c2dd94c9..701357c94 100644 --- a/source/m_lua.cc +++ b/source/m_lua.cc @@ -33,6 +33,7 @@ #include "main.h" #include "physfs.h" #include "sys_assert.h" +#include "sys_debug.h" #include "sys_xoshiro.h" static lua_State *LUA_ST; @@ -100,7 +101,7 @@ int gui_console_print(lua_State *L) res += 2; } - StdOutPrintf("%s", res); + printf("%s", res); } return 0; @@ -2134,10 +2135,12 @@ void ob_print_reference() if (!Script_CallFunc("ob_print_reference", 1)) { // clang-format off - StdOutPrintf(_("ob_print_reference: Error creating REFERENCE.txt!\n")); + printf(_("ob_print_reference: Error creating REFERENCE.txt!\n")); // clang-format on } - StdOutPrintf("\nA copy of this output can be found at %s\n", reference_file.c_str()); + // clang-format off + printf("\n%s %s\n", _("A copy of this output can be found at"), reference_file.c_str()); + // clang-format on } void ob_print_reference_json() @@ -2145,7 +2148,7 @@ void ob_print_reference_json() if (!Script_CallFunc("ob_print_reference_json", 1)) { // clang-format off - StdOutPrintf(_("ob_print_reference_json: Error printing json reference!\n")); + printf(_("ob_print_reference_json: Error printing json reference!\n")); // clang-format on } } diff --git a/source/m_manage.cc b/source/m_manage.cc index 4975c692f..d907e48cf 100644 --- a/source/m_manage.cc +++ b/source/m_manage.cc @@ -509,7 +509,7 @@ class UI_Manage_Config : public Fl_Double_Window bool LoadFromFile(std::string filename) { - FILE *fp = FileOpen(filename.c_str(), "rb"); + FILE *fp = FileOpen(filename, "rb"); if (!fp) { diff --git a/source/m_options.cc b/source/m_options.cc index f6121a140..413e549c4 100644 --- a/source/m_options.cc +++ b/source/m_options.cc @@ -114,13 +114,9 @@ void Parse_Option(const std::string &name, const std::string &value) { default_output_path = value; } - else - { - StdOutPrintf("%s '%s'\n", _("Unknown option: "), name.c_str()); - } } -static bool Options_ParseLine(std::string buf) +static bool Options_ParseLine(const std::string &buf) { std::string::size_type pos = 0; @@ -131,26 +127,17 @@ static bool Options_ParseLine(std::string buf) return true; } - // For options file, don't strip whitespace as it can cause issue with addon - // paths that have whitespace - Dasho - - /*while (std::find(buf.begin(), buf.end(), ' ') != buf.end()) { - buf.erase(std::find(buf.begin(), buf.end(), ' ')); - }*/ - - if (!isalpha(buf.front())) + if (!IsAlphaASCII(buf.front())) { - StdOutPrintf("%s [%s]\n", _("Weird option line: "), buf.c_str()); return false; } - // pos = buf.find('=', 0); // Fix pos after whitespace deletion std::string name = buf.substr(0, pos - 1); std::string value = buf.substr(pos + 2); if (name.empty() || value.empty()) { - StdOutPrintf(_("Name or value missing!\n")); + printf(_("Name or value missing!\n")); return false; } @@ -160,29 +147,42 @@ static bool Options_ParseLine(std::string buf) bool Options_Load(std::string filename) { - std::ifstream option_fp(filename, std::ios::in); + FILE *option_fp = FileOpen(filename, "r"); - if (!option_fp.is_open()) + if (!option_fp) { - StdOutPrintf(_("Missing Options file -- using defaults.\n\n")); + printf(_("Missing Options file -- using defaults.\n\n")); return false; } - for (std::string line; std::getline(option_fp, line);) + std::string buffer; + int c = EOF; + for (;;) { - Options_ParseLine(line); + buffer.clear(); + while ((c = fgetc(option_fp)) != EOF) + { + buffer.push_back(c); + if (c == '\n') + break; + } + + Options_ParseLine(buffer); + + if (feof(option_fp) || ferror(option_fp)) + break; } - option_fp.close(); + fclose(option_fp); return true; } bool Options_Save(std::string filename) { - std::ofstream option_fp(filename, std::ios::out); + FILE *option_fp = FileOpen(filename, "w"); - if (!option_fp.is_open()) + if (!option_fp) { LogPrintf("Error: unable to create file: %s\n(%s)\n\n", filename.c_str(), strerror(errno)); return false; @@ -193,41 +193,37 @@ bool Options_Save(std::string filename) LogPrintf("Saving options file...\n"); } - option_fp << "-- OPTIONS FILE : OBSIDIAN " << OBSIDIAN_SHORT_VERSION << " \"" << OBSIDIAN_CODE_NAME << "\"\n"; - option_fp << "-- Build " << OBSIDIAN_VERSION << "\n"; - option_fp << "-- Based on OBLIGE Level Maker (C) 2006-2017 Andrew Apted\n"; - option_fp << "-- " << OBSIDIAN_WEBSITE << "\n\n"; - - option_fp << "language = " << t_language << "\n"; - option_fp << "\n"; - - option_fp << "create_backups = " << (create_backups ? 1 : 0) << "\n"; - option_fp << "overwrite_warning = " << (overwrite_warning ? 1 : 0) << "\n"; - option_fp << "debug_messages = " << (debug_messages ? 1 : 0) << "\n"; - option_fp << "limit_break = " << (limit_break ? 1 : 0) << "\n"; - option_fp << "preserve_old_config = " << (preserve_old_config ? 1 : 0) << "\n"; - option_fp << "randomize_architecture = " << (randomize_architecture ? 1 : 0) << "\n"; - option_fp << "randomize_monsters = " << (randomize_monsters ? 1 : 0) << "\n"; - option_fp << "randomize_pickups = " << (randomize_pickups ? 1 : 0) << "\n"; - option_fp << "randomize_misc = " << (randomize_misc ? 1 : 0) << "\n"; - option_fp << "random_string_seeds = " << (random_string_seeds ? 1 : 0) << "\n"; + fprintf(option_fp, "-- OPTIONS FILE : OBSIDIAN %s \"%s\"\n", OBSIDIAN_SHORT_VERSION, OBSIDIAN_CODE_NAME); + fprintf(option_fp, "-- Build %s\n", OBSIDIAN_VERSION); + fprintf(option_fp, "-- Based on OBLIGE Level Maker (C) 2006-2017 Andrew Apted\n"); + fprintf(option_fp, "-- %s\n\n", OBSIDIAN_WEBSITE); + + fprintf(option_fp, "language = %s\n\n", t_language.c_str()); + + fprintf(option_fp, "create_backups = %d\n", (create_backups ? 1 : 0)); + fprintf(option_fp, "overwrite_warning = %d\n", (overwrite_warning ? 1 : 0)); + fprintf(option_fp, "debug_messages = %d\n", (debug_messages ? 1 : 0)); + fprintf(option_fp, "limit_break = %d\n", (limit_break ? 1 : 0)); + fprintf(option_fp, "preserve_old_config = %d\n", (preserve_old_config ? 1 : 0)); + fprintf(option_fp, "randomize_architecture = %d\n", (randomize_architecture ? 1 : 0)); + fprintf(option_fp, "randomize_monsters = %d\n", (randomize_monsters ? 1 : 0)); + fprintf(option_fp, "randomize_pickups = %d\n", (randomize_pickups ? 1 : 0)); + fprintf(option_fp, "randomize_misc = %d\n", (randomize_misc ? 1 : 0)); + fprintf(option_fp, "random_string_seeds = %d\n", (random_string_seeds ? 1 : 0)); #ifndef CONSOLE_ONLY - option_fp << "gui_simple_mode = " << (gui_simple_mode ? 1 : 0) << "\n"; + fprintf(option_fp, "gui_simple_mode = %d\n", (gui_simple_mode ? 1 : 0)); #endif - option_fp << "password_mode = " << (password_mode ? 1 : 0) << "\n"; - option_fp << "mature_word_lists = " << (mature_word_lists ? 1 : 0) << "\n"; - option_fp << "filename_prefix = " << filename_prefix << "\n"; - option_fp << "custom_prefix = " << custom_prefix << "\n"; - std::string dop = StringFormat("default_output_path = %s\n", default_output_path.c_str()); - option_fp.write(dop.c_str(), dop.size()); - - option_fp << "\n"; + fprintf(option_fp, "password_mode = %d\n", (password_mode ? 1 : 0)); + fprintf(option_fp, "mature_word_lists = %d\n", (mature_word_lists ? 1 : 0)); + fprintf(option_fp, "filename_prefix = %d\n", filename_prefix); + fprintf(option_fp, "custom_prefix = %d\n", custom_prefix); + fprintf(option_fp, "%s", StringFormat("default_output_path = %s\n\n", default_output_path.c_str()).c_str()); VFS_OptWrite(option_fp); Recent_Write(option_fp); - option_fp.close(); + fclose(option_fp); if (main_action != MAIN_SOFT_RESTART) { diff --git a/source/m_theme.cc b/source/m_theme.cc index 958fc4213..ba996b3be 100644 --- a/source/m_theme.cc +++ b/source/m_theme.cc @@ -294,9 +294,9 @@ static bool Theme_Options_ParseLine(std::string buf) bool Theme_Options_Load(std::string filename) { - std::ifstream option_fp(filename, std::ios::in); + FILE *option_fp = FileOpen(filename, "r"); - if (!option_fp.is_open()) + if (!option_fp) { LogPrintf("Missing Theme file -- using defaults.\n\n"); return false; @@ -306,12 +306,25 @@ bool Theme_Options_Load(std::string filename) int error_count = 0; - for (std::string line; std::getline(option_fp, line);) + std::string buffer; + int c = EOF; + for (;;) { - if (!Theme_Options_ParseLine(line)) + buffer.clear(); + while ((c = fgetc(option_fp)) != EOF) + { + buffer.push_back(c); + if (c == '\n') + break; + } + + if (!Theme_Options_ParseLine(buffer)) { error_count += 1; } + + if (feof(option_fp) || ferror(option_fp)) + break; } if (error_count > 0) @@ -323,16 +336,16 @@ bool Theme_Options_Load(std::string filename) LogPrintf("DONE.\n\n"); } - option_fp.close(); + fclose(option_fp); return true; } bool Theme_Options_Save(std::string filename) { - std::ofstream option_fp(filename); + FILE *option_fp = FileOpen(filename, "w"); - if (!option_fp.is_open()) + if (!option_fp) { LogPrintf("Error: unable to create file: %s\n(%s)\n\n", filename.c_str(), strerror(errno)); return false; @@ -343,45 +356,45 @@ bool Theme_Options_Save(std::string filename) LogPrintf("Saving theme file...\n"); } - option_fp << "-- THEME FILE : OBSIDIAN " << OBSIDIAN_SHORT_VERSION << " \"" << OBSIDIAN_CODE_NAME << "\"\n"; - option_fp << "-- Build " << OBSIDIAN_VERSION << "\n"; - option_fp << "-- Based on OBLIGE Level Maker (C) 2006-2017 Andrew Apted\n"; - option_fp << "-- " << OBSIDIAN_WEBSITE << "\n\n"; - - option_fp << "window_scaling = " << NumToString(window_scaling) << "\n"; - option_fp << "font_scaling = " << NumToString(font_scaling) << "\n"; - option_fp << "font_theme = " << NumToString(font_theme) << "\n"; - option_fp << "widget_theme = " << NumToString(widget_theme) << "\n"; - option_fp << "box_theme = " << NumToString(box_theme) << "\n"; - option_fp << "button_theme = " << NumToString(button_theme) << "\n"; - option_fp << "color_scheme = " << NumToString(color_scheme) << "\n"; - option_fp << "text_red = " << NumToString(text_red) << "\n"; - option_fp << "text_green = " << NumToString(text_green) << "\n"; - option_fp << "text_blue = " << NumToString(text_blue) << "\n"; - option_fp << "text2_red = " << NumToString(text2_red) << "\n"; - option_fp << "text2_green = " << NumToString(text2_green) << "\n"; - option_fp << "text2_blue = " << NumToString(text2_blue) << "\n"; - option_fp << "bg_red = " << NumToString(bg_red) << "\n"; - option_fp << "bg_green = " << NumToString(bg_green) << "\n"; - option_fp << "bg_blue = " << NumToString(bg_blue) << "\n"; - option_fp << "bg2_red = " << NumToString(bg2_red) << "\n"; - option_fp << "bg2_green = " << NumToString(bg2_green) << "\n"; - option_fp << "bg2_blue = " << NumToString(bg2_blue) << "\n"; - option_fp << "button_red = " << NumToString(button_red) << "\n"; - option_fp << "button_green = " << NumToString(button_green) << "\n"; - option_fp << "button_blue = " << NumToString(button_blue) << "\n"; - option_fp << "gradient_red = " << NumToString(gradient_red) << "\n"; - option_fp << "gradient_green = " << NumToString(gradient_green) << "\n"; - option_fp << "gradient_blue = " << NumToString(gradient_blue) << "\n"; - option_fp << "border_red = " << NumToString(border_red) << "\n"; - option_fp << "border_green = " << NumToString(border_green) << "\n"; - option_fp << "border_blue = " << NumToString(border_blue) << "\n"; - option_fp << "gap_red = " << NumToString(gap_red) << "\n"; - option_fp << "gap_green = " << NumToString(gap_green) << "\n"; - option_fp << "gap_blue = " << NumToString(gap_blue) << "\n"; - option_fp << "\n\n"; - - option_fp.close(); + fprintf(option_fp, "-- THEME FILE : OBSIDIAN %s \"%s\"\n", OBSIDIAN_SHORT_VERSION, OBSIDIAN_CODE_NAME); + fprintf(option_fp, "-- Build %s\n", OBSIDIAN_VERSION); + fprintf(option_fp, "-- Based on OBLIGE Level Maker (C) 2006-2017 Andrew Apted\n"); + fprintf(option_fp, "-- %s\n\n", OBSIDIAN_WEBSITE); + + fprintf(option_fp, "window_scaling = %s\n", NumToString(window_scaling)); + fprintf(option_fp, "font_scaling = %s\n", NumToString(font_scaling)); + fprintf(option_fp, "font_theme = %s\n", NumToString(font_theme)); + fprintf(option_fp, "widget_theme = %s\n", NumToString(widget_theme)); + fprintf(option_fp, "box_theme = %s\n", NumToString(box_theme)); + fprintf(option_fp, "button_theme = %s\n", NumToString(button_theme)); + fprintf(option_fp, "color_scheme = %s\n", NumToString(color_scheme)); + fprintf(option_fp, "text_red = %s\n", NumToString(text_red)); + fprintf(option_fp, "text_green = %s\n", NumToString(text_green)); + fprintf(option_fp, "text_blue = %s\n", NumToString(text_blue)); + fprintf(option_fp, "text2_red = %s\n", NumToString(text2_red)); + fprintf(option_fp, "text2_green = %s\n", NumToString(text2_green)); + fprintf(option_fp, "text2_blue = %s\n", NumToString(text2_blue)); + fprintf(option_fp, "bg_red = %s\n", NumToString(bg_red)); + fprintf(option_fp, "bg_green = %s\n", NumToString(bg_green)); + fprintf(option_fp, "bg_blue = %s\n", NumToString(bg_blue)); + fprintf(option_fp, "bg2_red = %s\n", NumToString(bg2_red)); + fprintf(option_fp, "bg2_green = %s\n", NumToString(bg2_green)); + fprintf(option_fp, "bg2_blue = %s\n", NumToString(bg2_blue)); + fprintf(option_fp, "button_red = %s\n", NumToString(button_red)); + fprintf(option_fp, "button_green = %s\n", NumToString(button_green)); + fprintf(option_fp, "button_blue = %s\n", NumToString(button_blue)); + fprintf(option_fp, "gradient_red = %s\n", NumToString(gradient_red)); + fprintf(option_fp, "gradient_green = %s\n", NumToString(gradient_green)); + fprintf(option_fp, "gradient_blue = %s\n", NumToString(gradient_blue)); + fprintf(option_fp, "border_red = %s\n", NumToString(border_red)); + fprintf(option_fp, "border_green = %s\n", NumToString(border_green)); + fprintf(option_fp, "border_blue = %s\n", NumToString(border_blue)); + fprintf(option_fp, "gap_red = %s\n", NumToString(gap_red)); + fprintf(option_fp, "gap_green = %s\n", NumToString(gap_green)); + fprintf(option_fp, "gap_blue = %s\n", NumToString(gap_blue)); + fprintf(option_fp, "\n\n"); + + fclose(option_fp); if (main_action != MAIN_SOFT_RESTART) { diff --git a/source/m_trans.cc b/source/m_trans.cc index 4d2f842a6..aeb1ca170 100644 --- a/source/m_trans.cc +++ b/source/m_trans.cc @@ -1003,16 +1003,23 @@ void Trans_Read_PO_File(FILE *fp) // initialize po_state.Clear(); - // process one line on each iteration - static char line[MSG_BUF_LEN]; - po_state.line_number = 0; - while (fgets(line, sizeof(line), fp) != NULL) + std::string buffer; + int c = EOF; + for (;;) { + buffer.clear(); + while ((c = fgetc(fp)) != EOF) + { + buffer.push_back(c); + if (c == '\n') + break; + } + po_state.line_number += 1; - char *p = line; + char *p = buffer.data(); // skip any BOM (can occur at very start of file) if ((uint8_t)(p[0]) == 0xEF && (uint8_t)(p[1]) == 0xBB && (uint8_t)(p[2]) == 0xBF) @@ -1022,7 +1029,7 @@ void Trans_Read_PO_File(FILE *fp) // NOTE: I assume whitespace at start of line is not valid - if (isspace(*p) || *p == '#') + if (IsSpaceASCII(*p) || *p == '#') { continue; } @@ -1037,15 +1044,15 @@ void Trans_Read_PO_File(FILE *fp) // if we have a pending translation, add it now po_state.Push(); - if (strncmp(p, "msgctxt ", 8) == 0) + if (StringPrefixCompare(p, "msgctxt ") == 0) { po_state.SetContext(p + 8); } - else if (strncmp(p, "msgid ", 6) == 0) + else if (StringPrefixCompare(p, "msgid ") == 0) { po_state.SetId(p + 6); } - else if (strncmp(p, "msgstr ", 7) == 0) + else if (StringPrefixCompare(p, "msgstr ") == 0) { po_state.SetString(p + 7); } @@ -1053,6 +1060,9 @@ void Trans_Read_PO_File(FILE *fp) { LogPrintf("WARNING: unsupported keyword on line %d\n", po_state.line_number); } + + if (feof(fp) || ferror(fp)) + break; } // all done, add the final pending translation @@ -1079,9 +1089,9 @@ void Trans_Init() return; } - std::ifstream trans_fp(path); + FILE *trans_fp = FileOpen(path, "rb"); - if (!trans_fp.is_open()) + if (!trans_fp) { LogPrintf("WARNING: Error opening LANGS.txt!\n"); return; @@ -1089,11 +1099,27 @@ void Trans_Init() LogPrintf("Loading language list: %s\n", path.c_str()); - for (std::string line; std::getline(trans_fp, line);) + std::string buffer; + int c = EOF; + for (;;) { - Trans_ParseLangLine((char *)line.c_str()); + buffer.clear(); + + while ((c = fgetc(trans_fp)) != EOF) + { + buffer.push_back((char)c); + if (c == '\n') + break; + } + + Trans_ParseLangLine((char *)buffer.c_str()); + + if (feof(trans_fp) || ferror(trans_fp)) + break; } + fclose(trans_fp); + LogPrintf("DONE.\n\n"); } @@ -1132,7 +1158,7 @@ void Trans_SetLanguage() path = StringFormat("%s/language/%s.po", install_dir.c_str(), lang_plain.c_str()); } - FILE *fp = FileOpen(path.c_str(), "rb"); + FILE *fp = FileOpen(path, "rb"); if (!fp) { LogPrintf("No translation file: language/%s.po\n", lang_plain.c_str()); diff --git a/source/main.cc b/source/main.cc index 9cfbaa521..c689bde29 100644 --- a/source/main.cc +++ b/source/main.cc @@ -21,6 +21,8 @@ #include "main.h" +#include + #include "csg_main.h" #include "images.h" #ifndef CONSOLE_ONLY @@ -293,14 +295,14 @@ static void main_win_clippy_CB(Fl_Widget *w, void *data) static void ShowInfo() { - StdOutPrintf("\n" + printf("\n" "** %s %s \"%s\"\n" "** Build %s **\n" "** Based on OBLIGE Level Maker (C) 2006-2017 Andrew Apted **\n" "\n", OBSIDIAN_TITLE.c_str(), OBSIDIAN_SHORT_VERSION, OBSIDIAN_CODE_NAME.c_str(), OBSIDIAN_VERSION); - StdOutPrintf("Usage: Obsidian [options...] [key=value...]\n" + printf("Usage: Obsidian [options...] [key=value...]\n" "\n" "Available options:\n" " --version Display build information\n" @@ -334,12 +336,12 @@ static void ShowInfo() " (section should be 'c' or 'o')\n" "\n"); - StdOutPrintf("Please visit the web site for complete information:\n" + printf("Please visit the web site for complete information:\n" " %s \n" "\n", OBSIDIAN_WEBSITE); - StdOutPrintf("This program is free software, under the terms of the GNU General " + printf("This program is free software, under the terms of the GNU General " "Public\n" "License, and comes with ABSOLUTELY NO WARRANTY. See the " "documentation\n" @@ -351,7 +353,7 @@ static void ShowInfo() static void ShowVersion() { - StdOutPrintf("%s %s \"%s\" Build %s\n", OBSIDIAN_TITLE.c_str(), OBSIDIAN_SHORT_VERSION, OBSIDIAN_CODE_NAME.c_str(), + printf("%s %s \"%s\" Build %s\n", OBSIDIAN_TITLE.c_str(), OBSIDIAN_SHORT_VERSION, OBSIDIAN_CODE_NAME.c_str(), OBSIDIAN_VERSION); fflush(stdout); @@ -918,7 +920,7 @@ void Main::Ticker() } #endif -void Main::Detail::Shutdown(const bool error) +void Main::Shutdown(const bool error) { #ifndef CONSOLE_ONLY if (main_win) @@ -1296,7 +1298,7 @@ hardrestart:; #endif ShowInfo(); #if defined WIN32 && !defined CONSOLE_ONLY - std::cout << '\n' << "Close window when finished..."; + printf("\nClose window when finished..."); do { } while (true); @@ -1315,7 +1317,7 @@ hardrestart:; #endif ShowVersion(); #if defined WIN32 && !defined CONSOLE_ONLY - std::cout << '\n' << "Close window when finished..."; + printf("\nClose window when finished..."); do { } while (true); @@ -1333,7 +1335,7 @@ hardrestart:; { if (batch_arg + 1 >= argv::list.size() || argv::IsOption(batch_arg + 1)) { - StdErrPrintf("OBSIDIAN ERROR: missing filename for --batch\n"); + ErrorPrintf("OBSIDIAN ERROR: missing filename for --batch\n"); exit(EXIT_FAILURE); } @@ -1368,19 +1370,19 @@ hardrestart:; if (update_arg + 3 >= argv::list.size() || argv::IsOption(update_arg + 1) || argv::IsOption(update_arg + 2) || argv::IsOption(update_arg + 3)) { - StdErrPrintf("OBSIDIAN ERROR: missing one or more args for --update " + ErrorPrintf("OBSIDIAN ERROR: missing one or more args for --update " "
\n"); exit(EXIT_FAILURE); } if (argv::list[update_arg + 1].length() > 1) { - StdErrPrintf("OBSIDIAN ERROR: section name must be one character\n"); + ErrorPrintf("OBSIDIAN ERROR: section name must be one character\n"); exit(EXIT_FAILURE); } char section = argv::list[update_arg + 1][0]; if (section != 'c' && section != 'o') { - StdErrPrintf("OBSIDIAN ERROR: section name must be 'c' or 'o'\n"); + ErrorPrintf("OBSIDIAN ERROR: section name must be 'c' or 'o'\n"); exit(EXIT_FAILURE); } update_kv.section = section; @@ -1482,7 +1484,7 @@ softrestart:; { if (load_arg + 1 >= argv::list.size() || argv::IsOption(load_arg + 1)) { - StdErrPrintf("OBSIDIAN ERROR: missing filename for --load\n"); + ErrorPrintf("OBSIDIAN ERROR: missing filename for --load\n"); exit(EXIT_FAILURE); } @@ -1515,13 +1517,13 @@ softrestart:; ob_print_reference(); RefClose(); #if defined WIN32 && !defined CONSOLE_ONLY - std::cout << '\n' << "Close window when finished..."; + printf("\nClose window when finished..."); do { } while (true); #endif - Main::Detail::Shutdown(false); + Main::Shutdown(false); return 0; } @@ -1529,13 +1531,13 @@ softrestart:; { ob_print_reference_json(); #if defined WIN32 && !defined CONSOLE_ONLY - std::cout << '\n' << "Close window when finished..."; + printf("\nClose window when finished..."); do { } while (true); #endif - Main::Detail::Shutdown(false); + Main::Shutdown(false); return 0; } @@ -1573,20 +1575,20 @@ softrestart:; } Options_Save(options_file); Cookie_Save(config_file); - Main::Detail::Shutdown(false); + Main::Shutdown(false); return 0; } if (batch_output_file.empty()) { - StdErrPrintf("\nNo output filename given! Did you forget the --batch " + ErrorPrintf("\nNo output filename given! Did you forget the --batch " "parameter?\n"); LogPrintf("\nNo output filename given! Did you forget the --batch " "parameter?\n"); - Main::Detail::Shutdown(false); + Main::Shutdown(false); #if defined WIN32 && !defined CONSOLE_ONLY - std::cout << '\n' << "Close window when finished..."; + printf("\nClose window when finished..."); do { } while (true); @@ -1597,19 +1599,19 @@ softrestart:; Main_SetSeed(); if (!Build_Cool_Shit()) { - StdErrPrintf("FAILED!\n"); + ErrorPrintf("FAILED!\n"); LogPrintf("FAILED!\n"); - Main::Detail::Shutdown(false); + Main::Shutdown(false); #if defined WIN32 && !defined CONSOLE_ONLY - std::cout << '\n' << "Close window when finished..."; + printf("\nClose window when finished..."); do { } while (true); #endif return EXIT_FAILURE; } - Main::Detail::Shutdown(false); + Main::Shutdown(false); return 0; } @@ -1995,7 +1997,7 @@ softrestart:; } } - Main::Detail::Shutdown(false); + Main::Shutdown(false); return 0; } diff --git a/source/main.h b/source/main.h index 1794ad6eb..8aebc13b7 100644 --- a/source/main.h +++ b/source/main.h @@ -23,7 +23,6 @@ #include -#include #include #include #include @@ -192,10 +191,7 @@ void DLG_ManageConfig(); namespace Main { -namespace Detail -{ void Shutdown(bool error); -} template [[noreturn]] void FatalError(std::string_view msg, Args &&...args) { @@ -203,13 +199,13 @@ template [[noreturn]] void FatalError(std::string_view msg, A auto buffer = StringFormat(msg, std::forward(args)...); DLG_ShowError("%s", buffer.c_str()); #endif - Detail::Shutdown(true); + Shutdown(true); if (batch_mode) { - std::cout << "ERROR!\n"; + printf("ERROR!\n"); #ifdef WIN32 - std::cout << '\n' << "Close window when finished..."; + printf("\nClose window when finished..."); do { } while (true); @@ -230,10 +226,10 @@ template void ProgStatus(std::string_view msg, Args &&...args } else if (batch_mode) { - StdErrPrintf("%s\n", buffer.c_str()); + ErrorPrintf("%s\n", buffer.c_str()); } #else - StdErrPrintf("%s\n", buffer.c_str()); + ErrorPrintf("%s\n", buffer.c_str()); #endif } bool BackupFile(const std::string &filename); @@ -280,7 +276,7 @@ class game_interface_c // value is the result from the LUA script, and is false if // an error occurred or the user clicked Abort. // - // For DOOM this will run the AJBSP node builder. + // For idTech 1 games this will run the AJBSP node builder. // // Returns false on error. Note that Finish() is never // called if Start() fails. diff --git a/source/poly.cc b/source/poly.cc index 427d9a714..e9cd30e64 100644 --- a/source/poly.cc +++ b/source/poly.cc @@ -18,6 +18,7 @@ #include +#include "lib_util.h" #include "poly_local.h" #include "sys_macro.h" diff --git a/source/poly_util.cc b/source/poly_util.cc index 2334d0303..c0420fc7e 100644 --- a/source/poly_util.cc +++ b/source/poly_util.cc @@ -21,27 +21,6 @@ namespace ajpoly { -/* result is angle value (degrees) - */ -double ComputeAngle(double dx, double dy) -{ - double angle; - - if (dx == 0) - { - return (dy > 0) ? 90.0 : 270.0; - } - - angle = atan2((double)dy, (double)dx) * 180.0 / M_PI; - - if (angle < 0) - { - angle += 360.0; - } - - return angle; -} - char error_message[4000]; void SetErrorMsg(const char *str, ...) diff --git a/source/poly_util.h b/source/poly_util.h index e194c7805..97b1979fc 100644 --- a/source/poly_util.h +++ b/source/poly_util.h @@ -23,8 +23,5 @@ // set message for certain errors void SetErrorMsg(const char *str, ...); -// compute angle for a 2D vector -double ComputeAngle(double dx, double dy); - //--- editor settings --- // vi:ts=4:sw=4:noexpandtab diff --git a/source/sys_debug.cc b/source/sys_debug.cc index 523a0aa6f..efbddce85 100644 --- a/source/sys_debug.cc +++ b/source/sys_debug.cc @@ -19,19 +19,19 @@ // //------------------------------------------------------------------------ -#include +#include "sys_debug.h" -#include -#include +#include #include "lib_util.h" #include "m_lua.h" #include "main.h" +#include "sys_assert.h" #define DEBUG_BUF_LEN 20000 -std::fstream log_file; -std::fstream ref_file; +FILE *log_file = nullptr; +FILE *ref_file = nullptr; std::string log_filename; std::string ref_filename; @@ -44,9 +44,9 @@ bool LogInit(const std::string &filename) { log_filename = filename; - log_file.open(log_filename, std::ios::out); + log_file = FileOpen(log_filename, "w"); - if (!log_file.is_open()) + if (!log_file) { return false; } @@ -73,9 +73,9 @@ bool RefInit(const std::string &filename) FileDelete(ref_filename); } - ref_file.open(ref_filename, std::ios::out); + ref_file = FileOpen(ref_filename, "w"); - if (!ref_file.is_open()) + if (!ref_file) { return false; } @@ -114,7 +114,8 @@ void LogClose(void) { LogPrintf("\n====== END OF OBSIDIAN LOGS ======\n\n"); - log_file.close(); + fclose(log_file); + log_file = nullptr; log_filename.clear(); } @@ -123,14 +124,148 @@ void RefClose(void) { RefPrintf("\n====== END OF REFERENCE ======\n\n"); - ref_file.close(); + fclose(ref_file); + ref_file = nullptr; ref_filename.clear(); } -void LogReadLines(log_display_func_t display_func, void *priv_data) +void LogPrintf(const char *message, ...) +{ + if (!log_file && !terminal) + return; + + char message_buf[4096]; + + message_buf[4095] = 0; + + // Print the message into a text string + va_list argptr; + + va_start(argptr, message); + vsprintf(message_buf, message, argptr); + va_end(argptr); + + // I hope nobody is printing strings longer than 4096 chars... + SYS_ASSERT(message_buf[4095] == 0); + + if (log_file) + { + fprintf(log_file, "%s", message_buf); + fflush(log_file); + } + + if (terminal) + { + printf("%s", message_buf); + fflush(stdout); + } +} + +void RefPrintf(const char *message, ...) +{ + if (!ref_file && !terminal) + return; + + char message_buf[4096]; + + message_buf[4095] = 0; + + // Print the message into a text string + va_list argptr; + + va_start(argptr, message); + vsprintf(message_buf, message, argptr); + va_end(argptr); + + // I hope nobody is printing strings longer than 4096 chars... + SYS_ASSERT(message_buf[4095] == 0); + + if (ref_file) + { + fprintf(ref_file, "%s", message_buf); + fflush(ref_file); + } + + if (terminal) + { + printf("%s", message_buf); + fflush(stdout); + } +} + +void DebugPrintf(const char *message, ...) +{ + if (!debugging || (!log_file && !terminal)) + return; + + char message_buf[4096]; + + message_buf[4095] = 0; + + // Print the message into a text string + va_list argptr; + + va_start(argptr, message); + vsprintf(message_buf, message, argptr); + va_end(argptr); + + // I hope nobody is printing strings longer than 4096 chars... + SYS_ASSERT(message_buf[4095] == 0); + + if (log_file) + { + fprintf(log_file, "DEBUG: %s", message_buf); + fflush(log_file); + } + + if (terminal) + { + printf("DEBUG: %s", message_buf); + fflush(stdout); + } +} + +[[noreturn]] void ErrorPrintf(const char *message, ...) { + char message_buf[4096]; + + message_buf[4095] = 0; + + // Print the message into a text string + va_list argptr; + + va_start(argptr, message); + vsprintf(message_buf, message, argptr); + va_end(argptr); + // I hope nobody is printing strings longer than 4096 chars... + SYS_ASSERT(message_buf[4095] == 0); + + if (log_file) + { + fprintf(log_file, "ERROR: %s", message_buf); + fflush(log_file); + } + + if (terminal) + printf("ERROR: %s", message_buf); + + Main::Shutdown(true); +#if defined WIN32 && !defined CONSOLE_ONLY + if (batch_mode) + { + printf("\nClose window when finished..."); + do + { + } while (true); + } +#endif + exit(9); +} + +void LogReadLines(log_display_func_t display_func, void *priv_data) +{ if (!log_file) { return; @@ -141,35 +276,49 @@ void LogReadLines(log_display_func_t display_func, void *priv_data) // fussy about opening already open files (in Linux it would // not be an issue). - log_file.close(); + fclose(log_file); + log_file = nullptr; - log_file.open(log_filename, std::ios::in); + log_file = FileOpen(log_filename, "r"); // this is very unlikely to happen, but check anyway - if (!log_file.is_open()) + if (!log_file) { return; } std::string buffer; - while (std::getline(log_file, buffer)) + int c = EOF; + for (;;) { + buffer.clear(); + while ((c = fgetc(log_file)) != EOF) + { + buffer.push_back(c); + if (c == '\n') + break; + } + // remove any newline at the end (LF or CR/LF) StringRemoveCRLF(&buffer); // remove any DEL characters (mainly to workaround an FLTK bug) StringReplaceChar(&buffer, 0x7f, 0); - std::cout << buffer << std::endl; + buffer.push_back('\n'); display_func(buffer, priv_data); + + if (feof(log_file) || ferror(log_file)) + break; } // close the log file after current contents are read - log_file.close(); + fclose(log_file); + log_file = nullptr; // open the log file for writing again - log_file.open(log_filename, std::ios::app); + log_file = FileOpen(log_filename, "a"); } //--- editor settings --- diff --git a/source/sys_debug.h b/source/sys_debug.h index 507d8f3a5..906e85201 100644 --- a/source/sys_debug.h +++ b/source/sys_debug.h @@ -21,15 +21,10 @@ #pragma once -#include -#include -#include #include + extern bool terminal; extern bool debugging; -extern std::fstream log_file; -extern std::fstream ref_file; -extern std::string StringFormat(std::string_view str, ...); bool LogInit(const std::string &filename); // NULL for none void LogClose(void); @@ -39,48 +34,17 @@ void RefClose(void); void LogEnableDebug(bool enable); void LogEnableTerminal(bool enable); -template void LogPrintf(std::string_view str, Args &&...args) -{ - std::string msg = StringFormat(str, args...); - log_file << msg; - if (terminal) - { - std::cout << msg; - } -} -template void RefPrintf(std::string_view str, Args &&...args) -{ - std::string msg = StringFormat(str, args...); - ref_file << msg; - if (terminal) - { - std::cout << msg; - } -} -template void DebugPrintf(std::string_view str, Args &&...args) -{ - if (debugging) - { - std::string msg = StringFormat(str, args...); - log_file << msg; - if (terminal) - { - std::cout << msg; - } - } -} -template void StdOutPrintf(std::string_view str, Args &&...args) -{ - std::cout << StringFormat(str, args...); -} -template void StdErrPrintf(std::string_view str, Args &&...args) -{ - std::cerr << StringFormat(str, args...); -} -template void StreamPrintf(std::ostream &stream, std::string_view str, Args &&...args) -{ - stream << StringFormat(str, args...); -} +#ifdef __GNUC__ +void LogPrintf(const char *message, ...) __attribute__((format(printf, 1, 2))); +void RefPrintf(const char *message, ...) __attribute__((format(printf, 1, 2))); +void DebugPrintf(const char *message, ...) __attribute__((format(printf, 1, 2))); +[[noreturn]] void ErrorPrintf(const char *message, ...) __attribute__((format(printf, 1, 2))); +#else +void LogPrintf(const char *message, ...); +void RefPrintf(const char *message, ...); +void DebugPrintf(const char *message, ...); +[[noreturn]] void ErrorPrintf(const char *message, ...); +#endif using log_display_func_t = void (*)(std::string_view line, void *priv_data); diff --git a/source/ui_game.cc b/source/ui_game.cc index e3be38710..e01b8c3b8 100644 --- a/source/ui_game.cc +++ b/source/ui_game.cc @@ -19,8 +19,6 @@ // //---------------------------------------------------------------- -#include - #include "hdr_fltk.h" #include "hdr_lua.h" #include "hdr_ui.h" diff --git a/source/ui_module.cc b/source/ui_module.cc index d16753c7a..fa658af19 100644 --- a/source/ui_module.cc +++ b/source/ui_module.cc @@ -19,7 +19,7 @@ // //------------------------------------------------------------------------ -#include +#include #include "hdr_fltk.h" #include "hdr_lua.h" @@ -371,7 +371,7 @@ void UI_Module::AddSliderOption(std::string opt, std::string label, std::string } catch (std::exception &e) { - std::cout << e.what(); + fl_message(e.what()); } } skippreset: @@ -690,12 +690,12 @@ bool UI_Module::SetSliderOption(std::string option, std::string value) catch (std::out_of_range &e) { // This shouldn't happen - std::cout << e.what(); + fl_message(e.what()); } catch (std::exception &e) { // This shouldn't happen either - std::cout << e.what(); + fl_message(e.what()); } return true; } @@ -1020,7 +1020,7 @@ void UI_Module::callback_ManualEntry(Fl_Widget *w, void *data) } catch (std::exception &e) { - std::cout << e.what(); + fl_message(e.what()); } if (limit_break)