GPL + CeCILL Header change
[scilab.git] / scilab / modules / output_stream / src / cpp / diary_manager.cpp
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  * === LICENSE_END ===
14 *
15 */
16 /*--------------------------------------------------------------------------*/
17 #include "diary_manager.hxx"
18 #include "DiaryList.hxx"
19 #include "diary.h"
20 #include "sci_malloc.h"
21 /*--------------------------------------------------------------------------*/
22 static DiaryList *SCIDIARY = NULL;
23 /*--------------------------------------------------------------------------*/
24 static int createDiaryManager(void)
25 {
26     if (SCIDIARY == NULL)
27     {
28         SCIDIARY = new DiaryList();
29         return 0;
30     }
31     return 1;
32 }
33 /*--------------------------------------------------------------------------*/
34 static int destroyDiaryManager(void)
35 {
36     if (SCIDIARY)
37     {
38         delete SCIDIARY;
39         SCIDIARY = NULL;
40         return 0;
41     }
42     return 1;
43 }
44 /*--------------------------------------------------------------------------*/
45 wchar_t *getDiaryFilename(int _Id)
46 {
47     wchar_t *wcFilename = NULL;
48     if (SCIDIARY)
49     {
50         if (SCIDIARY->getFilename(_Id).compare(L""))
51         {
52             wcFilename = (wchar_t*) MALLOC(sizeof(wchar_t) * (SCIDIARY->getFilename(_Id).length() + 1));
53             if (wcFilename)
54             {
55                 wcscpy(wcFilename, SCIDIARY->getFilename(_Id).c_str());
56             }
57         }
58     }
59     return wcFilename;
60 }
61 /*--------------------------------------------------------------------------*/
62 wchar_t **getDiaryFilenames(int *array_size)
63 {
64     *array_size = 0;
65     if (SCIDIARY)
66     {
67         std::list<std::wstring> wstringFilenames = SCIDIARY->getFilenames();
68         *array_size = (int)wstringFilenames.size();
69         if (array_size > 0)
70         {
71             wchar_t **wcFilenames = (wchar_t **) MALLOC (sizeof(wchar_t*) * (*array_size));
72             int i = 0;
73             for (const auto& filename : wstringFilenames)
74             {
75                 wcFilenames[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (filename.length() + 1));
76                 wcscpy(wcFilenames[i], filename.data());
77                 ++i;
78             }
79             return wcFilenames;
80         }
81     }
82     return NULL;
83 }
84 /*--------------------------------------------------------------------------*/
85 int *getDiaryIDs(int *array_size)
86 {
87     *array_size = 0;
88     if (SCIDIARY)
89     {
90         int *iIDs = SCIDIARY->getIDs(array_size);
91         return iIDs;
92     }
93     return NULL;
94 }
95 /*--------------------------------------------------------------------------*/
96 double *getDiaryIDsAsDouble(int *array_size)
97 {
98     int *iIDs = getDiaryIDs(array_size);
99     if (*array_size > 0)
100     {
101         if (iIDs)
102         {
103             double *dIDs = new double[*array_size];
104             for (int i = 0; i < *array_size; i++)
105             {
106                 dIDs[i] = (double)iIDs[i];
107             }
108             delete [] iIDs;
109             return dIDs;
110         }
111         else
112         {
113             *array_size = 0;
114         }
115     }
116     return NULL;
117 }
118 /*--------------------------------------------------------------------------*/
119 int diaryCloseAll(void)
120 {
121     destroyDiaryManager();
122     return 0;
123 }
124 /*--------------------------------------------------------------------------*/
125 int diaryClose(int _iId)
126 {
127     if (SCIDIARY)
128     {
129         if (_iId > 0)
130         {
131             if (SCIDIARY->closeDiary(_iId))
132             {
133                 return 0;
134             }
135         }
136     }
137     return 1;
138 }
139 /*--------------------------------------------------------------------------*/
140 int diaryClose(wchar_t *filename)
141 {
142     if (SCIDIARY)
143     {
144         int iID = SCIDIARY->getID(filename);
145         if (iID > 0)
146         {
147             if (SCIDIARY->closeDiary(iID))
148             {
149                 return 0;
150             }
151         }
152     }
153     return 1;
154 }
155 /*--------------------------------------------------------------------------*/
156 int diaryPauseAll(void)
157 {
158     if (SCIDIARY)
159     {
160         SCIDIARY->setSuspendWrite(true);
161         return 0;
162     }
163     return 1;
164 }
165 /*--------------------------------------------------------------------------*/
166 int diaryPause(int _iId)
167 {
168     if (SCIDIARY)
169     {
170         SCIDIARY->setSuspendWrite(_iId, true);
171         return 0;
172     }
173     return 1;
174 }
175 /*--------------------------------------------------------------------------*/
176 int diaryPause(wchar_t *filename)
177 {
178     if (SCIDIARY)
179     {
180         int iID = SCIDIARY->getID(std::wstring(filename));
181         if (iID != -1)
182         {
183             SCIDIARY->setSuspendWrite(iID, true);
184             return 0;
185         }
186     }
187     return 1;
188 }
189 /*--------------------------------------------------------------------------*/
190 int diaryResumeAll(void)
191 {
192     if (SCIDIARY)
193     {
194         SCIDIARY->setSuspendWrite(false);
195         return 0;
196     }
197     return 1;
198 }
199 /*--------------------------------------------------------------------------*/
200 int diaryResume(int _iId)
201 {
202     if (SCIDIARY)
203     {
204         SCIDIARY->setSuspendWrite(_iId, false);
205         return 0;
206     }
207     return 1;
208 }
209 /*--------------------------------------------------------------------------*/
210 int diaryResume(wchar_t *filename)
211 {
212     if (SCIDIARY)
213     {
214         int iID = SCIDIARY->getID(std::wstring(filename));
215         if (iID != -1)
216         {
217             SCIDIARY->setSuspendWrite(iID, false);
218             return 0;
219         }
220     }
221     return 1;
222 }
223 /*--------------------------------------------------------------------------*/
224 int diaryExists(int _iId)
225 {
226     if (SCIDIARY)
227     {
228         if (SCIDIARY->exists(_iId))
229         {
230             return 0;
231         }
232     }
233     return 1;
234 }
235 /*--------------------------------------------------------------------------*/
236 int diaryExists(wchar_t *filename)
237 {
238     if (SCIDIARY)
239     {
240         if (SCIDIARY->exists(std::wstring(filename)))
241         {
242             return 0;
243         }
244     }
245     return 1;
246 }
247 /*--------------------------------------------------------------------------*/
248 int diaryNew(wchar_t *filename, bool autorename)
249 {
250     createDiaryManager();
251
252     if (SCIDIARY)
253     {
254         return SCIDIARY->openDiary(std::wstring(filename), autorename);
255     }
256
257     return -1;
258 }
259 /*--------------------------------------------------------------------------*/
260 int diaryAppend(wchar_t *filename)
261 {
262     createDiaryManager();
263     if (SCIDIARY)
264     {
265         return SCIDIARY->openDiary(std::wstring(filename), 1, false);
266     }
267     return -1;
268 }
269 /*--------------------------------------------------------------------------*/
270 int diaryWrite(const wchar_t *wstr, BOOL bInput)
271 {
272     if (SCIDIARY)
273     {
274         if (bInput)
275         {
276             SCIDIARY->write(std::wstring(wstr), true);
277         }
278         else
279         {
280             SCIDIARY->write(std::wstring(wstr), false);
281         }
282         return 0;
283     }
284     return 1;
285 }
286 /*--------------------------------------------------------------------------*/
287 int diaryWriteln(const wchar_t *wstr, BOOL bInput)
288 {
289     if (SCIDIARY)
290     {
291         if (bInput)
292         {
293             SCIDIARY->writeln(std::wstring(wstr), true);
294         }
295         else
296         {
297             SCIDIARY->writeln(std::wstring(wstr), false);
298         }
299         return 0;
300     }
301     return 1;
302 }
303 /*--------------------------------------------------------------------------*/
304 int diarySetFilterMode(int _iId, diary_filter mode)
305 {
306     if (SCIDIARY)
307     {
308         SCIDIARY->setFilterMode(_iId, mode);
309         return 0;
310     }
311     return 1;
312 }
313 /*--------------------------------------------------------------------------*/
314 int diarySetPrefixMode(int ID_diary, diary_prefix_time_format iPrefixMode)
315 {
316     if (SCIDIARY)
317     {
318         SCIDIARY->setPrefixMode(ID_diary, iPrefixMode);
319         return 0;
320     }
321     return 1;
322 }
323 /*--------------------------------------------------------------------------*/
324 int diaryGetPrefixMode(int ID_diary)
325 {
326     if (SCIDIARY)
327     {
328         return SCIDIARY->getPrefixMode(ID_diary);
329     }
330     return -1;
331 }
332 /*--------------------------------------------------------------------------*/
333 int diarySetPrefixIoModeFilter(int ID_diary, diary_prefix_time_filter mode)
334 {
335     if (SCIDIARY)
336     {
337         SCIDIARY->setPrefixIoModeFilter(ID_diary, mode);
338         return 0;
339     }
340     return 1;
341 }
342 /*--------------------------------------------------------------------------*/
343 diary_prefix_time_filter diaryGetPrefixIoModeFilter(int ID_diary)
344 {
345     if (SCIDIARY)
346     {
347         return SCIDIARY->getPrefixIoModeFilter(ID_diary);
348     }
349     return PREFIX_FILTER_ERROR;
350 }
351 /*--------------------------------------------------------------------------*/