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