00001
00007 #include <string>
00008 #include <fstream>
00009 #include <cctype>
00010 #include <lector_palabras.h>
00011 using namespace std;
00012
00013
00014 Lector_Palabras::Lector_Palabras():
00015 f(0),fichero_abierto(false)
00016 {}
00017 Lector_Palabras::Lector_Palabras(std::istream& i):
00018 f(&i),fichero_abierto(false)
00019 {}
00020
00021 Lector_Palabras::Lector_Palabras(const string& nombre)
00022 {
00023 fichero_abierto=false;
00024 asociar(nombre);
00025 }
00026 Lector_Palabras::~Lector_Palabras()
00027 {
00028 if (fichero_abierto)
00029 delete dynamic_cast<ifstream *>(f);
00030 }
00031 void Lector_Palabras::asociar(istream& i)
00032 {
00033 if (fichero_abierto)
00034 delete dynamic_cast<ifstream *>(f);
00035 f= &i;
00036 fichero_abierto=false;
00037 }
00038 void Lector_Palabras::asociar(const string& nombre)
00039 {
00040 if (fichero_abierto)
00041 delete dynamic_cast<ifstream *>(f);
00042
00043 ifstream *fich= new ifstream(nombre.c_str());
00044 if (*fich) {
00045 fichero_abierto= true;
00046 f= fich;
00047 }
00048 else {
00049 fichero_abierto=false;
00050 delete fich;
00051 f= 0;
00052 }
00053 }
00054
00055 Lector_Palabras::operator void*() const
00056 {
00057 return (f==0)?0:static_cast<void*>(*f);
00058
00059 }
00060 bool Lector_Palabras::operator! () const
00061 {
00062 return (f==0)?true:!*f;
00063 }
00064
00065 Lector_Palabras& Lector_Palabras::operator>>(string& str)
00066 {
00067 str="";
00068 if (*this){
00069 char c;
00070 do {
00071 c= f->get();
00072 } while (c!=EOF && !isalpha(c));
00073 if (c==EOF) f->setstate(ios::failbit);
00074 if (isalpha(c)){
00075 str+=c;
00076 bool final= false;
00077 while (!final) {
00078 c=f->get();
00079 if (isalpha(c))
00080 str+=c;
00081 else final= true;
00082 }
00083 }
00084 }
00085 return *this;
00086 }