memory leak
[scilab.git] / scilab / modules / output_stream / sci_gateway / cpp / sci_diary.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 /*--------------------------------------------------------------------------*/
15 extern "C"
16 {
17 #include "gw_output_stream.h"
18 #include "stack-c.h"
19 #include "api_common.h"
20 #include "api_string.h"
21 #include "api_double.h"
22 #include "api_boolean.h"
23 #include "localization.h"
24 #include "charEncoding.h"
25 #include "Scierror.h"
26 #include "MALLOC.h"
27 }
28 /*--------------------------------------------------------------------------*/
29 #define DIARY_SECOND_ARG_LIST L"list"
30 #define DIARY_SECOND_ARG_CLOSE L"close"
31 #define DIARY_SECOND_ARG_PAUSE L"pause"
32 #define DIARY_SECOND_ARG_OFF L"off"
33 #define DIARY_SECOND_ARG_RESUME L"resume"
34 #define DIARY_SECOND_ARG_ON L"on"
35 #define DIARY_SECOND_ARG_NEW L"new"
36 #define DIARY_SECOND_ARG_APPEND L"append"
37 #define DIARY_SECOND_ARG_EXISTS L"exists"
38 #define DIARY_THIRD_ARG_FILTER_COMMAND L"filter=command"
39 #define DIARY_THIRD_ARG_FILTER_OUTPUT L"filter=output"
40 #define DIARY_THIRD_ARG_PREFIX_UNIX_EPOCH L"prefix=U"
41 #define DIARY_THIRD_ARG_PREFIX_DEFAULT L"prefix=YYYY-MM-DD hh:mm:ss"
42 #define DIARY_THIRD_ARG_PREFIX_ONLY_COMMANDS L"prefix-only-commands"
43 /*--------------------------------------------------------------------------*/
44 static int sci_diary_no_rhs(char *fname);
45 static int sci_diary_one_rhs(char *fname);
46 static int sci_diary_two_rhs(char *fname);
47 static int sci_diary_three_rhs(char *fname);
48 /*--------------------------------------------------------------------------*/
49 static double *getInputArgumentOneIDs(char *fname,int *sizeReturnedArray, int *ierror);
50 static wchar_t **getInputArgumentOneFilenames(char *fname,int *sizeReturnedArray, int *ierror);
51 static wchar_t *getInputArgumentTwo(char *fname, int *ierror);
52 static wchar_t **getInputArgumentThree(char *fname,int *sizeReturnedArray, int *ierror);
53 static int checkExistByIDs(char *fname, double *IDs, int size_IDs); 
54 static int checkExistByFilenames(char *fname, wchar_t **wcFilenames, int size_IDs); 
55 static int freeInput( wchar_t **wcInput, int size_IDs);
56 static int CloseByFilenames(char *fname);
57 static int CloseByIds(char *fname);
58 static int PauseByFilenames(char *fname);
59 static int PauseByIds(char *fname);
60 static int ResumeByFilenames(char *fname);
61 static int ResumeByIds(char *fname);
62 static int ExistByFilenames(char *fname);
63 static int ExistByIds(char *fname);
64 static int AppendByFilenames(char *fname, 
65                                                          diary_filter filterMode, 
66                                                          diary_prefix_time_format prefixMode, 
67                                                          diary_prefix_time_filter prefixModeFilter,
68                                                          bool suspended);
69 static int NewByFilenames(char *fname, 
70                                                   diary_filter filterMode, 
71                                                   diary_prefix_time_format prefixMode, 
72                                                   diary_prefix_time_filter prefixModeFilter,
73                                                   bool suspended);
74 /*--------------------------------------------------------------------------*/
75 int sci_diary(char *fname,unsigned long fname_len)
76 {
77         CheckRhs(0,3);
78         CheckLhs(0,2);
79
80         switch (Rhs)
81         {
82         case 0:
83                 return sci_diary_no_rhs(fname);
84         case 1:
85                 return sci_diary_one_rhs(fname);
86         case 2:
87                 return sci_diary_two_rhs(fname);
88         case 3:
89                 return sci_diary_three_rhs(fname);
90         }
91         return 0;
92 }
93 /*--------------------------------------------------------------------------*/
94 static int sci_diary_no_rhs(char *fname)
95 {
96         // [ids, filenames] = diary()
97         // [ids, filenames] = diary([],"list")
98
99         int nb_diary_ids = 0;
100         double *diary_ids  = getDiaryIDsAsDouble(&nb_diary_ids);
101
102         if ( (diary_ids) && (nb_diary_ids > 0) )
103         {
104                 createMatrixOfDouble(Rhs + 1, nb_diary_ids, 1, diary_ids);
105                 LhsVar(1) = Rhs + 1;
106
107                 delete [] diary_ids;
108                 diary_ids = NULL;
109                 nb_diary_ids = 0;
110         }
111         else
112         {
113                 if (nb_diary_ids == 0)
114                 {
115                         createMatrixOfDouble(Rhs + 1, 0, 0, NULL);
116                         LhsVar(1) = Rhs + 1;
117
118                         if (diary_ids) 
119                         {
120                                 delete [] diary_ids;
121                                 diary_ids = NULL;
122                                 nb_diary_ids = 0;
123                         }
124                 }
125                 else
126                 {
127                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
128                         return 0;
129                 }
130         }
131
132         if (Lhs == 2)
133         {
134                 int nb_diary_filenames = 0;
135                 wchar_t **wcdiary_filenames = getDiaryFilenames(&nb_diary_filenames);
136
137                 if ( (wcdiary_filenames) && (nb_diary_filenames > 0) )
138                 {
139                         createMatrixOfWideString(Rhs + 2, nb_diary_filenames, 1, wcdiary_filenames);
140                         LhsVar(2) = Rhs + 2;
141
142                         freeInput(wcdiary_filenames, nb_diary_filenames);
143                         nb_diary_filenames = 0;
144                 }
145                 else
146                 {
147                         if (nb_diary_filenames == 0)
148                         {
149                                 createMatrixOfDouble(Rhs + 2, 0, 0, NULL);
150                                 LhsVar(2) = Rhs + 2;
151                                 if (wcdiary_filenames)
152                                 {
153                                         freeInput(wcdiary_filenames, nb_diary_filenames);
154                                         nb_diary_filenames = 0;
155                                 }
156                         }
157                         else
158                         {
159                                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
160                                 return 0;
161                         }
162                 }
163         }
164
165         C2F(putlhsvar)();
166
167         return 0;
168 }
169 /*--------------------------------------------------------------------------*/
170 static int sci_diary_one_rhs(char *fname)
171 {
172         int *piAddressVarOne = NULL;
173
174         getVarAddressFromPosition(1, &piAddressVarOne);
175
176         if (getVarType(piAddressVarOne) == sci_matrix)
177         {
178                 int IDs_size = 0;
179                 int ierr = 0;
180                 double * IDs = getInputArgumentOneIDs(fname, &IDs_size, &ierr);
181                 if (ierr) return 0;
182
183                 // diary([])
184                 // diary(0)
185                 // close all
186                 if ( (IDs_size == 0) ||
187                          ((IDs_size == 1) && ((int)IDs[0] == 0)))
188                 {
189                         diaryCloseAll();
190                         C2F(putlhsvar)();
191                 }
192                 else
193                 {
194                         Scierror(999,_("%s: Wrong value for input argument #%d: 0 expected.\n"),fname,1);
195                 }
196         }
197         else if (getVarType(piAddressVarOne) == sci_strings)
198         {
199                 int ierr = 0;
200                 int sizewcFilenames = 0;
201                 wchar_t ** wcFilenames = getInputArgumentOneFilenames(fname,&sizewcFilenames,&ierr);
202                 if (ierr) return 0;
203
204                 if (sizewcFilenames == 1)
205                 {
206                         if (diaryExists(wcFilenames[0]))
207                         {
208                                 double dID = (double)diaryNew(wcFilenames[0], false);
209                                 if (dID == -1)
210                                 {
211                                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
212                                         if (utf_str)
213                                         {
214                                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
215                                                 FREE(utf_str); utf_str = NULL;
216                                         }
217                                         else
218                                         {
219                                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
220                                         }
221                                         return 0;
222                                 }
223
224                                 createMatrixOfDouble(Rhs + 1, 1, 1, &dID);
225                                 LhsVar(1) = Rhs + 1;
226
227                                 if (Lhs == 2)
228                                 {
229                                         wchar_t **wfilenameUsed = new wchar_t*[1];
230                                         wfilenameUsed[0] = getDiaryFilename((int)dID);
231                                         createMatrixOfWideString(Rhs + 2, 1, 1, wfilenameUsed);
232                                         LhsVar(2) = Rhs + 2;
233                                         freeInput(wfilenameUsed,1);
234                                 }
235                         }
236                         else // diary(filename) exists (close diary)
237                         {
238                                 if (diaryClose(wcFilenames[0]))
239                                 {
240                                         if (wcFilenames)
241                                         {
242                                                 if (wcFilenames[0]) {FREE(wcFilenames[0]); wcFilenames[0] = NULL;}
243                                                 FREE(wcFilenames); wcFilenames = NULL;
244                                                 Scierror(999,_("%s: error can not close diary.\n"),fname);
245                                                 return 0;
246                                         }
247                                 }
248                                 LhsVar(1) = 0; 
249                         }
250
251                         freeInput(wcFilenames,sizewcFilenames);
252                         C2F(putlhsvar)();
253                 }
254                 else
255                 {
256                         freeInput(wcFilenames,sizewcFilenames);
257                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname, 1);
258                 }
259         }
260         else
261         {
262                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
263         }
264         return 0;
265 }
266 /*--------------------------------------------------------------------------*/
267 static int sci_diary_two_rhs(char *fname)
268 {
269         int ierr = 0;
270         wchar_t *wcArgumentTwo = getInputArgumentTwo(fname, &ierr);
271
272         int *piAddressVarOne = NULL;
273         getVarAddressFromPosition(1, &piAddressVarOne);
274
275         if (ierr) return 0;
276
277         if (wcArgumentTwo)
278         {
279                 if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_LIST) == 0)
280                 {
281                         int m1 = 0, n1 = 0;
282                         getVarDimension(piAddressVarOne,&m1,&n1);
283                         if ( (m1 == n1) && (n1 == 0) )
284                         {
285                                 // diary() == diary([], "list")
286                                 return sci_diary_no_rhs(fname);
287                         }
288                         else
289                         {
290                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
291                         }
292                 }
293                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_CLOSE) == 0)
294                 {
295                         if (getVarType(piAddressVarOne) == sci_matrix)
296                         {
297                                 return CloseByIds(fname);
298                         }
299                         else if (getVarType(piAddressVarOne) == sci_strings)
300                         {
301                                 return CloseByFilenames(fname);
302                         }
303                         else
304                         {
305                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
306                                 return 0;
307                         }
308                 }
309                 else if ( (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_PAUSE) == 0) ||
310                                   (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_OFF) == 0) )
311                 {
312                         if (getVarType(piAddressVarOne) == sci_matrix)
313                         {
314                                 return PauseByIds(fname);
315                         }
316                         else if (getVarType(piAddressVarOne) == sci_strings)
317                         {
318                                 return PauseByFilenames(fname);
319                         }
320                         else
321                         {
322                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
323                                 return 0;
324                         }
325                 }
326                 else if ( (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_RESUME) == 0) ||
327                                   (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_ON) == 0) )
328                 {
329                         if (getVarType(piAddressVarOne) == sci_matrix)
330                         {
331                                 return ResumeByIds(fname);
332                         }
333                         else if (getVarType(piAddressVarOne) == sci_strings)
334                         {
335                                 return ResumeByFilenames(fname);
336                         }
337                         else
338                         {
339                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
340                                 return 0;
341                         }
342                 }
343                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_NEW) == 0)
344                 {
345                         if (getVarType(piAddressVarOne) == sci_strings)
346                         {
347                                 return NewByFilenames(fname, DIARY_FILTER_INPUT_AND_OUTPUT, 
348                                         PREFIX_TIME_FORMAT_UNIX_EPOCH, 
349                                         PREFIX_FILTER_NONE, false);
350                         }
351                         else
352                         {
353                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
354                                 return 0;
355                         }
356                 }
357                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_APPEND) == 0)
358                 {
359                         if (getVarType(piAddressVarOne) == sci_strings)
360                         {
361                                 return AppendByFilenames(fname, DIARY_FILTER_INPUT_AND_OUTPUT, 
362                                         PREFIX_TIME_FORMAT_UNIX_EPOCH, 
363                                         PREFIX_FILTER_NONE, false);
364                         }
365                         else
366                         {
367                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
368                                 return 0;
369                         }
370                 }
371                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_EXISTS) == 0)
372                 {
373                         if (getVarType(piAddressVarOne) == sci_matrix)
374                         {
375                                 return ExistByIds(fname);
376                         }
377                         else if (getVarType(piAddressVarOne) == sci_strings)
378                         {
379                                 return ExistByFilenames(fname);
380                         }
381                         else
382                         {
383                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
384                                 return 0;
385                         }
386                 }
387                 else
388                 {
389                         Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
390                         return 0;
391                 }
392         }
393         else
394         {
395                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
396         }
397         
398         return 0;
399 }
400 /*--------------------------------------------------------------------------*/
401 static int sci_diary_three_rhs(char *fname)
402 {
403         int ierr = 0;
404         int size_ArgThree = 0;
405         wchar_t **wcArgumentThree = getInputArgumentThree(fname, &size_ArgThree, &ierr);
406         if (ierr) return 0;
407
408         if (wcArgumentThree)
409         {
410                 diary_filter filterMode = DIARY_FILTER_INPUT_AND_OUTPUT;
411                 diary_prefix_time_format iPrefixMode = PREFIX_TIME_FORMAT_UNIX_EPOCH;
412                 diary_prefix_time_filter iPrefixIoModeFilter = PREFIX_FILTER_NONE;
413                 bool suspendedDiary = false;
414
415                 for (int i = 0; i < size_ArgThree; i++)
416                 {
417                         if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_FILTER_COMMAND) == 0)
418                         {
419                                 filterMode = DIARY_FILTER_ONLY_INPUT; // input only
420                         }
421                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_FILTER_OUTPUT) == 0)
422                         {
423                                 filterMode = DIARY_FILTER_ONLY_OUTPUT; // output only
424                         }
425                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_UNIX_EPOCH) == 0)
426                         {
427                                 iPrefixMode = PREFIX_TIME_FORMAT_UNIX_EPOCH;
428                                 if (iPrefixIoModeFilter == PREFIX_FILTER_NONE) iPrefixIoModeFilter = PREFIX_FILTER_INPUT_AND_OUTPUT;
429                         }
430                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_DEFAULT) == 0)
431                         {
432                                 iPrefixMode = PREFIX_TIME_FORMAT_ISO_8601;
433                                 if (iPrefixIoModeFilter == PREFIX_FILTER_NONE) iPrefixIoModeFilter = PREFIX_FILTER_INPUT_AND_OUTPUT;
434                         }
435                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_ONLY_COMMANDS) == 0)
436                         {
437                                 iPrefixIoModeFilter = PREFIX_FILTER_ONLY_INPUT;
438                         }
439                         else if ( (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_ON) == 0) || (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_RESUME) == 0) )
440                         {
441                                 suspendedDiary = false;
442                         }
443                         else if ( (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_OFF) == 0) || (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_PAUSE) == 0) )
444                         {
445                                 suspendedDiary = true;
446                         }
447                         else
448                         {
449                                 freeInput(wcArgumentThree, size_ArgThree);
450                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 3);
451                                 return 0;
452                         }                       
453                 }
454                 freeInput(wcArgumentThree, size_ArgThree);
455
456                 wchar_t *wcArgumentTwo = getInputArgumentTwo(fname, &ierr);
457                 if (ierr) return 0;
458                 if (wcArgumentTwo)
459                 {
460                         int *piAddressVarOne = NULL;
461                         getVarAddressFromPosition(1, &piAddressVarOne);
462
463                         if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_NEW) == 0)
464                         {
465                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
466                                 if (getVarType(piAddressVarOne) == sci_strings)
467                                 {
468                                         return NewByFilenames(fname, filterMode, iPrefixMode, iPrefixIoModeFilter, suspendedDiary);
469                                 }
470                                 else
471                                 {
472                                         Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
473                                         return 0;
474                                 }
475                         }
476                         else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_APPEND) == 0)
477                         {
478                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
479                                 if (getVarType(piAddressVarOne) == sci_strings)
480                                 {
481                                         return AppendByFilenames(fname, filterMode, iPrefixMode, iPrefixIoModeFilter, suspendedDiary);
482                                 }
483                                 else
484                                 {
485                                         Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
486                                         return 0;
487                                 }
488                         }
489                         else
490                         {
491                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
492                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
493                                 return 0;
494                         }
495                 }
496                 else
497                 {
498                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
499                 }
500         }
501         else
502         {
503                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
504         }
505         return 0;
506 }
507 /*--------------------------------------------------------------------------*/
508 static double *getInputArgumentOneIDs(char *fname,int *sizeReturnedArray, int *ierror)
509 {
510         double *IDs = NULL;
511         *sizeReturnedArray = 0;
512         *ierror = 0;
513
514         int m1 = 0, n1 = 0;
515         int *piAddressVarOne = NULL;
516
517         getVarAddressFromPosition(1, &piAddressVarOne);
518
519         if (getVarType(piAddressVarOne) == sci_matrix)
520         {
521                 getMatrixOfDouble(piAddressVarOne,&m1,&n1,&IDs);
522                 if ( (m1 == 1) || (n1 == 1) )
523                 {
524                         *sizeReturnedArray = m1 * n1;
525                 }
526                 else if ( (m1 == 0) || (n1 == 0) )
527                 {
528                         *sizeReturnedArray = 0;
529                         *ierror = 2;
530                         return NULL;
531                 }
532                 else
533                 {
534                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,1);
535                         *ierror = 1;
536                 }
537         }
538         else
539         {
540                 Scierror(999,_("%s: Wrong type for input argument #%d: A scalar expected.\n"),fname,1);
541                 *ierror = 1;
542         }
543
544         return IDs;
545 }
546 /*--------------------------------------------------------------------------*/
547 static wchar_t **getInputArgumentOneFilenames(char *fname,int *sizeReturnedArray, int *ierror)
548 {
549         wchar_t** wcFilenames = NULL;
550         *sizeReturnedArray = 0;
551         *ierror = 0;
552
553         int m1 = 0, n1 = 0;
554         int *piAddressVarOne = NULL;
555
556         getVarAddressFromPosition(1, &piAddressVarOne);
557
558         if (getVarType(piAddressVarOne) == sci_strings)
559         {
560                 int *lenStVarOne = NULL;
561
562                 getVarDimension(piAddressVarOne,&m1,&n1);
563
564                 if (m1*n1 < 1)
565                 {
566                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname,1);
567                         *ierror = 1;
568                 }
569                 else if ( (m1 == 1) || (n1 == 1) )
570                 {
571                         lenStVarOne = (int *)MALLOC(sizeof(int) * (m1 * n1));
572                         if (lenStVarOne == NULL)
573                         {
574                                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
575                                 *ierror = 1;
576                         }
577                         else
578                         {
579                                 getMatrixOfWideString(piAddressVarOne, &m1, &n1, lenStVarOne, wcFilenames);
580                                 wcFilenames = (wchar_t **)MALLOC(sizeof(wchar_t *) * (m1 * n1));
581                                 if (wcFilenames == NULL)
582                                 {
583                                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
584                                         *ierror = 1;
585                                 }
586                                 else
587                                 {
588                                         getMatrixOfWideString(piAddressVarOne, &m1, &n1, lenStVarOne, wcFilenames);
589                                         *sizeReturnedArray = m1 * n1;
590                                 }
591                         }
592                 }
593                 else
594                 {
595                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,1);
596                         *ierror = 1;
597                 }
598         }
599         else
600         {
601                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
602                 *ierror = 1;
603         }
604         return wcFilenames;
605 }
606 /*--------------------------------------------------------------------------*/
607 static wchar_t *getInputArgumentTwo(char *fname, int *ierror)
608 {
609         wchar_t *wcInputArgumentTwo = NULL;
610         *ierror = 0;
611
612         int m2 = 0, n2 = 0;
613         int *piAddressVarTwo = NULL;
614
615         getVarAddressFromPosition(2, &piAddressVarTwo);
616
617         if (getVarType(piAddressVarTwo) == sci_strings)
618         {
619                 int lenStVarTwo = 0;
620
621                 getVarDimension(piAddressVarTwo,&m2,&n2);
622                 if ( (m2 != n2) && (n2 != 1) ) 
623                 {
624                         Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
625                         *ierror = 1;
626                 }
627                 else
628                 {
629                         getMatrixOfWideString(piAddressVarTwo,&m2,&n2,&lenStVarTwo,&wcInputArgumentTwo);
630                         if (wcInputArgumentTwo == NULL)
631                         {
632                                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
633                                 *ierror = 1;
634                         }
635                 }
636         }
637         else
638         {
639                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
640                 *ierror = 1;
641         }
642         return wcInputArgumentTwo;
643 }
644 /*--------------------------------------------------------------------------*/
645 static wchar_t** getInputArgumentThree(char *fname,int *sizeReturnedArray, int *ierror)
646 {
647         wchar_t** wcInputArgumentThree = NULL;
648         *sizeReturnedArray = 0;
649         *ierror = 0;
650
651         int m3 = 0, n3 = 0;
652         int *piAddressVarThree = NULL;
653
654         getVarAddressFromPosition(3, &piAddressVarThree);
655
656         if (getVarType(piAddressVarThree) == sci_strings)
657         {
658                 int *lenStVarThree = NULL;
659
660                 getVarDimension(piAddressVarThree,&m3,&n3);
661
662                 if (m3*n3 < 1)
663                 {
664                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname,3);
665                         *ierror = 1;
666                 }
667                 else if ( (m3 == 1) || (n3 == 1) )
668                 {
669                         lenStVarThree = (int *)MALLOC(sizeof(int) * (m3 * n3));
670                         if (lenStVarThree == NULL)
671                         {
672                                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
673                                 *ierror = 1;
674                         }
675                         else
676                         {
677                                 getMatrixOfWideString(piAddressVarThree, &m3, &n3, lenStVarThree, wcInputArgumentThree);
678                                 wcInputArgumentThree = (wchar_t **)MALLOC(sizeof(wchar_t *) * (m3 * n3));
679                                 if (wcInputArgumentThree == NULL)
680                                 {
681                                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
682                                         *ierror = 1;
683                                 }
684                                 else
685                                 {
686                                         getMatrixOfWideString(piAddressVarThree, &m3, &n3, lenStVarThree, wcInputArgumentThree);
687                                         *sizeReturnedArray = m3 * n3;
688                                 }
689                         }
690                 }
691                 else
692                 {
693                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,3);
694                         *ierror = 1;
695                 }
696         }
697         else
698         {
699                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,3);
700                 *ierror = 1;
701         }
702         return wcInputArgumentThree;
703 }
704 /*--------------------------------------------------------------------------*/
705 static int checkExistByIDs(char *fname, double *IDs, int size_IDs)
706 {
707         if (IDs)
708         {
709                 for (int i = 0; i < size_IDs; i++)
710                 {
711                         if (diaryExists((int)IDs[i]))
712                         {
713                                 Scierror(999,_("%s: Wrong value for input argument #%d: diary ID %d not exists.\n"),fname,1, (int)IDs[i]);
714                                 return 1;
715                         }
716                 }
717                 return 0;
718         }
719         return 1;
720 }
721 /*--------------------------------------------------------------------------*/
722 static int checkExistByFilenames(char *fname, wchar_t **wcFilenames, int size_IDs)
723 {
724         if (wcFilenames)
725         {
726                 for (int i = 0; i < size_IDs; i++)
727                 {
728                         if (diaryExists(wcFilenames[i]))
729                         {
730                                 Scierror(999,_("%s: Wrong value for input argument #%d: diary filename not exists.\n"),fname,1);
731                                 return 1;
732                         }
733                 }
734                 return 0;
735         }
736         return 1;
737 }
738 /*--------------------------------------------------------------------------*/
739 static int freeInput(wchar_t **wcInput, int size_IDs)
740 {
741         if (wcInput)
742         {
743                 for(int i = 0; i < size_IDs; i++)
744                 {
745                         if (wcInput[i])
746                         {
747                                 FREE(wcInput[i]);
748                                 wcInput[i] = NULL;
749                         }
750                 }
751         }
752         return 0;
753 }
754 /*--------------------------------------------------------------------------*/
755 static int CloseByFilenames(char *fname)
756 {
757         wchar_t **wcFilenames = NULL;
758         int dIDs_size = 0;
759         int ierr = 0;
760
761         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
762         if (ierr) return 0;
763
764         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
765         if (ierr) 
766         {
767                 freeInput(wcFilenames, dIDs_size);
768                 return 0;
769         }
770
771         for (int i = 0; i < dIDs_size; i++)
772         {
773                 if (diaryClose(wcFilenames[i]))
774                 {
775                         freeInput(wcFilenames, dIDs_size);
776                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not close diary.\n"),fname);
777                         return 0;
778                 }
779         }
780
781         freeInput(wcFilenames, dIDs_size);
782         C2F(putlhsvar)();
783         return 0;
784 }
785 /*--------------------------------------------------------------------------*/
786 static int CloseByIds(char *fname)
787 {
788         double *dIDs = NULL;
789         int dIDs_size = 0;
790         int ierr = 0;
791
792         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
793
794         if (ierr == 2)
795         {
796                 // diary([], 'close')
797                 diaryCloseAll();
798                 C2F(putlhsvar)();
799                 return 0;
800         }
801         else if (ierr) return 0;
802
803         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
804         if (ierr) return 0;
805
806         for (int i = 0; i < dIDs_size; i++)
807         {
808                 if (diaryClose((int)dIDs[i]))
809                 {
810                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not close diary %d.\n"),fname, (int)dIDs[i]);
811                         return 0;
812                 }
813         }
814         C2F(putlhsvar)();
815         return 0;
816 }
817 /*--------------------------------------------------------------------------*/
818 static int PauseByFilenames(char *fname)
819 {
820         wchar_t **wcFilenames = NULL;
821         int dIDs_size = 0;
822         int ierr = 0;
823
824         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
825         if (ierr) return 0;
826
827         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
828         if (ierr) 
829         {
830                 freeInput(wcFilenames, dIDs_size);
831                 return 0;
832         }
833
834         for (int i = 0; i < dIDs_size; i++)
835         {
836                 if (diaryPause(wcFilenames[i]))
837                 {
838                         freeInput(wcFilenames, dIDs_size);
839                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not pause diary.\n"),fname);
840                         return 0;
841                 }
842         }
843
844         freeInput(wcFilenames, dIDs_size);
845
846         C2F(putlhsvar)();
847         return 0;
848 }
849 /*--------------------------------------------------------------------------*/
850 static int PauseByIds(char *fname)
851 {
852         double *dIDs = NULL;
853         int dIDs_size = 0;
854         int ierr = 0;
855
856         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
857         if (ierr) return 0;
858
859         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
860         if (ierr) return 0;
861
862         for (int i = 0; i < dIDs_size; i++)
863         {
864                 if (diaryPause((int)dIDs[i]))
865                 {
866                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not pause diary %d.\n"),fname, (int)dIDs[i]);
867                         return 0;
868                 }
869         }
870
871         C2F(putlhsvar)();
872
873         return 0;
874 }
875 /*--------------------------------------------------------------------------*/
876 static int ResumeByFilenames(char *fname)
877 {
878         wchar_t **wcFilenames = NULL;
879         int dIDs_size = 0;
880         int ierr = 0;
881         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
882         if (ierr) return 0;
883
884         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
885         if (ierr) 
886         {
887                 freeInput(wcFilenames, dIDs_size);
888                 return 0;
889         }
890
891         for (int i = 0; i < dIDs_size; i++)
892         {
893                 if (diaryResume(wcFilenames[i]))
894                 {
895                         freeInput(wcFilenames, dIDs_size);
896                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not resume diary.\n"),fname);
897                         return 0;
898                 }
899         }
900
901         freeInput(wcFilenames, dIDs_size);
902
903         C2F(putlhsvar)();
904
905         return 0;
906 }
907 /*--------------------------------------------------------------------------*/
908 static int ResumeByIds(char *fname)
909 {
910         double *dIDs = NULL;
911         int dIDs_size = 0;
912         int ierr = 0;
913
914         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
915         if (ierr) return 0;
916
917         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
918         if (ierr) return 0;
919
920         for (int i = 0; i < dIDs_size; i++)
921         {
922                 if (diaryResume((int)dIDs[i]))
923                 {
924                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not resume diary %d.\n"),fname, (int)dIDs[i]);
925                         return 0;
926                 }
927         }
928
929         C2F(putlhsvar)();
930
931         return 0;
932 }
933 /*--------------------------------------------------------------------------*/
934 static int ExistByFilenames(char *fname)
935 {
936         wchar_t **wcFilenames = NULL;
937         int dIDs_size = 0;
938         int ierr = 0;
939         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
940         if (ierr) return 0;
941
942         int *resultExist = (int*)MALLOC(sizeof(int)*dIDs_size);
943         if (resultExist == NULL)
944         {
945                 freeInput(wcFilenames,dIDs_size);
946                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
947                 return 0;
948         }
949         
950         for(int i=0; i < dIDs_size; i++)
951         {
952                 int bExists = diaryExists(wcFilenames[i]);
953                 if ( bExists == 0)
954                 {
955                         resultExist[i]  = TRUE;
956                 }
957                 else
958                 {
959                         resultExist[i]  = FALSE;
960                 }
961         }
962         freeInput(wcFilenames,dIDs_size);
963
964         createMatrixOfBoolean(Rhs + 1, 1, dIDs_size, resultExist);
965         FREE(resultExist); resultExist = NULL;
966         LhsVar(1) = Rhs + 1;
967         C2F(putlhsvar)();
968         return 0;
969 }
970 /*--------------------------------------------------------------------------*/
971 static int ExistByIds(char *fname)
972 {
973         double *dIDs = NULL;
974         int dIDs_size = 0;
975         int ierr = 0;
976
977         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
978         if (ierr) return 0;
979
980         int *resultExist = (int*)MALLOC(sizeof(int)*dIDs_size);
981         if (resultExist == NULL)
982         {
983                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
984                 return 0;
985         }
986
987         for(int i=0; i < dIDs_size; i++)
988         {
989                 int bExists = diaryExists((int)dIDs[i]);
990                 if ( bExists == 0)
991                 {
992                         resultExist[i]  = TRUE;
993                 }
994                 else
995                 {
996                         resultExist[i]  = FALSE;
997                 }
998         }
999
1000         createMatrixOfBoolean(Rhs + 1, 1, dIDs_size, resultExist);
1001         FREE(resultExist); resultExist = NULL;
1002         LhsVar(1) = Rhs + 1;
1003         C2F(putlhsvar)();
1004         return 0;
1005 }
1006 /*--------------------------------------------------------------------------*/
1007 static int AppendByFilenames(char *fname, 
1008                                                          diary_filter filterMode, 
1009                                                          diary_prefix_time_format prefixMode, 
1010                                                          diary_prefix_time_filter prefixModeFilter,
1011                                                          bool suspended)
1012 {
1013         wchar_t **wcFilenames = NULL;
1014         int dIDs_size = 0;
1015         int ierr = 0;
1016
1017         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1018         if (ierr) return 0;
1019
1020         if (dIDs_size == 1)
1021         {
1022                 double dID = (double)diaryAppend(wcFilenames[0]);
1023                 if (dID == -1)
1024                 {
1025                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
1026                         if (utf_str)
1027                         {
1028                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
1029                                 FREE(utf_str); utf_str = NULL;
1030                         }
1031                         else
1032                         {
1033                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
1034                         }
1035                         freeInput(wcFilenames, dIDs_size);
1036                         return 0;
1037                 }
1038
1039                 freeInput(wcFilenames, dIDs_size);
1040
1041                 diarySetFilterMode((int)dID, filterMode);
1042                 diarySetPrefixMode((int)dID, prefixMode);
1043                 diarySetPrefixIoModeFilter((int)dID, prefixModeFilter);
1044                 if (suspended) diaryPause((int)dID);
1045
1046                 createMatrixOfDouble(Rhs + 1, 1, 1, &dID);
1047                 LhsVar(1) = Rhs + 1;
1048
1049                 if (Lhs == 2)
1050                 {
1051                         wchar_t **wfilenameUsed = new wchar_t*[1];
1052                         wfilenameUsed[0] = getDiaryFilename((int)dID);
1053                         createMatrixOfWideString(Rhs + 2, 1, 1, wfilenameUsed);
1054                         LhsVar(2) = Rhs + 2;
1055                         freeInput(wfilenameUsed, 1);
1056                 }
1057                 C2F(putlhsvar)();
1058         }
1059         else
1060         {
1061                 freeInput(wcFilenames, dIDs_size);
1062                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
1063         }
1064
1065         return 0;
1066 }
1067 /*--------------------------------------------------------------------------*/
1068 static int NewByFilenames(char *fname,
1069                                                   diary_filter filterMode, 
1070                                                   diary_prefix_time_format prefixMode, 
1071                                                   diary_prefix_time_filter prefixModeFilter, 
1072                                                   bool suspended)
1073 {
1074         wchar_t **wcFilenames = NULL;
1075         int dIDs_size = 0;
1076         int ierr = 0;
1077
1078         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1079         if (ierr) return 0;
1080
1081         if (dIDs_size == 1)
1082         {
1083                 double dID = (double)diaryNew(wcFilenames[0], true);
1084                 if (dID == -1)
1085                 {
1086                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
1087                         if (utf_str)
1088                         {
1089                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
1090                                 FREE(utf_str); utf_str = NULL;
1091                         }
1092                         else
1093                         {
1094                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
1095                         }
1096                         freeInput(wcFilenames, dIDs_size);
1097                         return 0;
1098                 }
1099
1100                 freeInput(wcFilenames, dIDs_size);
1101
1102                 diarySetFilterMode((int)dID, filterMode);
1103                 diarySetPrefixMode((int)dID, prefixMode);
1104                 diarySetPrefixIoModeFilter((int)dID, prefixModeFilter);
1105                 if (suspended) diaryPause((int)dID);
1106
1107                 createMatrixOfDouble(Rhs + 1, 1, 1, &dID);
1108                 LhsVar(1) = Rhs + 1;
1109
1110                 if (Lhs == 2)
1111                 {
1112                         wchar_t **wfilenameUsed = new wchar_t*[1];
1113                         wfilenameUsed[0] = getDiaryFilename((int)dID);
1114                         createMatrixOfWideString(Rhs + 2, 1, 1, wfilenameUsed);
1115                         LhsVar(2) = Rhs + 2;
1116                         freeInput(wfilenameUsed, 1);
1117                 }
1118                 C2F(putlhsvar)();
1119         }
1120         else
1121         {
1122                 freeInput(wcFilenames, dIDs_size);
1123                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
1124         }
1125
1126         return 0;
1127 }
1128 /*--------------------------------------------------------------------------*/