2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Allan CORNET
4 * Copyright (C) 2009 - DIGITEO - Allan CORNET
5 * Copyright (C) 2010 - DIGITEO - Antoine ELIAS
6 * Copyright (C) 2011 - DIGITEO - Cedric DELAMARRE
8 * This file must be used under the terms of the CeCILL.
9 * This source file is licensed as described in the file COPYING, which
10 * you should have received as part of this distribution. The terms
11 * are also available at
12 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 /*--------------------------------------------------------------------------*/
16 #include "filemanager.hxx"
17 #include "fileio_gw.hxx"
18 #include "function.hxx"
21 #include "scilab_sprintf.hxx"
22 #include "overload.hxx"
23 #include "execvisitor.hxx"
28 #include "localization.h"
30 #include "sci_malloc.h"
31 #include "configvariable_interface.h"
34 /*--------------------------------------------------------------------------*/
36 static BOOL forceSTDERRredirect = TRUE;
39 using namespace types;
41 /*--------------------------------------------------------------------------*/
43 Function::ReturnValue sci_mfprintf(types::typed_list &in, int _iRetCount, types::typed_list &out)
46 BOOL isSTDErr = FALSE;
47 int iFile = -1; //default file : last opened file
48 unsigned int iNumberPercent = 0;
49 unsigned int iNumberCols = 0;
52 wchar_t* wcsInput = NULL;
53 wchar_t** wcsStringToWrite = NULL;
57 Scierror(77, _("%s: Wrong number of input argument(s): At least %d expected.\n"), "mfprintf", 2);
58 return types::Function::Error;
61 if (in[0]->isDouble() == false)
63 Scierror(999, _("%s: Wrong type for input argument #%d: A Real expected.\n"), "mfprintf", 1);
64 return types::Function::Error;
67 types::Double* pFileId = in[0]->getAs<types::Double>();
68 if (pFileId->isScalar() == false || pFileId->isComplex())
70 Scierror(999, _("%s: Wrong type for input argument #%d: A Real expected.\n"), "mfprintf", 1);
71 return types::Function::Error;
74 if (in[1]->isString() == false)
76 Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "mfprintf", 2);
77 return types::Function::Error;
80 types::String* pFileStr = in[1]->getAs<types::String>();
81 if (pFileStr->isScalar() == false)
83 Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "mfprintf", 2);
84 return types::Function::Error;
87 for (unsigned int i = 2 ; i < in.size() ; i++)
89 if (in[i]->isDouble() == false && in[i]->isString() == false)
91 ast::ExecVisitor exec;
92 std::wstring wstFuncName = L"%" + in[i]->getShortTypeStr() + L"_mfprintf";
93 return Overload::call(wstFuncName, in, _iRetCount, out, &exec);
97 // checking ID of file
98 iFile = static_cast<int>(pFileId->get(0));
100 if (FileManager::getFile(iFile) == NULL)
102 Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mfprintf", iFile);
103 return types::Function::Error;
110 if ((getScilabMode() == SCILAB_STD) && (forceSTDERRredirect == TRUE))
112 // Console redirect stderr --> CONOUT$
113 freopen("CONOUT$", "wb", stderr);
114 forceSTDERRredirect = FALSE;
122 Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mfprintf", iFile);
123 return types::Function::Error;
126 types::File* pFile = FileManager::getFile(iFile);
127 // file opened with fortran open function
128 if (pFile->getFileType() == 1)
130 Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mfprintf", iFile);
131 return types::Function::Error;
133 ifileMode = pFile->getFileModeAsInt();
137 /* checks file mode */
139 /* read only attrib 1xx*/
140 if ((ifileMode >= 100) && (ifileMode < 200) && ((ifileMode % 100) < 10) /* check that it is not r+ */ && !isSTD)
142 Scierror(999, _("%s: Wrong file mode: READ only.\n"), "mfprintf");
143 return types::Function::Error;
146 // Checking input string to write in file
148 wcsInput = pFileStr->get(0);
149 wcsStringToWrite = scilab_sprintf("mfprintf", wcsInput, in, &nbrOfLines, &iNewLine);
153 for (int i = 0; i < nbrOfLines; i++)
157 std::wcerr << wcsStringToWrite[i];
161 scilabForcedWriteW(wcsStringToWrite[i]);
163 scilabForcedWriteW(L"\n");
168 int iRet = mputl(iFile, wcsStringToWrite, nbrOfLines, (BOOL)iNewLine); // FALSE = don't add the "\n" at the end.
171 Scierror(999, _("%s: Error while writing in file: disk full or deleted file.\n"), "mprintf");
172 return types::Function::Error;
176 for (int i = 0; i < nbrOfLines; i++)
178 FREE(wcsStringToWrite[i]);
181 FREE(wcsStringToWrite);
182 return types::Function::OK;
184 /*--------------------------------------------------------------------------*/