diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-11-15 17:51:29 -0800 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-11-15 17:51:29 -0800 |
commit | f906959a09d58c85d87b445fd1791d91bf278bfa (patch) | |
tree | b65282b2c731d4eb2157ae719828cddf6b086943 /src/char | |
parent | 1fb7ce5a604db78c4d02f719053827269705ce13 (diff) | |
download | tmwa-f906959a09d58c85d87b445fd1791d91bf278bfa.tar.gz tmwa-f906959a09d58c85d87b445fd1791d91bf278bfa.tar.bz2 tmwa-f906959a09d58c85d87b445fd1791d91bf278bfa.tar.xz tmwa-f906959a09d58c85d87b445fd1791d91bf278bfa.zip |
Use new IO classes
Diffstat (limited to 'src/char')
-rw-r--r-- | src/char/char.cpp | 45 | ||||
-rw-r--r-- | src/char/int_party.cpp | 24 | ||||
-rw-r--r-- | src/char/int_storage.cpp | 21 | ||||
-rw-r--r-- | src/char/inter.cpp | 26 |
4 files changed, 48 insertions, 68 deletions
diff --git a/src/char/char.cpp b/src/char/char.cpp index 170e91a..a3b1a27 100644 --- a/src/char/char.cpp +++ b/src/char/char.cpp @@ -13,7 +13,6 @@ #include <ctime> #include <bitset> -#include <fstream> #include <set> #include "../strings/mstring.hpp" @@ -21,13 +20,14 @@ #include "../strings/zstring.hpp" #include "../strings/xstring.hpp" +#include "../io/lock.hpp" +#include "../io/read.hpp" + #include "../common/core.hpp" #include "../common/cxxstdio.hpp" #include "../common/db.hpp" #include "../common/extract.hpp" #include "../common/human_time_diff.hpp" -#include "../common/io.hpp" -#include "../common/lock.hpp" #include "../common/socket.hpp" #include "../common/timer.hpp" #include "../common/version.hpp" @@ -162,11 +162,10 @@ pid_t pid = 0; // For forked DB writes //------------------------------ void char_log(XString line) { - FILE *logfp = fopen(char_log_filename.c_str(), "a"); - if (!logfp) + io::AppendFile logfp(char_log_filename, true); + if (!logfp.is_open()) return; log_with_timestamp(logfp, line); - fclose(logfp); } //---------------------------------------------------------------------- @@ -436,7 +435,7 @@ int mmo_char_init(void) char_data.clear(); online_chars.clear(); - std::ifstream in(char_txt.c_str()); + io::ReadFile in(char_txt); if (!in.is_open()) { PRINTF("Characters file not found: %s.\n", char_txt); @@ -448,7 +447,7 @@ int mmo_char_init(void) int line_count = 0; FString line; - while (io::getline(in, line)) + while (in.getline(line)) { line_count++; @@ -494,9 +493,8 @@ int mmo_char_init(void) static void mmo_char_sync(void) { - int lock; - FILE *fp = lock_fopen(char_txt, &lock); - if (fp == NULL) + io::WriteLock fp(char_txt); + if (!fp.is_open()) { PRINTF("WARNING: Server can't not save characters.\n"); CHAR_LOG("WARNING: Server can't not save characters.\n"); @@ -507,11 +505,9 @@ void mmo_char_sync(void) for (mmo_charstatus& cd : char_data) { FString line = mmo_char_tostr(&cd); - fwrite(line.data(), 1, line.size(), fp); - fputc('\n', fp); + fp.put_line(line); } FPRINTF(fp, "%d\t%%newid%%\n", char_id_count); - lock_fclose(fp, char_txt, &lock); } } @@ -725,14 +721,13 @@ static void create_online_files(void) { // write files - FILE *fp = fopen(online_txt_filename.c_str(), "w"); - if (fp != NULL) + io::WriteFile fp(online_txt_filename); + if (fp.is_open()) { - FILE *fp2 = fopen(online_html_filename.c_str(), "w"); - if (fp2 != NULL) + io::WriteFile fp2(online_html_filename); + if (fp2.is_open()) { // get time -#warning "Need to convert/check the PHP code" timestamp_seconds_buffer timetemp; stamp_time(timetemp); // write heading @@ -836,9 +831,7 @@ void create_online_files(void) } FPRINTF(fp2, " </BODY>\n"); FPRINTF(fp2, "</HTML>\n"); - fclose(fp2); } - fclose(fp); } return; @@ -1363,7 +1356,7 @@ void parse_tologin(int fd) if (RFIFOREST(fd) < 6) return; // Deletion of all characters of the account -#warning "This comment is a lie, but it's still true." +//#warning "This comment is a lie, but it's still true." // needs to use index because they may move during resize for (int idx = 0; idx < char_data.size(); idx++) { @@ -2551,7 +2544,7 @@ int lan_config_read(ZString lancfgName) lan_map_ip = IP4_LOCALHOST; lan_subnet = IP4Mask(IP4_LOCALHOST, IP4_BROADCAST); - std::ifstream in(lancfgName.c_str()); + io::ReadFile in(lancfgName); if (!in.is_open()) { @@ -2562,7 +2555,7 @@ int lan_config_read(ZString lancfgName) PRINTF("---start reading of Lan Support configuration...\n"); FString line; - while (io::getline(in, line)) + while (in.getline(line)) { XString w1; ZString w2; @@ -2626,7 +2619,7 @@ int char_config_read(ZString cfgName) { struct hostent *h = NULL; - std::ifstream in(cfgName.c_str()); + io::ReadFile in(cfgName); if (!in.is_open()) { @@ -2635,7 +2628,7 @@ int char_config_read(ZString cfgName) } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { XString w1; ZString w2; diff --git a/src/char/int_party.cpp b/src/char/int_party.cpp index 0597efd..41aa9c3 100644 --- a/src/char/int_party.cpp +++ b/src/char/int_party.cpp @@ -3,17 +3,16 @@ #include <cstdlib> #include <cstring> -#include <fstream> - #include "../strings/mstring.hpp" #include "../strings/fstring.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/io.hpp" -#include "../common/lock.hpp" #include "../common/mmo.hpp" #include "../common/socket.hpp" @@ -105,14 +104,14 @@ bool extract(XString str, party *p) // パーティデータのロード int inter_party_init(void) { - std::ifstream in(party_txt.c_str()); + io::ReadFile in(party_txt); if (!in.is_open()) return 1; // TODO: convert to use char_id, and change to extract() FString line; int c = 0; - while (io::getline(in, line)) + while (in.getline(line)) { int i, j = 0; if (SSCANF(line, "%d\t%%newid%%\n%n", &i, &j) == 1 && j > 0 @@ -144,19 +143,17 @@ int inter_party_init(void) // パーティーデータのセーブ用 static -void inter_party_save_sub(struct party *data, FILE *fp) +void inter_party_save_sub(struct party *data, io::WriteFile& fp) { FString line = inter_party_tostr(data); - FPRINTF(fp, "%s\n", line); + fp.put_line(line); } // パーティーデータのセーブ int inter_party_save(void) { - FILE *fp; - int lock; - - if ((fp = lock_fopen(party_txt, &lock)) == NULL) + io::WriteLock fp(party_txt); + if (!fp.is_open()) { PRINTF("int_party: cant write [%s] !!! data is lost !!!\n", party_txt); @@ -164,9 +161,6 @@ int inter_party_save(void) } for (auto& pair : party_db) inter_party_save_sub(&pair.second, fp); -// FPRINTF(fp, "%d\t%%newid%%\n", party_newid); - lock_fclose(fp, party_txt, &lock); -// PRINTF("int_party: %s saved.\n", party_txt); return 0; } diff --git a/src/char/int_storage.cpp b/src/char/int_storage.cpp index 205b21e..304a792 100644 --- a/src/char/int_storage.cpp +++ b/src/char/int_storage.cpp @@ -4,17 +4,17 @@ #include <cstring> #include <functional> -#include <fstream> #include "../strings/mstring.hpp" #include "../strings/fstring.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/io.hpp" -#include "../common/lock.hpp" #include "../common/mmo.hpp" #include "../common/socket.hpp" @@ -104,7 +104,7 @@ int inter_storage_init(void) { int c = 0; - std::ifstream in(storage_txt.c_str()); + io::ReadFile in(storage_txt); if (!in.is_open()) { PRINTF("cant't read : %s\n", storage_txt); @@ -112,7 +112,7 @@ int inter_storage_init(void) } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { struct storage s {}; if (extract(line, &s)) @@ -131,21 +131,20 @@ int inter_storage_init(void) } static -void inter_storage_save_sub(struct storage *data, FILE *fp) +void inter_storage_save_sub(struct storage *data, io::WriteFile& fp) { FString line = storage_tostr(data); if (line) - FPRINTF(fp, "%s\n", line); + fp.put_line(line); } //--------------------------------------------------------- // 倉庫データを書き込む int inter_storage_save(void) { - FILE *fp; - int lock; + io::WriteLock fp(storage_txt); - if ((fp = lock_fopen(storage_txt, &lock)) == NULL) + if (!fp.is_open()) { PRINTF("int_storage: cant write [%s] !!! data is lost !!!\n", storage_txt); @@ -153,8 +152,6 @@ int inter_storage_save(void) } for (auto& pair : storage_db) inter_storage_save_sub(&pair.second, fp); - lock_fclose(fp, storage_txt, &lock); -// PRINTF("int_storage: %s saved.\n",storage_txt); return 0; } diff --git a/src/char/inter.cpp b/src/char/inter.cpp index b951cc1..330ea66 100644 --- a/src/char/inter.cpp +++ b/src/char/inter.cpp @@ -4,7 +4,6 @@ #include <cstdlib> #include <cstring> -#include <fstream> #include <vector> #include "../strings/mstring.hpp" @@ -12,11 +11,12 @@ #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/io.hpp" -#include "../common/lock.hpp" #include "../common/socket.hpp" #include "../common/timer.hpp" #include "../common/utils.hpp" @@ -110,11 +110,11 @@ int inter_accreg_init(void) { int c = 0; - std::ifstream in(accreg_txt.c_str()); + io::ReadFile in(accreg_txt); if (!in.is_open()) return 1; FString line; - while (io::getline(in, line)) + while (in.getline(line)) { struct accreg reg {}; if (extract(line, ®)) @@ -134,13 +134,12 @@ int inter_accreg_init(void) // アカウント変数のセーブ用 static -void inter_accreg_save_sub(struct accreg *reg, FILE *fp) +void inter_accreg_save_sub(struct accreg *reg, io::WriteFile& fp) { if (reg->reg_num > 0) { FString line = inter_accreg_tostr(reg); - fwrite(line.data(), 1, line.size(), fp); - fputc('\n', fp); + fp.put_line(line); } } @@ -148,10 +147,8 @@ void inter_accreg_save_sub(struct accreg *reg, FILE *fp) static int inter_accreg_save(void) { - FILE *fp; - int lock; - - if ((fp = lock_fopen(accreg_txt, &lock)) == NULL) + io::WriteLock fp(accreg_txt); + if (!fp.is_open()) { PRINTF("int_accreg: cant write [%s] !!! data is lost !!!\n", accreg_txt); @@ -159,7 +156,6 @@ int inter_accreg_save(void) } for (auto& pair : accreg_db) inter_accreg_save_sub(&pair.second, fp); - lock_fclose(fp, accreg_txt, &lock); return 0; } @@ -173,7 +169,7 @@ int inter_accreg_save(void) static int inter_config_read(ZString cfgName) { - std::ifstream in(cfgName.c_str()); + io::ReadFile in(cfgName); if (!in.is_open()) { PRINTF("file not found: %s\n", cfgName); @@ -181,7 +177,7 @@ int inter_config_read(ZString cfgName) } FString line; - while (io::getline(in, line)) + while (in.getline(line)) { XString w1; ZString w2; |