#include "vocab.h" #include #include #include #include Vocab::Vocab() { std::srand(time(NULL)); } Vocab::Vocab(QString filename) { std::srand(time(NULL)); load(filename); } void Vocab::load(QString filename) { QFile file(filename); if(!file.exists()) std::cerr << "can't find " << filename.toStdString() << std::endl; else if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { std::cerr << "file is open" << std::endl; QTextStream stream(&file); stream.setCodec("UTF-8"); for(QString line = stream.readLine(512); line != QString::null; line = stream.readLine(512)) { if(line.length() > 2) { int firstChar = line.at(0).unicode(); line.remove(0, 2); switch(firstChar) { case 'n' : addNom(line); break; case 'v' : addVerbe(line); break; case 'a' : addAdjectif(line); break; } } } file.close(); } } void Vocab::save(QString filename) { QFile file(filename); if(file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream(&file); stream.setCodec("UTF-8"); stream << "#NOMS :\n"; for(QString n : noms) stream << "n:" << n << "\n"; stream << "\n#VERBES :\n"; for(QString v : verbes) stream << "v:" << v << "\n"; stream << "\n#ADJECTIFS :\n"; for(QString a : adjectifs) stream << "a:" << a << "\n"; stream.flush(); file.close(); } } void Vocab::append(Vocab& voc) { for(QString n : voc.noms) addNom(n); for(QString v : voc.verbes) addVerbe(v); for(QString a : voc.adjectifs) addAdjectif(a); } void Vocab::addNom(QString nom) { noms.push_back(nom); } void Vocab::addAdjectif(QString adj) { adjectifs.push_back(adj); } void Vocab::addVerbe(QString ver) { verbes.push_back(ver); } QString Vocab::getRandNom() { if(noms.size()) return noms[std::rand()%noms.size()]; else return ""; } QString Vocab::getRandAdjectif() { if(adjectifs.size()) return adjectifs[std::rand()%adjectifs.size()]; else return ""; } QString Vocab::getRandVerbe() { if(verbes.size()) return verbes[std::rand()%verbes.size()]; else return ""; }