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