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