1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - INRIA - Vincent COUVERT
4  * Copyright (C) 2010 - DIGITEO - Yann COLLETTE
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16 
17 #include "matfile_manager.h"
18 #include "localization.h"
19 #include "gw_matio.h"
20 #include "Scierror.h"
21 #include "sciprint.h"
22 
23 #include "api_scilab.h"
24 
25 /*******************************************************************************
26 Interface for MATIO function called Mat_Close
27 Scilab function name : matfile_close
28 *******************************************************************************/
sci_matfile_close(char * fname,void * pvApiCtx)29 int sci_matfile_close(char *fname, void* pvApiCtx)
30 {
31     mat_t * matfile = NULL;
32     int fileIndex = 0;
33     int nbRow = 0, nbCol = 0;
34     int * fd_addr = NULL;
35     int flag = 1, var_type;
36     double * fd_val = NULL;
37     SciErr sciErr;
38 
39     CheckRhs(1, 1);
40     CheckLhs(0, 1);
41 
42     /* First Rhs is the index of the file to close */
43     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &fd_addr);
44     if (sciErr.iErr)
45     {
46         printError(&sciErr, 0);
47         return 0;
48     }
49     sciErr = getVarType(pvApiCtx, fd_addr, &var_type);
50     if (sciErr.iErr)
51     {
52         printError(&sciErr, 0);
53         return 0;
54     }
55 
56     if (var_type == sci_matrix)
57     {
58         sciErr = getMatrixOfDouble(pvApiCtx, fd_addr, &nbRow, &nbCol, &fd_val);
59         if (sciErr.iErr)
60         {
61             printError(&sciErr, 0);
62             return 0;
63         }
64         if (nbRow * nbCol != 1)
65         {
66             Scierror(999, _("%s: Wrong size for first input argument: Single double expected.\n"), fname);
67             return FALSE;
68         }
69         fileIndex = (int) * fd_val;
70     }
71     else
72     {
73         Scierror(999, _("%s: Wrong type for first input argument: Double expected.\n"), fname);
74         return FALSE;
75     }
76 
77     /* Gets the corresponding matfile to close it */
78     /* The manager clears its static matfile table */
79     matfile_manager(MATFILEMANAGER_DELFILE, &fileIndex, &matfile);
80 
81     /* If the file has not already been closed, it's closed */
82     if (matfile != NULL)
83     {
84         flag = Mat_Close(matfile);
85     }
86     else /* The user is informed */
87     {
88         sciprint("File already closed.\n");
89     }
90 
91     /* Return execution flag */
92     var_type = (flag == 0);
93     createScalarBoolean(pvApiCtx, Rhs + 1, var_type);
94 
95     LhsVar(1) = Rhs + 1;
96 
97     PutLhsVar();
98 
99     return TRUE;
100 }
101