new diary (SEP 23)
[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-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)
27 {
28         return openDiary(_wfilename,0);
29 }
30 /*--------------------------------------------------------------------------*/ 
31 int DiaryList::openDiary(std::wstring _wfilename,int _mode)
32 {
33         int ID = -1;
34         Diary newDiary(_wfilename, _mode,findFreeID());
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 ) wFilename = i->getFilename();
95         }
96         return wFilename;
97 }
98 /*--------------------------------------------------------------------------*/ 
99 std::wstring * DiaryList::getFilenames(int *sizeFilenames)
100 {
101         std::wstring *wFilenames = NULL;
102         *sizeFilenames = 0;
103
104         LSTDIARY.sort(compareDiary);
105
106         *sizeFilenames = (int)LSTDIARY.size();
107         if (*sizeFilenames > 0)
108         {
109                 int i = 0;
110                 std::list<Diary>::iterator iter;
111                 wFilenames = new std::wstring[*sizeFilenames];
112                 
113                 for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
114                 {
115                         wFilenames[i++] = iter->getFilename();
116                 }
117         }
118         return wFilenames;
119 }
120 /*--------------------------------------------------------------------------*/ 
121 void DiaryList::write(std::wstring _wstr, bool bInput)
122 {
123         std::list<Diary>::iterator i;
124         for( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
125         {
126                 i->write(_wstr, bInput);
127         }
128 }
129 /*--------------------------------------------------------------------------*/ 
130 void DiaryList::writeln(std::wstring _wstr, bool bInput)
131 {
132         std::list<Diary>::iterator i;
133         for( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
134         {
135                 i->writeln(_wstr, bInput);
136         }
137 }
138 /*--------------------------------------------------------------------------*/ 
139 bool DiaryList::exists(int ID_diary)
140 {
141         std::list<Diary>::iterator i;
142         for( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
143         {
144                 if ( i->getID() == ID_diary ) return true;
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 ) return true;
155         }
156         return false;
157 }
158 /*--------------------------------------------------------------------------*/ 
159 int DiaryList::findFreeID(void)
160 {
161         int freeID = 1;
162         std::list<Diary>::iterator i;
163
164         LSTDIARY.sort(compareDiary);
165
166         for( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
167         {
168                 if ( freeID >= i->getID() )
169                 {
170                         freeID++;
171                 }
172         }
173         return freeID;
174 }
175 /*--------------------------------------------------------------------------*/ 
176 void DiaryList::setSuspendWrite(bool bWith)
177 {
178         std::list<Diary>::iterator iter;
179
180         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
181         {
182                 iter->setSuspendWrite(bWith);
183         }
184 }
185 /*--------------------------------------------------------------------------*/ 
186 bool *DiaryList::getSuspendWrite(int *sizeboolArray)
187 {
188         bool *bSuspend = NULL;
189         *sizeboolArray = 0;
190
191         LSTDIARY.sort(compareDiary);
192
193         *sizeboolArray = (int)LSTDIARY.size();
194         if (*sizeboolArray > 0)
195         {
196                 int i = 0;
197                 bSuspend = new bool[*sizeboolArray];
198                 std::list<Diary>::iterator iter;
199                 for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
200                 {
201                         bSuspend[i++] = iter->getSuspendWrite();
202                 }
203         }
204         return bSuspend;
205 }
206 /*--------------------------------------------------------------------------*/ 
207 void DiaryList::setSuspendWrite(int  ID_diary, bool bWith)
208 {
209         std::list<Diary>::iterator iter;
210
211         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
212         {
213                 if (iter->getID() == ID_diary)
214                 {
215                         iter->setSuspendWrite(bWith);
216                 }
217         }
218 }
219 /*--------------------------------------------------------------------------*/ 
220 bool DiaryList::getSuspendWrite(int  ID_diary)
221 {
222         std::list<Diary>::iterator iter;
223
224         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
225         {
226                 if (iter->getID() == ID_diary)
227                 {
228                         return iter->getSuspendWrite();
229                 }
230         }
231         return false;
232 }
233 /*--------------------------------------------------------------------------*/ 
234 int DiaryList::getID(std::wstring _wfilename)
235 {
236         std::list<Diary>::iterator i;
237         for( i = LSTDIARY.begin(); i != LSTDIARY.end(); i++)
238         {
239                 if ( i->getFilename().compare(getFullFilename(_wfilename)) == 0 ) return i->getID();
240         }
241         return -1;
242 }
243 /*--------------------------------------------------------------------------*/ 
244 void DiaryList::setFilterMode(int ID_diary, int mode)
245 {
246         std::list<Diary>::iterator iter;
247
248         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
249         {
250                 if (iter->getID() == ID_diary)
251                 {
252                         return iter->setIOMode(mode);
253                 }
254         }
255 }
256 /*--------------------------------------------------------------------------*/ 
257 void DiaryList::setPrefixMode(int ID_diary,int iPrefixMode)
258 {
259         std::list<Diary>::iterator iter;
260
261         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
262         {
263                 if (iter->getID() == ID_diary)
264                 {
265                         iter->setPrefixMode(iPrefixMode);
266                         break;
267                 }
268         }
269 }
270 /*--------------------------------------------------------------------------*/ 
271 int DiaryList::getPrefixMode(int ID_diary)
272 {
273         std::list<Diary>::iterator iter;
274
275         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
276         {
277                 if (iter->getID() == ID_diary)
278                 {
279                         return iter->getPrefixMode();
280                 }
281         }
282         return -1;
283 }
284 /*--------------------------------------------------------------------------*/ 
285 void DiaryList::setPrefixIoModeFilter(int ID_diary,int mode)
286 {
287         std::list<Diary>::iterator iter;
288
289         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
290         {
291                 if (iter->getID() == ID_diary)
292                 {
293                         iter->setPrefixIoModeFilter(mode);
294                         break;
295                 }
296         }
297 }
298 /*--------------------------------------------------------------------------*/ 
299 int DiaryList::getPrefixIoModeFilter(int ID_diary)
300 {
301         std::list<Diary>::iterator iter;
302
303         for( iter = LSTDIARY.begin(); iter != LSTDIARY.end(); iter++)
304         {
305                 if (iter->getID() == ID_diary)
306                 {
307                         return iter->getPrefixIoModeFilter();
308                 }
309         }
310         return -1;
311 }
312 /*--------------------------------------------------------------------------*/ 
313