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