Dictionary.C 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WApplication>
  7. #include <Wt/WStringUtil>
  8. #include "Dictionary.h"
  9. #include <fstream>
  10. #include <iostream>
  11. #include <time.h>
  12. #include <stdlib.h>
  13. std::wstring RandomWord(Dictionary dictionary)
  14. {
  15. std::ifstream dict;
  16. if (dictionary == DICT_NL) {
  17. dict.open((Wt::WApplication::appRoot() + "dict-nl.txt").c_str());
  18. } else { // english is default
  19. dict.open((Wt::WApplication::appRoot() + "dict.txt").c_str());
  20. }
  21. std::string retval;
  22. int numwords = 0;
  23. while(dict) {
  24. getline(dict, retval);
  25. numwords++;
  26. }
  27. dict.clear();
  28. dict.seekg(0);
  29. srand(time(0));
  30. int selection = rand() % numwords; // not entirely uniform, but who cares?
  31. while(selection--) {
  32. getline(dict, retval);
  33. }
  34. getline(dict, retval);
  35. for(unsigned int i = 0; i < retval.size(); ++i)
  36. if(retval[i] < 'A' || retval[i] > 'Z')
  37. std::cout << "word " << retval
  38. << " contains illegal data at pos " << i << std::endl;
  39. return Wt::widen(retval);
  40. }