1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#ifndef __FileWordGenerator_h__
#define __FileWordGenerator_h__
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace std;
#include "WordGeneratorBase.h"
namespace Dasher {
/**
* This class implements the Word Generator interface. This means
* that after construction, your primary method of retrieving words
* will be calling GetNextWord(). For specifics on Word Generators,
* see CWordGeneratorBase.
*
* This class specifically reads words from a given file. Files ARE
* kept open for the lifetime of this object and the size of the file
* should not pose an issue.
*
* However, if you read in a file that has unreasonably long lines,
* the behavior is undefined as you may cause the file to be read in all
* at once.
*/
class CFileWordGenerator : public CWordGeneratorBase, public AbstractParser {
public:
CFileWordGenerator(CMessageDisplay *pMsgs, const CAlphInfo *pAlph, const CAlphabetMap *pAlphMap);
///Attempt to read from an arbitrary stream. Returns false, as we
/// only support reading game mode sentences from files.
bool Parse(const std::string &strDesc, std::istream &in, bool bUser);
///Attempt to open the specified file. Return true for success, false for failure
bool ParseFile(const std::string &strFileName, bool bUser);
virtual ~CFileWordGenerator() {
m_sFileHandle.close();
}
/**
* Return the next line from the file
* @throw Throws an exception if the file cannot be read.
*/
virtual std::string GetLine();
void setAcceptUser(bool bAcceptUser) {m_bAcceptUser = bAcceptUser;}
bool HasLines() {return !m_vLineIndices.empty();}
private:
/* ---------------------------------------------------------------------
* Member Variables
* ---------------------------------------------------------------------
*/
/**
* The path to the file this generator reads from.
*/
std::string m_sPath;
/**
* The input stream that acts as the handle to the underlying file.
*/
ifstream m_sFileHandle;
std::vector<streampos> m_vLineIndices;
bool m_bAcceptUser;
};
}
#endif
|