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