add some Scierror after printError to create an error in Scilab
[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_scilab.h"
20 #include "localization.h"
21 #include "charEncoding.h"
22 #include "Scierror.h"
23 #include "MALLOC.h"
24 #include "freeArrayOfString.h"
25 }
26 /*--------------------------------------------------------------------------*/
27 #define DIARY_SECOND_ARG_LIST L"list"
28 #define DIARY_SECOND_ARG_CLOSE L"close"
29 #define DIARY_SECOND_ARG_PAUSE L"pause"
30 #define DIARY_SECOND_ARG_OFF L"off"
31 #define DIARY_SECOND_ARG_RESUME L"resume"
32 #define DIARY_SECOND_ARG_ON L"on"
33 #define DIARY_SECOND_ARG_NEW L"new"
34 #define DIARY_SECOND_ARG_APPEND L"append"
35 #define DIARY_SECOND_ARG_EXISTS L"exists"
36 #define DIARY_THIRD_ARG_FILTER_COMMAND L"filter=command"
37 #define DIARY_THIRD_ARG_FILTER_OUTPUT L"filter=output"
38 #define DIARY_THIRD_ARG_PREFIX_UNIX_EPOCH L"prefix=U"
39 #define DIARY_THIRD_ARG_PREFIX_DEFAULT L"prefix=YYYY-MM-DD hh:mm:ss"
40 #define DIARY_THIRD_ARG_PREFIX_ONLY_COMMANDS L"prefix-only-commands"
41 /*--------------------------------------------------------------------------*/
42 static int sci_diary_no_rhs(char *fname);
43 static int sci_diary_one_rhs(char *fname);
44 static int sci_diary_two_rhs(char *fname);
45 static int sci_diary_three_rhs(char *fname);
46 /*--------------------------------------------------------------------------*/
47 static double *getInputArgumentOneIDs(char *fname,int *sizeReturnedArray, int *ierror);
48 static wchar_t **getInputArgumentOneFilenames(char *fname,int *sizeReturnedArray, int *ierror);
49 static wchar_t *getInputArgumentTwo(char *fname, int *ierror);
50 static wchar_t **getInputArgumentThree(char *fname,int *sizeReturnedArray, int *ierror);
51 static int checkExistByIDs(char *fname, double *IDs, int size_IDs); 
52 static int checkExistByFilenames(char *fname, wchar_t **wcFilenames, int size_IDs); 
53 static int CloseByFilenames(char *fname);
54 static int CloseByIds(char *fname);
55 static int PauseByFilenames(char *fname);
56 static int PauseByIds(char *fname);
57 static int ResumeByFilenames(char *fname);
58 static int ResumeByIds(char *fname);
59 static int ExistByFilenames(char *fname);
60 static int ExistByIds(char *fname);
61 static int AppendByFilenames(char *fname, 
62                                                          diary_filter filterMode, 
63                                                          diary_prefix_time_format prefixMode, 
64                                                          diary_prefix_time_filter prefixModeFilter,
65                                                          bool suspended);
66 static int NewByFilenames(char *fname, 
67                                                   diary_filter filterMode, 
68                                                   diary_prefix_time_format prefixMode, 
69                                                   diary_prefix_time_filter prefixModeFilter,
70                                                   bool suspended);
71 /*--------------------------------------------------------------------------*/
72 int sci_diary(char *fname,unsigned long fname_len)
73 {
74         CheckRhs(0,3);
75         CheckLhs(0,2);
76
77         switch (Rhs)
78         {
79         case 0:
80                 return sci_diary_no_rhs(fname);
81         case 1:
82                 return sci_diary_one_rhs(fname);
83         case 2:
84                 return sci_diary_two_rhs(fname);
85         case 3:
86                 return sci_diary_three_rhs(fname);
87         }
88         return 0;
89 }
90 /*--------------------------------------------------------------------------*/
91 static int sci_diary_no_rhs(char *fname)
92 {
93         // [ids, filenames] = diary()
94         // [ids, filenames] = diary([],"list")
95         SciErr sciErr;
96         int nb_diary_ids = 0;
97         double *diary_ids  = getDiaryIDsAsDouble(&nb_diary_ids);
98
99         if ( (diary_ids) && (nb_diary_ids > 0) )
100         {
101                 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, nb_diary_ids, 1, diary_ids);
102                 if(sciErr.iErr)
103                 {
104                         printError(&sciErr, 0);
105             Scierror(999,_("%s: Memory allocation error.\n"), fname);
106                         return 0;
107                 }
108
109                 LhsVar(1) = Rhs + 1;
110
111                 delete [] diary_ids;
112                 diary_ids = NULL;
113                 nb_diary_ids = 0;
114         }
115         else
116         {
117                 if (nb_diary_ids == 0)
118                 {
119                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, NULL);
120                         if(sciErr.iErr)
121                         {
122                                 printError(&sciErr, 0);
123                 Scierror(999,_("%s: Memory allocation error.\n"), fname);
124                                 return 0;
125                         }
126                         LhsVar(1) = Rhs + 1;
127
128                         if (diary_ids) 
129                         {
130                                 delete [] diary_ids;
131                                 diary_ids = NULL;
132                                 nb_diary_ids = 0;
133                         }
134                 }
135                 else
136                 {
137                         Scierror(999,_("%s: Memory allocation error.\n"),fname);
138                         return 0;
139                 }
140         }
141
142         if (Lhs == 2)
143         {
144                 int nb_diary_filenames = 0;
145                 wchar_t **wcdiary_filenames = getDiaryFilenames(&nb_diary_filenames);
146
147                 if ( (wcdiary_filenames) && (nb_diary_filenames > 0) )
148                 {
149                         sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 2, nb_diary_filenames, 1, wcdiary_filenames);
150                         if(sciErr.iErr)
151                         {
152                                 printError(&sciErr, 0);
153                 Scierror(999,_("%s: Memory allocation error.\n"), fname);
154                                 return 0;
155                         }
156
157                         LhsVar(2) = Rhs + 2;
158
159                         freeArrayOfWideString(wcdiary_filenames, nb_diary_filenames);
160                         nb_diary_filenames = 0;
161                 }
162                 else
163                 {
164                         if (nb_diary_filenames == 0)
165                         {
166                                 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, 0, 0, NULL);
167                                 if(sciErr.iErr)
168                                 {
169                                         printError(&sciErr, 0);
170                     Scierror(999,_("%s: Memory allocation error.\n"), fname);
171                                         return 0;
172                                 }
173
174                                 LhsVar(2) = Rhs + 2;
175                                 if (wcdiary_filenames)
176                                 {
177                                         freeArrayOfWideString(wcdiary_filenames, nb_diary_filenames);
178                                         nb_diary_filenames = 0;
179                                 }
180                         }
181                         else
182                         {
183                                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
184                                 return 0;
185                         }
186                 }
187         }
188
189         PutLhsVar();
190
191         return 0;
192 }
193 /*--------------------------------------------------------------------------*/
194 static int sci_diary_one_rhs(char *fname)
195 {
196         SciErr sciErr;
197         int iType       = 0;
198         int *piAddressVarOne = NULL;
199
200         sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
201         if(sciErr.iErr)
202         {
203                         printError(&sciErr, 0);
204             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
205                         return 0;
206         }
207
208         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
209         if(sciErr.iErr)
210         {
211                         printError(&sciErr, 0);
212             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
213                         return 0;
214         }
215
216         if (iType == sci_matrix)
217         {
218                 int IDs_size = 0;
219                 int ierr = 0;
220                 double * IDs = getInputArgumentOneIDs(fname, &IDs_size, &ierr);
221                 if (ierr) return 0;
222
223                 // diary([])
224                 // diary(0)
225                 // close all
226                 if ( (IDs_size == 0) ||
227                          ((IDs_size == 1) && ((int)IDs[0] == 0)))
228                 {
229                         diaryCloseAll();
230                         PutLhsVar();
231                 }
232                 else
233                 {
234                         Scierror(999,_("%s: Wrong value for input argument #%d: 0 expected.\n"),fname,1);
235                 }
236         }
237         else if (iType == sci_strings)
238         {
239                 int ierr = 0;
240                 int sizewcFilenames = 0;
241                 wchar_t ** wcFilenames = getInputArgumentOneFilenames(fname,&sizewcFilenames,&ierr);
242                 if (ierr) return 0;
243
244                 if (sizewcFilenames == 1)
245                 {
246                         if (diaryExists(wcFilenames[0]))
247                         {
248                                 double dID = (double)diaryNew(wcFilenames[0], false);
249                                 if (dID == -1)
250                                 {
251                                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
252                                         if (utf_str)
253                                         {
254                                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
255                                                 FREE(utf_str); utf_str = NULL;
256                                         }
257                                         else
258                                         {
259                                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
260                                         }
261                                         return 0;
262                                 }
263
264                                 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 1, 1, &dID);
265                                 if(sciErr.iErr)
266                                 {
267                                                 printError(&sciErr, 0);
268                         Scierror(999,_("%s: Memory allocation error.\n"), fname);
269                                                 return 0;
270                                 }
271
272                                 LhsVar(1) = Rhs + 1;
273
274                                 if (Lhs == 2)
275                                 {
276                                         wchar_t **wfilenameUsed = new wchar_t*[1];
277                                         wfilenameUsed[0] = getDiaryFilename((int)dID);
278                                         sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 2, 1, 1, wfilenameUsed);
279                                         if(sciErr.iErr)
280                                         {
281                                                         printError(&sciErr, 0);
282                             Scierror(999,_("%s: Memory allocation error.\n"), fname);
283                                                         return 0;
284                                         }
285
286                                         LhsVar(2) = Rhs + 2;
287                                         FREE(wfilenameUsed[0]);
288                                         delete [] wfilenameUsed;
289                                 }
290                         }
291                         else // diary(filename) exists (close diary)
292                         {
293                                 if (diaryClose(wcFilenames[0]))
294                                 {
295                                         if (wcFilenames)
296                                         {
297                                                 freeArrayOfWideString(wcFilenames, 1);
298                                                 Scierror(999,_("%s: error can not close diary.\n"),fname);
299                                                 return 0;
300                                         }
301                                 }
302                                 LhsVar(1) = 0; 
303                         }
304
305                         freeArrayOfWideString(wcFilenames,sizewcFilenames);
306                         PutLhsVar();
307                 }
308                 else
309                 {
310                         freeArrayOfWideString(wcFilenames,sizewcFilenames);
311                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname, 1);
312                 }
313         }
314         else
315         {
316                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
317         }
318         return 0;
319 }
320 /*--------------------------------------------------------------------------*/
321 static int sci_diary_two_rhs(char *fname)
322 {
323         int ierr = 0;
324         wchar_t *wcArgumentTwo = getInputArgumentTwo(fname, &ierr);
325         SciErr sciErr;
326         int *piAddressVarOne = NULL;
327
328         sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
329         if(sciErr.iErr)
330         {
331                         printError(&sciErr, 0);
332             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
333                         return 0;
334         }
335
336         if (ierr) return 0;
337
338         if (wcArgumentTwo)
339         {
340                 if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_LIST) == 0)
341                 {
342                         int m1 = 0, n1 = 0;
343                         sciErr = getVarDimension(pvApiCtx, piAddressVarOne,&m1,&n1);
344                         if(sciErr.iErr)
345                         {
346                                 printError(&sciErr, 0);
347                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
348                                 return 0;
349                         }
350
351                         if ( (m1 == n1) && (n1 == 0) )
352                         {
353                                 // diary() == diary([], "list")
354                                 return sci_diary_no_rhs(fname);
355                         }
356                         else
357                         {
358                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
359                         }
360                 }
361                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_CLOSE) == 0)
362                 {
363                         int iType = 0;
364                         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
365                         if(sciErr.iErr)
366                         {
367                                 printError(&sciErr, 0);
368                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
369                                 return 0;
370                         }
371
372                         if(iType == sci_matrix)
373                         {
374                                 return CloseByIds(fname);
375                         }
376                         else if (iType == sci_strings)
377                         {
378                                 return CloseByFilenames(fname);
379                         }
380                         else
381                         {
382                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
383                                 return 0;
384                         }
385                 }
386                 else if ( (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_PAUSE) == 0) ||
387                                   (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_OFF) == 0) )
388                 {
389                         int iType = 0;
390                         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
391                         if(sciErr.iErr)
392                         {
393                                 printError(&sciErr, 0);
394                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
395                                 return 0;
396                         }
397
398                         if (iType == sci_matrix)
399                         {
400                                 return PauseByIds(fname);
401                         }
402                         else if(iType == sci_strings)
403                         {
404                                 return PauseByFilenames(fname);
405                         }
406                         else
407                         {
408                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
409                                 return 0;
410                         }
411                 }
412                 else if ( (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_RESUME) == 0) ||
413                                   (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_ON) == 0) )
414                 {
415                         int iType = 0;
416                         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
417                         if(sciErr.iErr)
418                         {
419                                 printError(&sciErr, 0);
420                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
421                                 return 0;
422                         }
423
424                         if (iType == sci_matrix)
425                         {
426                                 return ResumeByIds(fname);
427                         }
428                         else if (iType == sci_strings)
429                         {
430                                 return ResumeByFilenames(fname);
431                         }
432                         else
433                         {
434                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
435                                 return 0;
436                         }
437                 }
438                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_NEW) == 0)
439                 {
440                         int iType = 0;
441                         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
442                         if(sciErr.iErr)
443                         {
444                                 printError(&sciErr, 0);
445                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
446                                 return 0;
447                         }
448
449                         if (iType == sci_strings)
450                         {
451                                 return NewByFilenames(fname, DIARY_FILTER_INPUT_AND_OUTPUT, 
452                                         PREFIX_TIME_FORMAT_UNIX_EPOCH, 
453                                         PREFIX_FILTER_NONE, false);
454                         }
455                         else
456                         {
457                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
458                                 return 0;
459                         }
460                 }
461                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_APPEND) == 0)
462                 {
463                         int iType = 0;
464                         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
465                         if(sciErr.iErr)
466                         {
467                                 printError(&sciErr, 0);
468                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
469                                 return 0;
470                         }
471
472                         if (iType == sci_strings)
473                         {
474                                 return AppendByFilenames(fname, DIARY_FILTER_INPUT_AND_OUTPUT, 
475                                         PREFIX_TIME_FORMAT_UNIX_EPOCH, 
476                                         PREFIX_FILTER_NONE, false);
477                         }
478                         else
479                         {
480                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
481                                 return 0;
482                         }
483                 }
484                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_EXISTS) == 0)
485                 {
486                         int iType = 0;
487                         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
488                         if(sciErr.iErr)
489                         {
490                                 printError(&sciErr, 0);
491                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
492                                 return 0;
493                         }
494
495                         if (iType == sci_matrix)
496                         {
497                                 return ExistByIds(fname);
498                         }
499                         else if (iType == sci_strings)
500                         {
501                                 return ExistByFilenames(fname);
502                         }
503                         else
504                         {
505                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
506                                 return 0;
507                         }
508                 }
509                 else
510                 {
511                         Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
512                         return 0;
513                 }
514         }
515         else
516         {
517                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
518         }
519         
520         return 0;
521 }
522 /*--------------------------------------------------------------------------*/
523 static int sci_diary_three_rhs(char *fname)
524 {
525         int ierr = 0;
526         int size_ArgThree = 0;
527         wchar_t **wcArgumentThree = getInputArgumentThree(fname, &size_ArgThree, &ierr);
528         if (ierr) return 0;
529
530         if (wcArgumentThree)
531         {
532                 diary_filter filterMode = DIARY_FILTER_INPUT_AND_OUTPUT;
533                 diary_prefix_time_format iPrefixMode = PREFIX_TIME_FORMAT_UNIX_EPOCH;
534                 diary_prefix_time_filter iPrefixIoModeFilter = PREFIX_FILTER_NONE;
535                 bool suspendedDiary = false;
536
537                 for (int i = 0; i < size_ArgThree; i++)
538                 {
539                         if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_FILTER_COMMAND) == 0)
540                         {
541                                 filterMode = DIARY_FILTER_ONLY_INPUT; // input only
542                         }
543                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_FILTER_OUTPUT) == 0)
544                         {
545                                 filterMode = DIARY_FILTER_ONLY_OUTPUT; // output only
546                         }
547                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_UNIX_EPOCH) == 0)
548                         {
549                                 iPrefixMode = PREFIX_TIME_FORMAT_UNIX_EPOCH;
550                                 if (iPrefixIoModeFilter == PREFIX_FILTER_NONE) iPrefixIoModeFilter = PREFIX_FILTER_INPUT_AND_OUTPUT;
551                         }
552                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_DEFAULT) == 0)
553                         {
554                                 iPrefixMode = PREFIX_TIME_FORMAT_ISO_8601;
555                                 if (iPrefixIoModeFilter == PREFIX_FILTER_NONE) iPrefixIoModeFilter = PREFIX_FILTER_INPUT_AND_OUTPUT;
556                         }
557                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_ONLY_COMMANDS) == 0)
558                         {
559                                 iPrefixIoModeFilter = PREFIX_FILTER_ONLY_INPUT;
560                         }
561                         else if ( (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_ON) == 0) || (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_RESUME) == 0) )
562                         {
563                                 suspendedDiary = false;
564                         }
565                         else if ( (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_OFF) == 0) || (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_PAUSE) == 0) )
566                         {
567                                 suspendedDiary = true;
568                         }
569                         else
570                         {
571                                 freeArrayOfWideString(wcArgumentThree, size_ArgThree);
572                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 3);
573                                 return 0;
574                         }                       
575                 }
576                 freeArrayOfWideString(wcArgumentThree, size_ArgThree);
577
578                 wchar_t *wcArgumentTwo = getInputArgumentTwo(fname, &ierr);
579                 if (ierr) return 0;
580                 if (wcArgumentTwo)
581                 {
582                         int *piAddressVarOne = NULL;
583                         SciErr sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
584                         if(sciErr.iErr)
585                         {
586                                 printError(&sciErr, 0);
587                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
588                                 return 0;
589                         }
590
591                         if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_NEW) == 0)
592                         {
593                                 int iType = 0;
594                                 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
595                                 if(sciErr.iErr)
596                                 {
597                                         printError(&sciErr, 0);
598                     Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
599                                         return 0;
600                                 }
601
602                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
603                                 if (iType == sci_strings)
604                                 {
605                                         return NewByFilenames(fname, filterMode, iPrefixMode, iPrefixIoModeFilter, suspendedDiary);
606                                 }
607                                 else
608                                 {
609                                         Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
610                                         return 0;
611                                 }
612                         }
613                         else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_APPEND) == 0)
614                         {
615                                 int iType = 0;
616                                 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
617                                 if(sciErr.iErr)
618                                 {
619                                         printError(&sciErr, 0);
620                     Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
621                                         return 0;
622                                 }
623
624                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
625                                 if (iType == sci_strings)
626                                 {
627                                         return AppendByFilenames(fname, filterMode, iPrefixMode, iPrefixIoModeFilter, suspendedDiary);
628                                 }
629                                 else
630                                 {
631                                         Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
632                                         return 0;
633                                 }
634                         }
635                         else
636                         {
637                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
638                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
639                                 return 0;
640                         }
641                 }
642                 else
643                 {
644                         Scierror(999,_("%s: Memory allocation error.\n"),fname);
645                 }
646         }
647         else
648         {
649                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
650         }
651         return 0;
652 }
653 /*--------------------------------------------------------------------------*/
654 static double *getInputArgumentOneIDs(char *fname,int *sizeReturnedArray, int *ierror)
655 {
656         double *IDs = NULL;
657         *sizeReturnedArray = 0;
658         *ierror = 0;
659
660         int m1 = 0, n1 = 0;
661         int *piAddressVarOne = NULL;
662
663         SciErr sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
664         if(sciErr.iErr)
665         {
666                 printError(&sciErr, 0);
667         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
668                 return 0;
669         }
670
671         int iType = 0;
672         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
673         if(sciErr.iErr)
674         {
675                 printError(&sciErr, 0);
676         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
677                 return 0;
678         }
679
680         if (iType == sci_matrix)
681         {
682                 sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne,&m1,&n1,&IDs);
683                 if(sciErr.iErr)
684                 {
685                         printError(&sciErr, 0);
686             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
687                         return 0;
688                 }
689
690                 if ( (m1 == 1) || (n1 == 1) )
691                 {
692                         *sizeReturnedArray = m1 * n1;
693                 }
694                 else if ( (m1 == 0) || (n1 == 0) )
695                 {
696                         *sizeReturnedArray = 0;
697                         *ierror = 2;
698                         return NULL;
699                 }
700                 else
701                 {
702                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,1);
703                         *ierror = 1;
704                 }
705         }
706         else
707         {
708                 Scierror(999,_("%s: Wrong type for input argument #%d: A scalar expected.\n"),fname,1);
709                 *ierror = 1;
710         }
711
712         return IDs;
713 }
714 /*--------------------------------------------------------------------------*/
715 static wchar_t **getInputArgumentOneFilenames(char *fname,int *sizeReturnedArray, int *ierror)
716 {
717         wchar_t** wcFilenames = NULL;
718         *sizeReturnedArray = 0;
719         *ierror = 0;
720
721         int m1 = 0, n1 = 0;
722         int *piAddressVarOne = NULL;
723
724         SciErr sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
725         if(sciErr.iErr)
726         {
727                 printError(&sciErr, 0);
728         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
729                 return 0;
730         }
731
732         int iType = 0;
733         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
734         if(sciErr.iErr)
735         {
736                 printError(&sciErr, 0);
737         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
738                 return 0;
739         }
740
741         if (iType == sci_strings)
742         {
743                 int *lenStVarOne = NULL;
744
745                 sciErr = getVarDimension(pvApiCtx, piAddressVarOne,&m1,&n1);
746                 if(sciErr.iErr)
747                 {
748                         printError(&sciErr, 0);
749             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
750                         return 0;
751                 }
752
753                 if (m1*n1 < 1)
754                 {
755                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname,1);
756                         *ierror = 1;
757                 }
758                 else if ( (m1 == 1) || (n1 == 1) )
759                 {
760                         lenStVarOne = (int *)MALLOC(sizeof(int) * (m1 * n1));
761                         if (lenStVarOne == NULL)
762                         {
763                                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
764                                 *ierror = 1;
765                         }
766                         else
767                         {
768                                 sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL);
769                                 if(sciErr.iErr)
770                                 {
771                                         printError(&sciErr, 0);
772                     Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
773                                         return 0;
774                                 }
775
776                                 wcFilenames = (wchar_t **)MALLOC(sizeof(wchar_t *) * (m1 * n1));
777                                 if (wcFilenames == NULL)
778                                 {
779                                         Scierror(999,_("%s: Memory allocation error.\n"),fname);
780                                         *ierror = 1;
781                                 }
782                                 else
783                                 {
784                                         for (int i = 0; i < m1 * n1;i++)
785                                         {
786                                                 wcFilenames[i] = (wchar_t*)MALLOC(sizeof(wchar_t)* (lenStVarOne[i] + 1));
787                                                 if (wcFilenames[i] == NULL)
788                                                 {
789                                                         Scierror(999,_("%s: Memory allocation error.\n"),fname);
790                                                         *ierror = 1;
791                                                 }
792                                         }
793
794                                         sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, wcFilenames);
795                                         if(sciErr.iErr)
796                                         {
797                                                 printError(&sciErr, 0);
798                         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
799                                                 return 0;
800                                         }
801
802                                         *sizeReturnedArray = m1 * n1;
803                                 }
804                         }
805                 }
806                 else
807                 {
808                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,1);
809                         *ierror = 1;
810                 }
811         }
812         else
813         {
814                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
815                 *ierror = 1;
816         }
817         return wcFilenames;
818 }
819 /*--------------------------------------------------------------------------*/
820 static wchar_t *getInputArgumentTwo(char *fname, int *ierror)
821 {
822         wchar_t *wcInputArgumentTwo = NULL;
823         *ierror = 0;
824
825         int m2 = 0, n2 = 0;
826         int *piAddressVarTwo = NULL;
827
828         SciErr sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
829         if(sciErr.iErr)
830         {
831                 printError(&sciErr, 0);
832         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
833                 return 0;
834         }
835
836         int iType = 0;
837         sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType);
838         if(sciErr.iErr)
839         {
840                 printError(&sciErr, 0);
841         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
842                 return 0;
843         }
844
845         if (iType == sci_strings)
846         {
847                 int lenStVarTwo = 0;
848
849                 sciErr = getVarDimension(pvApiCtx, piAddressVarTwo,&m2,&n2);
850                 if(sciErr.iErr)
851                 {
852                         printError(&sciErr, 0);
853                         return 0;
854                 }
855
856                 if ( (m2 != n2) && (n2 != 1) ) 
857                 {
858                         Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
859                         *ierror = 1;
860                 }
861                 else
862                 {
863                         // get length lenStVarTwo
864                         sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo,&m2,&n2,&lenStVarTwo, NULL);
865                         if(sciErr.iErr)
866                         {
867                                 printError(&sciErr, 0);
868                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
869                                 return 0;
870                         }
871
872                         wcInputArgumentTwo = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarTwo + 1));
873
874                         if (wcInputArgumentTwo == NULL)
875                         {
876                                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
877                                 *ierror = 1;
878                         }
879
880                         sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo,&m2,&n2,&lenStVarTwo,&wcInputArgumentTwo);
881                         if(sciErr.iErr)
882                         {
883                                 printError(&sciErr, 0);
884                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
885                                 return 0;
886                         }
887                 }
888         }
889         else
890         {
891                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
892                 *ierror = 1;
893         }
894         return wcInputArgumentTwo;
895 }
896 /*--------------------------------------------------------------------------*/
897 static wchar_t** getInputArgumentThree(char *fname,int *sizeReturnedArray, int *ierror)
898 {
899         wchar_t** wcInputArgumentThree = NULL;
900         *sizeReturnedArray = 0;
901         *ierror = 0;
902
903         int m3 = 0, n3 = 0;
904         int *piAddressVarThree = NULL;
905
906         SciErr sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
907         if(sciErr.iErr)
908         {
909                 printError(&sciErr, 0);
910         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
911                 return 0;
912         }
913
914         int iType = 0;
915         sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType);
916         if(sciErr.iErr)
917         {
918                 printError(&sciErr, 0);
919         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
920                 return 0;
921         }
922
923         if (iType == sci_strings)
924         {
925                 int *lenStVarThree = NULL;
926
927                 sciErr = getVarDimension(pvApiCtx, piAddressVarThree,&m3,&n3);
928                 if(sciErr.iErr)
929                 {
930                         printError(&sciErr, 0);
931             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
932                         return 0;
933                 }
934
935                 if (m3*n3 < 1)
936                 {
937                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname,3);
938                         *ierror = 1;
939                 }
940                 else if ( (m3 == 1) || (n3 == 1) )
941                 {
942                         lenStVarThree = (int *)MALLOC(sizeof(int) * (m3 * n3));
943                         if (lenStVarThree == NULL)
944                         {
945                                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
946                                 *ierror = 1;
947                         }
948                         else
949                         {
950                                 sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarThree, &m3, &n3, lenStVarThree, NULL);
951                                 if(sciErr.iErr)
952                                 {
953                                         printError(&sciErr, 0);
954                     Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
955                                         return 0;
956                                 }
957
958                                 wcInputArgumentThree = (wchar_t **)MALLOC(sizeof(wchar_t *) * (m3 * n3));
959                                 if (wcInputArgumentThree == NULL)
960                                 {
961                                         Scierror(999,_("%s: Memory allocation error.\n"),fname);
962                                         *ierror = 1;
963                                 }
964                                 else
965                                 {
966                                         for (int i = 0; i < m3 * n3; i++)
967                                         {
968                                                 wcInputArgumentThree[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarThree[i] + 1));
969                                         }
970
971                                         sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarThree, &m3, &n3, lenStVarThree, wcInputArgumentThree);
972                                         if(sciErr.iErr)
973                                         {
974                                                 printError(&sciErr, 0);
975                         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
976                                                 return 0;
977                                         }
978
979                                         *sizeReturnedArray = m3 * n3;
980                                 }
981                         }
982                 }
983                 else
984                 {
985                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,3);
986                         *ierror = 1;
987                 }
988         }
989         else
990         {
991                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,3);
992                 *ierror = 1;
993         }
994         return wcInputArgumentThree;
995 }
996 /*--------------------------------------------------------------------------*/
997 static int checkExistByIDs(char *fname, double *IDs, int size_IDs)
998 {
999         if (IDs)
1000         {
1001                 for (int i = 0; i < size_IDs; i++)
1002                 {
1003                         if (diaryExists((int)IDs[i]))
1004                         {
1005                                 Scierror(999,_("%s: Wrong value for input argument #%d: diary ID %d not exists.\n"),fname,1, (int)IDs[i]);
1006                                 return 1;
1007                         }
1008                 }
1009                 return 0;
1010         }
1011         return 1;
1012 }
1013 /*--------------------------------------------------------------------------*/
1014 static int checkExistByFilenames(char *fname, wchar_t **wcFilenames, int size_IDs)
1015 {
1016         if (wcFilenames)
1017         {
1018                 for (int i = 0; i < size_IDs; i++)
1019                 {
1020                         if (diaryExists(wcFilenames[i]))
1021                         {
1022                                 Scierror(999,_("%s: Wrong value for input argument #%d: diary filename not exists.\n"),fname,1);
1023                                 return 1;
1024                         }
1025                 }
1026                 return 0;
1027         }
1028         return 1;
1029 }
1030 /*--------------------------------------------------------------------------*/
1031 static int CloseByFilenames(char *fname)
1032 {
1033         wchar_t **wcFilenames = NULL;
1034         int dIDs_size = 0;
1035         int ierr = 0;
1036
1037         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1038         if (ierr) return 0;
1039
1040         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
1041         if (ierr) 
1042         {
1043                 freeArrayOfWideString(wcFilenames, dIDs_size);
1044                 return 0;
1045         }
1046
1047         for (int i = 0; i < dIDs_size; i++)
1048         {
1049                 if (diaryClose(wcFilenames[i]))
1050                 {
1051                         freeArrayOfWideString(wcFilenames, dIDs_size);
1052                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not close diary.\n"),fname);
1053                         return 0;
1054                 }
1055         }
1056
1057         freeArrayOfWideString(wcFilenames, dIDs_size);
1058         PutLhsVar();
1059         return 0;
1060 }
1061 /*--------------------------------------------------------------------------*/
1062 static int CloseByIds(char *fname)
1063 {
1064         double *dIDs = NULL;
1065         int dIDs_size = 0;
1066         int ierr = 0;
1067
1068         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
1069
1070         if (ierr == 2)
1071         {
1072                 // diary([], 'close')
1073                 diaryCloseAll();
1074                 PutLhsVar();
1075                 return 0;
1076         }
1077         else if (ierr) return 0;
1078
1079         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
1080         if (ierr) return 0;
1081
1082         for (int i = 0; i < dIDs_size; i++)
1083         {
1084                 if (diaryClose((int)dIDs[i]))
1085                 {
1086                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not close diary %d.\n"),fname, (int)dIDs[i]);
1087                         return 0;
1088                 }
1089         }
1090         PutLhsVar();
1091         return 0;
1092 }
1093 /*--------------------------------------------------------------------------*/
1094 static int PauseByFilenames(char *fname)
1095 {
1096         wchar_t **wcFilenames = NULL;
1097         int dIDs_size = 0;
1098         int ierr = 0;
1099
1100         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1101         if (ierr) return 0;
1102
1103         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
1104         if (ierr) 
1105         {
1106                 freeArrayOfWideString(wcFilenames, dIDs_size);
1107                 return 0;
1108         }
1109
1110         for (int i = 0; i < dIDs_size; i++)
1111         {
1112                 if (diaryPause(wcFilenames[i]))
1113                 {
1114                         freeArrayOfWideString(wcFilenames, dIDs_size);
1115                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not pause diary.\n"),fname);
1116                         return 0;
1117                 }
1118         }
1119
1120         freeArrayOfWideString(wcFilenames, dIDs_size);
1121
1122         PutLhsVar();
1123         return 0;
1124 }
1125 /*--------------------------------------------------------------------------*/
1126 static int PauseByIds(char *fname)
1127 {
1128         double *dIDs = NULL;
1129         int dIDs_size = 0;
1130         int ierr = 0;
1131
1132         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
1133         if (ierr) return 0;
1134
1135         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
1136         if (ierr) return 0;
1137
1138         for (int i = 0; i < dIDs_size; i++)
1139         {
1140                 if (diaryPause((int)dIDs[i]))
1141                 {
1142                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not pause diary %d.\n"),fname, (int)dIDs[i]);
1143                         return 0;
1144                 }
1145         }
1146
1147         PutLhsVar();
1148
1149         return 0;
1150 }
1151 /*--------------------------------------------------------------------------*/
1152 static int ResumeByFilenames(char *fname)
1153 {
1154         wchar_t **wcFilenames = NULL;
1155         int dIDs_size = 0;
1156         int ierr = 0;
1157         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1158         if (ierr) return 0;
1159
1160         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
1161         if (ierr) 
1162         {
1163                 freeArrayOfWideString(wcFilenames, dIDs_size);
1164                 return 0;
1165         }
1166
1167         for (int i = 0; i < dIDs_size; i++)
1168         {
1169                 if (diaryResume(wcFilenames[i]))
1170                 {
1171                         freeArrayOfWideString(wcFilenames, dIDs_size);
1172                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not resume diary.\n"),fname);
1173                         return 0;
1174                 }
1175         }
1176
1177         freeArrayOfWideString(wcFilenames, dIDs_size);
1178
1179         PutLhsVar();
1180
1181         return 0;
1182 }
1183 /*--------------------------------------------------------------------------*/
1184 static int ResumeByIds(char *fname)
1185 {
1186         double *dIDs = NULL;
1187         int dIDs_size = 0;
1188         int ierr = 0;
1189
1190         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
1191         if (ierr) return 0;
1192
1193         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
1194         if (ierr) return 0;
1195
1196         for (int i = 0; i < dIDs_size; i++)
1197         {
1198                 if (diaryResume((int)dIDs[i]))
1199                 {
1200                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not resume diary %d.\n"),fname, (int)dIDs[i]);
1201                         return 0;
1202                 }
1203         }
1204
1205         PutLhsVar();
1206
1207         return 0;
1208 }
1209 /*--------------------------------------------------------------------------*/
1210 static int ExistByFilenames(char *fname)
1211 {
1212         wchar_t **wcFilenames = NULL;
1213         int dIDs_size = 0;
1214         int ierr = 0;
1215         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1216         if (ierr) return 0;
1217
1218         int *resultExist = (int*)MALLOC(sizeof(int)*dIDs_size);
1219         if (resultExist == NULL)
1220         {
1221                 freeArrayOfWideString(wcFilenames,dIDs_size);
1222                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
1223                 return 0;
1224         }
1225         
1226         for(int i=0; i < dIDs_size; i++)
1227         {
1228                 int bExists = diaryExists(wcFilenames[i]);
1229                 if ( bExists == 0)
1230                 {
1231                         resultExist[i]  = TRUE;
1232                 }
1233                 else
1234                 {
1235                         resultExist[i]  = FALSE;
1236                 }
1237         }
1238         freeArrayOfWideString(wcFilenames,dIDs_size);
1239
1240         SciErr sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, dIDs_size, resultExist);
1241         if(sciErr.iErr)
1242         {
1243                 printError(&sciErr, 0);
1244         Scierror(999,_("%s: Memory allocation error.\n"), fname);
1245                 return 0;
1246         }
1247
1248         FREE(resultExist); resultExist = NULL;
1249         LhsVar(1) = Rhs + 1;
1250         PutLhsVar();
1251         return 0;
1252 }
1253 /*--------------------------------------------------------------------------*/
1254 static int ExistByIds(char *fname)
1255 {
1256         double *dIDs = NULL;
1257         int dIDs_size = 0;
1258         int ierr = 0;
1259
1260         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
1261         if (ierr) return 0;
1262
1263         int *resultExist = (int*)MALLOC(sizeof(int)*dIDs_size);
1264         if (resultExist == NULL)
1265         {
1266                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
1267                 return 0;
1268         }
1269
1270         for(int i=0; i < dIDs_size; i++)
1271         {
1272                 int bExists = diaryExists((int)dIDs[i]);
1273                 if ( bExists == 0)
1274                 {
1275                         resultExist[i]  = TRUE;
1276                 }
1277                 else
1278                 {
1279                         resultExist[i]  = FALSE;
1280                 }
1281         }
1282
1283         SciErr sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, dIDs_size, resultExist);
1284         if(sciErr.iErr)
1285         {
1286                 printError(&sciErr, 0);
1287         Scierror(999,_("%s: Memory allocation error.\n"), fname);
1288                 return 0;
1289         }
1290
1291         FREE(resultExist); resultExist = NULL;
1292         LhsVar(1) = Rhs + 1;
1293         PutLhsVar();
1294         return 0;
1295 }
1296 /*--------------------------------------------------------------------------*/
1297 static int AppendByFilenames(char *fname, 
1298                                                          diary_filter filterMode, 
1299                                                          diary_prefix_time_format prefixMode, 
1300                                                          diary_prefix_time_filter prefixModeFilter,
1301                                                          bool suspended)
1302 {
1303         SciErr sciErr;
1304         wchar_t **wcFilenames = NULL;
1305         int dIDs_size = 0;
1306         int ierr = 0;
1307
1308         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1309         if (ierr) return 0;
1310
1311         if (dIDs_size == 1)
1312         {
1313                 double dID = (double)diaryAppend(wcFilenames[0]);
1314                 if (dID == -1)
1315                 {
1316                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
1317                         if (utf_str)
1318                         {
1319                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
1320                                 FREE(utf_str); utf_str = NULL;
1321                         }
1322                         else
1323                         {
1324                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
1325                         }
1326                         freeArrayOfWideString(wcFilenames, dIDs_size);
1327                         return 0;
1328                 }
1329
1330                 freeArrayOfWideString(wcFilenames, dIDs_size);
1331
1332                 diarySetFilterMode((int)dID, filterMode);
1333                 diarySetPrefixMode((int)dID, prefixMode);
1334                 diarySetPrefixIoModeFilter((int)dID, prefixModeFilter);
1335                 if (suspended) diaryPause((int)dID);
1336
1337                 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 1, 1, &dID);
1338                 if(sciErr.iErr)
1339                 {
1340                         printError(&sciErr, 0);
1341             Scierror(999,_("%s: Memory allocation error.\n"), fname);
1342                         return 0;
1343                 }
1344
1345                 LhsVar(1) = Rhs + 1;
1346
1347                 if (Lhs == 2)
1348                 {
1349                         wchar_t **wfilenameUsed = new wchar_t*[1];
1350                         wfilenameUsed[0] = getDiaryFilename((int)dID);
1351                         sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 2, 1, 1, wfilenameUsed);
1352                         if(sciErr.iErr)
1353                         {
1354                                 printError(&sciErr, 0);
1355                 Scierror(999,_("%s: Memory allocation error.\n"), fname);
1356                                 return 0;
1357                         }
1358
1359                         LhsVar(2) = Rhs + 2;
1360                         freeArrayOfWideString(wfilenameUsed, 1);
1361                 }
1362                 PutLhsVar();
1363         }
1364         else
1365         {
1366                 freeArrayOfWideString(wcFilenames, dIDs_size);
1367                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
1368         }
1369
1370         return 0;
1371 }
1372 /*--------------------------------------------------------------------------*/
1373 static int NewByFilenames(char *fname,
1374                                                   diary_filter filterMode, 
1375                                                   diary_prefix_time_format prefixMode, 
1376                                                   diary_prefix_time_filter prefixModeFilter, 
1377                                                   bool suspended)
1378 {
1379         wchar_t **wcFilenames = NULL;
1380         int dIDs_size = 0;
1381         int ierr = 0;
1382
1383         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1384         if (ierr) return 0;
1385
1386         if (dIDs_size == 1)
1387         {
1388                 double dID = (double)diaryNew(wcFilenames[0], true);
1389                 if (dID == -1)
1390                 {
1391                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
1392                         if (utf_str)
1393                         {
1394                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
1395                                 FREE(utf_str); utf_str = NULL;
1396                         }
1397                         else
1398                         {
1399                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
1400                         }
1401                         freeArrayOfWideString(wcFilenames, dIDs_size);
1402                         return 0;
1403                 }
1404
1405                 freeArrayOfWideString(wcFilenames, dIDs_size);
1406
1407                 diarySetFilterMode((int)dID, filterMode);
1408                 diarySetPrefixMode((int)dID, prefixMode);
1409                 diarySetPrefixIoModeFilter((int)dID, prefixModeFilter);
1410                 if (suspended) diaryPause((int)dID);
1411
1412                 SciErr sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 1, 1, &dID);
1413                 LhsVar(1) = Rhs + 1;
1414
1415                 if (Lhs == 2)
1416                 {
1417                         wchar_t **wfilenameUsed = new wchar_t*[1];
1418                         wfilenameUsed[0] = getDiaryFilename((int)dID);
1419                         sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 2, 1, 1, wfilenameUsed);
1420                         if(sciErr.iErr)
1421                         {
1422                                 printError(&sciErr, 0);
1423                 Scierror(999,_("%s: Memory allocation error.\n"), fname);
1424                                 return 0;
1425                         }
1426
1427                         LhsVar(2) = Rhs + 2;
1428                         FREE(wfilenameUsed[0]);
1429                         delete [] wfilenameUsed;
1430                 }
1431                 PutLhsVar();
1432         }
1433         else
1434         {
1435                 freeArrayOfWideString(wcFilenames, dIDs_size);
1436                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
1437         }
1438
1439         return 0;
1440 }
1441 /*--------------------------------------------------------------------------*/