/*--------------------------------------------------------------------------*/ BOOL FileExist(char *filename) { #ifdef _MSC_VER wchar_t *wcFilename = to_wide_string(filename); if (wcFilename) { BOOL bOK = FileExistW(wcFilename); FREE(wcFilename); return bOK; } return FALSE; #else FILE* tmpFile=fopen(filename,"r"); if(tmpFile) { fclose(tmpFile); return TRUE; } else { return FALSE; } #endif }
/*--------------------------------------------------------------------------*/ int sci_movefile(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddressVarOne = NULL; wchar_t *pStVarOne = NULL; wchar_t *pStVarOneExpanded = NULL; int *piAddressVarTwo = NULL; wchar_t *pStVarTwo = NULL; wchar_t *pStVarTwoExpanded = NULL; /* Check Input & Output parameters */ CheckRhs(2, 2); CheckLhs(1, 2); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (!isStringType(pvApiCtx, piAddressVarOne)) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1); return 0; } if (!isScalar(pvApiCtx, piAddressVarOne)) { Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1); return 0; } sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } if (!isStringType(pvApiCtx, piAddressVarTwo)) { if (pStVarOne) { FREE(pStVarOne); pStVarOne = NULL; } Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2); return 0; } if (!isScalar(pvApiCtx, piAddressVarTwo)) { if (pStVarOne) { FREE(pStVarOne); pStVarOne = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2); return 0; } if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &pStVarOne)) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (getAllocatedSingleWideString(pvApiCtx, piAddressVarTwo, &pStVarTwo)) { if (pStVarOne) { freeAllocatedSingleWideString(pStVarOne); pStVarOne = NULL; } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } pStVarOneExpanded = expandPathVariableW(pStVarOne); pStVarTwoExpanded = expandPathVariableW(pStVarTwo); freeAllocatedSingleWideString(pStVarTwo); pStVarTwo = NULL; freeAllocatedSingleWideString(pStVarOne); pStVarOne = NULL; if (isdirW(pStVarOneExpanded) || FileExistW(pStVarOneExpanded)) { int ierrMove = 0; if (isdirW(pStVarOneExpanded)) { /* move a directory into a directory */ ierrMove = MoveDirectoryFunction(pStVarTwoExpanded, pStVarOneExpanded); } else if (FileExistW(pStVarOneExpanded)) { if (isdirW(pStVarTwoExpanded)) { /* move file into a existing directory */ /* copy file into a existing directory */ wchar_t *filename = getFilenameWithExtensionForMove(pStVarOneExpanded); if (filename) { #define FORMAT_FULLFILENAME "%s/%s" wchar_t *destFullFilename = NULL; /* remove last file separator if it exists */ if ((pStVarTwoExpanded[wcslen(pStVarTwoExpanded) - 1] == L'\\') || (pStVarTwoExpanded[wcslen(pStVarTwoExpanded) - 1] == L'/')) { pStVarTwoExpanded[wcslen(pStVarTwoExpanded) - 1] = L'\0'; } destFullFilename = (wchar_t *) MALLOC(sizeof(wchar_t) * ((int)wcslen(pStVarTwoExpanded) + (int)wcslen(filename) + (int)wcslen(L"/") + 1)); wcscpy(destFullFilename, pStVarTwoExpanded); wcscat(destFullFilename, L"/"); wcscat(destFullFilename, filename); ierrMove = MoveFileFunction(destFullFilename, pStVarOneExpanded); FREE(filename); filename = NULL; FREE(destFullFilename); destFullFilename = NULL; } else { if (pStVarOneExpanded) { FREE(pStVarOneExpanded); pStVarOneExpanded = NULL; } if (pStVarTwoExpanded) { FREE(pStVarTwoExpanded); pStVarTwoExpanded = NULL; } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { /* move a file into a file */ ierrMove = MoveFileFunction(pStVarTwoExpanded, pStVarOneExpanded); } } else { if (pStVarOneExpanded) { FREE(pStVarOneExpanded); pStVarOneExpanded = NULL; } if (pStVarTwo) { FREE(pStVarTwoExpanded); pStVarTwoExpanded = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: A valid filename or directory expected.\n"), fname, 1); return 0; } returnMoveFileResultOnStack(ierrMove, fname); } else { Scierror(999, _("%s: Wrong value for input argument #%d: A valid filename or directory expected.\n"), fname, 1); } if (pStVarOneExpanded) { FREE(pStVarOneExpanded); pStVarOneExpanded = NULL; } if (pStVarTwoExpanded) { FREE(pStVarTwoExpanded); pStVarTwoExpanded = NULL; } return 0; }
void ConfigVariable::whereErrorToString(std::wostringstream &ostr) { int iLenName = 1; bool isExecstr = false; bool isExecfile = false; // get max length of functions name and check if exec or execstr have been called. for (auto & where : m_WhereError) { if (isExecstr == false && where.m_name == L"execstr") { isExecstr = true; continue; } else if (isExecfile == false && where.m_name == L"exec") { isExecfile = true; continue; } iLenName = (std::max)((int)where.m_name.length(), iLenName); // in case of bin file, the file path and line is displayed only if the associated .sci file exists if (where.m_file_name != L"" && where.m_file_name.find(L".bin") != std::wstring::npos) { std::size_t pos = where.m_file_name.find_last_of(L"."); where.m_file_name.replace(pos, pos + 4, L".sci"); if (FileExistW(const_cast<wchar_t*>(where.m_file_name.c_str())) == false) { where.m_file_name = L""; } } } // add margin iLenName++; // initialize localized strings std::wstring wstrBuiltin(_W("in builtin ")); std::wstring wstrAtLine(_W("at line % 5d of function ")); std::wstring wstrExecStr(_W("at line % 5d of executed string ")); std::wstring wstrExecFile(_W("at line % 5d of executed file ")); // compute max size between "at line xxx of function" and "in builtin " // +1 : line number is pad to 5. length of "% 5d" + 1 == 5 int iMaxLen = (std::max)(wstrAtLine.length() + 1, wstrBuiltin.length()); if (isExecstr) { iMaxLen = (std::max)(((int)wstrExecStr.length()) + 1, iMaxLen); } if (isExecstr) { iMaxLen = (std::max)(((int)wstrExecFile.length()) + 1, iMaxLen); } // print call stack ostr << std::left; ostr.fill(L' '); for (auto & where : m_WhereError) { ostr.width(iMaxLen); if (where.m_line == 0) { ostr << wstrBuiltin; } else { if (where.m_name == L"execstr") { isExecstr = true; wchar_t wcsTmp[bsiz]; os_swprintf(wcsTmp, bsiz, wstrExecStr.c_str(), where.m_line); ostr << wcsTmp << std::endl; continue; } else if (where.m_name == L"exec") { wchar_t wcsTmp[bsiz]; os_swprintf(wcsTmp, bsiz, wstrExecFile.c_str(), where.m_line); ostr << wcsTmp << where.m_file_name << std::endl; continue; } else { wchar_t wcsTmp[bsiz]; os_swprintf(wcsTmp, bsiz, wstrAtLine.c_str(), where.m_line); ostr << wcsTmp; } } ostr.width(iLenName); ostr << where.m_name; if (where.m_file_name != L"") { // -1 because the first line of a function dec is : "function myfunc()" ostr << L"( " << where.m_file_name << L" " << _W("line") << L" " << where.m_macro_first_line + where.m_line - 1 << L" )"; } ostr << std::endl; } }
// ============================================================================= csvResult* csvRead(const char *filename, const char *separator, const char *decimal, const char **toreplace, int sizetoreplace, const char *regexpcomments, int header) { wchar_t *expandedFilename = NULL; wchar_t *wideFilename = NULL; csvResult *result = NULL; int fd = 0; int f_swap = 0; double res = 0.0; int errMOPEN = MOPEN_INVALID_STATUS; int errMGETL = MGETL_ERROR; wchar_t **pwstLines = NULL; char **pstLines = NULL; int nblines = 0; char **replacedInLines = NULL; char **pComments = NULL; int nbComments = 0; if ((filename == NULL) || (separator == NULL) || (decimal == NULL)) { return NULL; } wideFilename = to_wide_string((char*)filename); expandedFilename = expandPathVariableW(wideFilename); FREE(wideFilename); if (!FileExistW(expandedFilename)) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_FILE_NOT_EXIST; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } FREE(expandedFilename); return result; } errMOPEN = mopen(expandedFilename, L"rt", f_swap, &fd); // rt = read only if (expandedFilename) { FREE(expandedFilename); expandedFilename = NULL; } if (errMOPEN != MOPEN_NO_ERROR) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_MOPEN_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (header != 0) { mgetl(fd, header, &nblines, &errMGETL); } pwstLines = mgetl(fd, -1, &nblines, &errMGETL); pstLines = (char**)MALLOC(sizeof(char*) * nblines); { int i = 0; for (i = 0 ; i < nblines ; i++) { pstLines[i] = wide_string_to_UTF8(pwstLines[i]); } } mclose(fd); if (errMGETL != MGETL_NO_ERROR) { if (pwstLines) { freeArrayOfWideString(pwstLines, nblines); pwstLines = NULL; } result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_READLINES_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (regexpcomments) { int iErr = 0; pComments = extractComments((const char**)pstLines, nblines, regexpcomments, &nbComments, &iErr); if ((iErr == CAN_NOT_COMPILE_PATTERN) || (iErr == DELIMITER_NOT_ALPHANUMERIC)) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { if ((iErr == CAN_NOT_COMPILE_PATTERN) || (iErr == DELIMITER_NOT_ALPHANUMERIC)) { iErr = CSV_READ_REGEXP_ERROR; } result->err = (csvReadError)iErr; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (pComments) { char **pCleanedLines = NULL; int nbCleanedLines = 0; int i = 0; pCleanedLines = removeComments((const char**)pstLines, nblines, (const char*)regexpcomments, &nbCleanedLines, &iErr); if (pCleanedLines) { if (pwstLines) { freeArrayOfWideString(pwstLines, nblines); pwstLines = NULL; } FREE(pstLines); pstLines = pCleanedLines; nblines = nbCleanedLines; } } } if (toreplace && (sizetoreplace > 0)) { replacedInLines = replaceStrings((const char**)pstLines, nblines, toreplace, sizetoreplace); if (replacedInLines) { freeArrayOfString(pstLines, nblines); pstLines = replacedInLines; } } result = csvTextScan((const char**)pstLines, nblines, (const char*)separator, (const char*)decimal); freeArrayOfString(pstLines, nblines); freeArrayOfWideString(pwstLines, nblines); if (result) { result->pstrComments = pComments; result->nbComments = nbComments; } else { freeArrayOfString(pComments, nbComments); } return result; }
/*--------------------------------------------------------------------------*/ types::Function::ReturnValue sci_file(types::typed_list &in, int _iRetCount, types::typed_list &out) { types::String* pSAction = NULL; if (in.size() > 6) { Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "file", 0, 6); return types::Function::Error; } if (in.size() == 0) { return sci_file_no_rhs(in, _iRetCount, out); } if (in.size() == 1) { return sci_file_one_rhs(in, _iRetCount, out); } // get action if (in[0]->isString() == false) { Scierror(999, _("%s: Wrong type for input argument #%d : string expected.\n"), "file", 1); return types::Function::Error; } pSAction = in[0]->getAs<types::String>(); if (pSAction->isScalar() == false) { Scierror(999, _("%s: Wrong type for input argument #%d : A single string expected.\n"), "file", 1); return types::Function::Error; } if (wcscmp(pSAction->get(0), L"open") == 0) { types::String* pSPath = NULL; types::String* pSOption = NULL; types::Double* pSRecl = NULL; int iStatus = 0; int iAccess = 0; int iForm = 0; int iRecl = 0; int piMode[2] = {0, 0}; // get path if (in[1]->isString() == false) { Scierror(999, _("%s: Wrong type for input argument #%d : string expected.\n"), "file", 2); return types::Function::Error; } pSPath = in[1]->getAs<types::String>(); if (pSPath->isScalar() == false) { Scierror(999, _("%s: Wrong type for input argument #%d : A single string expected.\n"), "file", 2); return types::Function::Error; } // get optional inputs for (int i = 2; i < in.size(); i++) { if (in[i]->isString()) { pSOption = in[i]->getAs<types::String>(); } else if (i != 2 && in[i]->isDouble()) { pSRecl = in[i]->getAs<types::Double>(); if (pSRecl->isScalar() == false) { Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "file", i + 1); return types::Function::Error; } iRecl = (int)pSRecl->get(0); piMode[1] = iRecl; continue; } else { Scierror(999, _("%s: Wrong type for input argument #%d : string expected.\n"), "file", i + 1); return types::Function::Error; } if (pSOption->isScalar() == false) { Scierror(999, _("%s: Wrong type for input argument #%d : A single string expected.\n"), "file", i + 1); return types::Function::Error; } if (wcscmp(pSOption->get(0), L"new") == 0) { iStatus = 0; } else if (wcscmp(pSOption->get(0), L"old") == 0) { iStatus = 1; // file must already exists. if (FileExistW(pSPath->get(0)) == false) { if (_iRetCount == 2) { out.push_back(types::Double::Empty()); out.push_back(new types::Double(240)); return types::Function::OK; } else { char* pstrFilename = wide_string_to_UTF8(pSPath->get(0)); if (pstrFilename) { Scierror(240, _("%s: The file \"%s\" does not exist.\n"), "file", pstrFilename); FREE(pstrFilename); pstrFilename = NULL; } else { Scierror(240, _("%s: The file does not exist.\n"), "file"); } return types::Function::Error; } } } else if (wcscmp(pSOption->get(0), L"scratch") == 0) { iStatus = 2; } else if (wcscmp(pSOption->get(0), L"unknown") == 0) { iStatus = 3; } else if (wcscmp(pSOption->get(0), L"sequential") == 0) { iAccess = 0; } else if (wcscmp(pSOption->get(0), L"direct") == 0) { iAccess = 1; } else if (wcscmp(pSOption->get(0), L"formatted") == 0) { iForm = 0; } else if (wcscmp(pSOption->get(0), L"unformatted") == 0) { iForm = 1; } else { Scierror(999, _("%s: Wrong value for input argument #%d.\n"), "file", i + 1); return types::Function::Error; } } piMode[0] = iStatus + 10 * (iAccess + 10 * iForm); int lunit = 0; // file unit. 0 mean we open the file by this name. char* pstFilename = wide_string_to_UTF8(pSPath->get(0)); int iErr = C2F(clunit)(&lunit, pstFilename, piMode, (int)strlen(pstFilename)); if (iErr) { if (_iRetCount == 1) { switch (iErr) { case 65 : Scierror(iErr, _("%s: %d logical unit already used.\n"), "file", lunit); break; case 66 : Scierror(iErr, _("%s: Too many files opened!\n"), "file"); break; case 67 : Scierror(iErr, _("%s: Unknown file format.\n"), "file"); break; case 240 : Scierror(iErr, _("%s: File \"%s\" already exists or directory write access denied.\n"), "file", pstFilename); break; case 241 : Scierror(iErr, _("%s: File \"%s\" does not exist or read access denied.\n"), "file", pstFilename); break; default : Scierror(iErr, _("%s: Can not open File \"%s\"\n"), "file", pstFilename); } return types::Function::Error; } else { out.push_back(types::Double::Empty()); out.push_back(new types::Double((double)iErr)); return types::Function::OK; } } out.push_back(new types::Double((double)lunit)); if (_iRetCount == 2) { out.push_back(new types::Double(0.0)); } FREE(pstFilename); } else if (wcscmp(pSAction->get(0), L"close") == 0 || wcscmp(pSAction->get(0), L"rewind") == 0 || wcscmp(pSAction->get(0), L"backspace") == 0 || wcscmp(pSAction->get(0), L"last") == 0) { if (_iRetCount != 1) { Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "file", 1); return types::Function::Error; } if (in.size() != 2) { Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "file", 2); return types::Function::Error; } if (in[1]->isDouble() == false) { Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), "file", 2); return types::Function::Error; } types::Double* pDblFileUnit = in[1]->getAs<types::Double>(); double* pdblUnit = pDblFileUnit->get(); if (wcscmp(pSAction->get(0), L"close") == 0) { for (int i = 0; i < pDblFileUnit->getSize(); i++) { int iErr = mclose((int)(pdblUnit[i])); if (iErr) { Scierror(999, _("%s: Cannot close file %d.\n"), "file", (int)pdblUnit[i]); return types::Function::Error; } } } else if (wcscmp(pSAction->get(0), L"rewind") == 0) { int iFileUnit = (int)pdblUnit[0]; types::File* pFile = FileManager::getFile(iFileUnit); if (pFile && pFile->getFileType() == 2) { mseek(iFileUnit, 0, SEEK_SET); } else if (pFile && pFile->getFileType() == 1) { C2F(rewindinter)(&iFileUnit); } else { Scierror(999, _("%s: Unknown file format.\n"), "file"); return types::Function::Error; } } else if (wcscmp(pSAction->get(0), L"backspace") == 0) { int iFileUnit = (int)pdblUnit[0]; types::File* pFile = FileManager::getFile(iFileUnit); if (pFile && pFile->getFileType() == 2) { Scierror(999, _("%s: Wrong input argument #%d.\n"), "file", 1); return types::Function::Error; } else if (pFile && pFile->getFileType() == 1) { C2F(backspaceinter)(&iFileUnit); } else { Scierror(67, _("%s: Unknown file format.\n"), "file"); return types::Function::Error; } } else if (wcscmp(pSAction->get(0), L"last") == 0) { int iFileUnit = (int)pdblUnit[0]; types::File* pFile = FileManager::getFile(iFileUnit); if (pFile && pFile->getFileType() == 2) { mseek(iFileUnit, 0, SEEK_END); } else if (pFile && pFile->getFileType() == 1) { int iErr = 0; while (iErr == 0) { iErr = C2F(readinter)(&iFileUnit, "(a)", 1L); } if (iErr == 2) { Scierror(999, _("%s: \n"), "file"); return types::Function::Error; } C2F(backspaceinter)(&iFileUnit); } else { Scierror(67, _("%s: Unknown file format.\n"), "file"); return types::Function::Error; } } } else { Scierror(49, _("%s: Wrong value for input argument #%d: \"%s\", \"%s\", \"%s\", \"%s\", \"%s\" \n"), "file", 1, "open", "close", "rewind", "backspace", "last"); return types::Function::Error; } return types::Function::OK; }
types::Function::ReturnValue sci_hdf5_load(types::typed_list &in, int _iRetCount, types::typed_list& out) { int rhs = static_cast<int>(in.size()); if (rhs < 1) { Scierror(999, _("%s: Wrong number of input argument(s): at least %d expected.\n"), fname.data(), 1); return types::Function::Error; } if (in[0]->getId() != types::InternalType::IdScalarString) { Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), 1); return types::Function::Error; } wchar_t* wcfilename = expandPathVariableW(in[0]->getAs<types::String>()->get()[0]); char* cfilename = wide_string_to_UTF8(wcfilename); std::string filename(cfilename); std::wstring wfilename(wcfilename); FREE(cfilename); FREE(wcfilename); if (FileExistW(wfilename.data()) == FALSE) { Scierror(999, _("%s: Unable to open file: '%s'.\n"), fname.data(), filename.data()); return types::Function::Error; } //library ? if (isHDF5File(filename.data()) == false) { //lib file int err = 0; types::Library* lib = loadlib(in[0]->getAs<types::String>()->get()[0], &err); switch (err) { case 0: break; case 2: Scierror(999, "%s: %s", fname.data(), _("Redefining permanent variable.\n")); return types::Function::Error; default: Scierror(999, _("%s: %s is not a valid lib file.\n"), fname.data(), filename.data()); return types::Function::Error; } lib->killMe(); return types::Function::OK; } int iFile = openHDF5File(filename.data(), 0); if (iFile < 0) { Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), filename.data()); return types::Function::Error; } std::wstring wstFuncName; //manage version information int version = getSODFormatAttribute(iFile); closeHDF5File(iFile); bool needReprocess = false; switch (version) { case -1: case 1: { wstFuncName = L"hdf5_load_v1"; needReprocess = true; break; } case 2: { wstFuncName = L"hdf5_load_v2"; needReprocess = true; break; } case 3: { wstFuncName = L"hdf5_load_v3"; break; } default : { Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, version); return types::Function::Error; } } types::typed_list out1; types::Function::ReturnValue ret = Overload::call(wstFuncName, in, _iRetCount, out1); if (ret != types::Function::OK) { Scierror(999, _("%s: Unable to load '%s'\n"), fname.data(), filename.data()); return types::Function::Error; } if (needReprocess) { //call %sodload types::String* vars = out1[0]->getAs<types::String>(); vars->IncreaseRef(); int size = vars->getSize(); types::typed_list in2(1, vars); types::typed_list out2; std::wstring wstFuncName = L"%_sodload"; ret = Overload::call(wstFuncName, in2, size, out2); vars->DecreaseRef(); symbol::Context* ctx = symbol::Context::getInstance(); wchar_t** names = vars->get(); //update context with values return by %_sodload for (int i = 0; i < size; ++i) { ctx->put(symbol::Symbol(names[i]), out2[i]); } vars->killMe(); } else { out.push_back(out1.front()); } return ret; }
/*--------------------------------------------------------------------------*/ 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; }