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