remove getFullFilename from output_stream and use one in fileio
[scilab.git] / scilab / modules / output_stream / src / cpp / DiaryList.cpp
1 /*--------------------------------------------------------------------------*/
2 /*
3 * ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) DIGITEO - 2009 - Allan CORNET
5 *
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution.  The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11 *
12 */
13 /*--------------------------------------------------------------------------*/
14 #include "DiaryList.hxx"
15
16 extern "C"
17 {
18 #include "getFullFilename.h"
19 #include "sci_malloc.h"
20 }
21 /*--------------------------------------------------------------------------*/
22 DiaryList::DiaryList()
23 {
24 }
25 /*--------------------------------------------------------------------------*/
26 DiaryList::~DiaryList()
27 {
28     closeAllDiaries();
29 }
30 /*--------------------------------------------------------------------------*/
31 int DiaryList::openDiary(std::wstring _wfilename, bool autorename)
32 {
33     return openDiary(_wfilename, 0, autorename);
34 }
35 /*--------------------------------------------------------------------------*/
36 int DiaryList::openDiary(std::wstring _wfilename, int _mode, bool autorename)
37 {
38     int ID = -1;
39     Diary newDiary(_wfilename, _mode, findFreeID(), autorename);
40     if (newDiary.getID() != -1)
41     {
42         LSTDIARY.push_back(newDiary);
43         ID = newDiary.getID();
44     }
45     return ID;
46 }
47 /*--------------------------------------------------------------------------*/
48 bool DiaryList::closeDiary(int ID_diary)
49 {
50     std::list<Diary>::iterator iter = LSTDIARY.begin();
51     while (iter != LSTDIARY.end())
52     {
53         if (iter->getID() == ID_diary)
54         {
55             LSTDIARY.erase(iter++);
56             return true;
57         }
58         else
59         {
60             iter++;
61         }
62     }
63     return false;
64 }
65 /*--------------------------------------------------------------------------*/
66 void DiaryList::closeAllDiaries(void)
67 {
68     LSTDIARY.clear();
69 }
70 /*--------------------------------------------------------------------------*/
71 int * DiaryList::getIDs(int *sizeID)
72 {
73     int *IDs  = NULL;
74     *sizeID = 0;
75
76     LSTDIARY.sort(compareDiary);
77
78     *sizeID = (int)LSTDIARY.size();
79
80     if (*sizeID > 0)
81     {
82         std::list<Diary>::iterator iter;
83         int i = 0;
84         IDs = new int[*sizeID];
85         for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
86         {
87             IDs[i++] = iter->getID();
88         }
89     }
90     return IDs;
91 }
92 /*--------------------------------------------------------------------------*/
93 std::wstring DiaryList::getFilename(int ID_diary)
94 {
95     std::wstring wFilename(L"");
96     std::list<Diary>::iterator i;
97     for ( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
98     {
99         if ( i->getID() == ID_diary )
100         {
101             wFilename = i->getFilename();
102         }
103     }
104     return wFilename;
105 }
106 /*--------------------------------------------------------------------------*/
107 std::list<std::wstring> DiaryList::getFilenames()
108 {
109     LSTDIARY.sort(compareDiary);
110     int size = (int)LSTDIARY.size();
111     std::list<std::wstring> wFilenames(size);
112
113     if (size > 0)
114     {
115         for (auto& iter : LSTDIARY)
116         {
117             wFilenames.push_back(iter.getFilename());
118         }
119     }
120     return wFilenames;
121 }
122 /*--------------------------------------------------------------------------*/
123 void DiaryList::write(std::wstring _wstr, bool bInput)
124 {
125     std::list<Diary>::iterator i;
126     for ( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
127     {
128         i->write(_wstr, bInput);
129     }
130 }
131 /*--------------------------------------------------------------------------*/
132 void DiaryList::writeln(std::wstring _wstr, bool bInput)
133 {
134     std::list<Diary>::iterator i;
135     for ( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
136     {
137         i->writeln(_wstr, bInput);
138     }
139 }
140 /*--------------------------------------------------------------------------*/
141 bool DiaryList::exists(int ID_diary)
142 {
143     std::list<Diary>::iterator i;
144     for ( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
145     {
146         if ( i->getID() == ID_diary )
147         {
148             return true;
149         }
150     }
151     return false;
152 }
153 /*--------------------------------------------------------------------------*/
154 bool DiaryList::exists(std::wstring _wfilename)
155 {
156     std::list<Diary>::iterator i;
157     for ( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
158     {
159         wchar_t* wc = getFullFilenameW(_wfilename.data());
160         int comp = i->getFilename().compare(wc);
161         FREE(wc);
162         if (comp == 0)
163         {
164             return true;
165         }
166     }
167     return false;
168 }
169 /*--------------------------------------------------------------------------*/
170 int DiaryList::findFreeID(void)
171 {
172     int freeID = 1;
173     std::list<Diary>::iterator i;
174
175     LSTDIARY.sort(compareDiary);
176
177     for ( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
178     {
179         if ( freeID >= i->getID() )
180         {
181             freeID++;
182         }
183     }
184     return freeID;
185 }
186 /*--------------------------------------------------------------------------*/
187 void DiaryList::setSuspendWrite(bool bWith)
188 {
189     std::list<Diary>::iterator iter;
190
191     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
192     {
193         iter->setSuspendWrite(bWith);
194     }
195 }
196 /*--------------------------------------------------------------------------*/
197 bool *DiaryList::getSuspendWrite(int *sizeboolArray)
198 {
199     bool *bSuspend = NULL;
200     *sizeboolArray = 0;
201
202     LSTDIARY.sort(compareDiary);
203
204     *sizeboolArray = (int)LSTDIARY.size();
205     if (*sizeboolArray > 0)
206     {
207         int i = 0;
208         bSuspend = new bool[*sizeboolArray];
209         std::list<Diary>::iterator iter;
210         for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
211         {
212             bSuspend[i++] = iter->getSuspendWrite();
213         }
214     }
215     return bSuspend;
216 }
217 /*--------------------------------------------------------------------------*/
218 void DiaryList::setSuspendWrite(int  ID_diary, bool bWith)
219 {
220     std::list<Diary>::iterator iter;
221
222     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
223     {
224         if (iter->getID() == ID_diary)
225         {
226             iter->setSuspendWrite(bWith);
227         }
228     }
229 }
230 /*--------------------------------------------------------------------------*/
231 bool DiaryList::getSuspendWrite(int  ID_diary)
232 {
233     std::list<Diary>::iterator iter;
234
235     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
236     {
237         if (iter->getID() == ID_diary)
238         {
239             return iter->getSuspendWrite();
240         }
241     }
242     return false;
243 }
244 /*--------------------------------------------------------------------------*/
245 int DiaryList::getID(std::wstring _wfilename)
246 {
247     std::list<Diary>::iterator i;
248     for ( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
249     {
250         wchar_t* ws = getFullFilenameW(_wfilename.data());
251         int comp = i->getFilename().compare(ws);
252         FREE(ws);
253         if (comp == 0)
254         {
255             return i->getID();
256         }
257     }
258     return -1;
259 }
260 /*--------------------------------------------------------------------------*/
261 void DiaryList::setFilterMode(int ID_diary, diary_filter mode)
262 {
263     std::list<Diary>::iterator iter;
264
265     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
266     {
267         if (iter->getID() == ID_diary)
268         {
269             return iter->setIOMode(mode);
270         }
271     }
272 }
273 /*--------------------------------------------------------------------------*/
274 void DiaryList::setPrefixMode(int ID_diary, diary_prefix_time_format iPrefixMode)
275 {
276     std::list<Diary>::iterator iter;
277
278     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
279     {
280         if (iter->getID() == ID_diary)
281         {
282             iter->setPrefixMode(iPrefixMode);
283             break;
284         }
285     }
286 }
287 /*--------------------------------------------------------------------------*/
288 diary_prefix_time_format DiaryList::getPrefixMode(int ID_diary)
289 {
290     std::list<Diary>::iterator iter;
291
292     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
293     {
294         if (iter->getID() == ID_diary)
295         {
296             return iter->getPrefixMode();
297         }
298     }
299     return PREFIX_TIME_FORMAT_ERROR;
300 }
301 /*--------------------------------------------------------------------------*/
302 void DiaryList::setPrefixIoModeFilter(int ID_diary, diary_prefix_time_filter mode)
303 {
304     std::list<Diary>::iterator iter;
305
306     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
307     {
308         if (iter->getID() == ID_diary)
309         {
310             iter->setPrefixIoModeFilter(mode);
311             break;
312         }
313     }
314 }
315 /*--------------------------------------------------------------------------*/
316 diary_prefix_time_filter DiaryList::getPrefixIoModeFilter(int ID_diary)
317 {
318     std::list<Diary>::iterator iter;
319
320     for ( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
321     {
322         if (iter->getID() == ID_diary)
323         {
324             return iter->getPrefixIoModeFilter();
325         }
326     }
327     return PREFIX_FILTER_ERROR;
328 }
329 /*--------------------------------------------------------------------------*/
330