diff options
author | Adam Borowski <kilobyte@angband.pl> | 2010-12-16 15:56:14 +0100 |
---|---|---|
committer | Adam Borowski <kilobyte@angband.pl> | 2010-12-16 16:53:03 +0100 |
commit | ad3bbac308f31e2c8bef95e921152f34f00cc785 (patch) | |
tree | 8315917941e95878b6788b17603ecabbbd39df1c /crawl-ref | |
parent | c182b846613a797cb37fcda578553e40814b7e65 (diff) | |
download | crawl-ref-ad3bbac308f31e2c8bef95e921152f34f00cc785.tar.gz crawl-ref-ad3bbac308f31e2c8bef95e921152f34f00cc785.zip |
Rename TextFileReader to FileLineInput I didn't notice, use it for config files.
Diffstat (limited to 'crawl-ref')
-rw-r--r-- | crawl-ref/source/initfile.cc | 35 | ||||
-rw-r--r-- | crawl-ref/source/initfile.h | 35 | ||||
-rw-r--r-- | crawl-ref/source/macro.cc | 2 | ||||
-rw-r--r-- | crawl-ref/source/options.h | 4 | ||||
-rw-r--r-- | crawl-ref/source/unicode.cc | 6 | ||||
-rw-r--r-- | crawl-ref/source/unicode.h | 21 |
6 files changed, 36 insertions, 67 deletions
diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index 66ee55c3ae..71eeb16777 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -45,7 +45,6 @@ #include "tags.h" #include "travel.h" #include "items.h" -#include "unicode.h" #include "view.h" #include "viewchar.h" @@ -1333,8 +1332,8 @@ std::string read_init_file(bool runscript) const std::string init_file_name(_find_crawlrc()); - FILE* f = fopen_u(init_file_name.c_str(), "r"); - if (f == NULL) + FileLineInput f(init_file_name.c_str()); + if (f.error()) { if (!init_file_name.empty()) return make_stringf("(\"%s\" is not readable)", @@ -1354,8 +1353,7 @@ std::string read_init_file(bool runscript) #else Options.basefilename = "init.txt"; #endif - read_options(f, runscript); - fclose(f); + Options.read_options(f, runscript); Options.filename = "extra opts last"; Options.basefilename = "extra opts last"; @@ -1376,15 +1374,12 @@ std::string read_init_file(bool runscript) newgame_def read_startup_prefs() { #ifndef DISABLE_STICKY_STARTUP_OPTIONS - std::string fn = get_prefs_filename(); - FILE *f = fopen_u(fn.c_str(), "r"); - if (!f) + FileLineInput fl(get_prefs_filename().c_str()); + if (fl.error()) return newgame_def(); game_options temp; - FileLineInput fl(f); temp.read_options(fl, false); - fclose(f); return (temp.game); #endif // !DISABLE_STICKY_STARTUP_OPTIONS @@ -1472,12 +1467,6 @@ void save_player_name() #endif // !DISABLE_STICKY_STARTUP_OPTIONS } -void read_options(FILE *f, bool runscript) -{ - FileLineInput fl(f); - Options.read_options(fl, runscript); -} - void read_options(const std::string &s, bool runscript, bool clear_aliases) { StringLineInput st(s); @@ -1489,7 +1478,7 @@ game_options::game_options() reset_options(); } -void game_options::read_options(InitLineInput &il, bool runscript, +void game_options::read_options(LineInput &il, bool runscript, bool clear_aliases) { unsigned int line = 0; @@ -1509,7 +1498,7 @@ void game_options::read_options(InitLineInput &il, bool runscript, while (!il.eof()) { line_num++; - std::string s = il.getline(); + std::string s = il.get_line(); std::string str = s; line++; @@ -3461,13 +3450,9 @@ void game_options::include(const std::string &rawfilename, // Also unwind any aliases defined in included files. unwind_var<string_map> unwalias(aliases); - FILE* f = fopen_u(include_file.c_str(), "r"); - if (f) - { - FileLineInput fl(f); - this->read_options(fl, runscript, false); - fclose(f); - } + FileLineInput fl(include_file.c_str()); + if (!fl.error()) + read_options(fl, runscript, false); } void game_options::report_error(const std::string &error) diff --git a/crawl-ref/source/initfile.h b/crawl-ref/source/initfile.h index 18d77d0df6..99d172e655 100644 --- a/crawl-ref/source/initfile.h +++ b/crawl-ref/source/initfile.h @@ -12,6 +12,7 @@ #include <cstdio> #include "enum.h" +#include "unicode.h" enum drop_mode_type { @@ -27,7 +28,6 @@ std::string read_init_file(bool runscript = false); struct newgame_def; newgame_def read_startup_prefs(); -void read_options(FILE *f, bool runscript = false); void read_options(const std::string &s, bool runscript = false, bool clear_aliases = false); @@ -84,36 +84,7 @@ std::string channel_to_str(int ch); int str_to_channel(const std::string &); -class InitLineInput -{ -public: - virtual ~InitLineInput() { } - virtual bool eof() = 0; - virtual std::string getline() = 0; -}; - -class FileLineInput : public InitLineInput -{ -public: - FileLineInput(FILE *f) : file(f) { } - - bool eof() - { - return !file || feof(file); - } - - std::string getline() - { - char s[256] = ""; - if (!eof()) - fgets(s, sizeof s, file); - return (s); - } -private: - FILE *file; -}; - -class StringLineInput : public InitLineInput +class StringLineInput : public LineInput { public: StringLineInput(const std::string &s) : str(s), pos(0) { } @@ -123,7 +94,7 @@ public: return pos >= str.length(); } - std::string getline() + std::string get_line() { if (eof()) return ""; diff --git a/crawl-ref/source/macro.cc b/crawl-ref/source/macro.cc index 45c01cbfeb..5cb0c21e5a 100644 --- a/crawl-ref/source/macro.cc +++ b/crawl-ref/source/macro.cc @@ -986,7 +986,7 @@ static void _read_macros_from(const char* filename) return; std::string s; - TextFileReader f(filename); + FileLineInput f(filename); keyseq key, action; bool keymap = false; KeymapContext keymc = KMC_DEFAULT; diff --git a/crawl-ref/source/options.h b/crawl-ref/source/options.h index acbd2ee050..55836c5c8c 100644 --- a/crawl-ref/source/options.h +++ b/crawl-ref/source/options.h @@ -5,7 +5,7 @@ #include "pattern.h" #include "newgame_def.h" -class InitLineInput; +class LineInput; struct game_options { public: @@ -13,7 +13,7 @@ public: void reset_options(); void read_option_line(const std::string &s, bool runscripts = false); - void read_options(InitLineInput &, bool runscripts, + void read_options(LineInput &, bool runscripts, bool clear_aliases = true); void include(const std::string &file, bool resolve, bool runscript); diff --git a/crawl-ref/source/unicode.cc b/crawl-ref/source/unicode.cc index 87d43576e1..5f09ac9d1e 100644 --- a/crawl-ref/source/unicode.cc +++ b/crawl-ref/source/unicode.cc @@ -246,7 +246,7 @@ static bool _check_trail(FILE *f, const char* bytes, int len) return true; } -TextFileReader::TextFileReader(const char *name) +FileLineInput::FileLineInput(const char *name) { f = fopen_u(name, "r"); if (!f) @@ -283,13 +283,13 @@ TextFileReader::TextFileReader(const char *name) } } -TextFileReader::~TextFileReader() +FileLineInput::~FileLineInput() { if (f) fclose(f); } -std::string TextFileReader::get_line() +std::string FileLineInput::get_line() { ASSERT(f); std::wstring win; // actually, these are more of a lose diff --git a/crawl-ref/source/unicode.h b/crawl-ref/source/unicode.h index 5de066ea99..4e0ffa206a 100644 --- a/crawl-ref/source/unicode.h +++ b/crawl-ref/source/unicode.h @@ -4,6 +4,8 @@ * manipulation functions that act on character types. * Written by: Adam Borowski */ +#ifndef UNICODE_H +#define UNICODE_H int wctoutf8(char *d, ucs_t s); int utf8towc(ucs_t *d, const char *s); @@ -25,7 +27,16 @@ static inline std::string mb_to_utf8(const std::string &s) #define OUTS(x) utf8_to_mb(x).c_str() -class TextFileReader +class LineInput +{ +public: + virtual ~LineInput() {} + virtual bool eof() = 0; + virtual bool error() { return false; }; + virtual std::string get_line() = 0; +}; + +class FileLineInput : public LineInput { enum bom_type { @@ -40,9 +51,11 @@ class TextFileReader bom_type bom; bool seen_eof; public: - TextFileReader(const char *name); - ~TextFileReader(); - bool eof() { return seen_eof; }; + FileLineInput(const char *name); + ~FileLineInput(); + bool eof() { return seen_eof || !f; }; bool error() { return !f; }; std::string get_line(); }; + +#endif |