F.E.I.S/tests/nowide.cpp

47 lines
1.3 KiB
C++
Raw Normal View History

#include <cstddef>
2022-04-03 17:11:31 +02:00
#include <cstring>
2022-04-03 15:59:05 +02:00
#include <filesystem>
2022-04-03 17:03:45 +02:00
#include <iostream>
2022-04-03 17:11:31 +02:00
#include <string>
2022-04-03 15:59:05 +02:00
#include <tinyfiledialogs.h>
#include <nowide/args.hpp>
#include <nowide/fstream.hpp>
#include <nowide/iostream.hpp>
2022-04-04 00:20:45 +02:00
template <class T>
void check_file(T& file) {
if (not file) {
nowide::cerr << "Can't open file" << std::endl;
return;
}
std::size_t total_lines = 0;
while (file) {
if(file.get() == '\n') {
total_lines++;
}
}
nowide::cout << "File has " << total_lines << " lines" << std::endl;
}
2022-04-03 16:55:04 +02:00
int main() {
2022-04-03 15:59:05 +02:00
const char* _filepath = tinyfd_openFileDialog(
"Open File", nullptr, 0, nullptr, nullptr, false
);
if (_filepath == nullptr) {
nowide::cerr << "No file chosen" << std::endl;
2022-04-04 00:20:45 +02:00
return 0;
}
2022-04-04 00:20:45 +02:00
nowide::cout << "const char* _filepath = " << _filepath << std::endl;
2022-04-03 17:11:31 +02:00
auto u8string = std::u8string(_filepath, _filepath + std::strlen(_filepath));
2022-04-04 00:20:45 +02:00
nowide::cout << "std::u8string{_filepath)} = " << reinterpret_cast<const char*>(u8string.c_str()) << std::endl;
2022-04-03 17:11:31 +02:00
2022-04-04 00:20:45 +02:00
std::filesystem::path u8path{u8string};
nowide::cout << "std::filesystem::path{u8string} = " << u8path << std::endl;
2022-04-03 17:11:31 +02:00
2022-04-04 00:20:45 +02:00
nowide::cout << "nowide::ifstream{u8path} : " << std::endl;
nowide::ifstream file_from_path(u8path.string());
check_file(file_from_path);
return 0;
}