Ejemplo n.º 1
0
XMLDocument::XMLDocument(const char *path, bool validate, std::string * error, const char * encoding, const bool html): XMLObject()
{
    char *expandedPath = expandPathVariable(const_cast<char *>(path));
    if (expandedPath)
    {
        if (html)
        {
            document = readHTMLDocument(const_cast<const char *>(expandedPath), encoding, error);
        }
        else
        {
            document = readDocument(const_cast<const char *>(expandedPath), encoding, validate, error);
        }

        FREE(expandedPath);
        if (document)
        {
            openDocs.push_back(this);
            scope->registerPointers(document, this);
        }
    }
    else
    {
        document = 0;
        *error = std::string(gettext("Invalid file name: ")) + std::string(path);
    }

    id = scope->getVariableId(*this);
    scilabType = XMLDOCUMENT;
}
Ejemplo n.º 2
0
XMLDocument::XMLDocument(char *uri, char *version): XMLObject()
{
    char *newUri = 0;
    char *expandedPath = 0;

    if (!version)
    {
        version = const_cast < char *>("1.0");
    }
    document = xmlNewDoc((xmlChar *) version);
    openDocs.push_back(this);
    scope->registerPointers(document, this);
    id = scope->getVariableId(*this);
    scilabType = XMLDOCUMENT;

    expandedPath = expandPathVariable(const_cast < char *>(uri));

    if (expandedPath)
    {
        newUri = (char *)xmlMalloc(sizeof(char) * (strlen(expandedPath) + 1));
        memcpy(newUri, expandedPath, sizeof(char) * (strlen(expandedPath) + 1));
        document->URL = (xmlChar *) newUri;
        FREE(expandedPath);
    }
}
Ejemplo n.º 3
0
/*------------------------------------------------------------------------*/
BOOL HistoryFile::setDefaultFilename(void)
{
    const ScilabPreferences* prefs = getScilabPreferences();
    if (prefs != NULL && prefs->historyFile != NULL)
    {
        const char* prefHistoryFile = prefs->historyFile;
        this->setFilename(expandPathVariable((char*)prefHistoryFile));
        return TRUE;
    }
    else
    {
        std::string filename(DEFAULT_HISTORY_FILE);
        char *SCIHOME = getSCIHOME();
        if (SCIHOME)
        {
            std::string scihome(SCIHOME);
            std::string sep(DIR_SEPARATOR);
            this->setFilename(scihome + sep + filename);
            return TRUE;
        }
        else
        {
            this->setFilename(filename);
            return FALSE;
        }
    }
}
Ejemplo n.º 4
0
/*--------------------------------------------------------------------------*/
static int GetIndexLastModifiedFileInList(char **ListFilename, int numberelemnts)
{
#ifdef _MSC_VER
    struct _stat buf;
#else
    struct stat buf;
#endif

    int i = 0;
    int RetVal = 0;

    int RetIndex = 1;
    long int MaxTime = 0;

    for (i = 0; i < numberelemnts ; i++)
    {

        int resultstat = 0;
        char *FileName = NULL;

        FileName = expandPathVariable(ListFilename[i]);

#ifdef _MSC_VER
        if (FileName)
        {
            if ( (FileName[strlen(FileName) - 1] == '/') || (FileName[strlen(FileName) - 1] == '\\') )
            {
                FileName[strlen(FileName) - 1] = '\0';
            }

        }

        {
            wchar_t *pszFileName = to_wide_string(FileName);
            resultstat = _wstat(pszFileName, &buf );
            FREE(pszFileName);
        }
#else
        resultstat = stat(FileName, &buf );
#endif
        if (resultstat == 0)
        {
            if ((long int)buf.st_mtime > MaxTime)
            {
                MaxTime = (long int)buf.st_mtime;
                RetIndex = i + 1;
            }
        }

        FREE(FileName);
        FileName = NULL;
    }

    RetVal = RetIndex;
    return RetVal;
}
Ejemplo n.º 5
0
/*--------------------------------------------------------------------------*/
int sci_xinit(char * fname, void *pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    char * path = 0;
    char * realPath = 0;

    CheckInputArgument(pvApiCtx, 1, 1);

    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
    if (err.iErr)
    {
        printError(&err, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    if (!isStringType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
    {
        Scierror(999, gettext("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, addr, &path) != 0)
    {
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 0;
    }

    realPath = expandPathVariable(path);

    if (realPath)
    {
        org_scilab_modules_graphic_export::Driver::setPath(getScilabJavaVM(), realPath);
        FREE(realPath);
    }
    else
    {
        Scierror(999, _("%s: Invalid path: %s.\n"), fname, path);
        return 0;
    }

    freeAllocatedSingleString(path);

    LhsVar(1) = 0;
    PutLhsVar();

    return 0;
}
Ejemplo n.º 6
0
void XMLDocument::setDocumentURL(const std::string & url) const
{
    char *expandedPath = 0;
    char *newURL = 0;
    expandedPath = expandPathVariable(const_cast < char *>(url.c_str()));

    if (expandedPath)
    {
        xmlFree((void *)document->URL);
        newURL = (char *)xmlMalloc(sizeof(char) * (strlen(expandedPath) + 1));
        memcpy(newURL, expandedPath, sizeof(char) * (strlen(expandedPath) + 1));
        document->URL = (xmlChar *) newURL;
        FREE(expandedPath);
    }
}
Ejemplo n.º 7
0
XMLValidationRelaxNG::XMLValidationRelaxNG(const char *path, std::string * error): XMLValidation()
{
    char *expandedPath = expandPathVariable(const_cast < char *>(path));
    if (expandedPath)
    {
        xmlRelaxNGParserCtxt *pctxt = xmlRelaxNGNewParserCtxt(expandedPath);
        FREE(expandedPath);
        if (!pctxt)
        {
            if (errorBuffer)
            {
                delete errorBuffer;
            }
            errorBuffer = new std::string(gettext("Cannot create a validation context"));
            *error = *errorBuffer;
        }
        else
        {
            validationFile = (void *)xmlRelaxNGParse(pctxt);
            xmlRelaxNGFreeParserCtxt(pctxt);
            if (!validationFile)
            {
                if (errorBuffer)
                {
                    delete errorBuffer;
                }
                errorBuffer = new std::string(gettext("Cannot parse the Relax NG grammar"));
                *error = *errorBuffer;
            }
            else
            {
                openValidationFiles.push_back(this);
            }
        }
    }
    else
    {
        *error = std::string(gettext("Invalid file name: ")) + std::string(path);
    }

    scope->registerPointers(validationFile, this);
    id = scope->getVariableId(*this);
}
Ejemplo n.º 8
0
/*--------------------------------------------------------------------------*/
int C2F(cluni0)(char *in_name, char *out_name, int *out_n, long int lin, long int lout)
{
    char *expandedVar = NULL;
    in_name[lin] = 0;

    expandedVar = expandPathVariable(in_name);
    if (expandedVar)
    {
        strcpy(out_name, expandedVar);
        FREE(expandedVar);
        expandedVar = NULL;
        *out_n = (int) strlen(out_name);
    }
    else
    {
        strcpy(out_name, in_name);
        *out_n = (int) strlen(out_name);
    }

    return 0;
}
Ejemplo n.º 9
0
/*--------------------------------------------------------------------------*/
int sci_removedir(char *fname,unsigned long l)
{
	CheckRhs(1,1);
	CheckLhs(0,1);

	if (GetType(1) == sci_strings)
	{
		BOOL bOK = FALSE;
		int m1 = 0, n1 = 0, l1 = 0;
		char *expandedpath = NULL;
		char *VarName = NULL;

		GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
		VarName = cstk(l1);

		expandedpath = expandPathVariable(VarName);
		if (expandedpath)
		{
			if ( isdir(expandedpath) )
			{
				bOK = removedir(expandedpath);
			}

			FREE(expandedpath);
			expandedpath = NULL;
		}

		m1 = 1; n1 = 1;
		CreateVar(Rhs+1,MATRIX_OF_BOOLEAN_DATATYPE, &m1, &n1 ,&l1);
		*istk(l1) = bOK;

		LhsVar(1)=Rhs+1;
		PutLhsVar();
	}
	else
	{
		Scierror(999,_("%s: Wrong type for input argument: A string expected.\n"), fname);
	}
	return 0;
}
Ejemplo n.º 10
0
int sci_listvar_in_hdf5_v1(char *fname, int* pvCtx)
{
    SciErr sciErr;
    int *piAddr     = NULL;
    char* pstFile   = NULL;
    int iFile       = 0;
    int iNbItem     = 0;
    VarInfo_v1* pInfo  = NULL;

    CheckInputArgument(pvCtx, 1, 1);
    CheckOutputArgument(pvCtx, 1, 4);

    sciErr = getVarAddressFromPosition(pvCtx, 1, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (getAllocatedSingleString(pvCtx, piAddr, &pstFile))
    {
        if (pstFile)
        {
            FREE(pstFile);
        }

        Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 1);
        return 1;
    }

    char* pstFileName = expandPathVariable(pstFile);
    iFile = openHDF5File(pstFileName, 0);
    if (iFile < 0)
    {
        FREE(pstFileName);
        Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFile);
        FREE(pstFile);
        return 1;
    }
    FREE(pstFileName);
    FREE(pstFile);

    iNbItem = getVariableNames_v1(iFile, NULL);
    if (iNbItem != 0)
    {
        char** pstVarNameList = (char**)MALLOC(sizeof(char*) * iNbItem);
        bool b;
        pInfo = (VarInfo_v1*)MALLOC(iNbItem * sizeof(VarInfo_v1));

        if (nbOutputArgument(pvCtx) == 1)
        {
            sciprint("Name                     Type           Size            Bytes\n");
            sciprint("---------------------------------------------------------------\n");
        }

        iNbItem = getVariableNames_v1(iFile, pstVarNameList);
        for (int i = 0; i < iNbItem; i++)
        {
            int iDataSetId = getDataSetIdFromName_v1(iFile, pstVarNameList[i]);
            if (iDataSetId == 0)
            {
                break;
            }

            strncpy(pInfo[i].varName, pstVarNameList[i], sizeof(pInfo[i].varName));
            b = read_data_v1(pvCtx, iDataSetId, 0, NULL, &pInfo[i]) == false;
            closeDataSet_v1(iDataSetId);

            if (b)
            {
                break;
            }

            if (nbOutputArgument(pvCtx) == 1)
            {
                sciprint("%s\n", pInfo[i].pstInfo);
            }
        }

        freeArrayOfString(pstVarNameList, iNbItem);
    }
    else
    {
        //no variable returms [] for each Lhs
        for (int i = 0 ; i < nbOutputArgument(pvCtx) ; i++)
        {
            createEmptyMatrix(pvCtx, nbInputArgument(pvCtx) + i + 1);
            AssignOutputVariable(pvCtx, i + 1) = nbInputArgument(pvCtx) + i + 1;
        }

        ReturnArguments(pvCtx);
        return 0;
    }

    closeHDF5File(iFile);

    //1st Lhs
    char** pstVarName = (char**)MALLOC(sizeof(char*) * iNbItem);
    for (int i = 0 ; i < iNbItem ; i++)
    {
        pstVarName[i] = pInfo[i].varName;
    }

    sciErr = createMatrixOfString(pvCtx, nbInputArgument(pvCtx) + 1, iNbItem, 1, pstVarName);
    FREE(pstVarName);
    if (sciErr.iErr)
    {
        FREE(pInfo);
        printError(&sciErr, 0);
        return 1;
    }

    AssignOutputVariable(pvCtx, 1) = nbInputArgument(pvCtx) + 1;

    if (nbOutputArgument(pvCtx) > 1)
    {
        //2nd Lhs
        double* pdblType;
        sciErr = allocMatrixOfDouble(pvCtx, nbInputArgument(pvCtx) + 2, iNbItem, 1, &pdblType);
        if (sciErr.iErr)
        {
            FREE(pInfo);
            printError(&sciErr, 0);
            return 1;
        }

        for (int i = 0 ; i < iNbItem ; i++)
        {
            pdblType[i] = pInfo[i].iType;
        }

        AssignOutputVariable(pvCtx, 2) = nbInputArgument(pvCtx) + 2;

        if (nbOutputArgument(pvCtx) > 2)
        {
            //3rd Lhs
            int* pList = NULL;
            sciErr = createList(pvCtx, nbInputArgument(pvCtx) + 3, iNbItem, &pList);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                double* pdblDims = NULL;
                allocMatrixOfDoubleInList(pvCtx, nbInputArgument(pvCtx) + 3, pList, i + 1, 1, pInfo[i].iDims, &pdblDims);
                for (int j = 0 ; j < pInfo[i].iDims ; j++)
                {
                    pdblDims[j] = pInfo[i].piDims[j];
                }
            }

            AssignOutputVariable(pvCtx, 3) = nbInputArgument(pvCtx) + 3;
        }

        if (nbOutputArgument(pvCtx) > 3)
        {
            //4th Lhs
            double* pdblSize;
            sciErr = allocMatrixOfDouble(pvCtx, nbInputArgument(pvCtx) + 4, iNbItem, 1, &pdblSize);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                pdblSize[i] = pInfo[i].iSize;
            }

            AssignOutputVariable(pvCtx, 4) = nbInputArgument(pvCtx) + 4;
        }

    }

    FREE(pInfo);
    ReturnArguments(pvCtx);
    return 0;
}
Ejemplo n.º 11
0
/*--------------------------------------------------------------------------*/
int sci_mgetl(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int numberOfLinesToRead = -1;

    Rhs = Max(0, Rhs);

    CheckRhs(1, 2);
    CheckLhs(1, 1);

    if (Rhs == 2)
    {
        int *piAddressVarTwo = NULL;

        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 ( isDoubleType(pvApiCtx, piAddressVarTwo) )
        {
            double dValue = 0.;
            if (!isScalar(pvApiCtx, piAddressVarTwo))
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: Integer expected.\n"), fname, 2);
                return 0;
            }

            if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dValue) == 0)
            {
                numberOfLinesToRead = (int)dValue;
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: Integer expected.\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;
    }

    if ( isStringType(pvApiCtx, piAddressVarOne) || isDoubleType(pvApiCtx, piAddressVarOne) )
    {
        char **wcReadedStrings = NULL;
        int numberOfLinesReaded = 0;
        int fileDescriptor = -1;
        int iErrorMgetl = 0;
        BOOL bCloseFile = FALSE;

        if (!isScalar(pvApiCtx, piAddressVarOne))
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: String or logical unit expected.\n"), fname, 1);
            return 0;
        }

        if (isStringType(pvApiCtx, piAddressVarOne))
        {
            char *fileName = NULL;
            if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &fileName) == 0)
            {
                char *expandedFileName = expandPathVariable(fileName);
                freeAllocatedSingleString(fileName);
                fileName = NULL;

                if (IsAlreadyOpenedInScilab(expandedFileName))
                {
                    int fd = GetIdFromFilename(expandedFileName);
                    fileDescriptor = fd;
                    if (expandedFileName)
                    {
                        FREE(expandedFileName);
                        expandedFileName = NULL;
                    }
                    bCloseFile = FALSE;
                }
                else
                {
#define READ_ONLY_TEXT_MODE "rt"
                    int fd = 0;
                    int f_swap = 0;
                    double res = 0.0;
                    int ierr = 0;

                    C2F(mopen)(&fd, expandedFileName, READ_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
                    bCloseFile = TRUE;

                    switch (ierr)
                    {
                        case MOPEN_NO_ERROR:
                            fileDescriptor = fd;
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            break;
                        case MOPEN_NO_MORE_LOGICAL_UNIT:
                        {
                            Scierror(66, _("%s: Too many files opened!\n"), fname);
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            return 0;
                        }
                        break;
                        case MOPEN_CAN_NOT_OPEN_FILE:
                        {
                            Scierror(999, _("%s: Cannot open file %s.\n"), fname, expandedFileName);
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            return 0;
                        }
                        break;
                        case MOPEN_NO_MORE_MEMORY:
                        {
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            Scierror(999, _("%s: No more memory.\n"), fname);
                            return 0;
                        }
                        break;
                        case MOPEN_INVALID_FILENAME:
                        {
                            Scierror(999, _("%s: invalid filename %s.\n"), fname, expandedFileName);
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            return 0;
                        }
                        break;
                        case MOPEN_INVALID_STATUS:
                        default:
                        {
                            if (expandedFileName)
                            {
                                FREE(expandedFileName);
                                expandedFileName = NULL;
                            }
                            Scierror(999, _("%s: invalid status.\n"), fname);
                            return 0;
                        }
                        break;
                    }
                }
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }
        else /* double */
        {
            double dValue = 0.;

            if ( !getScalarDouble(pvApiCtx, piAddressVarOne, &dValue) )
            {
                FILE *fd = NULL;
                fileDescriptor = (int)dValue;
                if ((fileDescriptor == STDIN_ID) || (fileDescriptor == STDOUT_ID))
                {
                    SciError(244);
                    return 0;
                }

                fd = GetFileOpenedInScilab(fileDescriptor);
                if (fd == NULL)
                {
                    Scierror(245, _("%s: No input file associated to logical unit %d.\n"), fname, fileDescriptor);
                    return 0;
                }
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }

        wcReadedStrings = mgetl(fileDescriptor, numberOfLinesToRead, &numberOfLinesReaded, &iErrorMgetl);

        if (bCloseFile)
        {
            double dErrClose = 0.;
            C2F(mclose)(&fileDescriptor, &dErrClose);
        }

        switch (iErrorMgetl)
        {
            case MGETL_NO_ERROR:
            {
                if (numberOfLinesReaded == 0)
                {
                    if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
                    {
                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }
                else
                {
                    int m = numberOfLinesReaded;
                    int n = 1;

                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, wcReadedStrings);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        Scierror(17, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }

                freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                wcReadedStrings = NULL;
            }
            break;

            case MGETL_EOF:
            {
                if (numberOfLinesReaded == 0)
                {
                    if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
                    {
                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }
                else
                {
                    int m = numberOfLinesReaded;
                    int n = 1;

                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, wcReadedStrings);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        Scierror(17, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                    wcReadedStrings = NULL;
                }
            }
            break;

            case MGETL_MEMORY_ALLOCATION_ERROR:
            {
                if (wcReadedStrings)
                {
                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                    wcReadedStrings = NULL;
                }
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
            break;

            case MGETL_ERROR:
            {
                if (wcReadedStrings)
                {
                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
                    wcReadedStrings = NULL;
                }
                Scierror(999, _("%s: error.\n"), fname);
                return 0;
            }
            break;
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
    }
    else
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String or logical unit expected.\n"), fname, 1);
    }

    return 0;
}
Ejemplo n.º 12
0
/*--------------------------------------------------------------------------*/
int sci_xls_open(char *fname, unsigned long fname_len)
{
#undef IN
#define max_char_xls_open 256
    int i = 0, m1 = 0, n1 = 0, l1 = 0, l2 = 0, one = 1, fd = 0, f_swap = 0;
    int ierr = 0, ns = 0, result = 0;
    double res;
    char **sst = NULL;
    char **Sheetnames = NULL;
    int *Abspos = NULL;
    int nsheets = 0;
    char *filename_IN = NULL;
    char TMP[max_char_xls_open];

    char sep[2];
    char *TMPDIR = NULL;

#ifdef _MSC_VER
    sep[0] = '\\';
#else
    sep[0] = '/';
#endif
    sep[1] = '\0';

    CheckLhs(4, 4);
    CheckRhs(1, 1);

    if (VarType(1) != sci_strings)
    {
        Scierror(999, "%s: Invalid type of input argument: String expected.", fname);
        return 0;
    }

    /*  checking variable file */
    GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);

    filename_IN = expandPathVariable(cstk(l1));
    if (filename_IN)
    {
        /* bug 5615 */
        /* remove blank characters @ the end */
        int len = (int)strlen(filename_IN);
        int i = 0;

        if (len >= 1)
        {
            for (i = len - 1; i >= 0; i--)
            {
                if (filename_IN[i] == ' ')
                {
                    filename_IN[i] = '\0';
                }
                else
                {
                    break;
                }
            }
        }

        if (!FileExist(filename_IN))
        {
            Scierror(999, _("The file %s does not exist.\n"), filename_IN);
            return 0;
        }
    }

    TMPDIR = getTMPDIR();
    strcpy(TMP, TMPDIR);
    if (TMPDIR)
    {
        FREE(TMPDIR);
        TMPDIR = NULL;
    }

    strcat(TMP, sep);
    strcat(TMP, xls_basename(filename_IN));
    result = ripole(filename_IN, TMP, 0, 0);
    if (result != OLE_OK)
    {
        if (result == OLEER_NO_INPUT_FILE)
        {
            Scierror(999, _("The file %s does not exist.\n"), filename_IN);
        }
        else if (result == OLEER_NOT_OLE_FILE ||
                 result == OLEER_INSANE_OLE_FILE ||
                 result == OLEER_LOADFAT_BAD_BOUNDARY || result == OLEER_MINIFAT_READ_FAIL || result == OLEER_PROPERTIES_READ_FAIL)
        {
            Scierror(999, _("%s: File %s is not an ole2 file.\n"), fname, filename_IN);
            if (filename_IN)
            {
                FREE(filename_IN);
                filename_IN = NULL;
            }
        }
        else if (result == -1)
        {
            Scierror(999, _("%s: Cannot open file %s.\n"), fname, filename_IN);
            if (filename_IN)
            {
                FREE(filename_IN);
                filename_IN = NULL;
            }
        }
        return 0;
    }
    strcat(TMP, sep);
    strcat(TMP, "Workbook");
    C2F(mopen) (&fd, TMP, "rb", &f_swap, &res, &ierr);
    if (ierr != 0)
    {
        Scierror(999, _("%s: There is no xls stream in the ole2 file %s.\n"), fname, filename_IN);
        if (filename_IN)
        {
            FREE(filename_IN);
            filename_IN = NULL;
        }
        return 0;
    }

    if (filename_IN)
    {
        FREE(filename_IN);
        filename_IN = NULL;
    }

    CreateVar(Rhs + 1, MATRIX_OF_INTEGER_DATATYPE, &one, &one, &l2);
    *istk(l2) = fd;             /* logical unit */

    xls_open(&ierr, &fd, &sst, &ns, &Sheetnames, &Abspos, &nsheets);
    /*return *err:
     * 0 = OK
     * 1 = not an OLE file
     * 2 = no Workbook included
     * 3 = memory allocation problem
     * 4 = incorrect file
     * 5 = not a BIFF8 xls file
     */
    switch (ierr)
    {
        case 0:
            /* OK */
            break;

        case 1:
            Scierror(999, _("%s: Not an ole2 file.\n"), fname);
            return 0;

        case 2:
            Scierror(999, _("%s: The file has no Workbook directory.\n"), fname);
            return 0;

        case 3:
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;

        case 4:
            Scierror(990, _("%s: Incorrect or corrupted file.\n"), fname);
            return 0;

        case 5:
            Scierror(999, _("%s: Only BIFF8 file format is handled.\n"), fname);
            return 0;

        default:
            break;

    }

    if (ns != 0)
    {
        /* Create a typed list to return the properties */
        CreateVarFromPtr(Rhs + 2, MATRIX_OF_STRING_DATATYPE, &one, &ns, sst);
        freeArrayOfString(sst, ns);
    }
    else
    {
        CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &ns, &ns, &l2);
    }

    if (nsheets != 0)
    {
        /* Create a typed list to return the properties */
        CreateVarFromPtr(Rhs + 3, MATRIX_OF_STRING_DATATYPE, &one, &nsheets, Sheetnames);
        freeArrayOfString(Sheetnames, nsheets);

        CreateVar(Rhs + 4, MATRIX_OF_DOUBLE_DATATYPE, &one, &nsheets, &l2);
        for (i = 0; i < nsheets; i++)
        {
            *stk(l2 + i) = Abspos[i];
        }
        if (Abspos)
        {
            FREE(Abspos);
            Abspos = NULL;
        }
    }
    else
    {
        CreateVar(Rhs + 3, MATRIX_OF_DOUBLE_DATATYPE, &nsheets, &nsheets, &l2);
        CreateVar(Rhs + 4, MATRIX_OF_DOUBLE_DATATYPE, &nsheets, &nsheets, &l2);
    }

    LhsVar(1) = Rhs + 1;
    LhsVar(2) = Rhs + 2;
    LhsVar(3) = Rhs + 3;
    LhsVar(4) = Rhs + 4;

    PutLhsVar();

    return 0;
}
Ejemplo n.º 13
0
int sci_addlocalizationdomain(char *fname, void* pvApiCtx)
{
    SciErr sciErr;
    int* piAddr1 = NULL;
    char* pstDomain = NULL;

    int* piAddr2 = NULL;
    char* pstPath = NULL;
    char* expandedPath = NULL;

    char* pstRet = NULL;

    int iRhs = nbInputArgument(pvApiCtx);
    int iLhs = nbOutputArgument(pvApiCtx);


    CheckInputArgument(pvApiCtx, 2, 2);
    CheckOutputArgument(pvApiCtx, 1, 1);

    //get domain name
    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), "addlocalizationdomain", 1);
        return 0;
    }

    if (isStringType(pvApiCtx, piAddr1) == 0 || isScalar(pvApiCtx, piAddr1) == 0)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "addlocalizationdomain", 1);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddr1, &pstDomain))
    {
        Scierror(999, _("%s: Memory allocation error.\n"), "addlocalizationdomain");
        return 0;
    }

    //get domain path
    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), "addlocalizationdomain", 2);
        return 0;
    }

    if (isStringType(pvApiCtx, piAddr2) == 0 || isScalar(pvApiCtx, piAddr2) == 0)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "addlocalizationdomain", 2);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddr2, &pstPath))
    {
        Scierror(999, _("%s: Memory allocation error.\n"), "addlocalizationdomain");
        return 0;
    }

    expandedPath = expandPathVariable(pstPath);
    pstRet = bindtextdomain(pstDomain, expandedPath);

    freeAllocatedSingleString(pstPath);
    FREE(expandedPath);

    if (pstRet == NULL)
    {
        Scierror(999, _("%s: Unable to add new domain %s.\n"), "addlocalizationdomain", pstDomain);
        freeAllocatedSingleString(pstDomain);
        return 0;
    }

    bind_textdomain_codeset (pstDomain, "UTF-8"); /*such that gettext and dgettext return UTF8 string*/

    if (createScalarBoolean(pvApiCtx, iRhs + 1, 1))
    {
        Scierror(999, _("%s: Unable to add new domain %s.\n"), "addlocalizationdomain", pstDomain);
        freeAllocatedSingleString(pstDomain);
        return 0;
    }

    freeAllocatedSingleString(pstDomain);
    AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
    ReturnArguments(pvApiCtx);
    return 0;
}
Ejemplo n.º 14
0
int sci_hdf5_load_v1(char *fn, int* pvApiCtx)
{
    SciErr sciErr;

    int* piAddr = NULL;
    char* pstFilename = NULL;
    char* pstExpandedFilename = NULL;
    bool bImport = true;

    const int nbIn = Rhs;
    int iSelectedVar = Rhs - 1;

    CheckInputArgumentAtLeast(pvApiCtx, 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

    iCloseList = 0;

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFilename))
    {
        if (pstFilename)
        {
            FREE(pstFilename);
        }

        Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), 2);
        return 1;
    }

    //open hdf5 file
    pstExpandedFilename = expandPathVariable(pstFilename);
    int iFile = openHDF5File(pstExpandedFilename, 0);
    if (iFile < 0)
    {
        FREE(pstExpandedFilename);
        Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), pstFilename);
        FREE(pstFilename);
        return 1;
    }

    FREE(pstExpandedFilename);
    FREE(pstFilename);

    std::vector<wchar_t*> varList;
    if (iSelectedVar)
    {
        //selected variable
        char* pstVarName = NULL;
        for (int i = 0 ; i < iSelectedVar ; i++)
        {
            sciErr = getVarAddressFromPosition(pvApiCtx, i + 2, &piAddr);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 1;
            }

            if (getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName))
            {
                if (pstVarName)
                {
                    FREE(pstVarName);
                }

                Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), i + 1);
                return 1;
            }

            if (import_variable_v1(pvApiCtx, iFile, pstVarName) == false)
            {
                FREE(pstVarName);
                bImport = false;
                break;
            }

            varList.push_back(to_wide_string(pstVarName));
            FREE(pstVarName);
            pstVarName = NULL;
        }
    }
    else
    {
        //all variables
        int iNbItem = 0;
        iNbItem = getVariableNames_v1(iFile, NULL);
        if (iNbItem != 0)
        {
            char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem);

            iNbItem = getVariableNames_v1(iFile, pstVarNameList);

            //import all data
            for (int i = 0; i < iNbItem; i++)
            {
                if (import_variable_v1(pvApiCtx, iFile, pstVarNameList[i]) == false)
                {
                    bImport = false;
                    break;
                }

                varList.push_back(to_wide_string(pstVarNameList[i]));
            }

            freeArrayOfString(pstVarNameList, iNbItem);
        }
    }
    //close the file
    closeHDF5File(iFile);

    if (bImport == true && varList.size() != 0)
    {
        createMatrixOfWideString(pvApiCtx, nbIn + 1, 1, static_cast<int>(varList.size()), varList.data());
    }
    else
    {
        createEmptyMatrix(pvApiCtx, nbIn + 1);
    }

    for (auto & i : varList)
    {
        FREE(i);
    }

    AssignOutputVariable(pvApiCtx, 1) = nbIn + 1;
    ReturnArguments(pvApiCtx);

    //  printf("End gateway !!!\n");
    return 0;
}
Ejemplo n.º 15
0
/*--------------------------------------------------------------------------*/ 
int sci_mputl(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int *piAddressVarTwo = NULL;

    char **pStVarOne			= NULL;
    int *lenStVarOne			= NULL;
    int mOne = 0, nOne = 0;
    int mnOne = 0;

    char *filename = NULL;
    int fileDescriptor = -1;
    BOOL bCloseFile = FALSE;

    int i = 0;
    int mputlErr = MPUTL_ERROR;

    if (Rhs != 2)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
        return 0;
    }

    if (Lhs != 1)
    {
        Scierror(999, _("%s: Wrong number of output arguments: %d 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 ( isDoubleType(pvApiCtx, piAddressVarTwo) )
    {
        double dValue = 0.;

        if (!isScalar(pvApiCtx, piAddressVarTwo))
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: Integer expected.\n"), fname, 2);
            return 0;
        }

        if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dValue) == 0)
        {
            fileDescriptor = (int)dValue;
        }
        else
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    } 
    else if ( isStringType(pvApiCtx, piAddressVarTwo) )
    {
        if (!isScalar(pvApiCtx, piAddressVarTwo))
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 2);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &filename) == 0)
        {
            #define WRITE_ONLY_TEXT_MODE "wt"
            int f_swap = 0;
            double res = 0.0;
            int ierr = 0;
            char *expandedFileName = expandPathVariable(filename);
            
            C2F(mopen)(&fileDescriptor, expandedFileName, WRITE_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
            if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}

            switch (ierr)
            {
            case MOPEN_NO_ERROR:
                bCloseFile = TRUE;
                break;
            case MOPEN_NO_MORE_LOGICAL_UNIT:
                {
                    freeAllocatedSingleString(filename);
                    Scierror(66, _("%s: Too many files opened!\n"), fname);
                    return 0;
                }
                break;
            case MOPEN_CAN_NOT_OPEN_FILE:
                {
                    Scierror(999, _("%s: Cannot open file %s.\n"), fname, filename);
                    freeAllocatedSingleString(filename);
                    return 0;
                }
                break;
            case MOPEN_NO_MORE_MEMORY:
                {
                    freeAllocatedSingleString(filename);
                    Scierror(999, _("%s: No more memory.\n"), fname);
                    return 0;
                }
                break;
            case MOPEN_INVALID_FILENAME:
                {
                    if (filename)
                    {
                        Scierror(999, _("%s: invalid filename %s.\n"), fname, filename);
                    }
                    else
                    {
                        freeAllocatedSingleString(filename);
                        Scierror(999, _("%s: invalid filename.\n"), fname);
                    }
                    return 0;
                }
                break;
            case MOPEN_INVALID_STATUS: default:
                {
                    freeAllocatedSingleString(filename);
                    Scierror(999, _("%s: invalid status.\n"), fname);
                    return 0;
                }
                break;
            }
            bCloseFile = TRUE;
            freeAllocatedSingleString(filename);
        }
        else
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else
    {
        Scierror(999,_("%s: Wrong type for input argument #%d: a String or Integer expected.\n"), fname, 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: String expected.\n"), fname, 1);
        return 0;
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne,&mOne, &nOne, NULL, NULL);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if ( !((mOne == 1) || (nOne == 1)) )
    {
        Scierror(999,_("%s: Wrong size for input argument #%d: A 1-by-n or m-by-1 array expected.\n"), fname, 1);
        return 0;
    }

    mnOne = mOne * nOne;

    lenStVarOne = (int*)MALLOC(sizeof(int) * mnOne);
    if (lenStVarOne == NULL)
    {
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 0;
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne,&mOne, &nOne, lenStVarOne, NULL);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    pStVarOne = (char**) MALLOC(sizeof(char*) * mnOne);
    if (pStVarOne == NULL)
    {
        FREE(lenStVarOne); lenStVarOne = NULL;
        Scierror(999, _("%s: No more memory.\n"), fname);
        return 0;
    }

    for (i = 0; i < mnOne; i++)
    {
        pStVarOne[i] = (char*)MALLOC(sizeof(char) * (lenStVarOne[i] + 1));
        if (pStVarOne[i] == NULL)
        {
            freeArrayOfString(pStVarOne, i);
            if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }
    }

    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, pStVarOne);
    if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
    if(sciErr.iErr)
    {
        freeArrayOfString(pStVarOne, mnOne);
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    mputlErr = mputl(fileDescriptor, pStVarOne, mnOne);
    freeArrayOfString(pStVarOne, mnOne);

    if (bCloseFile)
    {
        double dErrClose = 0.;
        C2F(mclose)(&fileDescriptor, &dErrClose);
        bCloseFile = FALSE;
    }

    switch (mputlErr)
    {
    case MPUTL_NO_ERROR:
        createScalarBoolean(pvApiCtx, Rhs + 1, TRUE);
        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        break;

    case MPUTL_INVALID_FILE_DESCRIPTOR:
        // commented for compatiblity
        // Scierror(999, _("%s: invalid file descriptor.\n"), fname);
        // break;
    case MPUTL_ERROR:
    case MPUTL_NO_WRITE_RIGHT:
    default:
        createScalarBoolean(pvApiCtx, Rhs + 1, FALSE);
        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        break;
    }

    return 0;
}
Ejemplo n.º 16
0
/*--------------------------------------------------------------------------*/
int sci_export_to_hdf5(char *fname, unsigned long fname_len)
{
    int iNbVar          = 0;
    int** piAddrList    = NULL;
    char** pstNameList  = NULL;
    char *pstFileName   = NULL;
    bool bExport        = false;
    bool bAppendMode    = false;

    SciErr sciErr;

    int iRhs = nbInputArgument(pvApiCtx);
    CheckInputArgumentAtLeast(pvApiCtx, 1);
    CheckOutputArgument(pvApiCtx, 0, 1);

    pstNameList = (char**)MALLOC(sizeof(char*) * iRhs);
    iNbVar = extractVarNameList(1, iRhs, pstNameList);
    if (iNbVar == 0)
    {
        FREE(pstNameList);
        return 1;
    }

    piAddrList = (int**)MALLOC(sizeof(int*) * (iNbVar));
    for (int i = 1 ; i < iRhs ; i++)
    {
        if (strcmp(pstNameList[i], "-append") == 0)
        {
            bAppendMode = true;
        }
        else
        {
            sciErr = getVarAddressFromName(pvApiCtx, pstNameList[i], &piAddrList[i]);
            if (sciErr.iErr)
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: Defined variable expected.\n"), fname, i + 1);
                printError(&sciErr, 0);
                return 1;
            }
        }
    }

    iLevel = 0;
    // open hdf5 file
    pstFileName = expandPathVariable(pstNameList[0]);
    int iH5File = 0;
    if (bAppendMode)
    {
        iH5File = openHDF5File(pstFileName, bAppendMode);
        if (iH5File < 0)
        {
            iH5File = createHDF5File(pstFileName);
        }
    }
    else
    {
        iH5File = createHDF5File(pstFileName);
    }


    if (iH5File < 0)
    {
        FREE(pstFileName);
        if (iH5File == -2)
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: \"%s\" is a directory"), fname, 1, pstNameList[0]);
        }
        else
        {
            Scierror(999, _("%s: Cannot open file %s.\n"), fname, pstNameList[0]);
        }

        return 1;
    }

    if (bAppendMode)
    {
        int iVersion = getSODFormatAttribute(iH5File);
        if (iVersion != -1 && iVersion != SOD_FILE_VERSION)
        {
            //to update version must be the same
            closeHDF5File(iH5File);
            Scierror(999, _("%s: Wrong SOD file format version. Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion);
            return 1;
        }
    }

    // export data
    for (int i = 1 ; i < iRhs ; i++)
    {
        if (strcmp(pstNameList[i], "-append") == 0)
        {
            continue;
        }

        if (isVarExist(iH5File, pstNameList[i]))
        {
            if (bAppendMode)
            {
                if (deleteHDF5Var(iH5File, pstNameList[i]))
                {
                    closeHDF5File(iH5File);
                    Scierror(999, _("%s: Unable to delete existing variable \"%s\"."), fname, pstNameList[i]);
                    return 1;
                }
            }
            else
            {
                closeHDF5File(iH5File);
                Scierror(999, _("%s: Variable \'%s\' already exists in file \'%s\'\nUse -append option to replace existing variable\n."), fname, pstNameList[i], pstNameList[0]);
                return 1;
            }
        }

        bExport = export_data(iH5File, piAddrList[i], pstNameList[i]);
        if (bExport == false)
        {
            break;
        }
    }

    if (bExport && iRhs != 1)
    {
        //add or update scilab version and file version in hdf5 file
        if (updateScilabVersion(iH5File) < 0)
        {
            closeHDF5File(iH5File);
            Scierror(999, _("%s: Unable to update Scilab version in \"%s\"."), fname, pstNameList[0]);
            return 1;
        }

        if (updateFileVersion(iH5File) < 0)
        {
            closeHDF5File(iH5File);
            Scierror(999, _("%s: Unable to update HDF5 format version in \"%s\"."), fname, pstNameList[0]);
            return 1;
        }
    }

    //close hdf5 file
    closeHDF5File(iH5File);

    //delete file in case of error but nor in append mode
    if (bExport == false && bAppendMode == false && iRhs != 1)
    {
        //remove file
        deleteafile(pstFileName);
    }
    FREE(pstFileName);

    //create boolean return value
    int *piReturn = NULL;
    sciErr = allocMatrixOfBoolean(pvApiCtx, iRhs + 1, 1, 1, &piReturn);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (bExport == true || iRhs == 1)
    {
        piReturn[0] = 1;
    }
    else
    {
        piReturn[0] = 0;
    }


    //free memory
    for (int i = 0 ; i < iRhs ; i++)
    {
        FREE(pstNameList[i]);
    }
    FREE(pstNameList);

    FREE(piAddrList);

    LhsVar(1) = iRhs + 1;
    PutLhsVar();
    return 0;
}
Ejemplo n.º 17
0
int sci_import_from_hdf5(char *fname, unsigned long fname_len)
{
    SciErr sciErr;

    int* piAddr = NULL;
    char* pstFilename = NULL;
    char* pstExpandedFilename = NULL;
    bool bImport = true;

    int iSelectedVar = Rhs - 1;

    CheckInputArgumentAtLeast(pvApiCtx, 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFilename))
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
        return 1;
    }

    //open hdf5 file
    pstExpandedFilename = expandPathVariable(pstFilename);
    int iFile = openHDF5File(pstExpandedFilename, 0);
    if (iFile < 0)
    {
        Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFilename);
        FREE(pstExpandedFilename);
        FREE(pstFilename);
        return 1;
    }

    FREE(pstExpandedFilename);
    FREE(pstFilename);

    //manage version information
    int iVersion = getSODFormatAttribute(iFile);
    if (iVersion != SOD_FILE_VERSION)
    {
        if (iVersion > SOD_FILE_VERSION)
        {
            //can't read file with version newer that me !
            Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion);
            return 1;
        }
        else
        {
            //call older import functions and exit or ... EXIT !
            if (iVersion == 1 || iVersion == -1)
            {
                //sciprint("old sci_import_from_hdf5_v1\n");
                return sci_import_from_hdf5_v1(fname, fname_len);
            }
        }
    }

    if (iSelectedVar)
    {
        //selected variable
        char* pstVarName = NULL;
        for (int i = 0 ; i < iSelectedVar ; i++)
        {
            sciErr = getVarAddressFromPosition(pvApiCtx, i + 2, &piAddr);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 1;
            }

            if (getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName))
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, i + 1);
                return 1;
            }

            if (import_variable(iFile, pstVarName) == false)
            {
                bImport = false;
                break;
            }

            FREE(pstVarName);
        }
    }
    else
    {
        //all variables
        int iNbItem = 0;
        iNbItem = getVariableNames(iFile, NULL);
        if (iNbItem != 0)
        {
            char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem);

            iNbItem = getVariableNames(iFile, pstVarNameList);

            //import all data
            for (int i = 0; i < iNbItem; i++)
            {
                if (import_variable(iFile, pstVarNameList[i]) == false)
                {
                    bImport = false;
                    break;
                }
            }
        }
    }
    //close the file
    closeHDF5File(iFile);

    int *piReturn = NULL;

    sciErr = allocMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, 1, &piReturn);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (bImport == true)
    {
        piReturn[0] = 1;
    }
    else
    {
        piReturn[0] = 0;
    }

    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
    ReturnArguments(pvApiCtx);

    //  printf("End gateway !!!\n");
    return 0;
}
Ejemplo n.º 18
0
int sci_hdf5_listvar_v2(char *fname, int* pvApiCtx)
{
    SciErr sciErr;
    int *piAddr     = NULL;
    char* pstFile   = NULL;
    int iFile       = 0;
    int iNbItem     = 0;
    VarInfo* pInfo  = NULL;
    const int nbIn = nbInputArgument(pvApiCtx);

    CheckInputArgument(pvApiCtx, 1, 1);
    CheckOutputArgument(pvApiCtx, 1, 4);

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFile))
    {
        if (pstFile)
        {
            FREE(pstFile);
        }

        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
        return 1;
    }

    char* pstFileName = expandPathVariable(pstFile);
    iFile = openHDF5File(pstFileName, 0);
    if (iFile < 0)
    {
        Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFile);
        FREE(pstFileName);
        FREE(pstFile);
        return 1;
    }

    FREE(pstFileName);
    FREE(pstFile);

    //manage version information
    int iVersion = getSODFormatAttribute(iFile);
    if (iVersion != SOD_FILE_VERSION)
    {
        if (iVersion > SOD_FILE_VERSION)
        {
            //can't read file with version newer that me !
            closeHDF5File(iFile);
            Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion);
            return 1;
        }
        else
        {
            //call older import functions and exit or ... EXIT !
            if (iVersion == 1 || iVersion == -1)
            {
                //sciprint("old sci_listvar_in_hdf5_v1\n");
                return sci_listvar_in_hdf5_v1(fname, pvApiCtx);
            }
        }
    }

    iNbItem = getVariableNames(iFile, NULL);
    if (iNbItem != 0)
    {
        char** pstVarNameList = (char**)MALLOC(sizeof(char*) * iNbItem);
        pInfo = (VarInfo*)MALLOC(iNbItem * sizeof(VarInfo));
        int b;

        if (Lhs == 1)
        {
            sciprint("Name                     Type           Size            Bytes\n");
            sciprint("---------------------------------------------------------------\n");
        }

        iNbItem = getVariableNames(iFile, pstVarNameList);
        for (int i = 0; i < iNbItem; i++)
        {
            int iDataSetId = getDataSetIdFromName(iFile, pstVarNameList[i]);
            if (iDataSetId == 0)
            {
                break;
            }

            strncpy(pInfo[i].varName, pstVarNameList[i], sizeof(pInfo[i].varName) - 1);
            pInfo[i].iSize = 0;
            b = read_data(iDataSetId, 0, NULL, &pInfo[i]) == false;
            if (b)
            {
                break;
            }

            if (Lhs == 1)
            {
                sciprint("%s\n", pInfo[i].pstInfo);
            }
        }

        freeArrayOfString(pstVarNameList, iNbItem);
    }
    else
    {
        //no variable returms [] for each Lhs
        for (int i = 0 ; i < Lhs ; i++)
        {
            createEmptyMatrix(pvApiCtx, nbIn + i + 1);
            AssignOutputVariable(pvApiCtx, i + 1) = nbIn + i + 1;
        }

        ReturnArguments(pvApiCtx);
        return 0;
    }

    closeHDF5File(iFile);

    //1st Lhs
    char** pstVarName = (char**)MALLOC(sizeof(char*) * iNbItem);
    for (int i = 0 ; i < iNbItem ; i++)
    {
        pstVarName[i] = pInfo[i].varName;
    }

    sciErr = createMatrixOfString(pvApiCtx, nbIn + 1, iNbItem, 1, pstVarName);
    FREE(pstVarName);
    if (sciErr.iErr)
    {
        FREE(pInfo);
        printError(&sciErr, 0);
        return 1;
    }

    AssignOutputVariable(pvApiCtx, 1) = nbIn + 1;

    if (Lhs > 1)
    {
        //2nd Lhs
        double* pdblType;
        sciErr = allocMatrixOfDouble(pvApiCtx, nbIn + 2, iNbItem, 1, &pdblType);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            FREE(pInfo);
            return 1;
        }

        for (int i = 0 ; i < iNbItem ; i++)
        {
            pdblType[i] = pInfo[i].iType;
        }

        AssignOutputVariable(pvApiCtx, 2) = nbIn + 2;

        if (Lhs > 2)
        {
            //3rd Lhs
            int* pList = NULL;
            sciErr = createList(pvApiCtx, nbIn + 3, iNbItem, &pList);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                double* pdblDims = NULL;
                allocMatrixOfDoubleInList(pvApiCtx, nbIn + 3, pList, i + 1, 1, pInfo[i].iDims, &pdblDims);
                for (int j = 0 ; j < pInfo[i].iDims ; j++)
                {
                    pdblDims[j] = pInfo[i].piDims[j];
                }
            }

            AssignOutputVariable(pvApiCtx, 3) = nbIn + 3;
        }

        if (Lhs > 3)
        {
            //4th Lhs
            double* pdblSize = NULL;
            sciErr = allocMatrixOfDouble(pvApiCtx, nbIn + 4, iNbItem, 1, &pdblSize);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                pdblSize[i] = pInfo[i].iSize;
            }

            AssignOutputVariable(pvApiCtx, 4) = nbIn + 4;
        }

    }

    FREE(pInfo);
    ReturnArguments(pvApiCtx);
    return 0;
}
Ejemplo n.º 19
0
/*--------------------------------------------------------------------------*/
int sci_fprintfMat(char *fname,unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int m1 = 0, n1 = 0;
    int iType1 = 0;

    int *piAddressVarTwo = NULL;
    int m2 = 0, n2 = 0;
    int iType2 = 0;

    fprintfMatError ierr = FPRINTFMAT_ERROR;

    char *filename = NULL;
    char *expandedFilename = NULL;
    char **textAdded = NULL;
    char *Format = NULL;
    double *dValues = NULL;
    char *separator = NULL;
    int m4n4 = 0;
    int i = 0;

    Nbvars = 0;
    CheckRhs(1,5);
    CheckLhs(1,1);

    if (Rhs >= 3)
    {
        int *piAddressVarThree = 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_strings)
        {
            Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3);
            return 0;
        }

        sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);

        if ( (m3 != n3) && (n3 != 1) ) 
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &Format))
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else
    {
        Format = strdup(DEFAULT_FPRINTFMAT_FORMAT);
    }

    if ( Rhs >= 4 )
    {
        int *piAddressVarFour = NULL;
        int *lengthStrings = NULL;
        int iType4	= 0;
        int m4 = 0, n4 = 0;

        sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
        if(sciErr.iErr)
        {
            if (Format) {FREE(Format); Format = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        sciErr = getVarType(pvApiCtx, piAddressVarFour, &iType4);
        if(sciErr.iErr)
        {
            if (Format) {FREE(Format); Format = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        if (iType4 != sci_strings)
        {
            if (Format) {FREE(Format); Format = NULL;}
            Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 4);
            return 0;
        }

        sciErr = getVarDimension(pvApiCtx, piAddressVarFour, &m4, &n4);
        if(sciErr.iErr)
        {
            if (Format) {FREE(Format); Format = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        if  (! ((m4 == 1) || (n4 == 1)))
        {
            if (Format) {FREE(Format); Format = NULL;}
            Scierror(999,_("%s: Wrong size for input argument #%d.\n"), fname, 4);
            return 0;
        }

        lengthStrings = (int*)MALLOC(sizeof(int) * (m4 * n4));
        if (lengthStrings == NULL)
        {
            if (Format) {FREE(Format); Format = NULL;}
            Scierror(999,_("%s: Memory allocation error.\n"),fname);
            return 0;
        }

        // get lengthStrings value
        sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &m4, &n4, lengthStrings, NULL);
        if(sciErr.iErr)
        {
            if (Format) {FREE(Format); Format = NULL;}
            if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        textAdded = (char**)MALLOC(sizeof(char*) * (m4 * n4));
        if (textAdded == NULL)
        {
            if (Format) {FREE(Format); Format = NULL;}
            if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
            Scierror(999,_("%s: Memory allocation error.\n"),fname);
            return 0;
        }

        for (i = 0; i < (m4 * n4); i++)
        {
            textAdded[i] = (char*)MALLOC(sizeof(char) * (lengthStrings[i] + 1));
            if (textAdded[i] == NULL)
            {
                freeArrayOfString(textAdded, m4 * n4);
                if (Format) {FREE(Format); Format = NULL;}
                if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
                Scierror(999,_("%s: Memory allocation error.\n"),fname);
                return 0;
            }
        }

        // get textAdded
        sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &m4, &n4, lengthStrings, textAdded);
        if (lengthStrings) {FREE(lengthStrings); lengthStrings = NULL;}
        if(sciErr.iErr)
        {
            freeArrayOfString(textAdded, m4 * n4);
            if (Format) {FREE(Format); Format = NULL;}
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
            return 0;
        }

        m4n4 = m4 * n4;
    }

    if (Rhs > 4)
    {
        int *piAddressVarFive = NULL;
        int iType5	= 0;
        int m5 = 0, n5 = 0;

        sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddressVarFive);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 5);
            return 0;
        }

        sciErr = getVarType(pvApiCtx, piAddressVarFive, &iType5);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 5);
            return 0;
        }

        if (iType5 != sci_strings)
        {
            Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 5);
            return 0;
        }

        sciErr = getVarDimension(pvApiCtx, piAddressVarFive, &m5, &n5);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 5);
            return 0;
        }

        if ( (m5 != n5) && (n5 != 1) ) 
        {
            Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 5);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarFive, &separator))
        {
            Scierror(999,_("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else
    {
        separator = strdup(DEFAULT_FPRINTFMAT_SEPARATOR);
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
    if(sciErr.iErr)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        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)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 0;
    }

    if (iType2 != sci_matrix)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong type for input argument #%d: Matrix of floating point numbers expected.\n"), fname, 2);
        return 0;
    }

    if (isVarComplex(pvApiCtx, piAddressVarTwo))
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong type for input argument #%d: Real values expected.\n"), fname, 2);
        return 0;
    }

    sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &dValues);
    if(sciErr.iErr)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        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)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        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)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    if (iType1 != sci_strings)
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
        return 0;
    }

    sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);

    if ( (m1 != n1) && (n1 != 1) ) 
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename))
    {
        if (textAdded) freeArrayOfString(textAdded, m4n4);
        if (Format) {FREE(Format); Format = NULL;}
        if (separator){FREE(separator); separator = NULL;}
        Scierror(999,_("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    expandedFilename = expandPathVariable(filename);
    ierr = fprintfMat(expandedFilename, Format, separator, dValues, m2, n2,textAdded, m4n4);
    if (expandedFilename) {FREE(expandedFilename); expandedFilename = NULL;}
    if (textAdded) freeArrayOfString(textAdded, m4n4);
    if (Format) {FREE(Format); Format = NULL;}
    if (separator){FREE(separator); separator = NULL;}

    switch(ierr)
    {
    case FPRINTFMAT_NO_ERROR:
        {
            LhsVar(1) = 0;
            if (filename) {FREE(filename); filename = NULL;}
            PutLhsVar();
        }
        break;
    case FPRINTFMAT_FOPEN_ERROR:
        {
            Scierror(999,_("%s: can not open file %s.\n"), fname, filename);
        }
        break;
    case FPRINTMAT_FORMAT_ERROR:
        {
            Scierror(999,_("%s: Invalid format.\n"), fname);
        }
        break;
    default:
    case FPRINTFMAT_ERROR:
        {
            Scierror(999,_("%s: error.\n"), fname);
        }
        break;
    }

    if (filename) {FREE(filename); filename = NULL;}
    return 0;
}
Ejemplo n.º 20
0
int ScilabGateway::addToClasspath(char * fname, const int envId, void * pvApiCtx)
{
    SciErr err;
    int * addr = 0;
    int rows;
    int cols;
    char ** className = 0;

    CheckInputArgumentAtLeast(pvApiCtx, 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

    ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
    ScilabGatewayOptions & options = env.getGatewayOptions();
    OptionsHelper::setCopyOccurred(false);
    ScilabObjects::initialization(env, pvApiCtx);
    options.setIsNew(false);

    for (int i = 1; i <= Rhs; i++)
    {
        err = getVarAddressFromPosition(pvApiCtx, i, &addr);
        if (err.iErr)
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        if (!isStringType(pvApiCtx, addr))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for argument #%d: string expected."), 1);
        }

        if (getAllocatedMatrixOfString(pvApiCtx, addr, &rows, &cols, &className))
        {
            throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
        }

        for (int j = 0; j < rows * cols; j++)
        {
            char * expandedPath = expandPathVariable(const_cast<char *>(className[j]));
            if (expandedPath)
            {
                try
                {
                    env.addtoclasspath(expandedPath);
                }
                catch (std::exception & /*e*/)
                {
                    FREE(expandedPath);
                    freeAllocatedMatrixOfString(rows, cols, className);
                    throw;
                }
                FREE(expandedPath);
            }
            else
            {
                std::string str(className[j]);
                freeAllocatedMatrixOfString(rows, cols, className);
                throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Cannot open the given file %s."), str.c_str());
            }
        }

        freeAllocatedMatrixOfString(rows, cols, className);
        className = 0;
    }

    LhsVar(1) = 0;
    PutLhsVar();

    return 0;
}
Ejemplo n.º 21
0
// =============================================================================
csvResult* csvRead(const char *filename, const char *separator, const char *decimal, const char **toreplace, int sizetoreplace, const char *regexpcomments)
{
    char *expandedFilename = NULL;
    csvResult *result = NULL;
    int fd = 0;
    int f_swap = 0;
    double res = 0.0;
    int errMOPEN = MOPEN_INVALID_STATUS;
    int errMGETL = MGETL_ERROR;
    double dErrClose = 0.;
    char **lines = NULL;
    int nblines = 0;
    char **replacedInLines = NULL;
    char **pComments = NULL;
    int nbComments = 0;

    if ((filename == NULL) || (separator == NULL) || (decimal == NULL))
    {
        return NULL;
    }

    expandedFilename = expandPathVariable((char*)filename);
    if (!FileExist(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;
        }
        if (expandedFilename)
        {
            FREE(expandedFilename);
            expandedFilename = NULL;
        }
        return result;
    }

    C2F(mopen)(&fd, expandedFilename, (char*)READ_ONLY_TEXT_MODE, &f_swap, &res, &errMOPEN);
    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;
    }

    lines = mgetl(fd, -1, &nblines, &errMGETL);

    C2F(mclose)(&fd, &dErrClose);

    if (errMGETL != MGETL_NO_ERROR)
    {
        if (lines)
        {
            freeArrayOfString(lines, nblines);
            lines = 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**)lines, nblines, (const char*)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**)lines, nblines, (const char*)regexpcomments, &nbCleanedLines, &iErr);
            if (pCleanedLines)
            {
                FREE(lines);
                lines = pCleanedLines;
                nblines = nbCleanedLines;
            }

        }
    }

    if (toreplace && (sizetoreplace > 0))
    {
        replacedInLines = replaceStrings((const char**)lines, nblines, toreplace, sizetoreplace);
        if (replacedInLines)
        {
            freeArrayOfString(lines, nblines);
            lines = replacedInLines;
        }
    }

    result = csvTextScan((const char**)lines, nblines, (const char*)separator, (const char*)decimal);
    if (lines)
    {
        freeArrayOfString(lines, nblines);
        lines = NULL;
    }

    if (result)
    {
        result->pstrComments = pComments;
        result->nbComments = nbComments;
    }

    return result;
}
Ejemplo n.º 22
0
/*--------------------------------------------------------------------------*/
int sci_uicontrol(char *fname, void* pvApiCtx)
{
    SciErr sciErr;

    int nbRow = 0, nbCol = 0, k = 0;
    int setStatus = SET_PROPERTY_SUCCEED;
    int PARENT_NOT_FOUND = -2;
    int NOT_FOUND = -1;
    int inputIndex = 0, beginIndex = 0;
    char *propertyName = NULL;
    char *styleProperty = NULL;

    int iPropertiesCount = sizeof(propertiesNames) / sizeof(char**);
    unsigned long GraphicHandle = 0;

    int found = 0;              /* Does the property exists ? */

    int *propertiesValuesIndices = NULL;

    int iParentType = -1;
    int *piParentType = &iParentType;
    int iParentStyle = -1;
    int *piParentStyle = &iParentStyle;

    int iParentUID      = 0;
    int iUicontrol      = 0;
    int iCurrentFigure  = 0;

    CheckOutputArgument(pvApiCtx, 0, 1);

    //init properties index
    init_property_index();

    if (nbInputArgument(pvApiCtx) == 0)
    {
        /* Create a pushbutton in current figure */

        /* Create a new pushbutton */
        GraphicHandle = getHandle(CreateUIControl(NULL));

        /* Set current figure as parent */
        iCurrentFigure = getCurrentFigure();
        if (iCurrentFigure == 0)
        {
            iCurrentFigure = createNewFigureWithAxes();
        }

        iUicontrol = getObjectFromHandle(GraphicHandle);
        setGraphicObjectRelationship(iCurrentFigure, iUicontrol);
    }
    else if (nbInputArgument(pvApiCtx) == 1)
    {
        /* Create a pushbutton in figure given as parameter */
        /* Or give focus to the uicontrol given as parameter */
        int* piAddr = NULL;
        int iType = 0;

        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
        if (sciErr.iErr)
        {
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
            return 0;
        }

        if (isHandleType(pvApiCtx, piAddr) == FALSE && isStringType(pvApiCtx, piAddr) == FALSE)
        {
            OverLoad(1);
            return FALSE;
        }
#if 0 // Allow XML loading
        else if (isStringType(pvApiCtx, piAddr))
        {
            char* pstXmlfile = NULL;
            char* pstExpandedPath = NULL;

            if (isScalar(pvApiCtx, piAddr) == 0)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
                return FALSE;
            }

            if (getAllocatedSingleString(pvApiCtx, piAddr, &pstXmlfile))
            {
                freeAllocatedSingleString(pstXmlfile);
                Scierror(999, _("%s: No more memory.\n"), fname);
                return FALSE;
            }

            pstExpandedPath = expandPathVariable(pstXmlfile);
            freeAllocatedSingleString(pstXmlfile);
            iUicontrol = xmlload(pstExpandedPath);
            if (iUicontrol < 1)
            {
                Scierror(999, _("%s: can not read file %s.\n"), fname, pstExpandedPath);
                FREE(pstExpandedPath);
                return 0;
            }
            FREE(pstExpandedPath);
            GraphicHandle = getHandle(iUicontrol);

            /* Create return variable */
            if (createScalarHandle(pvApiCtx, nbInputArgument(pvApiCtx) + 1, GraphicHandle))
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 1;
            }

            AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
            ReturnArguments(pvApiCtx);
            return TRUE;
        }
#endif // Allow XML loading
        else /* Get parent ID */
        {
            int* piAddr = NULL;
            long long hParent = 0;
            sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 1;
            }

            if (isScalar(pvApiCtx, piAddr) == 0)
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A graphic handle expected.\n"), fname, 1);
                return FALSE;
            }

            if (getScalarHandle(pvApiCtx, piAddr, &hParent))
            {
                Scierror(202, _("%s: Wrong type for input argument #%d: Handle matrix expected.\n"), fname, 1);
                return 1;
            }

            iParentUID = getObjectFromHandle((long)hParent);
            if (iParentUID != 0)
            {
                getGraphicObjectProperty(iParentUID, __GO_TYPE__, jni_int, (void **)&piParentType);
                if (iParentType == __GO_UICONTROL__)  /* Focus management */
                {
                    GraphicHandle = (unsigned long)hParent;
                    requestFocus(iParentUID);
                }
                else if (iParentType == __GO_FIGURE__ || iParentType == __GO_UIMENU__)  /* PushButton creation */
                {
                    /* Create a new pushbutton */
                    GraphicHandle = getHandle(CreateUIControl(NULL));
                    iUicontrol = getObjectFromHandle(GraphicHandle);

                    /* First parameter is the parent */
                    setGraphicObjectRelationship(iParentUID, iUicontrol);
                    setStatus = callSetProperty(pvApiCtx, iUicontrol, &hParent, sci_handles, 1, 1, (char*)propertiesNames[parent_property]);
                    if (setStatus == SET_PROPERTY_ERROR)
                    {
                        Scierror(999, _("%s: Could not set property '%s'.\n"), fname, propertyName);
                        return FALSE;
                    }
                }
                else
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A '%s', '%s' or '%s' handle expected.\n"), fname, 1, "Uicontrol",
                             "Figure", "Uimenu");
                    return FALSE;
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A '%s', '%s' or '%s' handle expected.\n"), fname, 1, "Uicontrol", "Figure",
                         "Uimenu");
                return FALSE;
            }
        }
    }
    else
    {
        if (!checkInputArgumentType(pvApiCtx, 1, sci_handles) && !checkInputArgumentType(pvApiCtx, 1, sci_strings))
        {
            OverLoad(1);
            return FALSE;
        }

        /* Allocate memory to store the position of properties in uicontrol call */
        if ((propertiesValuesIndices = (int*)MALLOC(sizeof(int) * iPropertiesCount)) == NULL)
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return FALSE;
        }

        /* Init all positions to NOT_FOUND */
        for (inputIndex = 0; inputIndex < iPropertiesCount; inputIndex++)
        {
            propertiesValuesIndices[inputIndex] = NOT_FOUND;    /* Property initialized as not found */
        }

        /**
         * Odd number of input arguments
         * First input is the parent ID
         * All event inputs are property names
         * All odd (except first) inputs are property values
         */
        if (nbInputArgument(pvApiCtx) % 2 == 1)
        {
            if ((!checkInputArgumentType(pvApiCtx, 1, sci_handles)))
            {
                if ((checkInputArgumentType(pvApiCtx, 1, sci_matrix)))
                {
                    int* piAddr = NULL;
                    double dblValue = 0;

                    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        return 1;
                    }

                    if (isScalar(pvApiCtx, piAddr) == 0)
                    {
                        Scierror(999, _("%s: Wrong size for input argument #%d: A graphic handle expected.\n"), fname, 1);
                        return FALSE;
                    }

                    if (getScalarDouble(pvApiCtx, piAddr, &dblValue))
                    {
                        Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
                        return 1;
                    }

                    iParentUID = getFigureFromIndex((int)dblValue);
                }
                else
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
                             "Frame uicontrol");
                    return FALSE;
                }
            }
            else /* Get parent ID */
            {
                int* piAddr = NULL;
                long long hParent = 0;
                sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 1;
                }

                if (isScalar(pvApiCtx, piAddr) == 0)
                {
                    Scierror(999, _("%s: Wrong size for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
                             "Frame uicontrol");
                    return FALSE;
                }

                if (getScalarHandle(pvApiCtx, piAddr, &hParent))
                {
                    Scierror(202, _("%s: Wrong type for input argument #%d: Handle matrix expected.\n"), fname, 1);
                    return 1;
                }

                iParentUID = getObjectFromHandle((long)hParent);
            }

            if (iParentUID == 0)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
                         "Frame uicontrol");
                return FALSE;
            }

            getGraphicObjectProperty(iParentUID, __GO_TYPE__, jni_int, (void **)&piParentType);
            if (iParentType != __GO_FIGURE__)
            {
                getGraphicObjectProperty(iParentUID, __GO_STYLE__, jni_int, (void **)&piParentStyle);
                if (iParentType != __GO_UICONTROL__ ||
                        (iParentStyle != __GO_UI_FRAME__ && iParentStyle != __GO_UI_TAB__ && iParentStyle != __GO_UI_LAYER__))
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
                             "Frame uicontrol");
                    return FALSE;
                }
            }
            /* First parameter is the parent */
            propertiesValuesIndices[parent_property] = 1;
            // First input parameter which is a property name
            beginIndex = 2;
        }
        /**
         * Even number of input arguments
         * All odd inputs are property names
         * All even inputs are property values
         */
        else
        {
            // First input parameter which is a property name
            beginIndex = 1;
        }

        /* Get all properties positions */
        for (inputIndex = beginIndex; inputIndex < Rhs; inputIndex = inputIndex + 2)
        {
            /* Read property name */
            if ((!checkInputArgumentType(pvApiCtx, inputIndex, sci_strings)))
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, inputIndex);
                return FALSE;
            }
            else
            {
                int* piAddr = NULL;
                sciErr = getVarAddressFromPosition(pvApiCtx, inputIndex, &piAddr);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 1;
                }

                if (getAllocatedSingleString(pvApiCtx, piAddr, &propertyName))
                {
                    Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, inputIndex);
                    return 1;
                }


                /* Bug 3031 */
                /* We only want to compare propertyName along its length */
                /* 'posi' must be matched to 'position' */
                found = 0;
                for (k = 0; k < iPropertiesCount ; k++)
                {
                    if (strlen(propertyName) <= strlen(propertiesNames[k]))
                    {
                        if (strnicmp(propertyName, propertiesNames[k], strlen(propertyName)) == 0)
                        {
                            propertiesValuesIndices[k] = inputIndex + 1;    /* Position of value for property */
                            found = 1;
                            break;
                        }
                    }
                }

                freeAllocatedSingleString(propertyName);

                if (found == 0)
                {
                    Scierror(999, _("%s: Unknown property: %s for '%s' handles.\n"), fname, propertyName, "Uicontrol");
                    return FALSE;
                }
            }
        }

        if (propertiesValuesIndices[style_property] != NOT_FOUND)    /* Style found */
        {
            if ((checkInputArgumentType(pvApiCtx, propertiesValuesIndices[style_property], sci_strings)))
            {
                int* piAddr = NULL;
                sciErr = getVarAddressFromPosition(pvApiCtx, propertiesValuesIndices[style_property], &piAddr);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 1;
                }

                if (getAllocatedSingleString(pvApiCtx, piAddr, &styleProperty))
                {
                    Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, propertiesValuesIndices[style_property]);
                    return 1;
                }

                if (strcmp(styleProperty, "frame") == 0)
                {
                    //check scrollable property to create a scroll frame instead of normal frame
                    if (propertiesValuesIndices[scrollable_property] != NOT_FOUND)
                    {
                        char* pstScroll = NULL;
                        int iScroll = 0;
                        sciErr = getVarAddressFromPosition(pvApiCtx, propertiesValuesIndices[scrollable_property], &piAddr);
                        if (sciErr.iErr)
                        {
                            printError(&sciErr, 0);
                            return 1;
                        }

                        if (isStringType(pvApiCtx, piAddr) == 0 && isBooleanType(pvApiCtx, piAddr) == 0 && isScalar(pvApiCtx, piAddr) == 0)
                        {
                            Scierror(202, _("%s: Wrong type for argument #%d: string or boolean expected.\n"), fname, propertiesValuesIndices[scrollable_property]);
                            return 1;
                        }

                        if (isStringType(pvApiCtx, piAddr))
                        {
                            if (getAllocatedSingleString(pvApiCtx, piAddr, &pstScroll))
                            {
                                Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, propertiesValuesIndices[scrollable_property]);
                                return 1;
                            }

                            if (strcmp(pstScroll, "on") == 0)
                            {
                                iScroll = 1;
                            }

                            freeAllocatedSingleString(pstScroll);
                        }
                        else
                        {
                            if (getScalarBoolean(pvApiCtx, piAddr, &iScroll))
                            {
                                Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, propertiesValuesIndices[scrollable_property]);
                                return 1;
                            }
                        }

                        if (iScroll)
                        {
                            freeAllocatedSingleString(styleProperty);
                            styleProperty = os_strdup("framescrollable");
                        }

                        propertiesValuesIndices[scrollable_property] = NOT_FOUND;
                    }
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, propertiesValuesIndices[style_property]);
                return FALSE;
            }
        }

        /* Create a new uicontrol */
        iUicontrol = CreateUIControl(styleProperty);
        freeAllocatedSingleString(styleProperty);
        if (iUicontrol == 0) /* Error in creation */
        {
            Scierror(999, _("%s: Could not create 'Uicontrol' handle.\n"), fname);
            return FALSE;
        }
        GraphicHandle = getHandle(iUicontrol);

        /* If no parent given then the current figure is the parent */
        if (propertiesValuesIndices[parent_property] == NOT_FOUND)
        {
            /* Set the parent */
            iCurrentFigure = getCurrentFigure();

            if (iCurrentFigure == 0)
            {
                iCurrentFigure = createNewFigureWithAxes();
            }

            propertiesValuesIndices[parent_property] = PARENT_NOT_FOUND;
        }

        /* Read and set all properties */
        for (inputIndex = 1; inputIndex < iPropertiesCount; inputIndex++)   /* Style has already been set */
        {
            if (propertiesValuesIndices[inputIndex] == PARENT_NOT_FOUND)
            {
                //special case for not specified parent
                //but set relationship at the good moment.
                setGraphicObjectRelationship(iCurrentFigure, iUicontrol);
            }
            else if (propertiesValuesIndices[inputIndex] != NOT_FOUND)
            {
                int* piAddr = NULL;
                sciErr = getVarAddressFromPosition(pvApiCtx, propertiesValuesIndices[inputIndex], &piAddr);
                if (sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 1;
                }

                if (inputIndex == user_data_property || inputIndex == userdata_property)   /* User data settings */
                {
                    nbRow = -1;
                    nbCol = -1;
                    setStatus = callSetProperty(pvApiCtx, iUicontrol, piAddr, 0, 0, 0, (char*)propertiesNames[inputIndex]);
                }
                else            /* All other properties */
                {
                    /* Read property value */
                    switch (getInputArgumentType(pvApiCtx, propertiesValuesIndices[inputIndex]))
                    {
                        case sci_matrix:
                        {
                            double* pdblValue = NULL;
                            sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &nbRow, &nbCol, &pdblValue);
                            if (sciErr.iErr)
                            {
                                printError(&sciErr, 0);
                                Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, propertiesValuesIndices[inputIndex]);
                                return 1;
                            }

                            setStatus = callSetProperty(pvApiCtx, iUicontrol, pdblValue, sci_matrix, nbRow, nbCol, (char*)propertiesNames[inputIndex]);
                            break;
                        }
                        case sci_strings:
                            /* Index for String & TooltipString properties: Can be more than one character string */
                            if ((inputIndex == string_property) || (inputIndex == tooltipstring_property))
                            {
                                char** pstValue = NULL;
                                if (getAllocatedMatrixOfString(pvApiCtx, piAddr, &nbRow, &nbCol, &pstValue))
                                {
                                    Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, propertiesValuesIndices[inputIndex]);
                                    return 1;
                                }

                                setStatus = callSetProperty(pvApiCtx, iUicontrol, pstValue, sci_strings, nbRow, nbCol, (char*)propertiesNames[inputIndex]);
                                freeAllocatedMatrixOfString(nbRow, nbCol, pstValue);
                            }
                            else
                            {
                                char* pstValue = NULL;
                                if (getAllocatedSingleString(pvApiCtx, piAddr, &pstValue))
                                {
                                    Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, propertiesValuesIndices[inputIndex]);
                                    return 1;
                                }

                                nbRow = (int)strlen(pstValue);
                                nbCol = 1;
                                setStatus = callSetProperty(pvApiCtx, iUicontrol, pstValue, sci_strings, nbRow, nbCol, (char*)propertiesNames[inputIndex]);
                                freeAllocatedSingleString(pstValue);
                            }
                            break;
                        case sci_handles:
                        {
                            long long* pHandles = NULL;
                            sciErr = getMatrixOfHandle(pvApiCtx, piAddr, &nbRow, &nbCol, &pHandles);
                            if (sciErr.iErr)
                            {
                                printError(&sciErr, 0);
                                Scierror(202, _("%s: Wrong type for input argument #%d: Handle matrix expected.\n"), fname, propertiesValuesIndices[inputIndex]);
                                return 1;
                            }

                            setStatus = callSetProperty(pvApiCtx, iUicontrol, pHandles, sci_handles, nbRow, nbCol, (char*)propertiesNames[inputIndex]);
                            break;
                        }
                        case sci_tlist: //constraints and border
                        {
                            setStatus = callSetProperty(pvApiCtx, iUicontrol, piAddr, sci_tlist, 1, 1, (char*)propertiesNames[inputIndex]);
                            break;
                        }
                        default:
                            setStatus = SET_PROPERTY_ERROR;
                            break;
                    }
                }
                if (setStatus == SET_PROPERTY_ERROR)
                {
                    Scierror(999, _("%s: Could not set property '%s'.\n"), fname, (char*)propertiesNames[inputIndex]);
                    return FALSE;
                }
            }
        }
    }

    if (propertiesValuesIndices != NULL
            && (propertiesValuesIndices[sliderstep_property] == NOT_FOUND &&
                (propertiesValuesIndices[min_property] != NOT_FOUND || propertiesValuesIndices[max_property] != NOT_FOUND)))    /* SliderStep property not set */
    {
        /* Set SliderStep property to [1/100*(Max-Min) 1/10*(Max-Min)] */
        double maxValue = 0;
        double* pdblMaxValue = &maxValue;
        double minValue = 0;
        double* pdblMinValue = &minValue;
        double pdblStep[2];

        getGraphicObjectProperty(iUicontrol, __GO_UI_MIN__, jni_double, (void**) &pdblMinValue);
        getGraphicObjectProperty(iUicontrol, __GO_UI_MAX__, jni_double, (void**) &pdblMaxValue);

        pdblStep[0] = 0.01 * (maxValue - minValue);
        pdblStep[1] = 0.1 * (maxValue - minValue);

        setGraphicObjectProperty(iUicontrol, __GO_UI_SLIDERSTEP__, pdblStep, jni_double_vector, 2);
    }

    if ((nbInputArgument(pvApiCtx) < 2) || (propertiesValuesIndices[position_property] == NOT_FOUND))    /* Position property not set */
    {
        double* pdblPosition = NULL;

        getGraphicObjectProperty(iUicontrol, __GO_POSITION__, jni_double_vector, (void**) &pdblPosition);
        setGraphicObjectProperty(iUicontrol, __GO_POSITION__, pdblPosition, jni_double_vector, 4);
        releaseGraphicObjectProperty(__GO_POSITION__, pdblPosition, jni_double_vector, 4);
    }

    if ((nbInputArgument(pvApiCtx) < 2) || (propertiesValuesIndices[visible_property] == NOT_FOUND))    /* Visible property not set */
    {
        /* Force the uicontrol to be visible because is invisible by default in the model (See bug #10346) */
        int b = (int)TRUE;
        setGraphicObjectProperty(iUicontrol, __GO_VISIBLE__, &b, jni_bool, 1);
    }

    FREE(propertiesValuesIndices);

    /* Create return variable */
    if (createScalarHandle(pvApiCtx, nbInputArgument(pvApiCtx) + 1, GraphicHandle))
    {
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 1;
    }

    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
    ReturnArguments(pvApiCtx);
    return TRUE;
}
Ejemplo n.º 23
0
/*--------------------------------------------------------------------------*/
int C2F(sci_getmd5) (char *fname, unsigned long fname_len)
{
    int m1 = 0, n1 = 0;

    int mn = 0;
    int i  = 0;

    char **Input_Matrix  = NULL;
    char **Output_Matrix = NULL;

    Rhs = Max(Rhs, 0);
    CheckRhs(1, 2) ;
    CheckLhs(1, 1) ;

    if (Rhs == 1)
    {
        if (GetType(1) == sci_strings)
        {
            GetRhsVar(1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, &Input_Matrix);
            mn = m1 * n1;

            Output_Matrix = (char**)MALLOC(sizeof(char*) * (mn));
            if (Output_Matrix)
            {
                for (i = 0; i < mn; i++)
                {
                    FILE *fp = NULL;
                    char *MD5 = NULL;
                    char *real_path = NULL;

                    /* Replaces SCI, ~, HOME, TMPDIR by the real path */
                    real_path = expandPathVariable(Input_Matrix[i]);

                    /* bug 4469 */
                    if (isdir(real_path))
                    {
                        Scierror(999, _("%s: The file %s does not exist.\n"), fname, Input_Matrix[i]);
                        freeArrayOfString(Output_Matrix, i);
                        freeArrayOfString(Input_Matrix, mn);
                        FREE(real_path);
                        real_path = NULL;
                        return 0;
                    }

                    wcfopen(fp, real_path, "rb");

                    if (real_path)
                    {
                        FREE(real_path);
                        real_path = NULL;
                    }

                    if (fp)
                    {
                        MD5 = md5_file(fp);
                        fclose(fp);
                        Output_Matrix[i] = strdup(MD5);
                        if (MD5)
                        {
                            FREE(MD5);
                            MD5 = NULL;
                        }
                    }
                    else
                    {
                        Scierror(999, _("%s: The file %s does not exist.\n"), fname, Input_Matrix[i]);
                        freeArrayOfString(Output_Matrix, i);
                        freeArrayOfString(Input_Matrix, mn);
                        return 0;
                    }
                }

                CreateVarFromPtr( Rhs + 1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, Output_Matrix );
                LhsVar(1) = Rhs + 1 ;
                PutLhsVar();
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type of input argument #%d: String expected.\n"), fname, 1);
        }
    }
    else /* Rhs == 2 */
    {
        if ( (GetType(1) == sci_strings) && (GetType(2) == sci_strings) )
        {
            int m2 = 0, n2 = 0, l2 = 0;
            char *Param2 = NULL;

            GetRhsVar(1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, &Input_Matrix);
            mn = m1 * n1;

            GetRhsVar(2, STRING_DATATYPE, &m2, &n2, &l2);
            Param2 = cstk(l2);

            if ( stricmp(Param2, "string") == 0 )
            {
                Output_Matrix = (char**)MALLOC(sizeof(char*) * (mn));

                if (Output_Matrix)
                {
                    for (i = 0; i < mn; i++)
                    {
                        char *MD5 = NULL;

                        MD5 = md5_str(Input_Matrix[i]);
                        Output_Matrix[i] = strdup(MD5);
                        if (MD5)
                        {
                            FREE(MD5);
                            MD5 = NULL;
                        }

                        if (Output_Matrix[i] == NULL)
                        {
                            freeArrayOfString(Input_Matrix, m1 * n1);
                            freeArrayOfString(Output_Matrix, i);
                            Scierror(999, ("%s: No more memory.\n"), fname);
                            return 0;
                        }
                    }

                    CreateVarFromPtr(Rhs + 1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, Output_Matrix );
                    LhsVar(1) = Rhs + 1 ;
                    PutLhsVar();
                }
                else
                {
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: \"%s\" expected.\n"), fname, 2, "string");
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input arguments #%d or #%d: Strings expected.\n"), fname, 1, 2);
        }
    }

    freeArrayOfString(Input_Matrix, mn);
    freeArrayOfString(Output_Matrix, mn);

    return 0;
}
Ejemplo n.º 24
0
/*--------------------------------------------------------------------------*/
int sci_fscanfMat(char *fname, void* pvApiCtx)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int m1 = 0, n1 = 0;
    int iType1 = 0;

    char *filename = NULL;
    char *expandedFilename = NULL;
    char *Format = NULL;
    char *separator = NULL;
    BOOL bIsDefaultSeparator = TRUE;

    fscanfMatResult *results = NULL;

    CheckRhs(1, 3);
    CheckLhs(1, 2);

    if (Rhs == 3)
    {
        int *piAddressVarThree = NULL;
        int m3 = 0, n3 = 0;
        int iType3 = 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;
        }

        if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 3);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &separator))
        {
            freeVar(&filename, &expandedFilename, &Format, &separator);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        bIsDefaultSeparator = FALSE;
    }

    if (Rhs >= 2)
    {
        int *piAddressVarTwo = NULL;
        int m2 = 0, n2 = 0;
        int iType2 = 0;

        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
        if (sciErr.iErr)
        {
            freeVar(&filename, &expandedFilename, &Format, &separator);
            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)
        {
            freeVar(&filename, &expandedFilename, &Format, &separator);
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
            return 0;
        }

        if (isStringType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0)
        {
            freeVar(&filename, &expandedFilename, &Format, &separator);
            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
            return 0;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &Format))
        {
            freeVar(&filename, &expandedFilename, &Format, &separator);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }
    }
    else
    {
        Format = os_strdup(DEFAULT_FSCANFMAT_FORMAT);
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
    if (sciErr.iErr)
    {
        freeVar(&filename, &expandedFilename, &Format, &separator);
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0)
    {
        freeVar(&filename, &expandedFilename, &Format, &separator);
        Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename))
    {
        freeVar(&filename, &expandedFilename, &Format, &separator);
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    expandedFilename = expandPathVariable(filename);
    if (bIsDefaultSeparator)
    {
#define NB_DEFAULT_SUPPORTED_SEPARATORS 2

        /* bug 8148 */
        /* default separator can be a space or a tabulation */
        char *supportedSeparators[NB_DEFAULT_SUPPORTED_SEPARATORS] = {DEFAULT_FSCANFMAT_SEPARATOR, "\t"};
        int i = 0;

        for (i = 0; i < NB_DEFAULT_SUPPORTED_SEPARATORS; i++)
        {
            results = fscanfMat(expandedFilename, Format, supportedSeparators[i]);
            if (results && results->err == FSCANFMAT_NO_ERROR)
            {
                break;
            }

            freeFscanfMatResult(results);
        }
    }
    else
    {
        results = fscanfMat(expandedFilename, Format, separator);
    }

    if (results == NULL)
    {
        freeVar(&filename, &expandedFilename, &Format, &separator);
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    freeVar(NULL, &expandedFilename, &Format, &separator);
    switch (results->err)
    {
        case FSCANFMAT_NO_ERROR:
        {
            if ( (results->values) && (results->m > 0) && (results->n > 0))
            {
                sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, results->m, results->n, results->values);
                if (sciErr.iErr)
                {
                    FREE(filename);
                    freeFscanfMatResult(results);
                    printError(&sciErr, 0);
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    return 0;
                }
            }
            else
            {
                if (createEmptyMatrix(pvApiCtx, Rhs + 1))
                {
                    FREE(filename);
                    freeFscanfMatResult(results);
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                    return 0;
                }
            }

            LhsVar(1) = Rhs + 1;

            if (Lhs == 2)
            {
                if (results->text)
                {
                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, results->sizeText, 1, (char const * const*) results->text);
                    if (sciErr.iErr)
                    {
                        FREE(filename);
                        freeFscanfMatResult(results);
                        printError(&sciErr, 0);
                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }
                else
                {
                    if (createSingleString(pvApiCtx, Rhs + 2, ""))
                    {
                        FREE(filename);
                        freeFscanfMatResult(results);
                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                        return 0;
                    }
                }

                LhsVar(2) = Rhs + 2;
            }

            freeFscanfMatResult(results);
            FREE(filename);
            PutLhsVar();
            return 0;
        }
        case FSCANFMAT_MOPEN_ERROR:
        {
            Scierror(999, _("%s: can not open file %s.\n"), fname, filename);
            FREE(filename);
            return 0;
        }
        case FSCANFMAT_READLINES_ERROR:
        {
            Scierror(999, _("%s: can not read file %s.\n"), fname, filename);
            FREE(filename);
            return 0;
        }
        case FSCANFMAT_FORMAT_ERROR:
        {
            Scierror(999, _("%s: Invalid format.\n"), fname);
            FREE(filename);
            return 0;
        }
        case FSCANFMAT_MEMORY_ALLOCATION:
        {
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            FREE(filename);
            return 0;
        }
        default:
        case FSCANFMAT_ERROR:
        {
            Scierror(999, _("%s: error.\n"), fname);
            FREE(filename);
            return 0;
        }
    }

    FREE(filename);
    freeFscanfMatResult(results);
    return 0;
}
Ejemplo n.º 25
0
/*--------------------------------------------------------------------------*/
int sci_archive_compress(char *fname, unsigned long fname_len)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    char *pStVarOne = NULL;
    char *pStVarOneExpanded = NULL;

    int *piAddressVarTwo = NULL;
    char **pStVarTwo = NULL;
    int m1 = 0;
    int n1 = 0;
    int i = 0;

    int *piAddressVarThree = NULL;
    char *pStVarThree = NULL;

    char **file_list = NULL;
    /* Check Input & Output parameters */
    CheckRhs(3, 3);
    CheckLhs(1, 2);
    int result = 0;
    int *error = 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;
    }

    if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0)
    {
        Scierror(999, _("%s: Wrong type 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, 1);
        return 0;
    }

    if (isDoubleType(pvApiCtx, piAddressVarTwo))
    {
        if (isEmptyMatrix(pvApiCtx, piAddressVarTwo))
        {
            if (createEmptyMatrix(pvApiCtx, Rhs + 2))
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
            return 0;
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        return 0;
    }

    if (isStringType(pvApiCtx, piAddressVarTwo) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 2);
        return 0;
    }

    if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarTwo, &m1, &n1, &pStVarTwo))
    {
        freeAllocatedMatrixOfString(m1, n1, pStVarTwo);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
        return 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;
    }

    if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3);
        return 0;
    }


    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &pStVarOne))
    {
        if (pStVarOne)
        {
            freeAllocatedSingleWideString(pStVarOne);
        }

        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &pStVarThree))
    {
        if (pStVarThree)
        {
            freeAllocatedSingleWideString(pStVarThree);
        }

        freeAllocatedSingleString(pStVarOne);
	freeAllocatedSingleString(pStVarTwo);
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
        return 0;
    }


    int size = 0;
    while(i < (m1*n1))
    {
	size++;
	if(file_list!=NULL)
	{
		file_list = (char**)REALLOC(file_list,sizeof(char*)*(size));
	}
	else
	{
		file_list = (char**)MALLOC(sizeof(char*)*(size));
	}
	file_list[size-1] = (char*)MALLOC(sizeof(char)*512);
	strcpy(file_list[size-1],expandPathVariable(pStVarTwo[i]));
	i++;
    }


    i = 0;
    if(strlen(pStVarThree)>2)
    {
	    Scierror(999, _("%s: Sorry unrecognised format type.\n"), fname);
	    return 0;
    }
    while(i<strlen(pStVarThree))
    {
	    if(pStVarThree[i]!='Z' && pStVarThree[i]!='j' && pStVarThree[i]!='y' && pStVarThree[i]!='z')
	    {
		Scierror(999, _("%s: Sorry unrecognised format type.\n"), fname);
		return 0;
	    }
	    i++;
    }
    result = archive_compress(pStVarOne,file_list,size,pStVarThree,&error);


                if(error == 1)
                {
                Scierror(999, _("%s: Sorry the file could not be opened.\n"), fname);
                return 0;
            }
                if(error == 2)
                {
                Scierror(999, _("%s: Sorry the file header could not be read\n"), fname);
                return 0;
            }
                if(error == 3)
                {
                Scierror(999, _("%s: Sorry the file header could not be written.\n"), fname);
                return 0;
            }

                freeAllocatedSingleString(pStVarOne);
		freeAllocatedSingleString(pStVarTwo);
                createScalarInteger32(pvApiCtx, Rhs + 1, result);
                LhsVar(1) = Rhs + 1;
                PutLhsVar();
                return 0;

            }
Ejemplo n.º 26
0
/*--------------------------------------------------------------------------*/
int sci_mopen(char *fname, unsigned long fname_len)
{
    int m1 = 0, n1 = 0, l1 = 0;
    int m2 = 0, n2 = 0, l2 = 0;
    int m3 = 0, n3 = 0, l3 = 0;
    int l4 = 0, l5 = 0, err = 3;
    int swap = 1, one = 1;
    char *status = NULL;
    char *filename = NULL;

    Nbvars = 0;
    CheckRhs(1, 3);
    CheckLhs(1, 2);

    if (GetType(1) != sci_strings)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
        return 0;
    }

    /*  checking variable file */
    GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);

    if ( Rhs >= 2)
    {
        if (GetType(2) != sci_strings)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
            return 0;
        }

        GetRhsVar(2, STRING_DATATYPE, &m2, &n2, &l2);
        status = cstk(l2);
    }
    else
    {
        status = "rb";
    }

    if ( Rhs >= 3)
    {
        if (GetType(3) == sci_matrix)
        {
            GetRhsVar(3, MATRIX_OF_INTEGER_DATATYPE, &m3, &n3, &l3);
            if (m3 * n3 == 1)
            {
                swap = *istk(l3);
            }
            else
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: An integer expected.\n"), fname, 3);
                return 0;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: An integer expected.\n"), fname, 3);
            return 0;
        }
    }

    CreateVar(Rhs + 1, MATRIX_OF_INTEGER_DATATYPE, &one, &one, &l4);
    CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &one, &one, &l5);

    filename = expandPathVariable(cstk(l1));
    if (filename)
    {
        C2F(mopen)(istk(l4), filename, status, &swap, stk(l5), &err);
    }

    if (err > (int)MOPEN_NO_ERROR)
    {
        if ( Lhs == 1)
        {
            switch (err)
            {
                case (int)MOPEN_NO_MORE_LOGICAL_UNIT :
                {
                    SciError(66);/* no more logical units */
                    FREE(filename);
                    filename = NULL;
                    return 0;
                }
                case (int)MOPEN_CAN_NOT_OPEN_FILE:
                {
                    Scierror(999, _("%s: Cannot open file %s.\n"), fname, filename);
                    FREE(filename);
                    filename = NULL;
                    return 0;
                }
                case (int)MOPEN_NO_MORE_MEMORY:
                {
                    FREE(filename);
                    filename = NULL;
                    Scierror(999, _("%s: No more memory.\n"), fname);
                    return 0;
                }
                case (int)MOPEN_INVALID_FILENAME:
                {
                    FREE(filename);
                    filename = NULL;
                    Scierror(999, _("%s: invalid filename.\n"), fname);
                    return 0;
                }
                case (int)MOPEN_INVALID_STATUS:
                {
                    FREE(filename);
                    filename = NULL;
                    Scierror(999, _("%s: invalid status.\n"), fname);
                    return 0;
                }
            }
        }
        else
        {
            *stk(l5) = - err;
        }
    }

    if (filename)
    {
        FREE(filename);
        filename = NULL;
    }

    LhsVar(1) = Rhs + 1;
    LhsVar(2) = Rhs + 2;
    PutLhsVar();

    return 0;
}
Ejemplo n.º 27
0
int sci_hdf5_load_v2(char *fn, int* pvApiCtx)
{
    SciErr sciErr;

    int* piAddr = NULL;
    char* pstFilename = NULL;
    char* pstExpandedFilename = NULL;
    bool bImport = true;
    const int nbIn = nbInputArgument(pvApiCtx);
    int iSelectedVar = nbIn - 1;

    CheckInputArgumentAtLeast(pvApiCtx , 1);
    CheckOutputArgument(pvApiCtx, 1, 1);

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFilename))
    {
        if (pstFilename)
        {
            freeAllocatedSingleString(pstFilename);
        }

        Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), 2);
        return 1;
    }

    //open hdf5 file
    pstExpandedFilename = expandPathVariable(pstFilename);
    int iFile = openHDF5File(pstExpandedFilename, 0);
    if (iFile < 0)
    {
        Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), pstFilename);
        FREE(pstExpandedFilename);
        FREE(pstFilename);
        return 1;
    }

    FREE(pstExpandedFilename);
    FREE(pstFilename);

    //manage version information
    int iVersion = getSODFormatAttribute(iFile);
    if (iVersion != SOD_FILE_VERSION)
    {
        if (iVersion > SOD_FILE_VERSION)
        {
            //can't read file with version newer that me !
            Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, iVersion);
            return 1;
        }
        else
        {
            //call older import functions and exit or ... EXIT !
            if (iVersion == 1 || iVersion == -1)
            {
                return sci_hdf5_load_v1(fn, pvApiCtx);
            }
        }
    }

    std::vector<wchar_t*> varList;
    if (iSelectedVar)
    {
        //selected variable
        char* pstVarName = NULL;
        for (int i = 0 ; i < iSelectedVar ; i++)
        {
            sciErr = getVarAddressFromPosition(pvApiCtx, i + 2, &piAddr);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 1;
            }

            if (getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName))
            {
                if (pstVarName)
                {
                    freeAllocatedSingleString(pstVarName);
                }

                Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), i + 1);
                return 1;
            }

            if (import_variable(pvApiCtx, iFile, pstVarName) == false)
            {
                FREE(pstVarName);
                bImport = false;
                break;
            }

            varList.push_back(to_wide_string(pstVarName));
            FREE(pstVarName);
            pstVarName = NULL;
        }
    }
    else
    {
        //all variables
        int iNbItem = 0;
        iNbItem = getVariableNames(iFile, NULL);
        if (iNbItem != 0)
        {
            char **pstVarNameList = (char **)MALLOC(sizeof(char *) * iNbItem);

            iNbItem = getVariableNames(iFile, pstVarNameList);

            //import all data
            for (int i = 0; i < iNbItem; i++)
            {
                if (import_variable(pvApiCtx, iFile, pstVarNameList[i]) == false)
                {
                    bImport = false;
                    break;
                }

                varList.push_back(to_wide_string(pstVarNameList[i]));
            }

            freeArrayOfString(pstVarNameList, iNbItem);
        }
    }
    //close the file
    closeHDF5File(iFile);

    if (bImport == true && varList.size() != 0)
    {
        createMatrixOfWideString(pvApiCtx, nbIn + 1, 1, static_cast<int>(varList.size()), varList.data());
    }
    else
    {
        createEmptyMatrix(pvApiCtx, nbIn + 1);
    }

    for (auto & i : varList)
    {
        FREE(i);
    }

    AssignOutputVariable(pvApiCtx, 1) = nbIn + 1;
    ReturnArguments(pvApiCtx);

    //  printf("End gateway !!!\n");
    return 0;
}
Ejemplo n.º 28
0
/*--------------------------------------------------------------------------*/
int sci_findfiles(char *fname, unsigned long fname_len)
{
    static int l1 = 0, n1 = 0, m1 = 0;
    char *pathextented = NULL;
    char *path = NULL;
    char *filespec = NULL;
    char **FilesList = NULL;
    int sizeListReturned = 0;

    Rhs = Max(Rhs, 0);
    CheckRhs(0, 2);
    CheckLhs(0, 1);

    switch (Rhs)
    {
        default:
        case 0:
        {
            int ierr = 0;

            path = scigetcwd(&ierr);

            if (ierr)
            {
                Scierror(999, _("%s: Error while trying to retrieve the name of the current directory.\n"), fname);
                return 0;
            }
            else
            {
                filespec = strdup(DEFAULT_FILESPEC);
            }
        }
        break;

        case 1:
        {
            if (GetType(1) == sci_strings)
            {
                GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);
                path = strdup(cstk(l1));
                filespec = strdup(DEFAULT_FILESPEC);
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1);
                return 0;
            }

        }
        break;

        case 2:
        {
            if ((GetType(1) == sci_strings) && (GetType(2) == sci_strings))
            {
                GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);
                path = strdup(cstk(l1));

                GetRhsVar(2, STRING_DATATYPE, &m1, &n1, &l1);
                filespec = strdup(cstk(l1));
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input arguments: Strings expected.\n"), fname);
                return 0;
            }
        }
        break;
    }

    pathextented = expandPathVariable(path);
    if (path)
    {
        FREE(path);
        path = NULL;
    }
    FilesList = findfiles(pathextented, filespec, &sizeListReturned, FALSE);
    if (pathextented)
    {
        FREE(pathextented);
        pathextented = NULL;
    }
    if (filespec)
    {
        FREE(filespec);
        filespec = NULL;
    }

    if (FilesList)
    {
        int ncol = 0, nrow = 0;

        ncol = 1;
        nrow = sizeListReturned;

        CreateVarFromPtr(Rhs + 1, MATRIX_OF_STRING_DATATYPE, &nrow, &ncol, FilesList);
        LhsVar(1) = Rhs + 1;
    }
    else
    {
        n1 = 0;
        m1 = 0;
        l1 = 0;
        CreateVarFromPtr(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &n1, &m1, &l1);
        LhsVar(1) = Rhs + 1;
    }

    freeArrayOfString(FilesList, sizeListReturned);

    PutLhsVar();
    return 0;
}
Ejemplo n.º 29
0
/*--------------------------------------------------------------------------*/
int xs2file(char * fname, ExportFileType fileType )
{
    SciErr sciErr;

    int* piAddrl1 = NULL;
    int* piAddrfileName = NULL;
    int* piAddrsciOrientation = NULL;
    int* piAddrquality = NULL;
    double* quality = NULL;

    /* Check input and output sizes */
    CheckOutputArgument(pvApiCtx, 0, 1);
    if (isVectorialExport(fileType) || fileType == JPG_EXPORT)
    {
        CheckInputArgument(pvApiCtx, 2, 3);
    }
    else
    {
        CheckInputArgument(pvApiCtx, 2, 2);
    }

    if ((!checkInputArgumentType(pvApiCtx, 1, sci_matrix)) && (!checkInputArgumentType(pvApiCtx, 1, sci_handles)))
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: An integer or a handle expected.\n"), fname, 1);
        AssignOutputVariable(pvApiCtx, 1) = 0;
        ReturnArguments(pvApiCtx);
        return 1;
    }

    if ( (checkInputArgumentType(pvApiCtx, 2, sci_strings)) )
    {
        char **fileName = NULL;
        char *real_filename = NULL;
        float jpegCompressionQuality = 0.95f;
        ExportOrientation orientation = EXPORT_PORTRAIT; /* default orientation */
        int m1 = 0, n1 = 0;
        int figurenum = -1;
        char* figureUID = NULL;
        char *status = NULL;

        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 1;
        }

        /* get handle by figure number */
        if (checkInputArgumentType(pvApiCtx, 1, sci_matrix))
        {
            // Retrieve a matrix of double at position 1.
            int* l1 = NULL;
            sciErr = getMatrixOfDoubleAsInteger(pvApiCtx, piAddrl1, &m1, &n1, &l1);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                Scierror(202, _("%s: Wrong type for argument %d: A real expected.\n"), fname, 1);
                return 1;
            }

            if (m1 * n1 != 1)
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 1);
                return 1;
            }

            figurenum = *l1;
            if (!sciIsExistingFigure(figurenum))
            {
                Scierror(999, "%s: Input argument #%d must be a valid figure_id.\n", fname, 1);
                return 1;
            }
            figureUID = (char*)getFigureFromIndex(figurenum);
        }
        /* check given handle */
        else if (checkInputArgumentType(pvApiCtx, 1, sci_handles))
        {
            int iHandleType = -1;
            int* piHandleType = &iHandleType;
            long long* l1 = NULL;

            // Retrieve a matrix of handle at position 1.
            sciErr = getMatrixOfHandle(pvApiCtx, piAddrl1, &m1, &n1, &l1);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                Scierror(202, _("%s: Wrong type for argument %d: Handle matrix expected.\n"), fname, 1);
                return 1;
            }

            if (m1 * n1 != 1)
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A graphic handle expected.\n"), fname, 1);
                return 1;
            }

            figureUID = (char*)getObjectFromHandle((unsigned long) * l1);
            if (figureUID == NULL)
            {
                Scierror(999, _("%s: Input argument #%d must be a valid handle.\n"), fname, 1);
                return 1;
            }

            getGraphicObjectProperty(figureUID, __GO_TYPE__, jni_int, (void**)&piHandleType);

            if (iHandleType != __GO_FIGURE__)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A ''%s'' handle expected.\n"), fname, 1, "Figure");
                return 1;
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A scalar or figure handle expected.\n"), fname, 1);
            return 1;
        }

        /* get file name */
        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrfileName);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 1;
        }

        // Retrieve a matrix of string at position 2.
        if (getAllocatedMatrixOfString(pvApiCtx, piAddrfileName, &m1, &n1, &fileName))
        {
            Scierror(202, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 2);
            return 1;
        }

        if (m1 * n1 == 1)
        {
            if (nbInputArgument(pvApiCtx) == 3)
            {
                int nbCol = 0;
                int nbRow = 0;

                if (isVectorialExport(fileType))
                {

                    char **sciOrientation = NULL;

                    if ((!checkInputArgumentType(pvApiCtx, 3, sci_strings)))
                    {
                        freeAllocatedMatrixOfString(m1, n1, fileName);
                        Scierror(999, _("%s: Wrong type for input argument #%d: Single character string expected.\n"), fname, 3);
                        return 1;
                    }

                    sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddrsciOrientation);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        return 1;
                    }

                    // Retrieve a matrix of string at position 3.
                    if (getAllocatedMatrixOfString(pvApiCtx, piAddrsciOrientation, &nbRow, &nbCol, &sciOrientation))
                    {
                        freeAllocatedMatrixOfString(m1, n1, fileName);
                        Scierror(202, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 3);
                        return 1;
                    }

                    if (nbRow * nbCol == 1)
                    {
                        /* Value should be 'landscape' or 'portrait' but check only the first character */
                        /* for compatibility with Scilab 4*/
                        if (strcmp(sciOrientation[0], "landscape") == 0 || strcmp(sciOrientation[0], "l") == 0)
                        {
                            freeAllocatedMatrixOfString(nbRow, nbCol, sciOrientation);
                            orientation = EXPORT_LANDSCAPE;
                        }
                        else if (strcmp(sciOrientation[0], "portrait") == 0 || strcmp(sciOrientation[0], "p") == 0)
                        {
                            freeAllocatedMatrixOfString(nbRow, nbCol, sciOrientation);
                            orientation = EXPORT_PORTRAIT;
                        }
                        else
                        {
                            freeAllocatedMatrixOfString(m1, n1, fileName);
                            freeAllocatedMatrixOfString(nbRow, nbCol, sciOrientation);
                            Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 3, "portrait", "landscape");
                            return 1;
                        }
                    }
                    else
                    {
                        freeAllocatedMatrixOfString(m1, n1, fileName);
                        freeAllocatedMatrixOfString(nbRow, nbCol, sciOrientation);
                        Scierror(999, _("%s: Wrong size for input argument #%d: Single character string expected.\n"), fname, 3);
                        return 1;
                    }
                }
                else
                {
                    sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddrquality);
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0);
                        return 1;
                    }

                    // Retrieve a matrix of double at position 3.
                    sciErr = getMatrixOfDouble(pvApiCtx, piAddrquality, &nbRow, &nbCol, &quality);
                    if (sciErr.iErr)
                    {
                        freeAllocatedMatrixOfString(m1, n1, fileName);
                        printError(&sciErr, 0);
                        Scierror(202, _("%s: Wrong type for argument %d: A real expected.\n"), fname, 3);
                        return 1;
                    }

                    if (nbRow != 1 || nbCol != 1 || *quality < 0 || *quality > 1)
                    {
                        freeAllocatedMatrixOfString(m1, n1, fileName);
                        Scierror(999, _("%s: Wrong type for input argument #%d: A real between 0 and 1 expected.\n"), fname, 3);
                        return 1;
                    }
                    jpegCompressionQuality = (float) * quality;
                }
            }

            /* Replaces SCI, ~, HOME, TMPDIR by the real path */
            real_filename = expandPathVariable(fileName[0]);

            /* Call the function for exporting file */
            status = exportToFile(figureUID, real_filename, fileType, jpegCompressionQuality, orientation);

            /* free pointers no more used */
            if (real_filename)
            {
                FREE(real_filename);
                real_filename = NULL;
            }
            freeAllocatedMatrixOfString(m1, n1, fileName);

            /* treat errors */
            if (strlen(status) != 0)
            {
                Scierror(999, _("%s: %s\n"), fname, status);
                return 1;
            }
        }
        else
        {
            freeAllocatedMatrixOfString(m1, n1, fileName);
            Scierror(999, _("%s: Wrong size for input argument #%d: Single character string expected.\n"), fname, 2);
            return 1;
        }
    }
    else
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: Single character string expected.\n"), fname, 2);
        return 1;
    }

    AssignOutputVariable(pvApiCtx, 1) = 0;
    ReturnArguments(pvApiCtx);

    return 0;
}