/*--------------------------------------------------------------------------*/ char *pathconvert(char* path, BOOL flagtrail, BOOL flagexpand, PathConvertType PType) { char *convertedPath = NULL; if (path) { wchar_t *wcpath = to_wide_string(path); if (wcpath) { wchar_t *wcconvertedPath = pathconvertW(wcpath, flagtrail, flagexpand, PType); if (wcconvertedPath) { convertedPath = wide_string_to_UTF8(wcconvertedPath); FREE(wcconvertedPath); wcconvertedPath = NULL; } } } return convertedPath; }
/*--------------------------------------------------------------------------*/ types::Function::ReturnValue sci_pathconvert(types::typed_list &in, int _iRetCount, types::typed_list &out) { PathConvertType PType = AUTO_STYLE; int iPathExpand = 1; int iPathTrail = 1; if (in.size() < 1 && in.size() > 4) { Scierror(999, _("%s: Wrong number of input arguments: %d to %d expected.\n"), "pathconvert" , 1, 4); return types::Function::Error; } if (_iRetCount != 1) { Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "pathconvert", 1); return types::Function::Error; } //get type if (in.size() > 3) { if (in[3]->isString() == false || in[3]->getAs<types::String>()->getSize() != 1) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "pathconvert", 4); return types::Function::Error; } wchar_t* pwstType = in[3]->getAs<types::String>()->get(0); if (wcscmp(pwstType, WINDOWS_TYPE) == 0) { PType = WINDOWS_STYLE; } else if (wcscmp(pwstType, UNIX_TYPE) == 0) { PType = UNIX_STYLE; } else { Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), "pathconvert", 4, _UNIX_TYPE, _WINDOWS_TYPE); return types::Function::Error; } } if (in.size() > 2) { if (in[2]->isBool() == false || in[2]->getAs<types::Bool>()->getSize() != 1) { Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), "pathconvert", 3); return types::Function::Error; } iPathExpand = in[2]->getAs<types::Bool>()->get()[0]; } if (in.size() > 1) { if (in[1]->isBool() == false || in[1]->getAs<types::Bool>()->getSize() != 1) { Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), "pathconvert", 2); return types::Function::Error; } iPathTrail = in[1]->getAs<types::Bool>()->get()[0]; } if (in[0]->isDouble() && in[0]->getAs<types::Double>()->isEmpty()) { out.push_back(types::Double::Empty()); return types::Function::OK; } if (in[0]->isString() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"), "pathconvert", 1); return types::Function::Error; } types::String* pS = in[0]->getAs<types::String>(); types::String* pOut = new types::String(pS->getRows(), pS->getCols()); wchar_t** pStr = pOut->get(); for (int i = 0 ; i < pS->getSize() ; i++) { pStr[i] = pathconvertW(pS->get(i), (BOOL) iPathTrail, (BOOL) iPathExpand, PType); } out.push_back(pOut); return types::Function::OK; //SciErr sciErr; //int *piAddressVarOne = NULL; //wchar_t **pStVarOne = NULL; //int iType1 = 0; //int *lenStVarOne = NULL; //int m1 = 0, n1 = 0; //wchar_t **results = NULL; //int i = 0; //BOOL flagtrail = TRUE; //BOOL flagexpand = TRUE; //PathConvertType PType = AUTO_STYLE; ///* Check Input & Output parameters */ //CheckRhs(1,4); //CheckLhs(1,1); //if (Rhs > 3) //{ // int *piAddressVarFour = NULL; // wchar_t *pStVarFour = NULL; // int iType4 = 0; // int lenStVarFour = 0; // int m4 = 0, n4 = 0; // sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // sciErr = getVarType(pvApiCtx, piAddressVarFour, &iType4); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if (iType4 != sci_strings) // { // Scierror(999,_("%s: Wrong type for input argument #%d: string expected.\n"), fname, 4); // return 0; // } // sciErr = getVarDimension(pvApiCtx, piAddressVarFour, &m4, &n4); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if ( (m4 != n4) && (n4 != 1) ) // { // Scierror(999,_("%s: Wrong size for input argument #%d: string expected.\n"), fname, 4); // return 0; // } // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarFour, &m4, &n4, &lenStVarFour, &pStVarFour); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // pStVarFour = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarFour + 1)); // if (pStVarFour == NULL) // { // Scierror(999,_("%s: Memory allocation error.\n"),fname); // return 0; // } // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarFour, &m4, &n4, &lenStVarFour, &pStVarFour); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if ( (wcscmp(pStVarFour, L"w") == 0) || (wcscmp(pStVarFour, L"u") == 0) ) // { // if (wcscmp(pStVarFour, L"w") == 0) // { // PType = WINDOWS_STYLE; // } // else // { // PType = UNIX_STYLE; // } // if (pStVarFour) {FREE(pStVarFour); pStVarFour = NULL;} // } // else // { // if (pStVarFour) {FREE(pStVarFour); pStVarFour = NULL;} // Scierror(999,_("%s: Wrong value for input argument #%d: ''w'' or ''u'' string expected.\n"), fname, 4); // return 0; // } //} //if (Rhs > 2) //{ // int *piAddressVarThree = NULL; // int *piData = NULL; // int iType3 = 0; // int m3 = 0, n3 = 0; // sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if (iType3 != sci_boolean) // { // Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3); // return 0; // } // sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if ( (m3 != n3) && (n3 != 1) ) // { // Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3); // return 0; // } // sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarThree, &m3, &n3, &piData); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // flagexpand = piData[0]; //} //if (Rhs > 1) //{ // int *piAddressVarTwo = NULL; // int *piData = NULL; // int iType2 = 0; // int m2 = 0, n2 = 0; // sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if (iType2 != sci_boolean) // { // Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 2); // return 0; // } // sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if ( (m2 != n2) && (n2 != 1) ) // { // Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 2); // return 0; // } // sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &piData); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // flagtrail = piData[0]; //} //sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); //if(sciErr.iErr) //{ // printError(&sciErr, 0); // return 0; //} //sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1); //if(sciErr.iErr) //{ // printError(&sciErr, 0); // return 0; //} //if (iType1 == sci_matrix) //{ // sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // if ( (m1 == n1) && (m1 == 0) ) // { // sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // LhsVar(1) = Rhs + 1; // C2F(putlhsvar)(); // } // else // { // Scierror(999,_("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1); // } //} //else if (iType1 == sci_strings) //{ // sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); // if (lenStVarOne == NULL) // { // Scierror(999,_("%s: Memory allocation error.\n"),fname); // return 0; // } // results = (wchar_t **)MALLOC(sizeof(wchar_t*) * (m1 * n1)); // if (results == NULL) // { // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} // freeArrayOfWideString(pStVarOne, m1 * n1); // Scierror(999,_("%s: Memory allocation error.\n"),fname); // return 0; // } // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); // if (pStVarOne == NULL) // { // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} // Scierror(999,_("%s: Memory allocation error.\n"),fname); // return 0; // } // for( i = 0; i < m1 * n1; i++) // { // pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarOne[i] + 1)); // if (pStVarOne[i] == NULL) // { // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} // freeArrayOfWideString(pStVarOne, m1 * n1); // Scierror(999,_("%s: Memory allocation error.\n"),fname); // return 0; // } // } // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // for( i = 0; i < m1 * n1; i++) // { // results[i] = pathconvertW(pStVarOne[i], flagtrail, flagexpand, PType); // } // sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, m1, n1, results); // if(sciErr.iErr) // { // printError(&sciErr, 0); // return 0; // } // LhsVar(1) = Rhs + 1; // C2F(putlhsvar)(); // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} // freeArrayOfWideString(results, m1 * n1); // freeArrayOfWideString(pStVarOne, m1 * n1); //} //else //{ // Scierror(999,_("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1); // return 0; //} //return 0; }
/*--------------------------------------------------------------------------*/ types::Function::ReturnValue sci_gwlib(types::typed_list &in, int /*_iRetCount*/, types::typed_list &out) { if (in.size() != 1) { Scierror(78, _("%s: Wrong number of input argument(s): %d expected.\n"), "lib", 1); return types::Function::Error; } types::InternalType* pIT = in[0]; if (pIT->isString() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "lib", 1); return types::Function::Error; } types::String *pS = pIT->getAs<types::String>(); if (pS->isScalar() == false) { Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), "lib", 1); return types::Function::Error; } wchar_t* pstPath = pS->get(0); wchar_t* pwstPath = pathconvertW(pstPath, TRUE, TRUE, AUTO_STYLE); int err = 0; types::Library* lib = loadlib(pwstPath, &err, false, false); FREE(pwstPath); switch (err) { case 0 : //no error break; case 1: { char* pst = wide_string_to_UTF8(pstPath); Scierror(999, _("%s: %s is not a valid lib path.\n"), "lib", pst); FREE(pst); return types::Function::Error; } case 2: { Scierror(999, "%s: %s", "lib", _("Redefining permanent variable.\n")); return types::Function::Error; } case 3: { Scierror(999, "%s: %s", "lib", _("Cannot read lib file: Not in proper XML format.\n")); return types::Function::Error; } case 4: { Scierror(999, "%s: %s", "lib", _("Old binary lib detected. Please recompile it for Scilab 6.\n")); return types::Function::Error; } default: { //nothing } } out.push_back(lib); return types::Function::OK; }
/*--------------------------------------------------------------------------*/ types::Function::ReturnValue sci_genlib(types::typed_list &in, int _iRetCount, types::typed_list &out) { int succes = 1; std::vector<std::wstring> failed_files; std::vector<std::wstring> success_files; std::vector<std::wstring> funcs; wchar_t pstParseFile[PATH_MAX + FILENAME_MAX]; wchar_t pstVerbose[65535]; int iNbFile = 0; wchar_t *pstParsePath = NULL; int iParsePathLen = 0; wchar_t* pstLibName = NULL; bool bVerbose = false; bool bForce = false; if (in.size() < 1 || in.size() > 4) { Scierror(78, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "genlib", 1, 4); return types::Function::Error; } //param 1, library name types::InternalType* pIT = in[0]; if (pIT->isString() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "genlib", 1); return types::Function::Error; } types::String *pS = pIT->getAs<types::String>(); if (pS->getSize() != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), "genlib", 1); return types::Function::Error; } pstLibName = pS->get(0); //param 2, library path if (in.size() > 1) { pIT = in[1]; if (pIT->isString() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "genlib", 2); return types::Function::Error; } } else { int ierr = 0; char* pstr = scigetcwd(&ierr); pIT = new types::String(pstr); FREE(pstr); } pS = pIT->getAs<types::String>(); if (pS->isScalar() == false) { Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), "genlib", 2); return types::Function::Error; } //param 3, force flag if (in.size() > 2) { pIT = in[2]; if (pIT->isBool() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar boolean expected.\n"), "genlib", 3); return types::Function::Error; } types::Bool* p = pIT->getAs<types::Bool>(); if (p->isScalar() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar boolean expected.\n"), "genlib", 3); return types::Function::Error; } bForce = p->get()[0] == 1; } if (in.size() > 3) { //verbose flag pIT = in[3]; if (pIT->isBool() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar boolean expected.\n"), "genlib", 3); return types::Function::Error; } types::Bool* p = pIT->getAs<types::Bool>(); if (p->isScalar() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar boolean expected.\n"), "genlib", 3); return types::Function::Error; } bVerbose = p->get()[0] == 1; } wchar_t* pstFile = pS->get(0); pstParsePath = pathconvertW(pstFile, TRUE, TRUE, AUTO_STYLE); if (in.size() == 1) { delete pS; } os_swprintf(pstParseFile, PATH_MAX + FILENAME_MAX, L"%lslib", pstParsePath); if (bVerbose) { os_swprintf(pstVerbose, 65535, _W("-- Creation of [%ls] (Macros) --\n").c_str(), pstLibName); //save current prompt mode int oldVal = ConfigVariable::getPromptMode(); //set mode silent for errors ConfigVariable::setPromptMode(0); scilabWriteW(pstVerbose); //restore previous prompt mode ConfigVariable::setPromptMode(oldVal); } MacroInfoList lstOld; if (FileExistW(pstParseFile)) { //read it to get previous information like md5 std::wstring libname; parseLibFile(pstParseFile, lstOld, libname); deleteafileW(pstParseFile); } xmlTextWriterPtr pWriter = openXMLFile(pstParseFile, pstLibName); if (pWriter == NULL) { os_swprintf(pstVerbose, 65535, _W("%ls: Cannot open file ''%ls''.\n").c_str(), L"genlib", pstParseFile); scilabWriteW(pstVerbose); out.push_back(new types::Bool(0)); FREE(pstParsePath); return types::Function::OK; } wchar_t **pstPath = findfilesW(pstParsePath, L"*.sci", &iNbFile, FALSE); if (pstPath) { types::Library* pLib = new types::Library(pstParsePath); for (int k = 0 ; k < iNbFile ; k++) { //version with direct parsing //parse the file to find all functions std::wstring stFullPath = std::wstring(pstParsePath) + std::wstring(pstPath[k]); std::wstring stFullPathBin(stFullPath); stFullPathBin.replace(stFullPathBin.end() - 3, stFullPathBin.end(), L"bin"); std::wstring pstPathBin(pstPath[k]); pstPathBin.replace(pstPathBin.end() - 3, pstPathBin.end(), L"bin"); //compute file md5 FILE* fmdf5 = os_wfopen(stFullPath.data(), L"rb"); if (fmdf5 == NULL) { char* pstr = wide_string_to_UTF8(stFullPath.data()); Scierror(999, _("%s: Cannot open file ''%s''.\n"), "genlib", pstr); FREE(pstr); FREE(pstParsePath); freeArrayOfWideString(pstPath, iNbFile); pLib->killMe(); return types::Function::Error; } char* md5 = md5_file(fmdf5); fclose(fmdf5); wchar_t* wmd5 = to_wide_string(md5); FREE(md5); std::wstring wide_md5(wmd5); FREE(wmd5); if (bForce == false) { //check if is exist in old file MacroInfoList::iterator it = lstOld.find(pstPathBin); if (it != lstOld.end()) { if (wide_md5 == (*it).second.md5) { //file not change, we can skip it AddMacroToXML(pWriter, (*it).second.name, pstPathBin, wide_md5); pLib->add((*it).second.name, new types::MacroFile((*it).second.name, stFullPathBin, pstLibName)); success_files.push_back(stFullPath); funcs.push_back((*it).second.name); continue; } } } if (bVerbose) { sciprint(_("%ls: Processing file: %ls\n"), L"genlib", pstPath[k]); } Parser parser; parser.parseFile(stFullPath, ConfigVariable::getSCIPath()); if (parser.getExitStatus() != Parser::Succeded) { if (_iRetCount != 4) { std::wstring wstrErr = parser.getErrorMessage(); wchar_t errmsg[256]; os_swprintf(errmsg, 256, _W("%ls: Error in file %ls.\n").c_str(), L"genlib", stFullPath.data()); wstrErr += errmsg; char* str = wide_string_to_UTF8(wstrErr.c_str()); Scierror(999, str); FREE(str); FREE(pstParsePath); freeArrayOfWideString(pstPath, iNbFile); closeXMLFile(pWriter); delete pLib; return types::Function::Error; } failed_files.push_back(stFullPath); succes = 0; continue; } //serialize ast ast::SerializeVisitor* s = new ast::SerializeVisitor(parser.getTree()); unsigned char* serialAst = s->serialize(); // Header is : buffer size (4 bytes) + scilab version (4 bytes) unsigned int size = *((unsigned int*)serialAst); FILE* f = os_wfopen(stFullPathBin.c_str(), L"wb"); fwrite(serialAst, 1, size, f); fclose(f); ast::exps_t LExp = parser.getTree()->getAs<ast::SeqExp>()->getExps(); for (ast::exps_t::iterator j = LExp.begin(), itEnd = LExp.end() ; j != itEnd ; ++j) { if ((*j)->isFunctionDec()) { ast::FunctionDec* pFD = (*j)->getAs<ast::FunctionDec>(); const std::wstring& name = pFD->getSymbol().getName(); if (name + L".sci" == pstPath[k]) { if (AddMacroToXML(pWriter, name, pstPathBin, wide_md5) == false) { os_swprintf(pstVerbose, 65535, _W("%ls: Warning: %ls information cannot be added to file %ls. File ignored\n").c_str(), L"genlib", pFD->getSymbol().getName().c_str(), pstPath[k]); scilabWriteW(pstVerbose); } pLib->add(name, new types::MacroFile(name, stFullPathBin, pstLibName)); success_files.push_back(stFullPath); funcs.push_back(name); break; } } } delete s; free(serialAst); delete parser.getTree(); } symbol::Context* ctx = symbol::Context::getInstance(); symbol::Symbol sym = symbol::Symbol(pstLibName); if (ctx->isprotected(sym) == false) { ctx->put(symbol::Symbol(pstLibName), pLib); } else { Scierror(999, _("Redefining permanent variable.\n")); freeArrayOfWideString(pstPath, iNbFile); FREE(pstParsePath); closeXMLFile(pWriter); delete pLib; return types::Function::Error; } } freeArrayOfWideString(pstPath, iNbFile); out.push_back(new types::Bool(succes)); if (_iRetCount > 1) { int size = static_cast<int>(funcs.size()); if (size == 0) { out.push_back(types::Double::Empty()); } else { types::String* s = new types::String(size, 1); for (int i = 0; i < size; ++i) { s->set(i, funcs[i].data()); } out.push_back(s); } } if (_iRetCount > 2) { int size = static_cast<int>(success_files.size()); if (size == 0) { out.push_back(types::Double::Empty()); } else { types::String* s = new types::String(size, 1); for (int i = 0; i < size; ++i) { s->set(i, success_files[i].data()); } out.push_back(s); } } if (_iRetCount > 3) { int size = static_cast<int>(failed_files.size()); if (size == 0) { out.push_back(types::Double::Empty()); } else { types::String* s = new types::String(size, 1); for (int i = 0; i < size; ++i) { s->set(i, failed_files[i].data()); } out.push_back(s); } } FREE(pstParsePath); closeXMLFile(pWriter); return types::Function::OK; }