76515232380f1177fe41d0b88be0595499076e88
[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], false);
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                                 LhsVar(1) = 0; 
251                         }
252
253                         freeInput(wcFilenames,sizewcFilenames);
254                         C2F(putlhsvar)();
255                 }
256                 else
257                 {
258                         freeInput(wcFilenames,sizewcFilenames);
259                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname, 1);
260                 }
261         }
262         else
263         {
264                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
265         }
266         return 0;
267 }
268 /*--------------------------------------------------------------------------*/
269 static int sci_diary_two_rhs(char *fname)
270 {
271         int ierr = 0;
272         wchar_t *wcArgumentTwo = getInputArgumentTwo(fname, &ierr);
273
274         int *piAddressVarOne = NULL;
275         getVarAddressFromPosition(1, &piAddressVarOne);
276
277         if (ierr) return 0;
278
279         if (wcArgumentTwo)
280         {
281                 if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_LIST) == 0)
282                 {
283                         int m1 = 0, n1 = 0;
284                         getVarDimension(piAddressVarOne,&m1,&n1);
285                         if ( (m1 == n1) && (n1 == 0) )
286                         {
287                                 // diary() == diary([], "list")
288                                 return sci_diary_no_rhs(fname);
289                         }
290                         else
291                         {
292                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
293                         }
294                 }
295                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_CLOSE) == 0)
296                 {
297                         if (getVarType(piAddressVarOne) == sci_matrix)
298                         {
299                                 return CloseByIds(fname);
300                         }
301                         else if (getVarType(piAddressVarOne) == sci_strings)
302                         {
303                                 return CloseByFilenames(fname);
304                         }
305                         else
306                         {
307                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
308                                 return 0;
309                         }
310                 }
311                 else if ( (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_PAUSE) == 0) ||
312                                   (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_OFF) == 0) )
313                 {
314                         if (getVarType(piAddressVarOne) == sci_matrix)
315                         {
316                                 return PauseByIds(fname);
317                         }
318                         else if (getVarType(piAddressVarOne) == sci_strings)
319                         {
320                                 return PauseByFilenames(fname);
321                         }
322                         else
323                         {
324                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
325                                 return 0;
326                         }
327                 }
328                 else if ( (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_RESUME) == 0) ||
329                                   (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_ON) == 0) )
330                 {
331                         if (getVarType(piAddressVarOne) == sci_matrix)
332                         {
333                                 return ResumeByIds(fname);
334                         }
335                         else if (getVarType(piAddressVarOne) == sci_strings)
336                         {
337                                 return ResumeByFilenames(fname);
338                         }
339                         else
340                         {
341                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
342                                 return 0;
343                         }
344                 }
345                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_NEW) == 0)
346                 {
347                         if (getVarType(piAddressVarOne) == sci_strings)
348                         {
349                                 return NewByFilenames(fname, DIARY_FILTER_INPUT_AND_OUTPUT, 
350                                         PREFIX_TIME_FORMAT_UNIX_EPOCH, 
351                                         PREFIX_FILTER_NONE, false);
352                         }
353                         else
354                         {
355                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
356                                 return 0;
357                         }
358                 }
359                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_APPEND) == 0)
360                 {
361                         if (getVarType(piAddressVarOne) == sci_strings)
362                         {
363                                 return AppendByFilenames(fname, DIARY_FILTER_INPUT_AND_OUTPUT, 
364                                         PREFIX_TIME_FORMAT_UNIX_EPOCH, 
365                                         PREFIX_FILTER_NONE, false);
366                         }
367                         else
368                         {
369                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
370                                 return 0;
371                         }
372                 }
373                 else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_EXISTS) == 0)
374                 {
375                         if (getVarType(piAddressVarOne) == sci_matrix)
376                         {
377                                 return ExistByIds(fname);
378                         }
379                         else if (getVarType(piAddressVarOne) == sci_strings)
380                         {
381                                 return ExistByFilenames(fname);
382                         }
383                         else
384                         {
385                                 Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
386                                 return 0;
387                         }
388                 }
389                 else
390                 {
391                         Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
392                         return 0;
393                 }
394         }
395         else
396         {
397                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
398         }
399         
400         return 0;
401 }
402 /*--------------------------------------------------------------------------*/
403 static int sci_diary_three_rhs(char *fname)
404 {
405         int ierr = 0;
406         int size_ArgThree = 0;
407         wchar_t **wcArgumentThree = getInputArgumentThree(fname, &size_ArgThree, &ierr);
408         if (ierr) return 0;
409
410         if (wcArgumentThree)
411         {
412                 diary_filter filterMode = DIARY_FILTER_INPUT_AND_OUTPUT;
413                 diary_prefix_time_format iPrefixMode = PREFIX_TIME_FORMAT_UNIX_EPOCH;
414                 diary_prefix_time_filter iPrefixIoModeFilter = PREFIX_FILTER_NONE;
415                 bool suspendedDiary = false;
416
417                 for (int i = 0; i < size_ArgThree; i++)
418                 {
419                         if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_FILTER_COMMAND) == 0)
420                         {
421                                 filterMode = DIARY_FILTER_ONLY_INPUT; // input only
422                         }
423                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_FILTER_OUTPUT) == 0)
424                         {
425                                 filterMode = DIARY_FILTER_ONLY_OUTPUT; // output only
426                         }
427                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_UNIX_EPOCH) == 0)
428                         {
429                                 iPrefixMode = PREFIX_TIME_FORMAT_UNIX_EPOCH;
430                                 if (iPrefixIoModeFilter == PREFIX_FILTER_NONE) iPrefixIoModeFilter = PREFIX_FILTER_INPUT_AND_OUTPUT;
431                         }
432                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_DEFAULT) == 0)
433                         {
434                                 iPrefixMode = PREFIX_TIME_FORMAT_ISO_8601;
435                                 if (iPrefixIoModeFilter == PREFIX_FILTER_NONE) iPrefixIoModeFilter = PREFIX_FILTER_INPUT_AND_OUTPUT;
436                         }
437                         else if (wcscmp(wcArgumentThree[i], DIARY_THIRD_ARG_PREFIX_ONLY_COMMANDS) == 0)
438                         {
439                                 iPrefixIoModeFilter = PREFIX_FILTER_ONLY_INPUT;
440                         }
441                         else if ( (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_ON) == 0) || (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_RESUME) == 0) )
442                         {
443                                 suspendedDiary = false;
444                         }
445                         else if ( (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_OFF) == 0) || (wcscmp(wcArgumentThree[i], DIARY_SECOND_ARG_PAUSE) == 0) )
446                         {
447                                 suspendedDiary = true;
448                         }
449                         else
450                         {
451                                 freeInput(wcArgumentThree, size_ArgThree);
452                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 3);
453                                 return 0;
454                         }                       
455                 }
456                 freeInput(wcArgumentThree, size_ArgThree);
457
458                 wchar_t *wcArgumentTwo = getInputArgumentTwo(fname, &ierr);
459                 if (ierr) return 0;
460                 if (wcArgumentTwo)
461                 {
462                         int *piAddressVarOne = NULL;
463                         getVarAddressFromPosition(1, &piAddressVarOne);
464
465                         if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_NEW) == 0)
466                         {
467                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
468                                 if (getVarType(piAddressVarOne) == sci_strings)
469                                 {
470                                         return NewByFilenames(fname, filterMode, iPrefixMode, iPrefixIoModeFilter, suspendedDiary);
471                                 }
472                                 else
473                                 {
474                                         Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
475                                         return 0;
476                                 }
477                         }
478                         else if (wcscmp(wcArgumentTwo, DIARY_SECOND_ARG_APPEND) == 0)
479                         {
480                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
481                                 if (getVarType(piAddressVarOne) == sci_strings)
482                                 {
483                                         return AppendByFilenames(fname, filterMode, iPrefixMode, iPrefixIoModeFilter, suspendedDiary);
484                                 }
485                                 else
486                                 {
487                                         Scierror(999,_("%s: Wrong type for input argument #%d.\n"),fname, 1);
488                                         return 0;
489                                 }
490                         }
491                         else
492                         {
493                                 FREE(wcArgumentTwo); wcArgumentTwo = NULL;
494                                 Scierror(999,_("%s: Wrong value for input argument #%d.\n"),fname, 2);
495                                 return 0;
496                         }
497                 }
498                 else
499                 {
500                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
501                 }
502         }
503         else
504         {
505                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
506         }
507         return 0;
508 }
509 /*--------------------------------------------------------------------------*/
510 static double *getInputArgumentOneIDs(char *fname,int *sizeReturnedArray, int *ierror)
511 {
512         double *IDs = NULL;
513         *sizeReturnedArray = 0;
514         *ierror = 0;
515
516         int m1 = 0, n1 = 0;
517         int *piAddressVarOne = NULL;
518
519         getVarAddressFromPosition(1, &piAddressVarOne);
520
521         if (getVarType(piAddressVarOne) == sci_matrix)
522         {
523                 getMatrixOfDouble(piAddressVarOne,&m1,&n1,&IDs);
524                 if ( (m1 == 1) || (n1 == 1) )
525                 {
526                         *sizeReturnedArray = m1 * n1;
527                 }
528                 else if ( (m1 == 0) || (n1 == 0) )
529                 {
530                         *sizeReturnedArray = 0;
531                         *ierror = 2;
532                         return NULL;
533                 }
534                 else
535                 {
536                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,1);
537                         *ierror = 1;
538                 }
539         }
540         else
541         {
542                 Scierror(999,_("%s: Wrong type for input argument #%d: A scalar expected.\n"),fname,1);
543                 *ierror = 1;
544         }
545
546         return IDs;
547 }
548 /*--------------------------------------------------------------------------*/
549 static wchar_t **getInputArgumentOneFilenames(char *fname,int *sizeReturnedArray, int *ierror)
550 {
551         wchar_t** wcFilenames = NULL;
552         *sizeReturnedArray = 0;
553         *ierror = 0;
554
555         int m1 = 0, n1 = 0;
556         int *piAddressVarOne = NULL;
557
558         getVarAddressFromPosition(1, &piAddressVarOne);
559
560         if (getVarType(piAddressVarOne) == sci_strings)
561         {
562                 int *lenStVarOne = NULL;
563
564                 getVarDimension(piAddressVarOne,&m1,&n1);
565
566                 if (m1*n1 < 1)
567                 {
568                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname,1);
569                         *ierror = 1;
570                 }
571                 else if ( (m1 == 1) || (n1 == 1) )
572                 {
573                         lenStVarOne = (int *)MALLOC(sizeof(int) * (m1 * n1));
574                         if (lenStVarOne == NULL)
575                         {
576                                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
577                                 *ierror = 1;
578                         }
579                         else
580                         {
581                                 getMatrixOfWideString(piAddressVarOne, &m1, &n1, lenStVarOne, wcFilenames);
582                                 wcFilenames = (wchar_t **)MALLOC(sizeof(wchar_t *) * (m1 * n1));
583                                 if (wcFilenames == NULL)
584                                 {
585                                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
586                                         *ierror = 1;
587                                 }
588                                 else
589                                 {
590                                         getMatrixOfWideString(piAddressVarOne, &m1, &n1, lenStVarOne, wcFilenames);
591                                         *sizeReturnedArray = m1 * n1;
592                                 }
593                         }
594                 }
595                 else
596                 {
597                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,1);
598                         *ierror = 1;
599                 }
600         }
601         else
602         {
603                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
604                 *ierror = 1;
605         }
606         return wcFilenames;
607 }
608 /*--------------------------------------------------------------------------*/
609 static wchar_t *getInputArgumentTwo(char *fname, int *ierror)
610 {
611         wchar_t *wcInputArgumentTwo = NULL;
612         *ierror = 0;
613
614         int m2 = 0, n2 = 0;
615         int *piAddressVarTwo = NULL;
616
617         getVarAddressFromPosition(2, &piAddressVarTwo);
618
619         if (getVarType(piAddressVarTwo) == sci_strings)
620         {
621                 int lenStVarTwo = 0;
622
623                 getVarDimension(piAddressVarTwo,&m2,&n2);
624                 if ( (m2 != n2) && (n2 != 1) ) 
625                 {
626                         Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
627                         *ierror = 1;
628                 }
629                 else
630                 {
631                         getMatrixOfWideString(piAddressVarTwo,&m2,&n2,&lenStVarTwo,&wcInputArgumentTwo);
632                         if (wcInputArgumentTwo == NULL)
633                         {
634                                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
635                                 *ierror = 1;
636                         }
637                 }
638         }
639         else
640         {
641                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
642                 *ierror = 1;
643         }
644         return wcInputArgumentTwo;
645 }
646 /*--------------------------------------------------------------------------*/
647 static wchar_t** getInputArgumentThree(char *fname,int *sizeReturnedArray, int *ierror)
648 {
649         wchar_t** wcInputArgumentThree = NULL;
650         *sizeReturnedArray = 0;
651         *ierror = 0;
652
653         int m3 = 0, n3 = 0;
654         int *piAddressVarThree = NULL;
655
656         getVarAddressFromPosition(3, &piAddressVarThree);
657
658         if (getVarType(piAddressVarThree) == sci_strings)
659         {
660                 int *lenStVarThree = NULL;
661
662                 getVarDimension(piAddressVarThree,&m3,&n3);
663
664                 if (m3*n3 < 1)
665                 {
666                         Scierror(999,_("%s: Wrong size for input argument #%d.\n"),fname,3);
667                         *ierror = 1;
668                 }
669                 else if ( (m3 == 1) || (n3 == 1) )
670                 {
671                         lenStVarThree = (int *)MALLOC(sizeof(int) * (m3 * n3));
672                         if (lenStVarThree == NULL)
673                         {
674                                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
675                                 *ierror = 1;
676                         }
677                         else
678                         {
679                                 getMatrixOfWideString(piAddressVarThree, &m3, &n3, lenStVarThree, wcInputArgumentThree);
680                                 wcInputArgumentThree = (wchar_t **)MALLOC(sizeof(wchar_t *) * (m3 * n3));
681                                 if (wcInputArgumentThree == NULL)
682                                 {
683                                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
684                                         *ierror = 1;
685                                 }
686                                 else
687                                 {
688                                         getMatrixOfWideString(piAddressVarThree, &m3, &n3, lenStVarThree, wcInputArgumentThree);
689                                         *sizeReturnedArray = m3 * n3;
690                                 }
691                         }
692                 }
693                 else
694                 {
695                         Scierror(999,_("%s: Wrong size for input argument #%d: A vector expected.\n"),fname,3);
696                         *ierror = 1;
697                 }
698         }
699         else
700         {
701                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,3);
702                 *ierror = 1;
703         }
704         return wcInputArgumentThree;
705 }
706 /*--------------------------------------------------------------------------*/
707 static int checkExistByIDs(char *fname, double *IDs, int size_IDs)
708 {
709         if (IDs)
710         {
711                 for (int i = 0; i < size_IDs; i++)
712                 {
713                         if (diaryExists((int)IDs[i]))
714                         {
715                                 Scierror(999,_("%s: Wrong value for input argument #%d: diary ID %d not exists.\n"),fname,1, (int)IDs[i]);
716                                 return 1;
717                         }
718                 }
719                 return 0;
720         }
721         return 1;
722 }
723 /*--------------------------------------------------------------------------*/
724 static int checkExistByFilenames(char *fname, wchar_t **wcFilenames, int size_IDs)
725 {
726         if (wcFilenames)
727         {
728                 for (int i = 0; i < size_IDs; i++)
729                 {
730                         if (diaryExists(wcFilenames[i]))
731                         {
732                                 Scierror(999,_("%s: Wrong value for input argument #%d: diary filename not exists.\n"),fname,1);
733                                 return 1;
734                         }
735                 }
736                 return 0;
737         }
738         return 1;
739 }
740 /*--------------------------------------------------------------------------*/
741 static int freeInput(wchar_t **wcInput, int size_IDs)
742 {
743         if (wcInput)
744         {
745                 for(int i = 0; i < size_IDs; i++)
746                 {
747                         if (wcInput[i])
748                         {
749                                 FREE(wcInput[i]);
750                                 wcInput[i] = NULL;
751                         }
752                 }
753         }
754         return 0;
755 }
756 /*--------------------------------------------------------------------------*/
757 static int CloseByFilenames(char *fname)
758 {
759         wchar_t **wcFilenames = NULL;
760         int dIDs_size = 0;
761         int ierr = 0;
762
763         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
764         if (ierr) return 0;
765
766         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
767         if (ierr) 
768         {
769                 freeInput(wcFilenames, dIDs_size);
770                 return 0;
771         }
772
773         for (int i = 0; i < dIDs_size; i++)
774         {
775                 if (diaryClose(wcFilenames[i]))
776                 {
777                         freeInput(wcFilenames, dIDs_size);
778                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not close diary.\n"),fname);
779                         return 0;
780                 }
781         }
782
783         freeInput(wcFilenames, dIDs_size);
784         C2F(putlhsvar)();
785         return 0;
786 }
787 /*--------------------------------------------------------------------------*/
788 static int CloseByIds(char *fname)
789 {
790         double *dIDs = NULL;
791         int dIDs_size = 0;
792         int ierr = 0;
793
794         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
795
796         if (ierr == 2)
797         {
798                 // diary([], 'close')
799                 diaryCloseAll();
800                 C2F(putlhsvar)();
801                 return 0;
802         }
803         else if (ierr) return 0;
804
805         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
806         if (ierr) return 0;
807
808         for (int i = 0; i < dIDs_size; i++)
809         {
810                 if (diaryClose((int)dIDs[i]))
811                 {
812                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not close diary %d.\n"),fname, (int)dIDs[i]);
813                         return 0;
814                 }
815         }
816         C2F(putlhsvar)();
817         return 0;
818 }
819 /*--------------------------------------------------------------------------*/
820 static int PauseByFilenames(char *fname)
821 {
822         wchar_t **wcFilenames = NULL;
823         int dIDs_size = 0;
824         int ierr = 0;
825
826         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
827         if (ierr) return 0;
828
829         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
830         if (ierr) 
831         {
832                 freeInput(wcFilenames, dIDs_size);
833                 return 0;
834         }
835
836         for (int i = 0; i < dIDs_size; i++)
837         {
838                 if (diaryPause(wcFilenames[i]))
839                 {
840                         freeInput(wcFilenames, dIDs_size);
841                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not pause diary.\n"),fname);
842                         return 0;
843                 }
844         }
845
846         freeInput(wcFilenames, dIDs_size);
847
848         C2F(putlhsvar)();
849         return 0;
850 }
851 /*--------------------------------------------------------------------------*/
852 static int PauseByIds(char *fname)
853 {
854         double *dIDs = NULL;
855         int dIDs_size = 0;
856         int ierr = 0;
857
858         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
859         if (ierr) return 0;
860
861         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
862         if (ierr) return 0;
863
864         for (int i = 0; i < dIDs_size; i++)
865         {
866                 if (diaryPause((int)dIDs[i]))
867                 {
868                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not pause diary %d.\n"),fname, (int)dIDs[i]);
869                         return 0;
870                 }
871         }
872
873         C2F(putlhsvar)();
874
875         return 0;
876 }
877 /*--------------------------------------------------------------------------*/
878 static int ResumeByFilenames(char *fname)
879 {
880         wchar_t **wcFilenames = NULL;
881         int dIDs_size = 0;
882         int ierr = 0;
883         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
884         if (ierr) return 0;
885
886         ierr = checkExistByFilenames(fname, wcFilenames, dIDs_size);
887         if (ierr) 
888         {
889                 freeInput(wcFilenames, dIDs_size);
890                 return 0;
891         }
892
893         for (int i = 0; i < dIDs_size; i++)
894         {
895                 if (diaryResume(wcFilenames[i]))
896                 {
897                         freeInput(wcFilenames, dIDs_size);
898                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not resume diary.\n"),fname);
899                         return 0;
900                 }
901         }
902
903         freeInput(wcFilenames, dIDs_size);
904
905         C2F(putlhsvar)();
906
907         return 0;
908 }
909 /*--------------------------------------------------------------------------*/
910 static int ResumeByIds(char *fname)
911 {
912         double *dIDs = NULL;
913         int dIDs_size = 0;
914         int ierr = 0;
915
916         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
917         if (ierr) return 0;
918
919         ierr = checkExistByIDs(fname, dIDs, dIDs_size);
920         if (ierr) return 0;
921
922         for (int i = 0; i < dIDs_size; i++)
923         {
924                 if (diaryResume((int)dIDs[i]))
925                 {
926                         Scierror(999,_("%s: Wrong value for input argument #%d: error can not resume diary %d.\n"),fname, (int)dIDs[i]);
927                         return 0;
928                 }
929         }
930
931         C2F(putlhsvar)();
932
933         return 0;
934 }
935 /*--------------------------------------------------------------------------*/
936 static int ExistByFilenames(char *fname)
937 {
938         wchar_t **wcFilenames = NULL;
939         int dIDs_size = 0;
940         int ierr = 0;
941         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
942         if (ierr) return 0;
943
944         int *resultExist = (int*)MALLOC(sizeof(int)*dIDs_size);
945         if (resultExist == NULL)
946         {
947                 freeInput(wcFilenames,dIDs_size);
948                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
949                 return 0;
950         }
951         
952         for(int i=0; i < dIDs_size; i++)
953         {
954                 int bExists = diaryExists(wcFilenames[i]);
955                 if ( bExists == 0)
956                 {
957                         resultExist[i]  = TRUE;
958                 }
959                 else
960                 {
961                         resultExist[i]  = FALSE;
962                 }
963         }
964         freeInput(wcFilenames,dIDs_size);
965
966         createMatrixOfBoolean(Rhs + 1, 1, dIDs_size, resultExist);
967         FREE(resultExist); resultExist = NULL;
968         LhsVar(1) = Rhs + 1;
969         C2F(putlhsvar)();
970         return 0;
971 }
972 /*--------------------------------------------------------------------------*/
973 static int ExistByIds(char *fname)
974 {
975         double *dIDs = NULL;
976         int dIDs_size = 0;
977         int ierr = 0;
978
979         dIDs = getInputArgumentOneIDs(fname, &dIDs_size, &ierr);
980         if (ierr) return 0;
981
982         int *resultExist = (int*)MALLOC(sizeof(int)*dIDs_size);
983         if (resultExist == NULL)
984         {
985                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
986                 return 0;
987         }
988
989         for(int i=0; i < dIDs_size; i++)
990         {
991                 int bExists = diaryExists((int)dIDs[i]);
992                 if ( bExists == 0)
993                 {
994                         resultExist[i]  = TRUE;
995                 }
996                 else
997                 {
998                         resultExist[i]  = FALSE;
999                 }
1000         }
1001
1002         createMatrixOfBoolean(Rhs + 1, 1, dIDs_size, resultExist);
1003         FREE(resultExist); resultExist = NULL;
1004         LhsVar(1) = Rhs + 1;
1005         C2F(putlhsvar)();
1006         return 0;
1007 }
1008 /*--------------------------------------------------------------------------*/
1009 static int AppendByFilenames(char *fname, 
1010                                                          diary_filter filterMode, 
1011                                                          diary_prefix_time_format prefixMode, 
1012                                                          diary_prefix_time_filter prefixModeFilter,
1013                                                          bool suspended)
1014 {
1015         wchar_t **wcFilenames = NULL;
1016         int dIDs_size = 0;
1017         int ierr = 0;
1018
1019         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1020         if (ierr) return 0;
1021
1022         if (dIDs_size == 1)
1023         {
1024                 double dID = (double)diaryAppend(wcFilenames[0]);
1025                 if (dID == -1)
1026                 {
1027                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
1028                         if (utf_str)
1029                         {
1030                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
1031                                 FREE(utf_str); utf_str = NULL;
1032                         }
1033                         else
1034                         {
1035                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
1036                         }
1037                         freeInput(wcFilenames, dIDs_size);
1038                         return 0;
1039                 }
1040
1041                 freeInput(wcFilenames, dIDs_size);
1042
1043                 diarySetFilterMode((int)dID, filterMode);
1044                 diarySetPrefixMode((int)dID, prefixMode);
1045                 diarySetPrefixIoModeFilter((int)dID, prefixModeFilter);
1046                 if (suspended) diaryPause((int)dID);
1047
1048                 createMatrixOfDouble(Rhs + 1, 1, 1, &dID);
1049                 LhsVar(1) = Rhs + 1;
1050
1051                 if (Lhs == 2)
1052                 {
1053                         wchar_t **wfilenameUsed = new wchar_t*[1];
1054                         wfilenameUsed[0] = getDiaryFilename((int)dID);
1055                         createMatrixOfWideString(Rhs + 2, 1, 1, wfilenameUsed);
1056                         LhsVar(2) = Rhs + 2;
1057                         if (wfilenameUsed) {delete [] wfilenameUsed; wfilenameUsed = NULL;}
1058                 }
1059                 C2F(putlhsvar)();
1060         }
1061         else
1062         {
1063                 freeInput(wcFilenames, dIDs_size);
1064                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
1065         }
1066
1067         return 0;
1068 }
1069 /*--------------------------------------------------------------------------*/
1070 static int NewByFilenames(char *fname,
1071                                                   diary_filter filterMode, 
1072                                                   diary_prefix_time_format prefixMode, 
1073                                                   diary_prefix_time_filter prefixModeFilter, 
1074                                                   bool suspended)
1075 {
1076         wchar_t **wcFilenames = NULL;
1077         int dIDs_size = 0;
1078         int ierr = 0;
1079
1080         wcFilenames = getInputArgumentOneFilenames(fname, &dIDs_size, &ierr);
1081         if (ierr) return 0;
1082
1083         if (dIDs_size == 1)
1084         {
1085                 double dID = (double)diaryNew(wcFilenames[0], true);
1086                 if (dID == -1)
1087                 {
1088                         char *utf_str = wide_string_to_UTF8(wcFilenames[0]);
1089                         if (utf_str)
1090                         {
1091                                 Scierror(999,_("%s: error can not create diary: %s.\n"), fname, utf_str);
1092                                 FREE(utf_str); utf_str = NULL;
1093                         }
1094                         else
1095                         {
1096                                 Scierror(999,_("%s: error can not create diary.\n"),fname);
1097                         }
1098                         freeInput(wcFilenames, dIDs_size);
1099                         return 0;
1100                 }
1101
1102                 freeInput(wcFilenames, dIDs_size);
1103
1104                 diarySetFilterMode((int)dID, filterMode);
1105                 diarySetPrefixMode((int)dID, prefixMode);
1106                 diarySetPrefixIoModeFilter((int)dID, prefixModeFilter);
1107                 if (suspended) diaryPause((int)dID);
1108
1109                 createMatrixOfDouble(Rhs + 1, 1, 1, &dID);
1110                 LhsVar(1) = Rhs + 1;
1111
1112                 if (Lhs == 2)
1113                 {
1114                         wchar_t **wfilenameUsed = new wchar_t*[1];
1115                         wfilenameUsed[0] = getDiaryFilename((int)dID);
1116                         createMatrixOfWideString(Rhs + 2, 1, 1, wfilenameUsed);
1117                         LhsVar(2) = Rhs + 2;
1118                         if (wfilenameUsed) {delete [] wfilenameUsed; wfilenameUsed = NULL;}
1119                 }
1120                 C2F(putlhsvar)();
1121         }
1122         else
1123         {
1124                 freeInput(wcFilenames, dIDs_size);
1125                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
1126         }
1127
1128         return 0;
1129 }
1130 /*--------------------------------------------------------------------------*/