Example #1
0
/*--------------------------------------------------------------------------*/
void C2F(mtell) (int *fd, double *offset, int *err)
{     
	FILE *fa= GetFileOpenedInScilab(*fd);
	if ( fa == (FILE *) 0 ) 
	{
		char *filename = GetFileNameOpenedInScilab(*fd);
		if (filename)
		{
			sciprint(_("%s: Error while opening, reading or writing '%s'.\n"),"mtell",filename);
		}
		else
		{
			sciprint(_("%s: Error while opening, reading or writing.\n"),"mtell");
		}
		
		*err=1;
		return;
	}
	*err = 0;
	#ifdef _MSC_VER
		#if _WIN64 
			*offset = (double) _ftelli64(fa) ;
		#else
			*offset = (double) ftell(fa) ;
		#endif
	#else
	*offset = (double) ftell(fa) ;
	#endif
}
Example #2
0
/*--------------------------------------------------------------------------*/
void C2F(mget) (int *fd, double *res, int *n, char *type, int *ierr)
{
    int nc, swap2;
    FILE *fa;

    nc = (int)strlen(type);
    *ierr = 0;
    if (nc == 0)
    {
        sciprint(_("%s: Wrong size for input argument #%d: Non-empty string expected.\n"), "mput", 4, type);
        *ierr = 2;
        return;
    }
    fa = GetFileOpenedInScilab(*fd);
    if (fa)
    {
        swap2 = GetSwapStatus(*fd);
        mget2(fa, swap2, res, *n, type, ierr);
        if (*ierr > 0)
        {
            sciprint(_("%s: Wrong value for input argument #%d: Format not recognized.\n"), "mget", 4);
        }
    }
    else
    {
        sciprint(_("%s: No input file associated to logical unit %d.\n"), "mget", *fd);
        *ierr = 3;
    }
}
Example #3
0
/*--------------------------------------------------------------------------*/
void C2F(getfileinfo)(int *fd, FILE *fa, int *swap2, int *type, int *mode, char *filename, int *lf, int *ierr)
{
	char *filenamefromfd = NULL;
	if (*fd<0 || *fd>=GetMaximumFileOpenedInScilab() ) 
	{
		*ierr=1;
		return;
	}
	if ( GetFileTypeOpenedInScilab(*fd) == 0 ) 
	{
		*ierr=2;
		return;
	}
	fa = GetFileOpenedInScilab(*fd);

	*swap2 = GetSwapStatus(*fd);
	*type = GetFileTypeOpenedInScilab(*fd);
	*mode = GetFileModeOpenedInScilab(*fd);
	filenamefromfd = GetFileNameOpenedInScilab(*fd);
	if (filenamefromfd) strcpy(filename,GetFileNameOpenedInScilab(*fd));
	else strcpy(filename,"");

	*lf=(int)strlen(filename);
	*ierr=0;
}
Example #4
0
/*--------------------------------------------------------------------------*/
void C2F(merror) (int *fd, int *res)
{
    FILE *fa = GetFileOpenedInScilab(*fd);
    if (fa)
    {

        *res = ferror(fa);
    }
    else
    {
        *res = EINVAL;
    }
}
Example #5
0
/*--------------------------------------------------------------------------*/
mputlError mputl(int fileDescriptor, char **pStrings, int sizeStrings)
{
    FILE *fw = NULL;
    int i = 0;

    if (pStrings == NULL)
    {
        return MPUTL_ERROR;
    }

    if (fileDescriptor == STDIN_ID)
    {
        return MPUTL_INVALID_FILE_DESCRIPTOR;
    }
    else if (fileDescriptor == STDOUT_ID)
    {
        fw = stdout;
    }
    else
    {
        fw = GetFileOpenedInScilab(fileDescriptor);
        if (fw)
        {
            int mode = GetFileModeOpenedInScilab(fileDescriptor);

            if ( (mode >= 100) && (mode < 200) )
            {
                return MPUTL_NO_WRITE_RIGHT;
            }
        }
        else
        {
            return MPUTL_INVALID_FILE_DESCRIPTOR;
        }
    }

    for (i = 0; i < sizeStrings; i++)
    {
        if (fw == stdout)
        {
            sciprint(MPUTL_FORMAT, pStrings[i]);
        }
        else
        {
            fprintf(fw, MPUTL_FORMAT, pStrings[i]);
        }
    }

    return MPUTL_NO_ERROR;
}
Example #6
0
/*--------------------------------------------------------------------------*/
void C2F(mput) (int *fd, double *res, int *n, char *type, int *ierr)
{
  int nc = 0,swap2 = 0;
  FILE *fa = NULL;
  *ierr = 0;
  if ((nc = (int)strlen(type)) == 0)
    {
      if ( getWarningMode() ) sciprint(_("%s: Wrong size for input argument #%d ('%s'): Non-empty string expected.\n"),"mput",4,type);
      *ierr = 2;
      return;
    }

  if ( *fd == -1  &&  GetFileOpenedInScilab(*fd) == NULL )
    {
      sciprint(_("%s: No File opened in Scilab.\n") , "mput" ) ;
      *ierr = 3 ;
      return ;
    }

  if ((fa = GetFileOpenedInScilab(*fd)) !=NULL)
    {
      swap2 = GetSwapStatus(*fd);
      mput2(fa,swap2,res,*n,type,ierr);
      if (*ierr > 0) {
	if ( getWarningMode() ) {
	  sciprint(_("%s: Wrong value for input argument #%d ('%s'): Format not recognized.\n"),"mput",4,type);
	}
      }
    }
  else
    {
      if ( getWarningMode() ) {
	sciprint(_("%s: Error while opening, reading or writing '%s'.\n"),"mput", GetFileNameOpenedInScilab(*fd));
      }
      *ierr=3;
    }
}
Example #7
0
/*--------------------------------------------------------------------------*/
char* GetFileNameOpenedInScilab(int Id)
{
    if (GetFileTypeOpenedInScilab(Id) == 1) // Fortran file
    {
        /* A exception for Id 5 and 6 */ 
        /* no name */
        if ((Id != 5) && (Id != 6))
        {
            return ScilabFileList[Id].ftname;
        }
    }
    else
    {
        if (GetFileOpenedInScilab(Id) != NULL) return ScilabFileList[Id].ftname;
    }
    return NULL;
}
Example #8
0
/* write data without convertion (res is supposed to have type type) */
void C2F(mputnc) (int *fd, void * res, int *n1, char *type, int *ierr)
{
  char c1,c2;
  int i,n;
  FILE *fa;
  n=*n1;
  *ierr=0;
  if ((fa = GetFileOpenedInScilab(*fd)) ==NULL) {
    if ( getWarningMode() )
      {
	sciprint(_("%s: No input file associated to logical unit %d.\n"),"mput",*fd);
      }
    *ierr=3;
    return;
  }

  c1 = ( strlen(type) > 1) ? type[1] : ' ';
  c2 = ( strlen(type) > 2) ? type[2] : ' ';
  switch ( type[0] )
    {
    case 'i' : MPUT_GEN_NC(int,c1);       break;
    case 'l' : MPUT_GEN_NC(int32_t,c1);      break;
    case 's' : MPUT_GEN_NC(short,c1);     break;
    case 'c' : MPUT_CHAR_NC(char) ;       break;
    case 'd' : MPUT_GEN_NC(double,c1);    break;
    case 'f' : MPUT_GEN_NC(float,c1);     break;
    case 'u' :
      switch ( c1 )
	{
	case 'i' :  MPUT_GEN_NC(unsigned int,c2); break;
	case 'l' :  MPUT_GEN_NC(uint32_t,c2); break;
	case 's' :  MPUT_GEN_NC(unsigned short,c2); break;
	case ' ' :  MPUT_GEN_NC(unsigned int,' '); break;
	case 'c' :  MPUT_CHAR_NC(unsigned char); break;
	default :  *ierr=1;return ;
	}
      break;
    default : *ierr=1; break;
    }
}
Example #9
0
/*--------------------------------------------------------------------------*/
void C2F(mclose) (int *fd, double *res)
{
    int fd1 = -1;
    *res = 0.0;

    switch ( *fd )
    {
        case ALL_FILES_DESCRIPTOR :
            /* closing all opened files */
            for ( fd1 = 0; fd1 < GetMaximumFileOpenedInScilab(); fd1++)
            {
                FILE* stream = GetFileOpenedInScilab(fd1) ;
                if ( stream )
                {
                    int res1 = 1;
                    res1 = fclose( stream );
                    // this function previously called ferror on a just before fclosed FILE* that could lead to crash at exit, depending on libc implementation.
                    if (res1 != 0)
                    {
                        *res = 1;
                    }
                    C2F(delfile)(&fd1);
                    /* bug 3897 */
                    /* initialize file ID */
                    SetCurrentFileId(-1);
                }
            }
            break;

        default :
        {
            fd1 = (*fd == -1 ) ? GetCurrentFileId() : Min(Max(*fd, 0), GetMaximumFileOpenedInScilab() - 1);
            if ( fd1 != -1 )
            {
                if ( GetFileOpenedInScilab(fd1) )
                {
                    int prevId = -1;

                    if (fclose(GetFileOpenedInScilab(fd1)))
                    {
                        *res = (double)ferror(GetFileOpenedInScilab(fd1));
                    }
                    C2F(delfile)(&fd1);

                    /* bug 3897 */
                    /* set as current file previous opened file if exists */
                    prevId = GetPreviousFileId();

                    if ( GetFileOpenedInScilab(prevId) )
                    {
                        SetCurrentFileId(prevId);
                    }
                }
                else
                {
                    *res = 0.0;
                    if (getWarningMode())
                    {
                        sciprint(_("%s: Cannot close file whose descriptor is %d: File is not active.\n"), "mclose", fd1);
                    }
                }
            }
            else
            {
                *res = -1.0;
                if (getWarningMode())
                {
                    sciprint(_("%s: Cannot close file whose descriptor is %d: No file to close.\n"), "mclose", fd1);
                }
            }
        }
    }
}
Example #10
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;
}
Example #11
0
/*--------------------------------------------------------------------------*/
void C2F(mgetnc) (int *fd, void *res, int *n1, char *type, int *ierr)
{
    char c1, c2;
    int i, items, n = *n1;
    FILE *fa;

    *ierr = 0;
    if ((fa = GetFileOpenedInScilab(*fd)) == NULL)
    {
        sciprint(_("%s: No input file associated to logical unit %d.\n"), "mget", *fd);
        *ierr = 3;
        return;
    }
    swap = GetSwapStatus(*fd);
    /* Safe version for optional type modifiers */
    c1 = (type[0] && type[1]) ? type[1] : ' ';
    c2 = (type[0] && type[1] && type[2]) ? type[2] : ' ';
    switch (type[0])
    {
        case 'i':
            MGET_GEN_NC(int, c1);
            break;

        case 'l':
            MGET_GEN_NC(long long, c1);
            break;

        case 's':
            MGET_GEN_NC(short, c1);
            break;

        case 'c':
            MGET_CHAR_NC(char);
            break;

        case 'd':
            MGET_GEN_NC(double, c1);
            break;

        case 'f':
            MGET_GEN_NC(float, c1);
            break;

        case 'u':
            switch (c1)
            {
                case 'i':
                    MGET_GEN_NC(unsigned int, c2);
                    break;

                case 'l':
                    MGET_GEN_NC(unsigned long long, c2);
                    break;

                case 's':
                    MGET_GEN_NC(unsigned short, c2);
                    break;

                case ' ':
                    MGET_GEN_NC(unsigned int, ' ');
                    break;

                case 'c':
                    MGET_CHAR_NC(unsigned char);
                    break;

                default:
                    *ierr = 1;
                    return;
            }
            break;
        default:
            *ierr = 1;
            return;
    }
    if (items != n)
    {
        *ierr = -(items) - 1;
        /** sciprint("Read %d out of\n",items,n); **/
    }
    return;
}
Example #12
0
/*--------------------------------------------------------------------------*/
void C2F(meof) (int *fd, double *res)
{
    FILE *fa = GetFileOpenedInScilab(*fd);
    *res = fa ? feof(fa) : 1;
}
Example #13
0
/*--------------------------------------------------------------------------*/
int sci_fscanf(char *fname,unsigned long fname_len)
{
	static int l1, m1, n1,l2,m2,n2,iarg,maxrow,nrow,rowcount,ncol;
	FILE  *f;
	int args,retval,err;
	int retval_s=0;
	entry *data=NULL;
	long int pos;
	int param1=0;

	rec_entry buf[MAXSCAN];
	sfdir  type[MAXSCAN],type_s[MAXSCAN];

	Nbvars = 0;
	CheckRhs(2,3);

	if (Rhs==3)
	{
		GetRhsVar(1,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
		if (m1*n1 != 1 )
		{
			Scierror(999,_("%s: Wrong size for input argument #%d: Scalar expected.\n"),fname,1);
			return 0;
		}
		iarg=2;
		maxrow=*istk(l1);
	}
	else
	{
		iarg=1;
		maxrow=1;
	}

	GetRhsVar(iarg,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
	GetRhsVar(iarg+1,STRING_DATATYPE,&m2,&n2,&l2);/* format */

	param1 = *istk(l1);
	StringConvert(cstk(l2));  /* conversion */

	switch (param1)
	{
	case 0:
		// stderr
		f = (FILE *)0;
		break;
	case 5:
		f = stdin;
		break;
	case 6:
		// stdout
		f = (FILE *)0;
		break;
	default:
		f = GetFileOpenedInScilab(param1);
		break;
	}

	if (f == (FILE *)0)
	{
		Scierror(999,_("%s: Wrong file descriptor: %d.\n"),fname,param1);
		return 0;
	}

	nrow = maxrow;
	rowcount = -1;

	while (1)
	{
		rowcount++;
		if ((maxrow >= 0) && (rowcount >= maxrow)) break;
		args = Rhs; /* args set to Rhs on entry */
		pos=ftell(f);
		if ( do_xxscanf("fscanf",f,cstk(l2),&args,(char *)0,&retval,buf,type) < 0 )  return 0;
		if ( retval == EOF)
		{
			/*
			Scierror(999,"Error: in %s: end of file reached\n",fname);
			*/
			break;
		}
		if ((err=Store_Scan(&nrow,&ncol,type_s,type,&retval,&retval_s,buf,&data,rowcount,args)) <0 )
		{
			switch (err)
			{
			case MISMATCH:
				if (maxrow>=0)
				{
					Free_Scan(rowcount,ncol,type_s,&data);
					Scierror(999,_("%s: Data mismatch.\n"),fname);
					return 0;
				}
				fseek(f,pos,SEEK_SET);
				break;

			case MEM_LACK:
				Free_Scan(rowcount,ncol,type_s,&data);
				Scierror(999,_("%s: No more memory.\n"),fname);
				return 0;
				break;
			}
			if (err==MISMATCH) break;
		}
	} /* while */

	/* create Scilab variable with each column of data */
	err=Sci_Store(rowcount,ncol,data,type_s,retval_s);
	Free_Scan(rowcount,ncol,type_s,&data);
	if (err==MEM_LACK) { Scierror(999,_("%s: No more memory.\n"),fname);}
	return 0;
}