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