mxArray *mxCreateCharMatrixFromStrings(int m, const char **str) { int n = 1; wchar_t** strings = NULL; strings = (wchar_t**)MALLOC(sizeof(wchar_t*) * m); for (int k = 0; k < m; k++) { strings[k] = to_wide_string(str[k]); } String *ptr = new String(m, n, strings); freeArrayOfWideString(strings, m); return (mxArray *)ptr; }
/*--------------------------------------------------------------------------*/ 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; }
/*--------------------------------------------------------------------------*/ static int returnMoveFileResultOnStack(int ierr, char *fname) { double dError = 0.; wchar_t **sciError = NULL; int m_out = 1, n_out = 1; sciError = (wchar_t **) MALLOC(sizeof(wchar_t *) * 1); if (sciError == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } #ifdef _MSC_VER if (ierr) { #define BUFFER_SIZE 1024 DWORD dw = GetLastError(); wchar_t buffer[BUFFER_SIZE]; if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, BUFFER_SIZE, NULL) == 0) { wcscpy(buffer, L"Unknown Error"); } // for compatibilty with copyfile, we return 0 (error) //dError = (double) dw; dError = (double)0; sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * ((int)wcslen(buffer) + 1)); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } wcscpy(sciError[0], buffer); } else { dError = 1.; sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * 1); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } wcscpy(sciError[0], L""); } #else if (ierr) { // for compatibilty with copyfile, we return 0 (error) //dError = (double) ierr; dError = (double)0.; sciError[0] = to_wide_string(strerror(errno)); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { dError = 1.; sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * 1); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); freeArrayOfWideString(sciError, 1); return 0; } wcscpy(sciError[0], L""); } #endif createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, &dError); LhsVar(1) = Rhs + 1; if (Lhs == 2) { createMatrixOfWideString(pvApiCtx, Rhs + 2, m_out, n_out, sciError); LhsVar(2) = Rhs + 2; } freeArrayOfWideString(sciError, 1); PutLhsVar(); return 0; }
// ============================================================================= 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; }
/*--------------------------------------------------------------------------*/ int sci_basename(char *fname,unsigned long fname_len) { SciErr sciErr; BOOL flagexpand = TRUE; /* default */ int *piAddressVarOne = NULL; wchar_t **pStVarOne = NULL; int *lenStVarOne = NULL; int iType1 = 0; int m1 = 0, n1 = 0; wchar_t **pStResult = NULL; /* Check Input & Output parameters */ CheckRhs(1,3); CheckLhs(1,1); 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); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if (iType3 != sci_boolean) { Scierror(999,_("%s: Wrong type 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); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if ( (m3 != n3) && (n3 != 1) ) { Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3); 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); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); 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); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); 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); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } } 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; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (iType1 == sci_matrix) { sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if ( (m1 == n1) && (m1 == 0) ) { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999,_("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; PutLhsVar(); } else { Scierror(999,_("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1); } } else if (iType1 == sci_strings) { int i = 0; sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999,_("%s: Memory allocation error.\n"),fname); return 0; } // get lenStVarOne value sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); if(sciErr.iErr) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); 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) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} Scierror(999,_("%s: Memory allocation error.\n"),fname); return 0; } } // get pStVarOne sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if(sciErr.iErr) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pStResult = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); if (pStResult == NULL) { if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} Scierror(999,_("%s: Memory allocation error.\n"),fname); return 0; } for (i=0;i< m1 * n1; i++) { pStResult[i] = basenameW(pStVarOne[i], flagexpand); } sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, m1, n1, pStResult); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999,_("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} freeArrayOfWideString(pStResult, m1 * n1); freeArrayOfWideString(pStVarOne, m1 * n1); PutLhsVar(); } else { Scierror(999,_("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1); } return 0; }
/*--------------------------------------------------------------------------*/ int sci_isdir(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddressVarOne = NULL; wchar_t **pStVarOne = NULL; int iType = 0; int *lenStVarOne = NULL; int m1 = 0, n1 = 0; BOOL *results = NULL; int i = 0; /* Check Input & Output parameters */ CheckRhs(1, 1); CheckLhs(1, 1); 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; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (iType != sci_strings) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1); return 0; } sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } results = (BOOL*)MALLOC(sizeof(BOOL) * (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, NULL); if (sciErr.iErr) { printError(&sciErr, 0); FREE(results); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); if (pStVarOne == NULL) { 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) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } for (i = 0; i < m1 * n1; i++) { wchar_t *expandedPath = expandPathVariableW(pStVarOne[i]); if (expandedPath) { results[i] = isdirW(expandedPath); FREE(expandedPath); expandedPath = NULL; } else { results[i] = FALSE; } } if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pStVarOne, m1 * n1); sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, results); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; if (results) { FREE(results); results = NULL; } PutLhsVar(); return 0; }
/*--------------------------------------------------------------------------*/ fscanfMatResult *fscanfMat(char *filename, char *format, char *separator) { int fd = 0; int f_swap = 0; double res = 0.0; int errMOPEN = MOPEN_INVALID_STATUS; int errMGETL = MGETL_ERROR; int i = 0; int nbLinesTextDetected = 0; int nbColumns = 0; int nbRows = 0; fscanfMatResult *resultFscanfMat = NULL; wchar_t **pwsLines = NULL; char **lines = NULL; int nblines = 0; double *dValues = NULL; wchar_t* filenameW = NULL; if ((filename == NULL) || (format == NULL) || (separator == NULL)) { return NULL; } if (!checkFscanfMatFormat(format)) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_FORMAT_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } return resultFscanfMat; } filenameW = to_wide_string(filename); errMOPEN = mopen(filenameW, READ_ONLY_TEXT_MODE, f_swap, &fd); FREE(filenameW); if (errMOPEN != MOPEN_NO_ERROR) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_MOPEN_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } return resultFscanfMat; } pwsLines = mgetl(fd, -1, &nblines, &errMGETL); mclose(fd); if (errMGETL != MGETL_NO_ERROR) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_READLINES_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } return resultFscanfMat; } lines = (char**)MALLOC(sizeof(char*) * nblines); for (i = 0; i < nblines; i++) { lines[i] = wide_string_to_UTF8(pwsLines[i]); } freeArrayOfWideString(pwsLines, nblines); lines = removeEmptyLinesAtTheEnd(lines, &nblines); lines = removeTextLinesAtTheEnd(lines, &nblines, format, separator); nbLinesTextDetected = getNumbersLinesOfText(lines, nblines, format, separator); nbRows = nblines - nbLinesTextDetected; nbColumns = getNumbersColumnsInLines(lines, nblines, nbLinesTextDetected, format, separator); dValues = getDoubleValuesFromLines(lines, nblines, nbLinesTextDetected, format, separator, nbColumns, nbRows); if (dValues) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { if (nbLinesTextDetected > 0) { if (lines) { for (i = nbLinesTextDetected; i < nblines; i++) { if (lines[i]) { FREE(lines[i]); lines[i] = NULL; } } } resultFscanfMat->text = lines; } else { freeArrayOfString(lines, nblines); resultFscanfMat->text = NULL; } resultFscanfMat->sizeText = nbLinesTextDetected; resultFscanfMat->m = nbRows; resultFscanfMat->n = nbColumns; resultFscanfMat->values = dValues; resultFscanfMat->err = FSCANFMAT_NO_ERROR; } else { FREE(dValues); freeArrayOfString(lines, nblines); } } else { freeArrayOfString(lines, nblines); if (nbColumns == 0 || nbRows == 0) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_READLINES_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } } } return resultFscanfMat; }
/*--------------------------------------------------------------------------*/ static int isasciiStrings(char *fname, int *piAddressVarOne) { SciErr sciErr; int m1 = 0, n1 = 0; wchar_t **pwcStVarOne = NULL; int *lenStVarOne = NULL; sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne) { BOOL *bOutputMatrix = NULL; int i = 0; int lengthAllStrings = 0; sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); if (sciErr.iErr) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pwcStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); for (i = 0; i < (m1 * n1); i++) { lengthAllStrings = lengthAllStrings + lenStVarOne[i]; pwcStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1)); if (pwcStVarOne[i] == NULL) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pwcStVarOne); if (sciErr.iErr) { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } bOutputMatrix = (BOOL*)MALLOC(sizeof(BOOL) * lengthAllStrings); if (bOutputMatrix) { int mOut = 0; int nOut = 0; int x = 0; for (i = 0; i < (m1 * n1); i++) { int j = 0; wchar_t* wcInput = pwcStVarOne[i]; int len = (int)wcslen(wcInput); for (j = 0; j < len; j++) { if (iswascii(wcInput[j])) { bOutputMatrix[x] = (int)TRUE; } else { bOutputMatrix[x] = (int)FALSE; } x++; } } mOut = 1; nOut = lengthAllStrings; sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, mOut, nOut, bOutputMatrix); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); if (bOutputMatrix) { FREE(bOutputMatrix); bOutputMatrix = NULL; } LhsVar(1) = Rhs + 1; PutLhsVar(); } else { if (lenStVarOne) { FREE(lenStVarOne); lenStVarOne = NULL; } freeArrayOfWideString(pwcStVarOne, m1 * n1); Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } return 0; }
/*--------------------------------------------------------------------------*/ int LineRead(int fd, char buf[], int n, int *cnt, int *nr) { int returnedInfo = READNEXTLINE_ERROR_ERROR_UNMANAGED; int nbLinesToRead = 1; int nbLinesReaded = 0; int mgetIerr = MGETL_ERROR; wchar_t **lines = mgetl(fd, nbLinesToRead, &nbLinesReaded, &mgetIerr); char* line = wide_string_to_UTF8(lines[0]); freeArrayOfWideString(lines, nbLinesReaded); *cnt = 0; *nr = 0; memset(buf, 0, n); strcpy(buf, EMPTYSTR); switch (mgetIerr) { case MGETL_NO_ERROR: { if (line && nbLinesReaded == 1) { /* current limitation (bsiz) of line readed by scilab */ if ((int)wcslen(lines[0]) < bsiz) { strcpy(buf, line); returnedInfo = READNEXTLINE_ERROR_EOL; } else { strncpy(buf, line, bsiz); returnedInfo = READNEXTLINE_ERROR_BUFFER_FULL; } } else { returnedInfo = READNEXTLINE_ERROR_EOF_REACHED; } } break; case MGETL_EOF: { if (lines) { if (nbLinesReaded == 0) { returnedInfo = READNEXTLINE_ERROR_EOF_REACHED; } else { /* current limitation (bsiz) of line readed by scilab */ if ((int)strlen(line) >= bsiz) { strcpy(buf, line); returnedInfo = READNEXTLINE_ERROR_EOF_REACHED_AFTER_EOL; } else { strncpy(buf, line, bsiz); returnedInfo = READNEXTLINE_ERROR_BUFFER_FULL; } } } else { returnedInfo = READNEXTLINE_ERROR_EOF_REACHED_BEFORE_EOL; } } break; case MGETL_MEMORY_ALLOCATION_ERROR: case MGETL_ERROR: default: { returnedInfo = READNEXTLINE_ERROR_ERROR_UNMANAGED; } break; } *cnt = (int)strlen(buf) + 1; *nr = *cnt; if (line) { FREE(line); } return returnedInfo; }
/*--------------------------------------------------------------------------*/ types::Function::ReturnValue sci_mgetl(types::typed_list &in, int _iRetCount, types::typed_list &out) { int iFileID = 0; int iErr = 0; bool bCloseFile = false; int iLinesExcepted = -1; int iLinesRead = -1; wchar_t** wcReadedStrings = NULL; if (in.size() < 1 || in.size() > 2) { Scierror(77, _("%s: Wrong number of input arguments: %d to %d expected.\n"), "mgetl" , 1, 2); return types::Function::OK; } if (in.size() == 2) { //number of lines if (in[1]->isDouble() == false) { Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), "mgetl", 2); return types::Function::Error; } if (in[1]->getAs<types::Double>()->isScalar() == false) { Scierror(999, _("%s: Wrong size for input argument #%d: An integer value expected.\n"), "mgetl", 2); return types::Function::Error; } if (in[1]->getAs<types::Double>()->get(0) != (int)in[1]->getAs<types::Double>()->get(0)) { Scierror(999, _("%s: Wrong value for input argument #%d: An integer value expected.\n"), "mgetl", 2); return types::Function::Error; } iLinesExcepted = static_cast<int>(in[1]->getAs<types::Double>()->get(0)); } if (in[0]->isDouble() && in[0]->getAs<types::Double>()->getSize() == 1) { iFileID = static_cast<int>(in[0]->getAs<types::Double>()->get(0)); } else if (in[0]->isString() && in[0]->getAs<types::String>()->getSize() == 1) { wchar_t *expandedFileName = expandPathVariableW(in[0]->getAs<types::String>()->get(0)); iErr = mopen(expandedFileName, L"rt", 0, &iFileID); if (iErr) { char* pst = wide_string_to_UTF8(expandedFileName); switch (iErr) { case MOPEN_NO_MORE_LOGICAL_UNIT: Scierror(66, _("%s: Too many files opened!\n"), "mgetl"); break; case MOPEN_CAN_NOT_OPEN_FILE: Scierror(999, _("%s: Cannot open file %s.\n"), "mgetl", pst); break; case MOPEN_NO_MORE_MEMORY: Scierror(999, _("%s: No more memory.\n"), "mgetl"); break; case MOPEN_INVALID_FILENAME: Scierror(999, _("%s: invalid filename %s.\n"), "mgetl", pst); break; default: //MOPEN_INVALID_STATUS Scierror(999, _("%s: invalid status.\n"), "mgetl"); break; } FREE(pst); FREE(expandedFileName); return types::Function::Error; } FREE(expandedFileName); bCloseFile = true; } else { //Error Scierror(999, _("%s: Wrong type for input argument #%d: a String or Integer expected.\n"), "mgetl", 1); return types::Function::Error; } switch (iFileID) { case 0: // stderr case 6: // stdout Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mgetl", iFileID); return types::Function::Error; default : { types::File* pFile = FileManager::getFile(iFileID); // file opened with fortran open function if (pFile == NULL || pFile->getFileType() == 1) { Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mgetl", iFileID); return types::Function::Error; } wcReadedStrings = mgetl(iFileID, iLinesExcepted, &iLinesRead, &iErr); switch (iErr) { case MGETL_MEMORY_ALLOCATION_ERROR : break; } } } if (wcReadedStrings && iLinesRead > 0) { types::String *pS = new types::String(iLinesRead, 1); pS->set(wcReadedStrings); out.push_back(pS); freeArrayOfWideString(wcReadedStrings, iLinesRead); } else { out.push_back(types::Double::Empty()); if (wcReadedStrings) { FREE(wcReadedStrings); } } if (bCloseFile) { mclose(iFileID); } return types::Function::OK; }
/*--------------------------------------------------------------------------*/ int sci_scinotes(char *fname, unsigned long fname_len) { SciErr sciErr; CheckRhs(0, 3); CheckLhs(0, 1); if (Rhs == 0) { try { callSciNotesW(NULL, 0); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); } } else { int m1 = 0, n1 = 0; int *piAddressVarOne = NULL; wchar_t **pStVarOne = NULL; int *lenStVarOne = NULL; int i = 0; int iType1 = 0; char *functionName = NULL; 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; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (iType1 != sci_strings) { Scierror(999, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 1); return 0; } /* get dimensions */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int *)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); return 0; } /* get lengths */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); FREE(lenStVarOne); return 0; } pStVarOne = (wchar_t **) MALLOC(sizeof(wchar_t *) * (m1 * n1)); if (pStVarOne == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); FREE(lenStVarOne); return 0; } for (i = 0; i < m1 * n1; i++) { pStVarOne[i] = (wchar_t *) MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1)); if (pStVarOne[i] == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); for (; i >= 0; i--) { FREE(pStVarOne[i]); } FREE(pStVarOne); FREE(lenStVarOne); return 0; } } /* get strings */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (Rhs >= 2) //get line numbers { int *piAddressVarTwo = NULL; int m2 = 0, n2 = 0; double *pdblVarTwo = NULL; int iType2 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (iType2 != sci_matrix && iType2 != sci_strings) { Scierror(999, _("%s: Wrong type for argument #%d: Real matrix or \'readonly\' expected.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (iType2 == sci_strings) { /* get dimensions */ wchar_t **pStVarTwo = NULL; int *lenStVarTwo = NULL; sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, lenStVarTwo, NULL); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (m2 != 1 || n2 != 1) { Scierror(999, _("%s: Wrong type for argument #%d: Real matrix or \'readonly\' expected.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } lenStVarTwo = (int *)MALLOC(sizeof(int)); if (lenStVarTwo == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } /* get lengths */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, lenStVarTwo, pStVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } pStVarTwo = (wchar_t **) MALLOC(sizeof(wchar_t *)); if (pStVarTwo == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } pStVarTwo[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * (lenStVarTwo[0] + 1)); if (pStVarTwo[0] == NULL) { Scierror(999, _("%s: No more memory.\n"), fname); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } /* get strings */ sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, lenStVarTwo, pStVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } try { callSciNotesWWithOption(pStVarOne, pStVarTwo, m1 * n1); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); FREE(pStVarTwo); FREE(lenStVarTwo); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } freeArrayOfWideString(pStVarTwo, 1); FREE(lenStVarTwo); } else { if (isVarComplex(pvApiCtx, piAddressVarTwo) == 1) { Scierror(999, _("%s: Wrong type for argument #%d: Real matrix expected.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdblVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (m2 * n2 != m1 * n1) { Scierror(999, _("%s: Wrong size for input arguments #%d and #%d: Same dimensions expected.\n"), fname, 1, 2); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } if (Rhs == 3) { int *piAddressVarThree = NULL; sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if (!isStringType(pvApiCtx, piAddressVarThree)) { Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 3); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } int ret = getAllocatedSingleString(pvApiCtx, piAddressVarThree, &functionName); if (ret) { Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 3); freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); return 0; } } try { callSciNotesWWithLineNumberAndFunction(pStVarOne, pdblVarTwo, functionName, m1 * n1); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); } } } else { try { callSciNotesW(pStVarOne, m1 * n1); } catch (GiwsException::JniCallMethodException exception) { Scierror(999, "%s: %s\n", fname, exception.getJavaDescription().c_str()); } catch (GiwsException::JniException exception) { Scierror(999, "%s: %s\n", fname, exception.whatStr().c_str()); } } freeArrayOfWideString(pStVarOne, m1 * n1); FREE(lenStVarOne); if (functionName) { freeAllocatedSingleString(functionName); } } LhsVar(1) = 0; PutLhsVar(); return 0; }
/*--------------------------------------------------------------------------*/ 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; }
/*------------------------------------------------------------------------*/ int sci_strindex(char *fname, unsigned long fname_len) { BOOL bStrindex_with_pattern = FALSE; int outIndex = 0; int numRow = 1; int *next = NULL; CheckRhs(2, 3); CheckLhs(1, 2); if (Rhs == 3) { int m3 = 0; int n3 = 0; char **Strings_Input3 = NULL; int m3n3 = 0; /* m3 * n3 */ if (VarType(3) != sci_strings) { Scierror(999, _("%s: Wrong type for input argument #%d: Character expected.\n"), fname, 3); return 0; } GetRhsVar(3, MATRIX_OF_STRING_DATATYPE, &m3, &n3, &Strings_Input3); m3n3 = m3 * n3; if (m3n3 != 1) { freeArrayOfString(Strings_Input3, m3n3); Scierror(999, _("%s: Wrong type for input argument #%d: Character expected.\n"), fname, 3); return 0; } if ( (strcmp(Strings_Input3[0], CHAR_R) == 0) || (strcmp(Strings_Input3[0], CHAR_S) == 0) ) { if (strcmp(Strings_Input3[0], CHAR_R) == 0) { bStrindex_with_pattern = TRUE; } else { bStrindex_with_pattern = FALSE; } freeArrayOfString(Strings_Input3, m3n3); } else { freeArrayOfString(Strings_Input3, m3n3); Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 3, CHAR_S, CHAR_R); return 0; } } if (VarType(1) == sci_matrix) { int m1 = 0; int n1 = 0; int l1 = 0; GetRhsVar(1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1); if ((m1 == 0) && (n1 == 0)) { CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1); LhsVar(1) = Rhs + 1 ; PutLhsVar(); return 0; } else { Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"), fname, 1); return 0; } } if ( (VarType(1) == sci_strings) && (VarType(2) == sci_strings) ) { int m1 = 0, n1 = 0; char **Strings_Input1 = NULL; wchar_t *wStrings_Input1 = NULL; int m1n1 = 0; /* m1 * n1 */ int m2 = 0, n2 = 0; char **Strings_Input2 = NULL; wchar_t **wStrings_Input2 = NULL; int m2n2 = 0; /* m2 * n2 */ In *values = NULL; int nbValues = 0; int nbposition = 0; int i = 0; GetRhsVar(1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, &Strings_Input1); m1n1 = m1 * n1; if (m1n1 != 1) { freeArrayOfString(Strings_Input1, m1n1); Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1); return 0; } wStrings_Input1 = to_wide_string(Strings_Input1[0]); if (wStrings_Input1 == NULL) { /* string conversion fails */ freeArrayOfString(Strings_Input1, m1n1); freeArrayOfString(Strings_Input2, m2n2); Scierror(999, _("%s: Wrong value for input argument #%d: A valid string expected (UTF-8 Encoding problem).\n"), fname, 1); return 0; } GetRhsVar(2, MATRIX_OF_STRING_DATATYPE, &m2, &n2, &Strings_Input2); m2n2 = m2 * n2; if ( (m2 != 1) && (n2 != 1) ) { freeArrayOfString(Strings_Input1, m1n1); freeArrayOfString(Strings_Input2, m2n2); Scierror(999, _("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"), fname, 2); return 0; } wStrings_Input2 = (wchar_t**)MALLOC(m2n2 * sizeof(wchar_t*)); for (i = 0 ; i < m2n2 ; i++) { wStrings_Input2[i] = to_wide_string(Strings_Input2[i]); } if ( (int)wcslen(wStrings_Input1) == 0 ) { values = (In*)MALLOC(sizeof(In)); } else { values = (In *)MALLOC( sizeof(In) * ( wcslen(wStrings_Input1) ) * m2n2); } if (bStrindex_with_pattern) { int x = 0; pcre_error_code answer = PCRE_FINISHED_OK; int Output_Start = 0; int Output_End = 0; int wcOutput_Start = 0; int wcstart_point = 0; int wcOutput_End = 0; for (x = 0; x < m2n2; ++x) { char *save = strdup(Strings_Input2[x]); if (save) { char *pointer = Strings_Input1[0]; wcstart_point = 0; do { strcpy(save, Strings_Input2[x]); Output_Start = 0; Output_End = 0; answer = pcre_private(pointer, save, &Output_Start, &Output_End, NULL, NULL); if ( answer == PCRE_FINISHED_OK ) { /* Start = End means that we matched a position and 0 characters. * Matching 0 characters, for us, means no match. */ if (Output_Start != Output_End) { char * strOutput_Start = strdup(pointer); char * strOutput_End = strdup(pointer); wchar_t *wcstrOutput_Start = NULL; wchar_t *wcstrOutput_End = NULL; /* calculates positions with wide characters */ strOutput_Start[Output_Start] = '\0'; strOutput_End[Output_End] = '\0'; wcstrOutput_Start = to_wide_string(strOutput_Start); wcstrOutput_End = to_wide_string(strOutput_End); if (wcstrOutput_Start) { wcOutput_Start = (int)wcslen(wcstrOutput_Start); FREE(wcstrOutput_Start); } else { wcOutput_Start = 0; } if (wcstrOutput_End) { wcOutput_End = (int)wcslen(wcstrOutput_End); FREE(wcstrOutput_End); } else { wcOutput_End = 0; } FREE(strOutput_Start); FREE(strOutput_End); /*adding the answer into the outputmatrix*/ values[nbValues].data = wcOutput_Start + wcstart_point + 1; values[nbValues].position = x + 1; nbValues++; } else if (Output_End == 0 && *pointer != '\0') { /* Avoid an infinite loop */ pointer++; } pointer = &pointer[Output_End]; wcstart_point = wcstart_point + wcOutput_End; } else { if (answer != NO_MATCH) { pcre_error(fname, answer); freeArrayOfString(Strings_Input1, m1n1); freeArrayOfString(Strings_Input2, m2n2); return 0; } } } while ( (answer == PCRE_FINISHED_OK) && (*pointer != '\0')); if (save) { FREE(save); save = NULL; } } else { freeArrayOfString(Strings_Input1, m1n1); freeArrayOfString(Strings_Input2, m2n2); Scierror(999, _("%s: No more memory.\n"), fname); return 0; } } sort_inert(values, values + nbValues, cmp); } else { /* We don't use pcre library */ int x = 0; for (x = 0; x < m2n2 ; ++x) { if ( wcslen(wStrings_Input2[x]) == 0 ) { freeArrayOfWideString(wStrings_Input2, m2n2); freeArrayOfString(Strings_Input2, m2n2); freeArrayOfString(Strings_Input1, m1n1); if (next) { FREE(next); next = NULL; } if (values) { FREE(values); values = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: Non-empty string expected.\n"), fname, 2); return 0; } if (Strings_Input2) { wchar_t *pCur = wStrings_Input1; do { pCur = wcsstr(pCur, wStrings_Input2[x]); if (pCur != NULL) { pCur++; values[nbValues++].data = (int)(pCur - wStrings_Input1); values[nbposition++].position = x + 1; } } while (pCur != NULL && *pCur != 0); //Plus tard /* values are sorted */ sort_inert(values, values + nbValues, cmp); } } } FREE(wStrings_Input1); freeArrayOfWideString(wStrings_Input2, m2n2); freeArrayOfString(Strings_Input1, m1n1); freeArrayOfString(Strings_Input2, m2n2); numRow = 1; outIndex = 0; CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &numRow, &nbValues, &outIndex); for ( i = 0 ; i < nbValues ; i++ ) { stk(outIndex)[i] = (double)values[i].data ; } LhsVar(1) = Rhs + 1 ; if (Lhs == 2) { numRow = 1; outIndex = 0; CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &numRow, &nbValues, &outIndex); for ( i = 0 ; i < nbValues ; i++ ) { stk(outIndex)[i] = (double)values[i].position ; } LhsVar(2) = Rhs + 2; } if (values) { FREE(values); values = NULL; } PutLhsVar(); } else { if (VarType(1) != sci_strings) { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1); } else { Scierror(999, _("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"), fname, 2); } return 0; } return 0; }
/*--------------------------------------------------------------------------*/ types::Function::ReturnValue sci_getversion(types::typed_list &in, int _iRetCount, types::typed_list &out) { if (in.size() > 2) { Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "getversion", 0, 2); return types::Function::Error; } if (in.size() == 0) { if (_iRetCount != 1 && _iRetCount != 2) { Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "getveresion", 1, 2); return types::Function::Error; } wchar_t *pwstVer = getScilabVersionAsWideString(); types::String* pOut1 = new types::String(pwstVer); out.push_back(pOut1); FREE(pwstVer); if (_iRetCount == 2) { int iOption = 0; wchar_t** pwstOption = getScilabVersionOptions(&iOption); types::String* pOut2 = new types::String(1, iOption); pOut2->set(pwstOption); out.push_back(pOut2); freeArrayOfWideString(pwstOption, iOption); } } else if (in.size() == 1) { if (in[0]->isString() == false || in[0]->getAs<types::String>()->isScalar() == false) { Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "getversion", 1); return types::Function::Error; } if (_iRetCount != 1) { Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "getveresion", 1); return types::Function::Error; } wchar_t* pwstModule = in[0]->getAs<types::String>()->get()[0]; if (with_module(pwstModule) || (wcscmp(pwstModule, L"scilab") == 0)) { int versionSize = 0; int *version = getModuleVersion(pwstModule, &versionSize); if (version == NULL) { Scierror(999, _("%s: Wrong file version.xml %s.\n"), "getversion", pwstModule); return types::Function::Error; } types::Double* pOut = new types::Double(1, versionSize); pOut->setInt(version); out.push_back(pOut); FREE(version); } } else //in.size() == 2 { if (in[0]->isString() == false || in[0]->getAs<types::String>()->getSize() != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "getversion", 1); return types::Function::Error; } if (in[1]->isString() == false || in[1]->getAs<types::String>()->getSize() != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "getversion", 2); return types::Function::Error; } wchar_t* pwstModule = in[0]->getAs<types::String>()->get()[0]; wchar_t* pwstOption = in[1]->getAs<types::String>()->get()[0]; if ( with_module(pwstModule) || (wcscmp(pwstModule, L"scilab") == 0) ) { if ( wcscmp(pwstOption, VERSION_STRING) == 0) { wchar_t *pwstInfo = getModuleVersionInfoAsString(pwstModule); types::String* pOut = new types::String(pwstInfo); out.push_back(pOut); FREE(pwstInfo); } } } return types::Function::OK; }
/*------------------------------------------------------------------------*/ int sci_strindex(char *fname,unsigned long fname_len) { BOOL bStrindex_with_pattern = FALSE; int outIndex = 0; int numRow = 1; int *next = NULL; CheckRhs(2,3); CheckLhs(1,2); if (Rhs == 3) { int m3 = 0; int n3 = 0; char **Strings_Input3 = NULL; int m3n3 = 0; /* m3 * n3 */ if (VarType(3) != sci_strings) { Scierror(999,_("%s: Wrong type for input argument #%d: Character expected.\n"),fname,3); return 0; } GetRhsVar(3, MATRIX_OF_STRING_DATATYPE, &m3, &n3, &Strings_Input3); m3n3 = m3 * n3; if (m3n3 != 1) { freeArrayOfString(Strings_Input3, m3n3); Scierror(999,_("%s: Wrong type for input argument #%d: Character expected.\n"), fname, 3); return 0; } if ( (strcmp(Strings_Input3[0],CHAR_R) == 0) || (strcmp(Strings_Input3[0],CHAR_S) == 0) ) { if (strcmp(Strings_Input3[0],CHAR_R) == 0) { bStrindex_with_pattern = TRUE; } else { bStrindex_with_pattern = FALSE; } freeArrayOfString(Strings_Input3, m3n3); } else { freeArrayOfString(Strings_Input3, m3n3); Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"),fname,3,CHAR_S,CHAR_R); return 0; } } if (VarType(1) == sci_matrix) { int m1 = 0; int n1 = 0; int l1 = 0; GetRhsVar(1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1); if ((m1 == 0) && (n1 == 0)) { CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1); LhsVar(1) = Rhs+1 ; PutLhsVar(); return 0; } else { Scierror(999,_("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"),fname,3); return 0; } } if ( (VarType(1) == sci_strings) && (VarType(2) == sci_strings) ) { int m1 = 0, n1 = 0; char **Strings_Input1 = NULL; wchar_t *wStrings_Input1 = NULL; int m1n1 = 0; /* m1 * n1 */ int m2 = 0, n2 = 0; char **Strings_Input2 = NULL; wchar_t **wStrings_Input2 = NULL; int m2n2 = 0; /* m2 * n2 */ struct In *values=NULL; int nbValues = 0; int nbposition = 0; int i = 0; GetRhsVar(1,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&Strings_Input1); m1n1 = m1*n1; if (m1n1 != 1) { freeArrayOfString(Strings_Input1,m1n1); Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1); return 0; } wStrings_Input1 = to_wide_string(Strings_Input1[0]); if (wStrings_Input1 == NULL) { /* string conversion fails */ freeArrayOfString(Strings_Input1,m1n1); freeArrayOfString(Strings_Input2,m2n2); Scierror(999,_("%s: Wrong value for input argument #%d: A valid string expected (UTF-8 Encoding problem).\n"),fname,1); return 0; } GetRhsVar(2,MATRIX_OF_STRING_DATATYPE,&m2,&n2,&Strings_Input2); m2n2 = m2*n2; if ( (m2 != 1) && (n2 != 1) ) { freeArrayOfString(Strings_Input1,m1n1); freeArrayOfString(Strings_Input2,m2n2); Scierror(999,_("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"),fname,2); return 0; } wStrings_Input2 = (wchar_t**)MALLOC(m2n2 * sizeof(wchar_t*)); for(i = 0 ; i < m2n2 ; i++) { wStrings_Input2[i] = to_wide_string(Strings_Input2[i]); } if ( (int)wcslen(wStrings_Input1) == 0 ) { values= (struct In*)MALLOC(sizeof(struct In)); } else { values = (struct In *)MALLOC( sizeof(struct In) * ( wcslen(wStrings_Input1) ) * m2n2); } if (bStrindex_with_pattern) { int x = 0; pcre_error_code w = PCRE_FINISHED_OK; int Output_Start = 0; int Output_End = 0; /* We use pcre library */ for (x = 0; x < m2n2; ++x) { w = pcre_private(Strings_Input1[0],Strings_Input2[x],&Output_Start,&Output_End); if ( w == PCRE_FINISHED_OK) { char *partStr = strdup(Strings_Input1[0]); wchar_t *wcpartStr = NULL; partStr[Output_Start] = '\0'; wcpartStr = to_wide_string(partStr); values[nbValues++].data = (int)wcslen(wcpartStr) + 1; /* adding the answer into the outputmatrix */ values[nbposition++].position = x+1; /* The number according to the str2 matrix */ if (partStr) {FREE(partStr); partStr = NULL;} if (wcpartStr) {FREE(wcpartStr); wcpartStr = NULL;} } else { if (w != NO_MATCH) { freeArrayOfString(Strings_Input1,m1n1); freeArrayOfString(Strings_Input2,m2n2); pcre_error(fname,w); return 0; } break; } } qsort(values,nbValues,sizeof(values[0]),cmp); } else { /* We don't use pcre library */ int x = 0; for (x=0; x < m2n2 ;++x) { if ( wcslen(wStrings_Input2[x]) == 0 ) { freeArrayOfString(Strings_Input2,m2n2); freeArrayOfString(Strings_Input1,m1n1); if (next) {FREE(next); next = NULL;} if (values) {FREE(values); values = NULL;} Scierror(999, _("%s: Wrong size for input argument #%d: Non-empty string expected.\n"), fname,2); return 0; } if (Strings_Input2) { wchar_t *pCur = wStrings_Input1; do { pCur = wcsstr(pCur, wStrings_Input2[x]); if (pCur != NULL) { pCur++; values[nbValues++].data = (int)(pCur - wStrings_Input1); values[nbposition++].position = x+1; } } while(pCur != NULL && *pCur != 0);//Plus tard /* values are sorted */ qsort(values,nbValues,sizeof(values[0]),cmp); } } } FREE(wStrings_Input1); freeArrayOfWideString(wStrings_Input2, m2n2); freeArrayOfString(Strings_Input1,m1n1); freeArrayOfString(Strings_Input2,m2n2); numRow = 1; outIndex = 0; CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbValues,&outIndex); for ( i = 0 ; i < nbValues ; i++ ) { stk(outIndex)[i] = (double)values[i].data ; } LhsVar(1) = Rhs+1 ; if (Lhs == 2) { numRow = 1; outIndex = 0; CreateVar(Rhs+2,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbposition,&outIndex); for ( i = 0 ; i < nbposition ; i++ ) { stk(outIndex)[i] = (double)values[i].position ; } LhsVar(2) = Rhs+2; } if (values) {FREE(values); values = NULL;} PutLhsVar(); } else { if(VarType(1) != sci_strings) { Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1); } else { Scierror(999,_("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"),fname,2); } return 0; } return 0; }