Constructeur de confort Création d'un graphe correctement initialisé à partir d'une chaîne de caractère OU un chemin vers un fichier contenant la chaîne de caractères. OU Une erreur si contenu est mal formatté ou qu'il contient des incohérences ou des erreurs.
50 if (bContenuEstChemin) {
51 std::string sFileContent(
""), sBuffer;
52 std::fstream FILfichier(cpContenu);
53 char sExceptionMessage[255];
57 if (FILfichier.is_open())
59 while (std::getline(FILfichier, sBuffer)) {
62 sFileContent += sBuffer + (!FILfichier.eof() ?
"\n" :
"");
66 cpContentToUse = _strdup(sFileContent.c_str());
71 sprintf_s(sExceptionMessage, 255,
"CGraphe::CGraphe(const char *cpChemin) : Impossible d'ouvrir le fichier \"%s\"\n", cpContenu);
72 throw CException(CGRAPHE_Ouverture_Fichier_Impossible, sExceptionMessage);
77 cpContentToUse = _strdup(cpContenu);
84 char sExceptionMessage[255];
86 std::string sRegexResult;
87 std::regex rRegex(
"NBSommets[ \\t]*=[ \\t]*([0-9]+)[ \\t]*\\nNBArcs[ \\t]*=[ \\t]*([0-9]+)[ \\t]*\\nSommets[ \\t]*=[ \\t]*(\\[)[ \\t]*\\n((?:Numero[ \\t]*=[ \\t]*[0-9]+\\n)*)\\][ \\t]*\\nArcs[ \\t]*=[ \\t]*(\\[)[ \\t]*\\n((?:Debut[ \\t]*=[ \\t]*[0-9]+[ \\t]*,[ \\t]*Fin[ \\t]*=[ \\t]*([0-9]+)[ \\t]*\\n)*)\\]\\s*");
88 std::cmatch cmMatchGlobal, cmMatchNumeric;
90 int iNbSommets, iNbArcs;
93 std::regex rNumericRegex(
"[0-9]+");
97 if (std::regex_match(cpContentToUse, cmMatchGlobal, rRegex)) {
100 for (
unsigned uiRegexIndex = 1; uiRegexIndex < cmMatchGlobal.size(); ++uiRegexIndex) {
101 sRegexResult = cmMatchGlobal[uiRegexIndex].str();
104 switch (sRegexResult[sRegexResult.length() - 1]) {
111 if (++uiRegexIndex < cmMatchGlobal.size()) {
113 sRegexResult = cmMatchGlobal[uiRegexIndex].str();
115 int iCurrentResIndex = 0, iResValue, iTempInitValue;
118 while (std::regex_search(sRegexResult.c_str(), cmMatchNumeric, rNumericRegex)) {
121 iResValue = atoi(cmMatchNumeric.str().c_str());
137 if (iCurrentResIndex % 2 == 0) {
138 iTempInitValue = iResValue;
152 sRegexResult = cmMatchNumeric.suffix();
158 if (iNbInit == 1 && iCurrentResIndex != iNbSommets) {
159 sprintf_s(sExceptionMessage, 255,
"CGraphe::CGraphe(const char *cpContenu, bool bContenuEstChemin) : %d sommets attendus %d sommets obtenus\n", iNbSommets, iCurrentResIndex);
160 throw CException(CGRAPHE_Erreur_NbArcs, sExceptionMessage);
165 else if (iNbInit == 2 && (iCurrentResIndex /= 2) != iNbArcs) {
166 sprintf_s(sExceptionMessage, 255,
"CGraphe::CGraphe(const char *cpContenu, bool bContenuEstChemin) : %d arcs attendus %d arcs obtenus\n", iNbArcs, iCurrentResIndex);
167 throw CException(CGRAPHE_Erreur_NbArcs, sExceptionMessage);
174 switch (uiRegexIndex) {
178 iNbSommets = atoi(sRegexResult.c_str());
183 iNbArcs = atoi(sRegexResult.c_str());
187 if (iNbSommets == 0 && iNbArcs != 0) {
188 throw CException(CGRAPHE_Erreur_NbArcs,
"CGraphe::CGraphe(const char *cpContenu, bool bContenuEstChemin) : Le nombre de sommets a été défini sur 0, le nombre d'arcs devrait l'être aussi.\n");
194 if (iNbArcs > (iNbSommets * iNbSommets - iNbSommets)) {
195 sprintf_s(sExceptionMessage, 255,
"CGraphe::CGraphe(const char *cpContenu, bool bContenuEstChemin) : Top d'arcs a initialiser, %d maximum .\n", (iNbSommets * iNbSommets - iNbSommets));
196 throw CException(CGRAPHE_Erreur_NbArcs, sExceptionMessage);
204 free(cpContentToUse);
207 throw CException(CGRAPHE_Erreur_Syntaxe,
"CGraphe::CGraphe(const char *cpContenu, bool bContenuEstChemin) : La chaîne de caractères ne correspond pas au format attendu\n");
211 throw CException(CGRAPHE_Erreur_Syntaxe,
"CGraphe::CGraphe(const char *cpContenu, bool bContenuEstChemin) : La chaîne de caractères est nulle\n");