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