1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| const std::vector<std::pair<std::wstring, std::wstring>>lire_paireCleValeur_depuisFichierTxt(std::wstring const& nomFichier, std::wstring separeteur)
// separeteur = 3 ! Ok !
// Mais : stoi ou... ???
// Que faire ?
{
std::string contenuFichier{ u8"" };
std::string ligneCourante{ u8"" };
std::vector<std::pair<std::wstring, std::wstring>> clevaleurs;
ifstream fichier{ nomFichier };
if (!fichier)
{
throw std::runtime_error("Fichier impossible à ouvrir.");
}
while (getline(fichier, ligneCourante, u8'\n'))
{
contenuFichier += ligneCourante + u8'\n';
}
if (contenuFichier == u8"")
{
throw std::runtime_error("Le fichier '" + wstr_to_u8(nomFichier) + "' est vide.");
}
//wstring_convert<codecvt_utf8_utf16<wchar_t>, wchar_t> convertiseur;
wstring_convert<codecvt_utf8<wchar_t>, wchar_t> convertiseur;
std::wstring converti = convertiseur.from_bytes(contenuFichier);
rtrim(converti);
converti += L"\n";
std::size_t pos = converti.length();
if (pos == std::wstring::npos)
return clevaleurs;
std::size_t pos2 = 0;
while (pos = converti.find(L"\n"))
{
if (converti[0] != converti.length())
{
pos2 = converti.find(L"...");
if (pos2 != std::wstring::npos && converti[0] == L'.' && converti[1] == L'.' && converti[2] == L'.')
{
clevaleurs.push_back(std::make_pair(L"
", L""));
return clevaleurs;
}
pos2 = converti.find(L"
");
if (pos2 != std::wstring::npos && converti[0] == L'
')
{
clevaleurs.push_back(std::make_pair(L"
", L""));
return clevaleurs;
}
pos2 = converti.find(L".");
if (pos2 != std::wstring::npos && converti[0] == L'
')
{
clevaleurs.push_back(std::make_pair(L"
", L""));
return clevaleurs;
}
pos2 = converti.find(separeteur);
if (pos2 == std::wstring::npos && pos != std::wstring::npos)
{
clevaleurs.push_back(std::make_pair(converti.substr(0, pos - 1), L""));
}
else if (pos2 == 0)
{
clevaleurs.push_back(std::make_pair(L"", converti.substr(3, (pos - 1) - 2)));
}
else if (pos > pos2)
{
clevaleurs.push_back(std::make_pair(converti.substr(0, pos2), converti.substr(pos2 + 3, pos - (pos2 + 3))));
}
else
{
// ???
break;
}
converti = converti.substr(pos + 1);
}
else
break;
}
return clevaleurs;
} |