Ejemplo n.º 1
0
int sci_call(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out)
{
    std::vector<Parameter> params(30);
    std::vector<int> output_order(nout);
    wchar_t* interf = NULL;
    if (nin < 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), fname, 1);
        return 1;
    }

    //1st is the interface name
    if (scilab_isString(env, in[0]) == 0 || scilab_isScalar(env, in[0]) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1);
        return 1;
    }

    scilab_getString(env, in[0], &interf);

    ConfigVariable::EntryPointStr* func = ConfigVariable::getEntryPoint(interf);
    if (func == NULL)
    {
        Scierror(999, _("%s: unable to find entry point %ls.\n"), fname, interf);
        return 1;
    }

    int pos = 1;
    bool hasOutputs = true;
    //inputs
    while (1)
    {
        //check "out" to break loop
        if (isOut(env, in[pos]))
        {
            hasOutputs = true;
            break;
        }

        if (pos > nin)
        {
            break;
        }

        int type = 0;
        if (nin < pos + 2)
        {
            Scierror(77, _("%s: Wrong number of input argument(s).\n"), fname);
            return 1;
        }

        type = scilab_getType(env, in[pos]);
        if (type != sci_matrix && type != sci_strings)
        {
            Scierror(77, _("%s: Wrong type for input argument #%d: A real matrix or a string expected.\n"), fname, pos + 1);
            return 1;
        }

        //data

        //position
        if (scilab_isDouble(env, in[pos + 1]) == 0 || scilab_isScalar(env, in[pos + 1]) == 0)
        {
            Scierror(77, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), fname, pos + 2);
            return 1;
        }

        double param_pos = 0;
        scilab_getDouble(env, in[pos + 1], &param_pos);

        //type
        if (scilab_isString(env, in[pos + 2]) == 0 || scilab_isScalar(env, in[pos + 2]) == 0)
        {
            Scierror(77, _("%s: Wrong type for input argument #%d : string expected.\n"), fname, pos + 3);
            return 1;
        }

        void* data = NULL;
        int row = 0;
        int col = 0;

        wchar_t* param_type = NULL;
        scilab_getString(env, in[pos + 2], &param_type);

        if (param_type[0] == L'c' || type == sci_strings)
        {
            if (param_type[0] != L'c' || type != sci_strings)
            {
                Scierror(77, _("%s: Wrong type for input argument #%d : string expected.\n"), fname, pos + 1);
                return 1;
            }
        }

        bool alloc = false;
        switch (param_type[0])
        {
            case L'c':
            {
                wchar_t* strs = NULL;
                scilab_getString(env, in[pos], &strs);
                char* c = wide_string_to_UTF8(strs);
                data = c;
                alloc = true;
                break;
            }
            case L'd':
            {
                double* dbls = NULL;
                scilab_getDoubleArray(env, in[pos], &dbls);
                data = dbls;
                break;
            }
            case L'r':
            {
                double* dbls = NULL;
                int size = scilab_getSize(env, in[pos]);
                scilab_getDoubleArray(env, in[pos], &dbls);
                float* f = (float*)malloc(size * sizeof(float));
                for (int i = 0; i < size; ++i)
                {
                    f[i] = (float)dbls[i];
                }

                data = f;
                alloc = true;
                break;
            }
            case L'i':
            {
                double* dbls = NULL;
                int size = scilab_getSize(env, in[pos]);
                scilab_getDoubleArray(env, in[pos], &dbls);
                int* ints = (int*)malloc(size * sizeof(int));
                for (int i = 0; i < size; ++i)
                {
                    ints[i] = (int)dbls[i];
                }

                data = ints;
                alloc = true;
                break;
            }
            default:
            {
                Scierror(77, _("%s: Wrong value for input argument #%d: '%s', '%s', '%s' or '%s' expected.\n"), fname, pos + 3, "d", "r", "i", "c");
                return 1;
            }
        }

        scilab_getDim2d(env, in[pos], &row, &col);

        Parameter& p = params[(int)param_pos - 1];
        p.alloc = alloc;
        p.data = data;
        p.row = row;
        p.col = col;
        p.type = param_type[0];

        pos += 3;
    }

    int output_pos = 0;
    //outputs
    if (hasOutputs)
    {
        ++pos; //avoid "out"
        while (1)
        {
            //check if is 3 or 1 arg ...
            if (scilab_isDouble(env, in[pos]) == 0)
            {
                Scierror(77, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, pos + 1);
                return 1;
            }

            if (scilab_isScalar(env, in[pos]))
            {
                double dorder = 0;
                scilab_getDouble(env, in[pos], &dorder);
                int order = (int)dorder;
                if (params[order - 1].data == nullptr)
                {
                    Scierror(77, _("%s: Wrong value for input argument #%d.\n"), fname, pos + 1);
                    return 1;
                }

                pos += 1;
                output_order[output_pos] = order - 1;
            }
            else
            {
                //dims
                double* dims = 0;
                scilab_getDoubleArray(env, in[pos], &dims);
                int size = (int)dims[0] * (int)dims[1];

                //pos
                if (scilab_isDouble(env, in[pos + 1]) == 0 || scilab_isScalar(env, in[pos + 1]) == 0)
                {
                    Scierror(77, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), fname, pos + 2);
                    return 1;
                }

                double param_pos = 0;
                scilab_getDouble(env, in[pos + 1], &param_pos);

                //type
                if (scilab_isString(env, in[pos + 2]) == 0 || scilab_isScalar(env, in[pos + 2]) == 0)
                {
                    Scierror(77, _("%s: Wrong type for input argument #%d : string expected.\n"), fname, pos + 3);
                    return 1;
                }

                wchar_t* param_type = NULL;
                scilab_getString(env, in[pos + 2], &param_type);

                void* data = NULL;

                switch (param_type[0])
                {
                    case L'c':
                    {
                        data = malloc((size + 1) * sizeof(char));
                        break;
                    }
                    case L'd':
                    {
                        data = malloc(size * sizeof(double));
                        break;
                    }
                    case L'r':
                    {
                        data = malloc(size * sizeof(float));
                        break;
                    }
                    case L'i':
                    {
                        data = malloc(size * sizeof(int));
                        break;
                    }
                }
                Parameter& p = params[(int)param_pos - 1];
                p.row = (int)dims[0];
                p.col = (int)dims[1];
                p.alloc = true;
                p.type = param_type[0];
                p.data = data;
                pos += 3;
                output_order[output_pos] = (int)param_pos - 1;
            }

            ++output_pos;

            if (pos + 1 > nin)
            {
                break;
            }
        }

    }
    //the unbelievable call !
    ((fct)func->functionPtr)(params[0].data, params[1].data, params[2].data, params[3].data, params[4].data, params[5].data, params[6].data, params[7].data, params[8].data, params[9].data,
                             params[10].data, params[11].data, params[12].data, params[13].data, params[14].data, params[15].data, params[16].data, params[17].data, params[18].data, params[19].data,
                             params[20].data, params[21].data, params[22].data, params[23].data, params[24].data, params[25].data, params[26].data, params[27].data, params[28].data, params[29].data);

    //create output variables
    for (int i = 0; i < nout; ++i)
    {
        Parameter& p = params[output_order[i]];

        switch (p.type)
        {
            case L'c':
            {
                wchar_t* w = to_wide_string((char*)p.data);
                scilabVar var = scilab_createString(env, w);
                out[i] = var;
                FREE(w);
                break;
            }
            case L'd':
            {
                scilabVar var = scilab_createDoubleMatrix2d(env, p.row, p.col, 0);
                scilab_setDoubleArray(env, var, (double*)p.data);
                out[i] = var;
                break;
            }
            case L'r':
            {
                double* d = NULL;
                scilabVar var = scilab_createDoubleMatrix2d(env, p.row, p.col, 0);
                scilab_getDoubleArray(env, var, &d);
                int size = p.row * p.col;
                for (int j = 0; j < size; ++j)
                {
                    d[j] = (double)((float*)p.data)[j];
                }

                out[i] = var;
                break;
            }
            case L'i':
            {
                double* d = NULL;
                scilabVar var = scilab_createDoubleMatrix2d(env, p.row, p.col, 0);
                scilab_getDoubleArray(env, var, &d);
                int size = p.row * p.col;
                for (int j = 0; j < size; ++j)
                {
                    d[j] = (double)((int*)p.data)[j];
                }

                out[i] = var;
                break;
            }
        }
    }
    return STATUS_OK;
}
Ejemplo n.º 2
0
int do_file_sorting(dataptr dz)
{
	int exit_status;
	int  n;
	int a_srate = 0;
	double sum;
	int  infilecnt = dz->all_words-1;
	int  fileno = 0, namestoresize = 0;
	char *filename = dz->wordstor[infilecnt];	/* name of input list file */
	char *monofile=NULL,     *stereofile = NULL, *analfile = NULL, *quadfile = NULL;
	char *pitchfile = NULL,  *transfile = NULL,  *formantfile = NULL;
	char *envfile = NULL,    *otherfile  = NULL, *namestore  = NULL;
	int  *posstore   = NULL;
	int  *lcnt       = NULL;
	char ***lstore   = NULL;
 	double *lenstore = NULL;
 	double *sortlens = NULL;
	char *file48 = ENDOFSTR, *file44 = ENDOFSTR, *file32 = ENDOFSTR;
	char *file24 = ENDOFSTR, *file22 = ENDOFSTR, *file16 = ENDOFSTR;
	int is_file48=FALSE, is_file44=FALSE, is_file32=FALSE, is_file24=FALSE, is_file22=FALSE, is_file16=FALSE;
	int is_mono_list=FALSE,  is_stereo_list=FALSE,is_quad_list=FALSE, is_anal_list=FALSE,is_pitch_list=FALSE;
	int is_trans_list=FALSE, is_fmnt_list=FALSE,  is_env_list=FALSE, is_other_list=FALSE;
	FILE *fp48 = NULL, *fp44 = NULL, *fp32 = NULL, *fp24 = NULL, *fp22 = NULL, *fp16 = NULL, 
	    *fpm = NULL, *fps = NULL, *fpa = NULL, *fpp = NULL, *fpt = NULL, *fpf = NULL, *fpe = NULL, *fpo = NULL, *fpq = NULL;
	char *p;
	int done_errmsg = 0;

	switch(dz->mode) {
	case(BY_DURATION):	
	case(BY_LOG_DUR):	
		if((lcnt = (int *)malloc((dz->iparam[SORT_LENCNT]+1) * sizeof(int)))==NULL) {
			sprintf(errstr,"INSUFFICIENT MEMORY for lcnt store.\n");
			return(MEMORY_ERROR);
		}
		if((sortlens  = (double *)malloc(dz->iparam[SORT_LENCNT] * sizeof(double)))==NULL) {
			sprintf(errstr,"INSUFFICIENT MEMORY for lens store.\n");
			return(MEMORY_ERROR);
		}
		sum = dz->param[SORT_SMALL];
		for(n=0;n<dz->iparam[SORT_LENCNT];n++) {
			lcnt[n] = 0;				
			sortlens[n] = sum;
			if(dz->mode==BY_LOG_DUR)
				sum *= dz->param[SORT_STEP];
			else
				sum += dz->param[SORT_STEP];
		}
		lcnt[dz->iparam[SORT_LENCNT]] = 0;
		sortlens[dz->iparam[SORT_LENCNT]-1] = dz->param[SORT_LARGE];
		dz->iparam[SORT_LENCNT]++;
		/* fall thro */
	case(IN_DUR_ORDER):	
	   	if((lstore = (char ***)malloc((dz->iparam[SORT_LENCNT]+1) * sizeof(char **)))==NULL) {
			sprintf(errstr,"INSUFFICIENT MEMORY for length store.\n");
			return(MEMORY_ERROR);
		}
		for(n=0;n<dz->iparam[SORT_LENCNT]+1;n++) {
// AVOID realloc
			if((lstore[n] = (char **)malloc(infilecnt * sizeof(char *)))==NULL) {
				sprintf(errstr,"INSUFFICIENT MEMORY for length store %d.\n",n+1);
				return(MEMORY_ERROR);
			}
		}
		break;
	}
	strip_extension(filename);
	if(sloom && (dz->mode == BY_FILETYPE || dz->mode == BY_SRATE)) {
		p = filename + strlen(filename) - 1;		/* Strip trailing zero from generic tempfilename */
		*p = ENDOFSTR;
	}
	for(n=0;n<infilecnt;n++) {
		if(!strcmp(dz->wordstor[infilecnt],dz->wordstor[n])) {
			sprintf(errstr,"The name of the listfile cannot be included in the listing!!\n");
			return(DATA_ERROR);
		}
	}
// AVOID realloc
	for(n=0;n<infilecnt;n++)
		namestoresize += strlen(dz->wordstor[n]) + 1;
	if((namestore = (char *)malloc(namestoresize * sizeof(char)))==NULL) {
		sprintf(errstr,"INSUFFICIENT MEMORY for name store.\n");
		return(MEMORY_ERROR);
	}
	namestoresize = 0;
	for(n=0;n<infilecnt;n++) {
	    if((dz->ifd[0] = sndopenEx(dz->wordstor[n],0,CDP_OPEN_RDONLY)) < 0) 	{
			if(dz->mode!=BY_FILETYPE)  {
				if(!done_errmsg) {
					sprintf(errstr,"Some files are NOT soundfiles.\n");
					print_outmessage_flush(errstr);
					done_errmsg = 1;
				}
			}
			dz->ifd[0] = -1;
			continue;
		} else if((dz->mode!=BY_FILETYPE) && filename_extension_is_not_sound(dz->wordstor[n])) {
			if(!done_errmsg) {
				sprintf(errstr,"Some files are NOT soundfiles.\n");
				print_outmessage_flush(errstr);
				done_errmsg = 1;
			}
			continue;
		}
		switch(dz->mode) {
		case(BY_SRATE):		
			if((exit_status = do_srates(filename,dz->wordstor[n],&file48,&file44,&file32,&file24,&file22,&file16,
			&is_file48,&is_file44,&is_file32,&is_file24,&is_file22,&is_file16,
			&fp48,&fp44,&fp32,&fp24,&fp22,&fp16,dz))<0)
				return(exit_status);
			break;
		case(BY_DURATION):	
		case(BY_LOG_DUR):	
			if((exit_status = do_lenths(filename,dz->wordstor[n],&namestore,lstore,&namestoresize,lcnt,sortlens,dz))<0)
				return(exit_status);
			break;
		case(IN_DUR_ORDER):	
			if((exit_status = do_order
			(filename,dz->wordstor[n],&namestore,&lenstore,&posstore,&lstore,&fileno,&namestoresize,dz))<0)
				return(exit_status);
			break;
		case(BY_FILETYPE):	
			if((exit_status = do_types(filename,dz->wordstor[n],
			&monofile,&stereofile,&quadfile,&analfile,&pitchfile,&transfile,&formantfile,&envfile,&otherfile,
			&is_mono_list,&is_stereo_list,&is_quad_list,&is_anal_list,
			&is_pitch_list,&is_trans_list,&is_fmnt_list,&is_env_list,&is_other_list,
			&fpm,&fps,&fpa,&fpp,&fpt,&fpf,&fpe,&fpo,&fpq,dz))<0)
				return(exit_status);
			break;
		case(FIND_ROGUES):	
			do_rogues(dz->wordstor[n],&a_srate,dz);
			break;
		}

		if(dz->ifd[0]!=-1 && sndcloseEx(dz->ifd[0])<0)
			fprintf(stdout,"WARNING: Failed to close sndfile %s.\n",dz->wordstor[n]);
		dz->ifd[0] = -1;
	}
	switch(dz->mode) {
	case(BY_LOG_DUR):
	case(BY_DURATION):	
		if((exit_status = output_lenths(filename,&otherfile,lstore,sortlens,lcnt,dz))<0)
			return(exit_status);
		break;
 	case(BY_SRATE):		
 		output_srates(is_file48,is_file44,is_file32,is_file24,is_file22,is_file16,
		file48,	file44,file32,file24,file22,file16);						
		break;
 	case(IN_DUR_ORDER):	
 		if((exit_status = output_order(filename,&otherfile,posstore,lenstore,lstore,fileno,dz))<0)
			return(exit_status);
 		break;
	case(BY_FILETYPE):	
		output_types(filename,is_mono_list,is_stereo_list,is_quad_list,
		is_anal_list,is_pitch_list,is_trans_list,is_fmnt_list,is_env_list,
		is_other_list,monofile,stereofile,quadfile,analfile,pitchfile,
		transfile,formantfile,envfile,otherfile);							
		break;

	}
	fflush(stdout);
	return(FINISHED);
}