1 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  *  Use of this source code is governed by a zlib-style license that can
4  *  be found in the License.txt file in the root of the source tree.
5  */
6 
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 //
9 // ZtringListList with file load/save
10 //
11 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 
13 //---------------------------------------------------------------------------
14 #include "ZenLib/PreComp.h"
15 #ifdef __BORLANDC__
16     #pragma hdrstop
17 #endif
18 //---------------------------------------------------------------------------
19 
20 //---------------------------------------------------------------------------
21 #include "ZenLib/Conf_Internal.h"
22 //---------------------------------------------------------------------------
23 
24 //---------------------------------------------------------------------------
25 #include "ZenLib/ZtringListListF.h"
26 #include "ZenLib/File.h"
27 //---------------------------------------------------------------------------
28 
29 namespace ZenLib
30 {
31 
32 //---------------------------------------------------------------------------
33 
34 //***************************************************************************
35 // Constructors/Destructor
36 //***************************************************************************
37 
38 //---------------------------------------------------------------------------
39 // Constructors
ZtringListListF_Common()40 void ZtringListListF::ZtringListListF_Common ()
41 {
42     Backup_Nb_Max=0;
43     Backup_Nb=0;
44     Sauvegarde=true;
45     #ifdef _UNICODE
46         Local=false;
47     #else
48         Local=true;
49     #endif
50 }
51 
ZtringListListF()52 ZtringListListF::ZtringListListF ()
53 :ZtringListList ()
54 {
55     ZtringListListF_Common();
56 }
57 
ZtringListListF(const ZtringListList & Source)58 ZtringListListF::ZtringListListF (const ZtringListList &Source)
59 :ZtringListList (Source)
60 {
61     ZtringListListF_Common();
62 }
63 
ZtringListListF(const Ztring & Source)64 ZtringListListF::ZtringListListF (const Ztring &Source)
65 :ZtringListList (Source)
66 {
67     ZtringListListF_Common();
68 }
69 
ZtringListListF(const Char * Source)70 ZtringListListF::ZtringListListF (const Char *Source)
71 :ZtringListList (Source)
72 {
73     ZtringListListF_Common();
74 }
75 
76 #ifdef _UNICODE
ZtringListListF(const char * Source)77 ZtringListListF::ZtringListListF (const char* Source)
78 :ZtringListList (Source)
79 {
80     ZtringListListF_Common();
81 }
82 #endif
83 
84 //***************************************************************************
85 // File management
86 //***************************************************************************
87 
88 //---------------------------------------------------------------------------
89 // Load
Load(const Ztring & NewFileName)90 bool ZtringListListF::Load (const Ztring &NewFileName)
91 {
92     clear();
93     if (!NewFileName.empty())
94         Name=NewFileName;
95 
96     size_t I1=Error;
97 
98     if (Name.find(__T(".csv"))!=Error)
99         I1=CSV_Charger();
100     if (Name.find(__T(".cfg"))!=Error)
101         I1=CFG_Charger();
102     if (I1!=Error)
103     {
104         Backup_Nb=0; //mettre ici le code pour trouver le nb de backup
105         return true;
106     }
107     else
108         return false;
109 }
110 
111 //---------------------------------------------------------------------------
112 // Load CSV
CSV_Charger()113 bool ZtringListListF::CSV_Charger ()
114 {
115     //Read file
116     File F;
117     if (!F.Open(Name))
118         return false;
119 
120     int8u* Buffer=new int8u[(size_t)F.Size_Get()+1];
121     size_t BytesCount=F.Read(Buffer, (size_t)F.Size_Get());
122     F.Close();
123     if (BytesCount==Error)
124     {
125         delete[] Buffer; //Buffer=NULL;
126         return false;
127     }
128     Buffer[(int32u)BytesCount]=(int8u)'\0';
129 
130     //Convert file in UTF-8 or Local
131     Ztring File;
132     if (!Local)
133     {
134         //UTF-8
135         File.From_UTF8((char*)Buffer, 0, BytesCount);
136         #ifdef _DEBUG
137         if (File.size()==0)
138              File.From_Local((char*)Buffer, 0, BytesCount);
139         #endif //_DEBUG
140     }
141     if (File.size()==0)
142         //Local of UTF-8 failed
143         File.From_Local((char*)Buffer, 0, BytesCount);
144 
145     //Separators
146     if (Separator[0]==__T("(Default)"))
147             Separator[0]=EOL;
148     Ztring SeparatorT=Separator[1];
149     Separator[1]=__T(";");
150 
151     //Writing
152     Write(File);
153 
154     //Separators
155     Separator[1]=SeparatorT;
156 
157     delete[] Buffer; //Buffer=NULL;
158     return true;
159 }
160 
161 //---------------------------------------------------------------------------
162 // Chargement CFG
CFG_Charger()163 bool ZtringListListF::CFG_Charger ()
164 {
165     //Read file
166     File F(Name);
167     int8u* Buffer=new int8u[(size_t)F.Size_Get()+1];
168     size_t BytesCount=F.Read(Buffer, (size_t)F.Size_Get());
169     F.Close();
170     if (BytesCount==Error)
171     {
172         delete[] Buffer; //Buffer=NULL;
173         return false;
174     }
175     Buffer[(int32u)BytesCount]=(int8u)'\0';
176 
177     //Convert File --> ZtringList
178     ZtringList List;
179     List.Separator_Set(0, EOL);
180     Ztring Z1;
181     Z1.From_UTF8((char*)Buffer, 0, BytesCount);
182     List.Write(Z1);
183 
184     Ztring SeparatorT=Separator[1];
185     Separator[1]=__T(";");
186 
187     Ztring Propriete, Valeur, Commentaire;
188 
189     for (size_t Pos=0; Pos<List.size(); Pos++)
190     {
191         const Ztring &Lu=List(Pos);
192         if (Lu.find(__T('='))>0)
193         {
194             //Obtention du Name
195             Propriete=Lu.SubString(Ztring(), __T("="));
196             NettoyerEspaces(Propriete);
197             //Obtention de la valeur
198             Valeur=Lu.SubString(__T("="), __T(";"), 0, Ztring_AddLastItem);
199             NettoyerEspaces(Valeur);
200         }
201         //Obtention du commentaire
202         Commentaire=Lu.SubString(__T(";"), Ztring(), 0, Ztring_AddLastItem);
203         NettoyerEspaces(Commentaire);
204         //Ecriture
205         push_back((Propriete+__T(";")+Valeur+__T(";")+Commentaire).c_str()); //Visual C++ 6 is old...
206     }
207     Separator[1]=SeparatorT;
208 
209     delete[] Buffer; //Buffer=NULL;
210     return true;
211 }
212 
213 //---------------------------------------------------------------------------
214 // Sauvegarde globale
Save(const Ztring & FileName)215 bool ZtringListListF::Save (const Ztring &FileName)
216 {
217     //Gestion de l'annulation de la sauvegarde
218     if (!Sauvegarde)
219         return true;
220 
221     if (FileName!=Ztring())
222         Name=FileName;
223 
224     //Gestion des backups
225     Backup_Nb=0;
226     int8u I2;
227     Separator[0]=EOL;
228     if (Backup_Nb_Max>0)
229     {
230         //TODO : not tested
231         for (int8u I1=Backup_Nb_Max-1; I1>0; I1--)
232         {
233             Ztring Z1=Name+__T(".sav"); Z1+=Ztring::ToZtring(I1);
234             Ztring Z2=Name+__T(".sav"); Z2+=Ztring::ToZtring(I1+1);
235             File::Delete(Z2.c_str());
236             I2=File::Move(Z1.c_str(), Z2.c_str());
237             if (I2 && !Backup_Nb)
238                 Backup_Nb=I2;
239         }
240         Ztring Z1=Name+__T(".sav0");
241         File::Delete(Z1.c_str());
242         File::Move(Name.c_str(), Z1.c_str());
243         Backup_Nb++;
244     }
245 
246     I2=0;
247     if (Name.find(__T(".csv"))!=Error)
248         I2=CSV_Sauvegarder();
249     if (Name.find(__T(".cfg"))!=Error)
250         I2=CFG_Sauvegarder();
251 
252     if (I2>0)
253     {
254         return true;
255     }
256     else
257         return false;
258 }
259 
260 //---------------------------------------------------------------------------
261 // Sauvegarde CSV
CSV_Sauvegarder()262 bool ZtringListListF::CSV_Sauvegarder ()
263 {
264     //Sauvegarde
265     File F;
266     if (!F.Create(Name, true))
267         return false;
268 
269     if (Separator[0]==__T("(Default)"))
270         Separator[0]=EOL;
271 
272     F.Write(Read());
273 
274     return true;
275 }
276 
277 //---------------------------------------------------------------------------
278 // Sauvegarde CFG
CFG_Sauvegarder()279 bool ZtringListListF::CFG_Sauvegarder ()
280 {
281     File F;
282     if (!F.Create(Name, true))
283         return false;
284 
285     Ztring ToWrite;
286     Ztring Propriete, Valeur, Commentaire;
287 
288     ;
289     for (size_t Pos=0; Pos<size(); Pos++)
290     {
291         Propriete=Read(Pos, 0);
292         Valeur=Read(Pos, 1);
293         Commentaire=Read(Pos, 2);
294         if (Propriete!=Ztring())
295         {
296             ToWrite+=Propriete+__T(" = ");
297             if (Valeur!=Ztring())
298                 ToWrite+=Valeur+__T(" ");
299         }
300         if (Commentaire!=Ztring())
301             ToWrite+=__T("; ")+Commentaire;
302         ToWrite+=EOL;
303     }
304     F.Write(ToWrite);
305 
306     return true;
307 }
308 
309 //---------------------------------------------------------------------------
310 // Annulation
Cancel()311 bool ZtringListListF::Cancel ()
312 {
313     Ztring Z1=Name+__T(".sav0"); //Visual C++ 6 patch
314     File::Delete(Name.c_str());
315     File::Move(Z1.c_str(), Name.c_str());
316     for (int8u I1=1; I1<=Backup_Nb; I1++)
317     {
318         Ztring Z2=Name+__T(".sav"); Z2+=Ztring::ToZtring(I1); //Visual C++ 6 patch
319         Ztring Z3=Name+__T(".sav"); Z3+=Ztring::ToZtring(I1-1); //Visual C++ 6 patch
320         File::Delete(Z3.c_str());
321         File::Move(Z2.c_str(), Z3.c_str());
322     }
323     Write(Ztring());
324     return CSV_Charger();
325 }
326 
327 //***************************************************************************
328 // Divers
329 //***************************************************************************
330 
331 //---------------------------------------------------------------------------
332 // Nettoyage
NettoyerEspaces(Ztring & ANettoyer)333 bool ZtringListListF::NettoyerEspaces (Ztring &ANettoyer)
334 {
335     size_t Debut=0;
336     while (Debut<ANettoyer.size() && ANettoyer[Debut]==__T(' '))
337         Debut++;
338     size_t Fin=ANettoyer.size()-1;
339     while (Fin!=(size_t)-1 && ANettoyer[Fin]==__T(' '))
340         Fin--;
341     if (Fin>=Debut)
342         ANettoyer=ANettoyer.substr(Debut, Fin-Debut+1);
343     else
344         ANettoyer=Ztring();
345     return true;
346 }
347 
348 //---------------------------------------------------------------------------
349 // Backup
Backup_Set(bool NewSave)350 void ZtringListListF::Backup_Set (bool NewSave)
351 {
352     Sauvegarde=NewSave;
353     Save();
354 }
355 
Backup_Count_Set(int8u NewCount)356 void ZtringListListF::Backup_Count_Set (int8u NewCount)
357 {
358     Backup_Nb_Max=NewCount;
359 }
360 
361 //---------------------------------------------------------------------------
362 // Local
Local_Set(bool NewLocal)363 void ZtringListListF::Local_Set (bool NewLocal)
364 {
365     Local=NewLocal;
366 }
367 
368 } //Namespace
369