detector.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <iostream>
  2. #include <string>
  3. #include <ZLibrary.h>
  4. #include <ZLFile.h>
  5. #include <ZLInputStream.h>
  6. #include <ZLLanguageDetector.h>
  7. void doIt(const std::string &inputFileName) {
  8. const size_t BUFFER_SIZE = 50000;
  9. char *buffer = new char[BUFFER_SIZE];
  10. shared_ptr<ZLInputStream> stream = ZLFile(inputFileName).inputStream();
  11. if (stream.isNull() || !stream->open()) {
  12. std::cerr << "Couldn't open file " << inputFileName << "\n";
  13. return;
  14. }
  15. size_t length = stream->read(buffer, BUFFER_SIZE);
  16. std::string out = inputFileName;
  17. std::cout << "It have been read " << length << " bytes from file " << out.substr(out.find_last_of("/")+1) << "\n";
  18. stream->close();
  19. shared_ptr<ZLLanguageDetector::LanguageInfo> resultLanguageInfoPtr;
  20. resultLanguageInfoPtr = ZLLanguageDetector().findInfo(buffer, length);
  21. if (!resultLanguageInfoPtr.isNull()) {
  22. std::cout << "Language: " << resultLanguageInfoPtr->Language << "\n";
  23. std::cout << "Encoding: " << resultLanguageInfoPtr->Encoding << "\n";
  24. }
  25. delete[] buffer;
  26. }
  27. int main(int argc, char **argv) {
  28. if (argc == 1) {
  29. std::cerr << "usage:\n " << argv[0] << " <file name> [<file name> ...]\n";
  30. }
  31. ZLibrary::init(argc, argv);
  32. for (int i = 1; i < argc; ++i) {
  33. std::cout << argv[i] << ":\n";
  34. doIt(argv[i]);
  35. }
  36. ZLibrary::shutdown();
  37. return 0;
  38. }