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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
| #include "StdAfx.h"
#include ".\customstringlist.h"
// Constructeur
CCustomStringList::CCustomStringList (CString sDefaultPath)
{
// type simple : chaine Alpha
p_iType = 0;
//Chemin par defaut
p_sPath = sDefaultPath;
}
// ********************** private ************************
POSITION CCustomStringList::getPositionFromIndex(int Index)
{
POSITION p = p_slListe.GetHeadPosition();
int i = 0;
while ( (i<Index) && (p != NULL))
{
p_slListe.GetNext(p);
i++;
}
return p;
}
//Cette fonction compare 2 dates en ligne et retourne
// -1 si d1 < d2 0 si d1 = d2 1 si d1 > d2
// ex : 010220050020 et 010320050020 -> -1
int CCustomStringList::CompareTwoDateInLine(CString d1,CString d2)
{
int r;
r = CompareTwoNumber(d1.Mid(4,4), d2.Mid(4,4)); // comparaison des années
if ( r == 0 ) r = CompareTwoNumber(d1.Mid(2,2), d2.Mid(2,2)); // comparaison des mois
if ( r == 0 ) r = CompareTwoNumber(d1.Mid(0,2), d2.Mid(0,2)); // comparaison des jours
if ( r == 0 ) r = CompareTwoNumber(d1.Mid(8,2), d2.Mid(8,2)); // comparaison des heures
if ( r == 0 ) r = CompareTwoNumber(d1.Mid(10,2), d2.Mid(10,2)); // comparaison des minutes
return r;
}
// Cette fonctions compare 2 nombres en string et retourne
// -1 si s1 < s2 0 si s1 = s2 1 si s1 > s2
// ex : "20" et "28" -> -1
int CCustomStringList::CompareTwoNumber(CString s1,CString s2)
{
int a = strtoul( s1, NULL, 10 ),
b = strtoul( s2, NULL, 10 ),
r;
if ( a < b ) r = -1;
else if ( a == b ) r = 0;
else r = 1;
return r;
}
//Cette fonction compare 2 mix chaine alpha / dates en ligne et retourne
// -1 si d1 < d2 0 si d1 = d2 1 si d1 > d2
// ex : Toto - 010220050020 et Toto - 010320050020 -> -1
int CCustomStringList::CompareTwoMixedAlphaDate(CString m1,CString m2)
{
int ret = m1.Left(m1.GetLength()-12) < m2.Left(m2.GetLength()-12);
if (ret == 0) ret = CompareTwoDateInLine(m1.Right(12), m2.Right(12));
return ret;
}
// Cette fonction retourne la position du plus petit (tri alpha ou date)
POSITION CCustomStringList::getPositionOfFirstSorted(void)
{
POSITION memPos,
p = p_slListe.GetHeadPosition(),
minPos = p;
CString ligne,
ligneMin = p_slListe.GetNext(p),
code,
codeMin,
nom,
nomMin;
BOOL Test;
if (p_iType == 3)
{
nomMin = GetExplodedTxt(ligneMin, 4);
codeMin = GetExplodedTxt(ligneMin, 3);
}
while ( p != NULL )
{
memPos = p;
ligne = p_slListe.GetNext(p);
switch(p_iType)
{
// type simple : chaine Alpha
case 0: Test = ligne < ligneMin;
break;
// type 1 : date
case 1: Test = CompareTwoDateInLine(ligne, ligneMin) < 0;
break;
// type 2 : chaine alpha - date
case 2: Test = CompareTwoMixedAlphaDate(ligne, ligneMin) < 0;
break;
// type 3 : il s'agit d'une ligne ticket
case 3: nom = GetExplodedTxt(ligne, 4);
if (nom == nomMin)
{
code = GetExplodedTxt(ligne, 3);
Test = code < codeMin;
}
else
Test = nom < nomMin;
if ( Test )
{
nomMin = nom;
codeMin = code;
}
break;
}
if ( Test )
{
ligneMin = ligne;
minPos = memPos;
}
}
return minPos;
}
// fonction privé qui retourne la position du pts virgule d'indice iIndex (le 1er a pour indice 0)
int CCustomStringList::FindPtsVirg(CString sTxt, int iIndex)
{
int i = -1,
p = -1;
do
{
p = sTxt.Find(";", p+1);
i++;
}while( (p!=-1) && (i != iIndex));
return p;
}
// fonction privé qui retourne le texte d'indice iIndex dans une chaine séparé par des ;
// ex : GetExplodedTxt("aa;zz;eee;rr;t;", 1) -> "zz"
CString CCustomStringList::GetExplodedTxt(CString sTxt, int iIndex)
{
CString ret = "";
int p1, p2;
if (iIndex == 0)
{
p1 = FindPtsVirg(sTxt, 0);
if (p1!=-1) ret = sTxt.Left(p1);
}
else
{
p1 = FindPtsVirg(sTxt, iIndex-1),
p2 = FindPtsVirg(sTxt, iIndex);
if ( (p1!=-1) && (p2!=-1) ) ret = sTxt.Mid(p1+1, p2-p1-1);
}
return ret;
}
// *********************** public *************************
//+++++ la classe utilisera les fonctions adapté lors du SORT en fonction de p_iType +++++//
// declare la liste comme etant une liste de chaine alpha,
void CCustomStringList::SetListAsAlpha(void)
{
p_iType = 0;
}
// declare la liste comme etant une liste de date,
void CCustomStringList::SetListAsDateList(void)
{
p_iType = 1;
}
// declare la liste comme etant une liste de mix chaine alpha et date,
void CCustomStringList::SetListAsMixedAlphaDate(void)
{
p_iType = 2;
}
// declare la liste comme etant une liste de mix chaine alpha et date,
void CCustomStringList::SetListAsTicketList(void)
{
p_iType = 3;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
int CCustomStringList::Add(CString NewItem)
{
p_slListe.AddTail(NewItem);
return (int)(p_slListe.GetCount() - 1);
}
int CCustomStringList::Count(void)
{
return (int)(p_slListe.GetCount());
}
CString CCustomStringList::GetItem(int Index)
{
POSITION p = getPositionFromIndex(Index);
if (p != NULL)
return p_slListe.GetAt(p);
else
return "";
}
void CCustomStringList::SetItem(int Index, CString sItem)
{
POSITION p = getPositionFromIndex(Index);
if (p != NULL) p_slListe.SetAt(p, sItem);
}
void CCustomStringList::RemoveItem(int Index)
{
p_slListe.RemoveAt(getPositionFromIndex(Index));
}
void CCustomStringList::Clear(void)
{
p_slListe.RemoveAll();
}
//Cette fonction effectue un tri en texte ascandant sur un cstringlist
void CCustomStringList::Sort(void)
{
POSITION pos;
CStringList cslTmp;
CString ligne;
if (p_iType == 3)
{
// pour le tri dans un fichier ticket, on saute les 3 premieres lignes
for (int i=0; i<3;i++)
{
cslTmp.AddTail(p_slListe.GetHead());
p_slListe.RemoveHead();
}
}
while (p_slListe.GetCount() > 0)
{
pos = getPositionOfFirstSorted();
ligne = p_slListe.GetAt(pos);
cslTmp.AddTail(ligne);
p_slListe.RemoveAt(pos);
}
for( pos = cslTmp.GetHeadPosition(); pos != NULL; )
{
p_slListe.AddTail(cslTmp.GetNext(pos));
}
}
// Path par defaut
void CCustomStringList::SetPath(CString sPath)
{
p_sPath = sPath;
}
CString CCustomStringList::GetPath(void)
{
return p_sPath;
}
BOOL CCustomStringList::Load(void)
{
if (p_sPath != "")
return LoadFromFile(p_sPath);
else
return FALSE;
}
BOOL CCustomStringList::LoadFromFile(CString FilePath)
{
CStdioFile fichier;
CString ligne;
BOOL ret = fichier.Open(FilePath, CFile::modeRead | CFile::typeText );
p_slListe.RemoveAll();
if (ret)
{
while(fichier.ReadString(ligne)) p_slListe.AddTail(ligne);
fichier.Close();
}
return ret;
}
BOOL CCustomStringList::Save(void)
{
if (p_sPath != "")
return SaveToFile(p_sPath);
else
return FALSE;
}
// Cette fonction enregistre la liste vers un fichier en passant par un fichier temporaire (précédé par un ~)
BOOL CCustomStringList::SaveToFile(CString FilePath)
{
CStdioFile fichier;
POSITION pos = p_slListe.GetHeadPosition();
int p = FilePath.ReverseFind('\\');
CString TmpFile = FilePath.Left(p+1) + "~" + FilePath.Right(FilePath.GetLength() - p -1);
BOOL ret = fichier.Open(TmpFile, CFile::modeCreate |CFile::modeWrite | CFile::typeText );
if ( (ret) && (pos != NULL) )
{
do
fichier.WriteString( p_slListe.GetNext(pos) + "\n");
while(pos != NULL);
fichier.Close();
remove(FilePath);
ret = rename(TmpFile, FilePath) == 0;
}
return ret;
}
// retourne la chaine complete (ticket) selon un code barre (pour le type 3)
CString CCustomStringList::FindTicket(CString sCodeBarre)
{
POSITION p = p_slListe.GetHeadPosition();
int i = 0;
CString ligne, ret = "";
while (p != NULL)
{
ligne = p_slListe.GetNext(p);
if (GetExplodedTxt(ligne, 3) == sCodeBarre)
{
ret = ligne;
p = NULL;
}
i++;
}
return ret;
}
// retourne la chaine complete (ticket) selon un code barre (pour le type 3)
int CCustomStringList::FindIndexOfTicket(CString sTicket)
{
POSITION p = p_slListe.GetHeadPosition();
int i = 0,
ret = -1;
CString ligne;
while (p != NULL)
{
ligne = p_slListe.GetNext(p);
if (ligne == sTicket)
{
ret = i;
p = NULL;
}
i++;
}
return ret;
} |
Partager