/*--------------------------------------------------------------------------*/ types::Callable::ReturnValue sci_msprintf(types::typed_list &in, int _iRetCount, types::typed_list &out) { if (in.size() < 1) { Scierror(999, _("%s: Wrong number of input arguments: at least %d expected.\n"), "msprintf", 1); return types::Function::Error; } if (in[0]->isString() == false || in[0]->getAs<types::String>()->getSize() != 1) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "msprintf" , 1); return types::Function::Error; } for (int i = 1 ; i < in.size() ; i++) { if (in[i]->isDouble() == false && in[i]->isString() == false) { std::wstring wstFuncName = L"%" + in[i]->getShortTypeStr() + L"_msprintf"; return Overload::call(wstFuncName, in, _iRetCount, out); } } int iOutputRows = 0; int iNewLine = 0; wchar_t* pwstInput = in[0]->getAs<types::String>()->get()[0]; wchar_t** pwstOutput = scilab_sprintf("msprintf", pwstInput, in, &iOutputRows, &iNewLine); if (pwstOutput == NULL) { return types::Function::Error; } types::String* pOut = new types::String(iOutputRows, 1); pOut->set(pwstOutput); freeArrayOfWideString(pwstOutput, iOutputRows); out.push_back(pOut); return types::Function::OK; }
/*--------------------------------------------------------------------------*/ types::Function::ReturnValue sci_mfprintf(types::typed_list &in, int _iRetCount, types::typed_list &out) { BOOL isSTD = FALSE; BOOL isSTDErr = FALSE; int iFile = -1; //default file : last opened file unsigned int iNumberPercent = 0; unsigned int iNumberCols = 0; int nbrOfLines = 0; int ifileMode = 0; wchar_t* wcsInput = NULL; wchar_t** wcsStringToWrite = NULL; if (in.size() < 2) { Scierror(77, _("%s: Wrong number of input argument(s): At least %d expected.\n"), "mfprintf", 2); return types::Function::Error; } if (in[0]->isDouble() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: A Real expected.\n"), "mfprintf", 1); return types::Function::Error; } types::Double* pFileId = in[0]->getAs<types::Double>(); if (pFileId->isScalar() == false || pFileId->isComplex()) { Scierror(999, _("%s: Wrong type for input argument #%d: A Real expected.\n"), "mfprintf", 1); return types::Function::Error; } if (in[1]->isString() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "mfprintf", 2); return types::Function::Error; } types::String* pFileStr = in[1]->getAs<types::String>(); if (pFileStr->isScalar() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "mfprintf", 2); return types::Function::Error; } for (unsigned int i = 2 ; i < in.size() ; i++) { if (in[i]->isDouble() == false && in[i]->isString() == false) { std::wstring wstFuncName = L"%" + in[i]->getShortTypeStr() + L"_mfprintf"; return Overload::call(wstFuncName, in, _iRetCount, out); } } // checking ID of file iFile = static_cast<int>(pFileId->get(0)); if (FileManager::getFile(iFile) == NULL) { Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mfprintf", iFile); return types::Function::Error; } switch (iFile) { case 0: #ifdef _MSC_VER if ((getScilabMode() == SCILAB_STD) && (forceSTDERRredirect == TRUE)) { // Console redirect stderr --> CONOUT$ freopen("CONOUT$", "wb", stderr); forceSTDERRredirect = FALSE; } #endif isSTDErr = TRUE; break; case 6: isSTD = TRUE; break; case 5: Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mfprintf", iFile); return types::Function::Error; default: isSTD = FALSE; types::File* pFile = FileManager::getFile(iFile); // file opened with fortran open function if (!pFile || pFile->getFileType() == 1) { Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mfprintf", iFile); return types::Function::Error; } ifileMode = pFile->getFileModeAsInt(); break; } /* checks file mode */ /* bug 3898 */ /* read only attrib 1xx*/ if ((ifileMode >= 100) && (ifileMode < 200) && ((ifileMode % 100) < 10) /* check that it is not r+ */ && !isSTD) { Scierror(999, _("%s: Wrong file mode: READ only.\n"), "mfprintf"); return types::Function::Error; } // Checking input string to write in file int iNewLine = 0; wcsInput = pFileStr->get(0); wcsStringToWrite = scilab_sprintf("mfprintf", wcsInput, in, &nbrOfLines, &iNewLine); if (isSTD) { for (int i = 0; i < nbrOfLines; i++) { if (isSTDErr) { std::wcerr << wcsStringToWrite[i]; } else { scilabForcedWriteW(wcsStringToWrite[i]); } scilabForcedWriteW(L"\n"); } } else { int iRet = mputl(iFile, wcsStringToWrite, nbrOfLines, (BOOL)iNewLine); // FALSE = don't add the "\n" at the end. if (iRet) { Scierror(999, _("%s: Error while writing in file: disk full or deleted file.\n"), "mprintf"); for (int i = 0; i < nbrOfLines; i++) { FREE(wcsStringToWrite[i]); } FREE(wcsStringToWrite); return types::Function::Error; } } for (int i = 0; i < nbrOfLines; i++) { FREE(wcsStringToWrite[i]); } FREE(wcsStringToWrite); return types::Function::OK; }