From 244a65e8556328642350575c4a94ee8fc1b676b4 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Fri, 17 May 2013 05:46:51 +0100 Subject: Clean up the FileReader class and all of the modules that use it. - Modules which use this class will now have to catch a CoreException when opening files if they wish to ignore the failed loading of a file. - m_randquote has been cleaned up massively and the RANDQUOTE command has been removed as it was pretty much useless. --- include/modules.h | 70 ++++++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 52 deletions(-) (limited to 'include/modules.h') diff --git a/include/modules.h b/include/modules.h index 176bdda34..89961a6f5 100644 --- a/include/modules.h +++ b/include/modules.h @@ -1281,76 +1281,42 @@ class CoreExport Module : public classbase, public usecountbase virtual void OnSetUserIP(LocalUser* user); }; -/** Caches a text file into memory and can be used to retrieve lines from it. - * This class contains methods for read-only manipulation of a text file in memory. - * Either use the constructor type with one parameter to load a file into memory - * at construction, or use the LoadFile method to load a file. - */ +/** Provides an easy method of reading a text file into memory. */ class CoreExport FileReader : public classbase { - /** The file contents + /** The lines of text in the file. */ - std::vector fc; + std::vector lines; /** Content size in bytes */ - unsigned long contentsize; - - /** Calculate content size in bytes - */ - void CalcSize(); + unsigned long totalSize; public: - /** Default constructor. - * This method does not load any file into memory, you must use the LoadFile method - * after constructing the class this way. - */ - FileReader(); - - /** Secondary constructor. - * This method initialises the class with a file loaded into it ready for GetLine and - * and other methods to be called. If the file could not be loaded, FileReader::FileSize - * returns 0. + /** Initializes a new file reader. */ - FileReader(const std::string &filename); + FileReader() : totalSize(0) { } - /** Default destructor. - * This deletes the memory allocated to the file. - */ - ~FileReader(); - - /** Used to load a file. - * This method loads a file into the class ready for GetLine and - * and other methods to be called. If the file could not be loaded, FileReader::FileSize - * returns 0. + /** Initializes a new file reader and reads the specified file. + * @param file The file to read into memory. */ - void LoadFile(const std::string &filename); + FileReader(const std::string& filename); - /** Returns the whole content of the file as std::string + /** Loads a text file from disk. + * @param filename The file to read into memory. + * @throw CoreException The file can not be loaded. */ - std::string Contents(); + void Load(const std::string& filename); - /** Returns the entire size of the file as std::string + /** Retrieves the entire contents of the file cache as a single string. */ - unsigned long ContentSize(); + std::string GetString(); - /** Returns true if the file exists - * This function will return false if the file could not be opened. + /** Retrieves the entire contents of the file cache as a vector of strings. */ - bool Exists(); + const std::vector& GetVector() { return lines; } - /** Retrieve one line from the file. - * This method retrieves one line from the text file. If an empty non-NULL string is returned, - * the index was out of bounds, or the line had no data on it. - */ - std::string GetLine(int x); - - /** Returns the size of the file in lines. - * This method returns the number of lines in the read file. If it is 0, no lines have been - * read into memory, either because the file is empty or it does not exist, or cannot be - * opened due to permission problems. - */ - int FileSize(); + unsigned long TotalSize() { return totalSize; } }; /** A list of modules -- cgit v1.2.3