From f906959a09d58c85d87b445fd1791d91bf278bfa Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Fri, 15 Nov 2013 17:51:29 -0800 Subject: Use new IO classes --- src/map/atcommand.cpp | 43 ++++++++++++++++++++----------------------- src/map/battle.cpp | 9 ++++----- src/map/clif.cpp | 9 +++++---- src/map/grfio.cpp | 8 ++++---- src/map/itemdb.cpp | 11 +++++------ src/map/map.cpp | 24 +++++++++++++----------- src/map/mob.cpp | 16 ++++++++-------- src/map/npc.cpp | 35 +++++++++++++++-------------------- src/map/pc.cpp | 9 ++++----- src/map/script.cpp | 27 ++++++++++++--------------- src/map/skill.cpp | 11 +++++------ 11 files changed, 95 insertions(+), 107 deletions(-) (limited to 'src/map') diff --git a/src/map/atcommand.cpp b/src/map/atcommand.cpp index c8826f7..55596b3 100644 --- a/src/map/atcommand.cpp +++ b/src/map/atcommand.cpp @@ -4,19 +4,19 @@ #include #include -#include - #include "../strings/mstring.hpp" #include "../strings/fstring.hpp" #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" #include "../strings/vstring.hpp" +#include "../io/read.hpp" +#include "../io/write.hpp" + #include "../common/core.hpp" #include "../common/cxxstdio.hpp" #include "../common/extract.hpp" #include "../common/human_time_diff.hpp" -#include "../common/io.hpp" #include "../common/mmo.hpp" #include "../common/nullpo.hpp" #include "../common/random.hpp" @@ -92,9 +92,9 @@ ATCE atcommand_charstreset(const int fd, dumb_ptr sd, void atcommand_config_write(ZString cfgName) { - FILE *out = fopen(cfgName.c_str(), "w"); + io::WriteFile out(cfgName); - if (!out) + if (!out.is_open()) { FPRINTF(stderr, "Failed to write atcommand config: %s\n", cfgName); return; @@ -117,8 +117,6 @@ void atcommand_config_write(ZString cfgName) cmd, info.args, cmd, info.level); } - - fclose(out); } @@ -164,11 +162,11 @@ bool asplit(ZString raw, F *first_arg, R *... rest_args) } static -FILE *get_gm_log(); +io::AppendFile *get_gm_log(); void log_atcommand(dumb_ptr sd, ZString cmd) { - FILE *fp = get_gm_log(); + io::AppendFile *fp = get_gm_log(); if (!fp) return; timestamp_seconds_buffer tmpstr; @@ -176,17 +174,16 @@ void log_atcommand(dumb_ptr sd, ZString cmd) MapName map = (sd->bl_m ? sd->bl_m->name_ : stringish("undefined.gat")); - FPRINTF(fp, "[%s] %s(%d,%d) %s(%d) : %s\n", + FPRINTF(*fp, "[%s] %s(%d,%d) %s(%d) : %s\n", tmpstr, map, sd->bl_x, sd->bl_y, sd->status.name, sd->status.account_id, cmd); - fflush(fp); } FString gm_log; -FILE *get_gm_log() +io::AppendFile *get_gm_log() { if (!gm_log) return NULL; @@ -197,26 +194,26 @@ FILE *get_gm_log() int month = ctime.tm_mon + 1; int logfile_nr = (year * 12) + month; - static FILE *gm_logfile = NULL; + static std::unique_ptr gm_logfile; static int last_logfile_nr = 0; if (logfile_nr == last_logfile_nr) - return gm_logfile; + return gm_logfile.get(); last_logfile_nr = logfile_nr; FString fullname = STRPRINTF("%s.%04d-%02d", gm_log, year, month); if (gm_logfile) - fclose(gm_logfile); + gm_logfile.reset(); - gm_logfile = fopen(fullname.c_str(), "a"); + gm_logfile = make_unique(fullname, true); if (!gm_logfile) { perror("GM log file"); gm_log = FString(); } - return gm_logfile; + return gm_logfile.get(); } bool is_atcommand(const int fd, dumb_ptr sd, @@ -322,7 +319,7 @@ AtCommandInfo *get_atcommandinfo_byname(XString name) int atcommand_config_read(ZString cfgName) { - std::ifstream in(cfgName.c_str()); + io::ReadFile in(cfgName); if (!in.is_open()) { PRINTF("At commands configuration file not found: %s\n", cfgName); @@ -330,7 +327,7 @@ int atcommand_config_read(ZString cfgName) } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { XString w1; ZString w2; @@ -1277,7 +1274,7 @@ ATCE atcommand_item(const int fd, dumb_ptr sd, { ItemName item_name; int number = 0, item_id; - struct item_data *item_data; + struct item_data *item_data = NULL; int get_count, i; if (!extract(message, record<' ', 1>(&item_name, &number))) @@ -3847,7 +3844,7 @@ static ATCE atcommand_character_item_list(const int fd, dumb_ptr sd, ZString message) { - struct item_data *item_data, *item_temp; + struct item_data *item_data = NULL, *item_temp; int i, j, count, counter, counter2; CharName character; @@ -4001,7 +3998,7 @@ ATCE atcommand_character_storage_list(const int fd, dumb_ptr s ZString message) { struct storage *stor; - struct item_data *item_data, *item_temp; + struct item_data *item_data = NULL, *item_temp; int i, j, count, counter, counter2; CharName character; @@ -4121,7 +4118,7 @@ static ATCE atcommand_character_cart_list(const int fd, dumb_ptr sd, ZString message) { - struct item_data *item_data, *item_temp; + struct item_data *item_data = NULL, *item_temp; int i, j, count, counter, counter2; CharName character; diff --git a/src/map/battle.cpp b/src/map/battle.cpp index c95ad61..457c37a 100644 --- a/src/map/battle.cpp +++ b/src/map/battle.cpp @@ -2,13 +2,12 @@ #include -#include - #include "../strings/fstring.hpp" #include "../strings/zstring.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" -#include "../common/io.hpp" #include "../common/nullpo.hpp" #include "../common/random.hpp" @@ -2405,7 +2404,7 @@ int battle_config_read(ZString cfgName) battle_config.mob_splash_radius = -1; } - std::ifstream in(cfgName.c_str()); + io::ReadFile in(cfgName); if (!in.is_open()) { PRINTF("file not found: %s\n", cfgName); @@ -2413,7 +2412,7 @@ int battle_config_read(ZString cfgName) } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { #define BATTLE_CONFIG_VAR(name) {{#name}, &battle_config.name} const struct diff --git a/src/map/clif.cpp b/src/map/clif.cpp index 0ac518e..1a38ebe 100644 --- a/src/map/clif.cpp +++ b/src/map/clif.cpp @@ -10,6 +10,8 @@ #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" +#include "../io/write.hpp" + #include "../common/cxxstdio.hpp" #include "../common/md5calc.hpp" #include "../common/random.hpp" @@ -5536,8 +5538,7 @@ void clif_parse(int fd) #ifdef DUMP_UNKNOWN_PACKET { int i; - FILE *fp; - char packet_txt[256] = "save/packet.txt"; + ZString packet_txt = "save/packet.txt"; PRINTF("---- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F"); for (i = 0; i < packet_len; i++) { @@ -5554,7 +5555,8 @@ void clif_parse(int fd) else if (sd) // not authentified! (refused by char-server or disconnect before to be authentified) PRINTF("\nAccount ID %d.\n", sd->bl_id); - if ((fp = fopen(packet_txt, "a")) == NULL) + io::AppendFile fp(packet_txt); + if (!fp.is_open()) { PRINTF("clif.c: cant write [%s] !!! data is lost !!!\n", packet_txt); @@ -5586,7 +5588,6 @@ void clif_parse(int fd) FPRINTF(fp, "%02X ", RFIFOB(fd, i)); } FPRINTF(fp, "\n\n"); - fclose(fp); } } #endif diff --git a/src/map/grfio.cpp b/src/map/grfio.cpp index 41a285f..c486cf4 100644 --- a/src/map/grfio.cpp +++ b/src/map/grfio.cpp @@ -10,22 +10,22 @@ #include #include -#include #include #include "../strings/mstring.hpp" #include "../strings/fstring.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" #include "../common/extract.hpp" -#include "../common/io.hpp" #include "../poison.hpp" static std::map load_resnametable() { - std::ifstream in("data/resnametable.txt"); + io::ReadFile in("data/resnametable.txt"); if (!in.is_open()) { fprintf(stderr, "Missing data/resnametable.txt"); @@ -34,7 +34,7 @@ std::map load_resnametable() std::map out; FString line; - while (io::getline(in, line)) + while (in.getline(line)) { MapName key; FString value; diff --git a/src/map/itemdb.cpp b/src/map/itemdb.cpp index a8d8b55..5a11aa1 100644 --- a/src/map/itemdb.cpp +++ b/src/map/itemdb.cpp @@ -3,16 +3,15 @@ #include #include -#include - #include "../strings/fstring.hpp" #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" #include "../common/db.hpp" #include "../common/extract.hpp" -#include "../common/io.hpp" #include "../common/nullpo.hpp" #include "../common/random.hpp" #include "../common/socket.hpp" @@ -157,10 +156,10 @@ static int itemdb_readdb(void) { int ln = 0, lines = 0; - const char *filename = "db/item_db.txt"; + ZString filename = "db/item_db.txt"; { - std::ifstream in(filename); + io::ReadFile in(filename); if (!in.is_open()) { @@ -171,7 +170,7 @@ int itemdb_readdb(void) lines = 0; FString line; - while (io::getline(in, line)) + while (in.getline(line)) { lines++; if (!line) diff --git a/src/map/map.cpp b/src/map/map.cpp index 8845b23..d259451 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -10,18 +10,18 @@ #include #include -#include - #include "../strings/fstring.hpp" #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" #include "../strings/vstring.hpp" +#include "../io/write.hpp" +#include "../io/read.hpp" + #include "../common/core.hpp" #include "../common/cxxstdio.hpp" #include "../common/db.hpp" #include "../common/extract.hpp" -#include "../common/io.hpp" #include "../common/random2.hpp" #include "../common/nullpo.hpp" #include "../common/socket.hpp" @@ -1343,7 +1343,7 @@ void map_delmap(MapName mapname) constexpr int LOGFILE_SECONDS_PER_CHUNK_SHIFT = 10; static -FILE *map_logfile = NULL; +std::unique_ptr map_logfile; static FString map_logfile_name; static @@ -1364,8 +1364,7 @@ void map_close_logfile(void) }; char **argv = const_cast(args); - fclose(map_logfile); - map_logfile = NULL; + map_logfile.reset(); if (!fork()) { @@ -1385,9 +1384,12 @@ void map_start_logfile(long index) "%s.%ld", map_logfile_name, map_logfile_index); - map_logfile = fopen(filename_buf.c_str(), "w+"); - if (!map_logfile) + map_logfile = make_unique(filename_buf); + if (!map_logfile->is_open()) + { + map_logfile.reset(); perror(map_logfile_name.c_str()); + } } static @@ -1417,7 +1419,7 @@ void map_log(XString line) map_start_logfile(i); } - log_with_timestamp(map_logfile, line); + log_with_timestamp(*map_logfile, line); } /*========================================== @@ -1429,7 +1431,7 @@ int map_config_read(ZString cfgName) { struct hostent *h = NULL; - std::ifstream in(cfgName.c_str()); + io::ReadFile in(cfgName); if (!in.is_open()) { PRINTF("Map configuration file not found at: %s\n", cfgName); @@ -1437,7 +1439,7 @@ int map_config_read(ZString cfgName) } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { XString w1; ZString w2; diff --git a/src/map/mob.cpp b/src/map/mob.cpp index 1885fbd..34374a7 100644 --- a/src/map/mob.cpp +++ b/src/map/mob.cpp @@ -7,14 +7,14 @@ #include #include -#include #include "../strings/fstring.hpp" #include "../strings/xstring.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" #include "../common/extract.hpp" -#include "../common/io.hpp" #include "../common/nullpo.hpp" #include "../common/random.hpp" #include "../common/socket.hpp" @@ -3415,19 +3415,19 @@ bool extract(XString str, LevelElement *le) static int mob_readdb(void) { - const char *filename = "db/mob_db.txt"; + ZString filename = "db/mob_db.txt"; for (mob_db_& e : mob_db) e = mob_db_{}; { - std::ifstream in(filename); + io::ReadFile in(filename); if (!in.is_open()) { return -1; } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { int mob_class; @@ -3632,17 +3632,17 @@ bool extract(XString str, MobSkillTarget *mst) static int mob_readskilldb(void) { - const char *filename = "db/mob_skill_db.txt"; + ZString filename = "db/mob_skill_db.txt"; { - std::ifstream in(filename); + io::ReadFile in(filename); if (!in.is_open()) { PRINTF("can't read %s\n", filename); return 0; } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { int mob_id; diff --git a/src/map/npc.cpp b/src/map/npc.cpp index e3038a1..e6c7445 100644 --- a/src/map/npc.cpp +++ b/src/map/npc.cpp @@ -12,6 +12,8 @@ #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" #include "../common/db.hpp" #include "../common/extract.hpp" @@ -1160,7 +1162,7 @@ void npc_convertlabel_db(ScriptLabel lname, int pos, dumb_ptr n */ static int npc_parse_script(XString w1, XString w2, NpcName w3, ZString w4, - XString first_line, FILE *fp, int *lines) + XString first_line, io::ReadFile& fp, int *lines) { int x, y; DIR dir = DIR::S; @@ -1206,14 +1208,11 @@ int npc_parse_script(XString w1, XString w2, NpcName w3, ZString w4, if (it != srcbuf.rend() && *it == '}') break; - char line_[1024]; - if (!fgets(line_, 1020, fp)) + FString line; + if (!fp.getline(line)) // eof break; (*lines)++; - if (feof(fp)) - break; - ZString line(strings::really_construct_from_a_pointer, line_, nullptr); if (!srcbuf) { // may be a no-op @@ -1224,6 +1223,7 @@ int npc_parse_script(XString w1, XString w2, NpcName w3, ZString w4, } else srcbuf += line; + srcbuf += '\n'; } script = parse_script(FString(srcbuf), startline); if (script == NULL) @@ -1391,7 +1391,7 @@ int npc_parse_script(XString w1, XString w2, NpcName w3, ZString w4, */ static int npc_parse_function(XString, XString, XString w3, ZString, - XString first_line, FILE *fp, int *lines) + XString first_line, io::ReadFile& fp, int *lines) { MString srcbuf; srcbuf += first_line.xislice_t(std::find(first_line.begin(), first_line.end(), '{')); @@ -1402,13 +1402,11 @@ int npc_parse_function(XString, XString, XString w3, ZString, auto it = std::find_if_not(srcbuf.rbegin(), srcbuf.rend(), [](char c){ return c == ' ' || c == '\n'; }); if (it != srcbuf.rend() && *it == '}') break; - char line_[1024]; - if (!fgets(line_, 1020, fp)) + + FString line; + if (!fp.getline(line)) break; (*lines)++; - if (feof(fp)) - break; - ZString line(strings::really_construct_from_a_pointer, line_, nullptr); if (!srcbuf) { srcbuf += line.xislice_t(std::find(line.begin(), line.end(), '{')); @@ -1416,6 +1414,7 @@ int npc_parse_function(XString, XString, XString w3, ZString, } else srcbuf += line; + srcbuf += '\n'; } std::unique_ptr script = parse_script(FString(srcbuf), startline); if (script == NULL) @@ -1722,8 +1721,8 @@ int do_init_npc(void) for (; !npc_srcs.empty(); npc_srcs.pop_front()) { FString nsl = npc_srcs.front(); - FILE *fp = fopen(nsl.c_str(), "r"); - if (fp == NULL) + io::ReadFile fp(nsl); + if (!fp.is_open()) { PRINTF("file not found : %s\n", nsl); exit(1); @@ -1731,12 +1730,9 @@ int do_init_npc(void) PRINTF("\rLoading NPCs [%d]: %-54s", npc_id - START_NPC_NUM, nsl); int lines = 0; - char line_[1024]; - while (fgets(line_, 1020, fp)) + FString zline; + while (fp.getline(zline)) { - // because it's still fgets - line_[strlen(line_) - 1] = '\0'; - ZString zline(strings::really_construct_from_a_pointer, line_, nullptr); XString w1, w2, w3, w4x; ZString w4z; lines++; @@ -1807,7 +1803,6 @@ int do_init_npc(void) PRINTF("odd script line: %s\n", zline); } } - fclose(fp); fflush(stdout); } PRINTF("\rNPCs Loaded: %d [Warps:%d Shops:%d Scripts:%d Mobs:%d] %20s\n", diff --git a/src/map/pc.cpp b/src/map/pc.cpp index a723555..618d491 100644 --- a/src/map/pc.cpp +++ b/src/map/pc.cpp @@ -4,13 +4,12 @@ #include #include -#include - #include "../strings/fstring.hpp" #include "../strings/zstring.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" -#include "../common/io.hpp" #include "../common/nullpo.hpp" #include "../common/random.hpp" #include "../common/socket.hpp" @@ -778,11 +777,11 @@ int pc_authok(int id, int login_id2, TimeT connect_until_time, void pc_show_motd(dumb_ptr sd) { sd->state.seen_motd = true; - std::ifstream in(motd_txt.c_str()); + io::ReadFile in(motd_txt); if (in.is_open()) { FString buf; - while (io::getline(in, buf)) + while (in.getline(buf)) { clif_displaymessage(sd->fd, buf); } diff --git a/src/map/script.cpp b/src/map/script.cpp index 4b009b1..f7a87a5 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -7,19 +7,18 @@ #include #include -#include - #include "../strings/mstring.hpp" #include "../strings/fstring.hpp" #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" +#include "../io/lock.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" #include "../common/db.hpp" #include "../common/extract.hpp" #include "../common/intern-pool.hpp" -#include "../common/io.hpp" -#include "../common/lock.hpp" #include "../common/random.hpp" #include "../common/socket.hpp" #include "../common/utils.hpp" @@ -688,7 +687,7 @@ void add_builtin_functions(void) static void read_constdb(void) { - std::ifstream in("db/const.txt"); + io::ReadFile in("db/const.txt"); if (!in.is_open()) { PRINTF("can't read db/const.txt\n"); @@ -696,7 +695,7 @@ void read_constdb(void) } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { if (line.startswith("//")) continue; @@ -704,6 +703,7 @@ void read_constdb(void) FString name; int val; int type = 0; // if not provided + // TODO get rid of SSCANF - this is the last serious use if (SSCANF(line, "%m[A-Za-z0-9_] %i %i", &name, &val, &type) < 2) continue; str_data_t *n = add_strp(name); @@ -4898,13 +4898,13 @@ void mapreg_setregstr(SIR reg, XString str) static void script_load_mapreg(void) { - std::ifstream in(mapreg_txt.c_str()); + io::ReadFile in(mapreg_txt); if (!in.is_open()) return; FString line; - while (io::getline(in, line)) + while (in.getline(line)) { XString buf1, buf2; int index = 0; @@ -4946,7 +4946,7 @@ void script_load_mapreg(void) *------------------------------------------ */ static -void script_save_mapreg_intsub(SIR key, int data, FILE *fp) +void script_save_mapreg_intsub(SIR key, int data, io::WriteFile& fp) { int num = key.base(), i = key.index(); ZString name = variable_names.outtern(num); @@ -4960,7 +4960,7 @@ void script_save_mapreg_intsub(SIR key, int data, FILE *fp) } static -void script_save_mapreg_strsub(SIR key, ZString data, FILE *fp) +void script_save_mapreg_strsub(SIR key, ZString data, io::WriteFile& fp) { int num = key.base(), i = key.index(); ZString name = variable_names.outtern(num); @@ -4976,16 +4976,13 @@ void script_save_mapreg_strsub(SIR key, ZString data, FILE *fp) static void script_save_mapreg(void) { - FILE *fp; - int lock; - - if ((fp = lock_fopen(mapreg_txt, &lock)) == NULL) + io::WriteLock fp(mapreg_txt); + if (!fp.is_open()) return; for (auto& pair : mapreg_db) script_save_mapreg_intsub(pair.first, pair.second, fp); for (auto& pair : mapregstr_db) script_save_mapreg_strsub(pair.first, pair.second, fp); - lock_fclose(fp, mapreg_txt, &lock); mapreg_dirty = 0; } diff --git a/src/map/skill.cpp b/src/map/skill.cpp index ab5f55b..9187335 100644 --- a/src/map/skill.cpp +++ b/src/map/skill.cpp @@ -5,15 +5,14 @@ #include #include -#include - #include "../strings/mstring.hpp" #include "../strings/fstring.hpp" #include "../strings/xstring.hpp" +#include "../io/read.hpp" + #include "../common/cxxstdio.hpp" #include "../common/extract.hpp" -#include "../common/io.hpp" #include "../common/nullpo.hpp" #include "../common/random.hpp" #include "../common/socket.hpp" @@ -1186,15 +1185,15 @@ int skill_readdb(void) for (skill_db_& skdb : skill_db) skdb = skill_db_{}; - std::ifstream in("db/skill_db.txt"); - if (!in) + io::ReadFile in("db/skill_db.txt"); + if (!in.is_open()) { PRINTF("can't read db/skill_db.txt\n"); return 1; } FString line_; - while (io::getline(in, line_)) + while (in.getline(line_)) { XString comment = "//"; XString line = line_.xislice_h(std::search(line_.begin(), line_.end(), comment.begin(), comment.end())).rstrip(); -- cgit v1.2.3-60-g2f50