Example #1
0
static mx_xscore_size_t _score2xscore(mx_real_t score, mx_stype_t type)
	{
	mx_xscore_size_t xs;

	/* first check parameters ... */
	if (!score)
		return(-1);
	if (!_stype_isvalid(type))
		rs_error("can't convert score of invalid type %d!",
			type);

	switch (type) {
		case mx_stype_Probability:
			xs = score * 10000.0;
			break;
		case mx_stype_LogProbability:
			xs = ((score < 650) ? (score) * 100.0 : 65535);
			break;
		default:
			rs_error("can't convert %s-type scores to external format!",
				mx_stype_text[type]);
		}

	return(xs);
	}
Example #2
0
float lsFloat_setIndex(lsFloat_t *il, int index, float i, float i0)
{
  float ret;
  int j;

  if (!il)
    rs_error("lsFloat_setIndex: got NULL-pointer list");
  if (index < 0)
    rs_error("lsFloat_setIndex: illegal index %d",index);

  if (index >= il->max_list) {
    int d = (index - il->max_list) / LIST_BUF + 1;
    il->list = rs_realloc(il->list,
			  (il->max_list+= d*LIST_BUF) * sizeof(float), 
			  "list items");
  }
  for (j=il->n_list; j <= index; j++) 
    il->list[j] = i0;

  ret = il->list[index];

  il->list[index] = i;
  if (il->n_list <= index)
    il->n_list = index+1;

  return (ret);
}
Example #3
0
lsBuffer_t *lsBuffer_init(lsBuffer_t *b, 
			  lsBuffer_Copy_t *_copy, lsBuffer_Free_t *_free)
{
  int err;

  if (!b)
    b = rs_malloc(sizeof(lsBuffer_t),"lsBuffer");

  lsPt_nil(&b->new);
  lsPt_nil(&b->orig);
  lsPt_nil(&b->copy);
  lsInt_nil(&b->status);
  b->update = 1;

  if ((err = pthread_mutex_init(&b->mutexNew, NULL)) != 0) {
    rs_error("lsBuffer_init: error initializing mutex: %d.", err);
  }
  if ((err = pthread_mutex_init(&b->mutexOrig, NULL)) != 0) {
    rs_error("lsBuffer_init: error initializing mutex: %d.", err);
  }
  if ((err = pthread_mutex_init(&b->mutexCopy, NULL)) != 0) {
    rs_error("lsBuffer_init: error initializing mutex: %d.", err);
  }
  if (!_copy || !_free)
    rs_error("lsBuffer_init: no copy or free function specified.");

  b->_copy = _copy;
  b->_free = _free;
  
  return (b);
}
Example #4
0
static mx_real_t _xscore2score(mx_xscore_size_t xs, mx_stype_t type)
	{
	mx_real_t score;

	/* first check parameters ... */
	if (!xs)
		return(-1);
	if (!_stype_isvalid(type))
		rs_error("can't convert score of invalid type %d!",
			type);

	switch (type) {
		case mx_stype_Probability:
			score = (mx_real_t)(xs) / 10000.0;
			break;
		case mx_stype_LogProbability:
			score = (mx_real_t)(xs) / 100.0;
			break;
		default:
			rs_error("can't convert %s-type scores from external format!",
				mx_stype_text[type]);
		}

	return(score);
	}
Example #5
0
int rs_bwrite(rs_IO *io, void *_ptr, int size, int n_elems)
	{
	char* ptr = (char*)_ptr;

	if (!ptr || !io)
		return(-1);

	switch(io->type) {
		case rs_iotype_mem:
			rs_error("binary write to memory not supported!");

			/* ACHTUNG: keine Grenzueberpruefung! */
			break;

		case rs_iotype_file:
			n_elems = _fwrite_padded(ptr, size, n_elems,
						io->base_size, io->fp);
			break;

		default:
			rs_error("IO type %d not supported by 'rs_bread()'!",
				io->type);
		}

	return(n_elems);
	}
Example #6
0
naive_bayes_classifier_t *nB_fscan(naive_bayes_classifier_t *nB, char *filename, fx_select_t *sel) { 
	int class_ind, start, end, i;
    FILE *fp = NULL, *feature_fp = NULL;
    mx_real_t *features=NULL;
    char *line=NULL, *token;
    char *fname =NULL;
    char *class_name = (char *) rs_malloc(STRING_LENGTH*sizeof(char),"String");
    int full_feature_dim=sel?sel->n_features:nB->feature_dim;

    if (!nB)
		rs_error("classifier could not be initialised!");
     
	fp = fopen(filename,"r");
    if (fp == NULL) {
		rs_error("Cannot open file %s!",filename);
    }
    else {
		features = (mx_real_t *) rs_calloc(full_feature_dim, sizeof(mx_real_t), "feature vector");
	 
	 	while ((line= (char *) rs_line_read(fp,';'))) {
	    	fname = (char *) strtok(line,"\t ");
	     	feature_fp = fopen(fname, "r");
	     	if (feature_fp == NULL) {
		 		rs_warning("can't open '%s '!", fname);
		 		while (getchar() != '\n');
		 			continue;
	     	}
	     	line+= strlen(fname)+1;

		    if (feature_fp) {
				for (token = (char *) strtok(line," "); token != NULL; token = (char *) strtok(NULL, " ")) {
		     		if (sscanf(token,"%[^[]s",class_name) !=1 ) 
						rs_error("wrong length information for file %s!",fname);
		     		token += strlen(class_name);
		     		if (sscanf(token,"[%d..%d]",&start,&end) !=2)
			 			rs_error("wrong length information for file %s!",fname);
		     		for (i=start;i<=end;i++) {
			 			if (fread(features,sizeof(mx_real_t),full_feature_dim,feature_fp)!=full_feature_dim)
			     			rs_perror("fread stopped after %d elements in file %s -- %d", i, fname, errno);
			 			else {
							if (sel) 
							    if ( fx_select_apply(&features,sel,features)!= sel->n_features)
									rs_error("Feature selection did not succeed!");
							class_ind = cl_name2number(&(nB->mapping),class_name,nB->n_classes);
							nB=nB_update_classifier(nB,class_ind,features);
			 			}
		     		}
				}
				fclose(feature_fp);
	     	}
		}
		nB_finish_classifier(nB);
	}
    fclose(fp);
    rs_free(class_name);
    rs_free(features);
    
    return nB;
}
Example #7
0
float lsFloat_get(lsFloat_t * il,int i)
{
  if (!il)
    rs_error("lsFloat_get: got NULL-pointer list");
  if (i < 0 || i >= il->n_list)
    rs_error("lsFloat_get: index [%d] out of range [%d..%d]",i,0,il->n_list);
  
  return (il->list[i]);
}
Example #8
0
rs_IO *rs_fopen(char *name, char *mode)
	{
	FILE *fp;
	rs_IO *io;
	int mode_read, mode_mapped;
	void *buf;
	int buf_size;

	/* Lese-/Schreibmodus bestimmen ... */
	if (strchr(mode, 'w') || strchr(mode, 'a'))
		mode_read = 0;
	else if (strchr(mode, 'r'))
		mode_read = 1;
	else	rs_error("illegal mode '%s' for 'rs_fopen()'!", mode);

	/* ... ggf. ge-"mapped" (nur lesend) ... */
	if (strchr(mode, 'm')) {
		if (mode_read)
			mode_mapped = 1;
		else	{
			mode_mapped = 0;
			rs_warning("mapped file IO not supported for writing!");
			}
		}
	else	mode_mapped = 0;

	/* ... Spezialnamen '-' umsetzen ... */
	if (strcmp(name, "-") == 0) {
		if (mode_mapped)
			rs_error("mapped file IO not supported for stdin/out!");

		fp = (mode_read) ? stdin : stdout;
		}
	else	fp = fopen(name, (mode_read) ? "r" : "w");
	if (!fp)
		return(NULL);
	
	if (!mode_mapped) {
		io = rs_malloc(sizeof(rs_IO), "IO data");
		io->type = rs_iotype_file;
		io->base_size = RS_BINIO_BASE_SIZE_DEFAULT;
		io->fp = fp;
		}
	else	{
		fseek(fp, 0, SEEK_END);
		buf_size = ftell(fp);
		rewind(fp);

		buf = rs_malloc(buf_size, "mapped file buffer");
		fread(buf, buf_size, 1, fp);

		io = rs_mopen(buf, buf_size, "r");
		}

	return(io);
	}
Example #9
0
/*
 * If the stream has no more data available, read some from F into
 * BUF, and let the stream use that.  On return, SEEN_EOF is true if
 * the end of file has passed into the stream.
 */
rs_result rs_infilebuf_fill(rs_job_t *job, rs_buffers_t *buf,
                            void *opaque)
{
    int                     len;
    rs_filebuf_t            *fb = (rs_filebuf_t *) opaque;
    FILE                    *f = fb->f;
        
    /* This is only allowed if either the buf has no input buffer
     * yet, or that buffer could possibly be BUF. */
    if (buf->next_in != NULL) {
        assert(buf->avail_in <= fb->buf_len);
        assert(buf->next_in >= fb->buf);
        assert(buf->next_in <= fb->buf + fb->buf_len);
    } else {
        assert(buf->avail_in == 0);
    }

    if (buf->eof_in || (buf->eof_in = feof(f))) {
        rs_trace("seen end of file on input");
        buf->eof_in = 1;
        return RS_DONE;
    }

    if (buf->avail_in)
        /* Still some data remaining.  Perhaps we should read
           anyhow? */
        return RS_DONE;
        
    len = fread(fb->buf, 1, fb->buf_len, f);
    if (len <= 0) {
        /* This will happen if file size is a multiple of input block len
         */
        if (feof(f)) {
            rs_trace("seen end of file on input");
            buf->eof_in = 1;
            return RS_DONE;
        }
        if (ferror(f)) {
            rs_error("error filling buf from file: %s",
                     strerror(errno));
            return RS_IO_ERROR;
        } else {
            rs_error("no error bit, but got %d return when trying to read",
                     len);
            return RS_IO_ERROR;
        }
    }
    buf->avail_in = len;
    buf->next_in = fb->buf;

    return RS_DONE;
}
Example #10
0
float lsFloat_delSwap(lsFloat_t *il, int i)
{
  float x;

  if (LS_isNIL(il))
    rs_error("ls_delIndex: got empty list");
  if (!LS_EXISTS(il,i))
    rs_error("ls_delIndex: illegal index %d",i);

  x = LS_GET(il,i);
  LS_GET(il,i) = LS_LAST(il);
  LS_N(il)--;
  return (x);
}
Example #11
0
int mx_scoreset_fread(mx_scoreset_t *scoreset, FILE *fp)
	{
	int i, j, tot_scores = 0;
	mx_scorelist_t *scorelist;
	mx_xscore_size_t n;
	mx_xscore_t xscore;

	if (!scoreset || !fp)
		return(-1);

	mx_scoreset_reset(scoreset);

	if (scoreset->n_lists > 1) {
		if (fread(&n, sizeof(n), 1, fp) != 1)
			return(0);
		if (n != scoreset->n_lists)
			rs_error("number of codebooks %d does not match"
				"external score data %d!",
				n, scoreset->n_lists);
		}

	for (i = 0; i < scoreset->n_lists; i++) {
		scorelist = scoreset->list[i];

		if (fread(&n, sizeof(n), 1, fp) != 1) {
			if (scoreset->n_lists > 1)
				rs_error("can't read # of scores!");
			else	return(0);
			}
		if (n <= 0)
			rs_error("illegal # of scores %d read!", n);

		for (j = 0; j < n; j++) {
			if (fread(&xscore, sizeof(xscore), 1, fp) != 1)
				rs_error("can't read external score!");
			if (xscore.id < 0)
				rs_error("illegal id %d external score data!",
					xscore.id);

			mx_scorelist_addscore(scorelist,
				xscore.id,
				_xscore2score(xscore.score, scorelist->type));
			}

		tot_scores += scorelist->n_scores;
		}

	return(tot_scores);
	}
Example #12
0
naive_bayes_classifier_t *nB_fread_classifier(char *model_name) {
    int class_ind, weight_no=0, n_classes, feature_dim, i;
    FILE *model_fp;
    char *line, *token;
    naive_bayes_classifier_t *naiveBayes=NULL;
    
    model_fp = fopen(model_name,"r");
    if (!model_fp)
	rs_error("can't open file %s!",model_name);      

    line = (char *) rs_line_read(model_fp,'#');
    token = (char *) strtok(line,"\t");
    n_classes = atoi(token);
    line += strlen(token)+1;
    feature_dim = atoi((char *) strtok(line,"\t"));
    

    naiveBayes = nB_new_classifier(n_classes,feature_dim);
    
    class_ind=0;
    while ((line= (char *) rs_line_read(model_fp,'#')) && class_ind < n_classes) {
	char *name =(char *) strtok(line,"\t");
	int len=strlen(name)+1;
	naiveBayes->mapping[class_ind] = (char *) rs_malloc(len*sizeof(char),"class name");
	strcpy(naiveBayes->mapping[class_ind],name);
	line+=len;
	naiveBayes->class_probs[class_ind] = (mx_real_t) atof((char *) strtok(line,"\t"));
	
	while (weight_no < feature_dim && fscanf(model_fp, "%g\t%g", &naiveBayes->means[class_ind][weight_no], &naiveBayes->std_dev[class_ind][weight_no]) == 2) 
	    weight_no++;
	if (weight_no<feature_dim-1)
	    rs_error("problem with means or standard devs in file %s!",model_name);
	weight_no=0;
	class_ind++;
    }

    if (class_ind <n_classes-1)
	rs_error("problem with class no %d in file %s!",class_ind,model_name);

    //fprintf(stderr,"Classifying into the following classes:\n");
    //for (i=0;i<n_classes;i++)
	//fprintf(stderr,"%s ",naiveBayes->mapping[i]);
    //fprintf(stderr,"\n");
    
    naiveBayes->finished=1;
    fclose(model_fp);
    return naiveBayes;
}
Example #13
0
/* Check that the given Rabin signature is valid. */
int rs_validate_rabin (const mpz_t sig,  /* purported signature of app */
		       int f,		 /* f value */
		       const mpz_t hash, /* MD5 hash of app */
                       const RSKey* key) /* key structure */
{
  mpz_t a, b;
  int result;

  if (!mpz_sgn(key->n)) {
    rs_error(key, NULL, "unable to validate: public key missing");
    return RS_ERR_MISSING_PUBLIC_KEY;
  }

  if (f < 0 || f > 3)
    return RS_SIGNATURE_INCORRECT;

  mpz_init(a);
  mpz_init(b);

  mpz_mul(a, sig, sig);
  mpz_mod(a, a, key->n);

  applyf(b, hash, key->n, f);

  result = mpz_cmp(a, b);

  mpz_clear(a);
  mpz_clear(b);
  return (result ? RS_SIGNATURE_INCORRECT : RS_SUCCESS);
}
Example #14
0
mx_real_t svm_class_prob(svm_classifier_t *svm, mx_real_t *instance, int class_ind) {
    int i, svm_type=svm_get_svm_type(svm->model);
    double *prob_estimates=NULL;
    struct svm_node *x;
	mx_real_t class_prob;

    if (svm_type!=C_SVC && svm_type != NU_SVC)
	rs_error("Cannot give class probability for 1-class or regression SVM!");

    x = (struct svm_node *) rs_malloc((svm->feature_dim+1)*sizeof(struct svm_node),"Feature vector representation for svm");
    for (i=0;i<svm->feature_dim;i++) {
		x[i].index=i+1;
		x[i].value=instance[i];
    }
    x[i].index=-1;

    _scale_instance(&x,svm->feature_dim,svm->max,svm->min);
    
    prob_estimates = (double *) rs_malloc(svm->n_classes*sizeof(double),"SVM probability estimates");
    
    svm_predict_probability(svm->model,x,prob_estimates);
    
	class_prob = prob_estimates[class_ind];
	rs_free (x);
	rs_free (prob_estimates);
	return class_prob;
}
Example #15
0
bool event_handler_pdu_receive(node_t *node, node_t *incoming_node, mac_pdu_t *pdu)
{
    bool all_ok = TRUE;

    incoming_node->mac_info->error = FALSE; /* emulate a MAC acknowledgment */

    switch (pdu->type) {

        case MAC_TYPE_IP : {
            ip_pdu_t *ip_pdu = pdu->sdu;
            pdu->sdu = NULL;

            if (!ip_node_receive(node, incoming_node, ip_pdu)) {
                all_ok = FALSE;
            }

            break;
        }

        default:
            rs_error("node '%s': unknown MAC type '0x%04X'", node->phy_info->name, pdu->type);
            all_ok = FALSE;
    }

    return all_ok;
}
Example #16
0
int _asegment_fixed(char *seg_info, dsp_sample_t *signal, int n_samples, dsp_sample_t ***signal_segment, int **length) {
	int i, frame_length, frame_shift;
	int n_segments =0;
	int *_length;
	dsp_sample_t **_segments;
	
	if (sscanf(seg_info,"%d:%d",&frame_length,&frame_shift) != 2) {
		rs_error("Frame length and/or frame shift information in wrong format!");
		return -1;
	}
	n_segments= (n_samples - frame_length) / frame_shift + 1;

	_segments = (dsp_sample_t **) rs_malloc(n_segments * sizeof(dsp_sample_t *),"Signal segments");
	_length = (int *) rs_malloc(n_segments*sizeof(int),"Segment lengths");

	for (i=0;i<n_segments;i++) {
		_segments[i] = (dsp_sample_t *) rs_malloc(frame_length * sizeof(dsp_sample_t),"signal segments"); 
		_length[i]=frame_length;
	
		if (!memcpy(_segments[i],signal+i*frame_shift,frame_length*sizeof(dsp_sample_t))) {
			rs_warning("Signal was shorter than expected!");
			return -1;
		}
	}

	*signal_segment=_segments;
	*length=_length;
	return n_segments;
}
Example #17
0
char *rs_line_read(FILE *fp, char comment_char)
	{
	static char *line = NULL;

	char *cp;
	int len, i;

	/* evtl. Zeilenpuffer erzeugen ... */
	if (line == NULL)
		line = rs_malloc(_line_len * sizeof(char), "line buffer");

	/* ... und solange noch Zeilen gelesen werden koennen ... */
	while (fgets(line, _line_len, fp) != NULL) {
		/* ... ggf. '\n' am Zeilenende loeschen ... */
		while (line[(len = strlen(line)) - 1] != '\n') {
			/* ... bei Pufferueberlauf ggf. erweitern ... */
			rs_warning("line longer than %d characters read - expanding!",
				_line_len);

			/* ... falls ueberlange TEXT-Zeile ... */
			for (i = 0; i < len; i++)
				if (!(isprint(line[i]) || isspace(line[i])))
					rs_error("non-printable character '0x%02x' encountered in excess text line!", line[i]);

			_line_len += RS_LINE_LEN_DEFAULT;
			line = rs_realloc(line, _line_len * sizeof(char),
					"extended line buffer");

			if (fgets(line + len, _line_len - len, fp) == NULL) {
				len++;
				break;
				}
			}
		line[len - 1] = '\0';

		/* ... ggf. Kommentare ueberspringen ... */
		if (comment_char && line[0] == comment_char)
			continue;

		/* ... und am Zeilenende loeschen falls nicht "escaped" ... */
		if (comment_char) {
			cp = line + 1;
			while (cp = strrchr(cp, comment_char)) {
				if (cp[-1] != '\\') {
					*cp = '\0';
					break;
					}
				}
			}

		/* ... und Zeilen mit Inhalt zurueckgeben */
		for (cp = line; *cp; cp++)
			if (!isspace(*cp))
				return(line);
		}

	/* ... sonst Dateiende signalisieren */
	return(NULL);
	}
Example #18
0
lsFloat_t * lsFloat_init(lsFloat_t * il)
{
  if (LS_isNIL(il)) {
    rs_error("lsFloat_init: got empty list");
  }
  LS_N(il)--;
  return (il);
}
Example #19
0
rs_result rs_signature_init(rs_signature_t *sig, int magic, int block_len,
                            int strong_len, rs_long_t sig_fsize)
{
    int max_strong_len;

    /* Check and set default arguments. */
    magic = magic ? magic : RS_BLAKE2_SIG_MAGIC;
    switch (magic) {
    case RS_BLAKE2_SIG_MAGIC:
        max_strong_len = RS_BLAKE2_SUM_LENGTH;
        break;
    case RS_MD4_SIG_MAGIC:
        max_strong_len = RS_MD4_SUM_LENGTH;
        break;
    default:
        rs_error("invalid magic %#x", magic);
        return RS_BAD_MAGIC;
    }
    strong_len = strong_len ? strong_len : max_strong_len;
    if (strong_len < 1 || max_strong_len < strong_len) {
        rs_error("invalid strong_sum_len %d for magic %#x", strong_len, magic);
        return RS_PARAM_ERROR;
    }
    /* Set attributes from args. */
    sig->magic = magic;
    sig->block_len = block_len;
    sig->strong_sum_len = strong_len;
    sig->count = 0;
    /* Calculate the number of blocks if we have the signature file size. */
    /* Magic+header is 12 bytes, each block thereafter is 4 bytes
       weak_sum+strong_sum_len bytes */
    sig->size = (int)(sig_fsize ? (sig_fsize - 12) / (4 + strong_len) : 0);
    if (sig->size)
        sig->block_sigs =
            rs_alloc(sig->size * rs_block_sig_size(sig),
                     "signature->block_sigs");
    else
        sig->block_sigs = NULL;
    sig->hashtable = NULL;
#ifndef HASHTABLE_NSTATS
    sig->calc_strong_count = 0;
#endif
    rs_signature_check(sig);
    return RS_DONE;
}
Example #20
0
svm_classifier_t *svm_finish_classifier(svm_classifier_t *svm) {
    int i, j, nr_fold=5, total_correct, best_correct=0, best_c=svm->param.C, best_g=svm->param.gamma;
    const char *error_msg;
    double *result = (double *) rs_malloc(sizeof(double)*svm->problem.l,"cross validation result");

    rs_msg("Building SVM classifier...");

    if (svm->finished)
	rs_warning("SVM classifier is already trained!");

    error_msg = svm_check_parameter(&(svm->problem),&(svm->param));
    
    if(error_msg) 
	rs_error("%s",error_msg);
    
    /* Skalierung */
    _create_scaling(svm->problem,svm->feature_dim,&(svm->max),&(svm->min));
    
    for (i=0;i<svm->problem.l;i++) 
	_scale_instance(&(svm->problem.x[i]),svm->feature_dim,svm->max,svm->min);
    
    /* Cross-Validation, um C und G zu bestimmen bei RBF-Kernel */
    if (svm->param.kernel_type == RBF) {
	svm->param.probability = 0;
	for (i=0;i<C_G_ITER;i++) {
	    total_correct=0;
	    svm->param.C=pow(2,C[i]);
	    svm->param.gamma=pow(2,G[i]);
	    svm_cross_validation(&(svm->problem),&(svm->param),nr_fold,result);
	    for(j=0;j<svm->problem.l;j++) {
		if(result[j] == svm->problem.y[j])
		    ++total_correct;
	    }
	    if (total_correct > best_correct) {
		best_correct=total_correct;
		best_c=C[i];
		best_g=G[i];
	    }
	    rs_msg("C-G-Selektion-Iteration # %d: tried c=%g and g=%g => CV rate is %g; current best c=%g and g=%g with CV rate %g",i+1,pow(2,C[i]),pow(2,G[i]),total_correct*100.0/svm->problem.l,pow(2,best_c),pow(2,best_g),best_correct*100.0/svm->problem.l);
	}
	
	/* Training */
	svm->param.C=pow(2,best_c);
	svm->param.gamma=pow(2,best_g);
	svm->param.probability = 1;
    }
    
    svm->model=svm_train(&(svm->problem),&(svm->param));
    svm->finished=1;

	// @begin_add_johannes
	rs_free (result);
	// @end_add_johannes

    return svm;
}
Example #21
0
lsFloat_t * lsFloat_tail(lsFloat_t *il)
{
  if (LS_isNIL(il)) {
    rs_error("lsFloat_tail: got empty list");
  }
  if ((--LS_N(il)) == 0)
    return (il);
  memmove(il->list, il->list+1, LS_N(il) * sizeof(float));
  return (il);
}
Example #22
0
int rs_bread(void *_ptr, int size, int n_elems, rs_IO *io)
	{
	int i;
	char *ptr = (char*)_ptr;

	if (!ptr || !io)
		return(-1);

	switch(io->type) {
		case rs_iotype_mem:
			if (io->base_size > size) {
				for (i = 0; i < n_elems; i++) {
					memcpy(ptr, io->mem.base + io->mem.pos,
							size);
					io->mem.pos += io->base_size;
					ptr += size;
					}
				}
			else if ((size % io->base_size) != 0)
				rs_error("can't convert %d-byte block data from %d-byte padding!",
					size, io->base_size);
			else	{
				memcpy(ptr, io->mem.base + io->mem.pos,
							size * n_elems);
				io->mem.pos += size * n_elems;
				}

			/* ACHTUNG: keine Grenzueberpruefung! */
			break;

		case rs_iotype_file:
			n_elems = _fread_padded(ptr, size, n_elems,
					io->base_size, io->fp);
			break;

		default:
			rs_error("IO type %d not supported by 'rs_bread()'!",
				io->type);
		}

	return(n_elems);
	}
Example #23
0
static void rdiff_options(poptContext opcon)
{
    int             c;
    char const      *a;
    
    while ((c = poptGetNextOpt(opcon)) != -1) {
        switch (c) {
        case 'h':
            help();
            exit(RS_DONE);
        case 'V':
            rdiff_show_version();
            exit(RS_DONE);
        case 'v':
            if (!rs_supports_trace()) {
                rs_error("library does not support trace");
            }
            rs_trace_set_level(RS_LOG_DEBUG);
            break;
            
        case OPT_GZIP:
        case OPT_BZIP2:
            if ((a = poptGetOptArg(opcon))) {
                int l = atoi(a);
                if (c == OPT_GZIP)
                    gzip_level = l;
                else
                    bzip2_level = l;
            } else {
                if (c == OPT_GZIP)
                    gzip_level = -1;      /* library default */
                else
                    bzip2_level = 9;      /* demand the best */
            }
            rs_error("sorry, compression is not really implemented yet");
            exit(RS_UNIMPLEMENTED);
            
        default:
            bad_option(opcon, c);
        }
    }
}
Example #24
0
lsFloat_t * lsFloat_Init(lsFloat_t * il)
{
  lsFloat_t *il_to = lsFloat_Nil();

  if (LS_isNIL(il)) {
    rs_error("lsFloat_Init: got empty list");
  }
  lsFloat_cpy(il_to,il);
  LS_N(il_to)--;
  return (il_to);
}
Example #25
0
lsFloat_t * lsFloat_Tail(lsFloat_t *il)
{
  lsFloat_t *il_to = lsFloat_Nil();
  if (LS_isNIL(il)) {
    rs_error("lsFloat_tail: got empty list");
  }
  if (LS_N(il) == 1)
    return (il_to);
  lsFloat_realloc(il_to, LS_N(il)-1);
  memcpy(il_to->list, il->list+1, (LS_N(il)-1) * sizeof(float));
  return (il_to);
}
Example #26
0
/** Open a file with special handling for '-' or unspecified filenames.
 *
 * \param filename - The filename to open.
 * \param mode - fopen style mode string.
 * \param force - bool to force overwriting of existing files.
 */
FILE *rs_file_open(char const *filename, char const *mode, int force)
{
    FILE           *f;
    int		    is_write;

    is_write = mode[0] == 'w';

    if (!filename  ||  !strcmp("-", filename)) {
	if (is_write) {
#if _WIN32
	    _setmode(_fileno(stdout), _O_BINARY);
#endif
	    return stdout;
	} else {
#if _WIN32
	    _setmode(_fileno(stdin), _O_BINARY);
#endif
	    return stdin;
	}
    }

    if (!force && is_write) {
	if ((f = fopen(filename, "rb"))) {
	    // File exists
	    rs_error("File exists \"%s\", aborting!", filename);
	    fclose(f);
	    exit(RS_IO_ERROR);
	}
    }

    if (!(f = fopen(filename, mode))) {
	rs_error("Error opening \"%s\" for %s: %s", filename,
		  is_write ? "write" : "read",
		  strerror(errno));
	exit(RS_IO_ERROR);
    }

    return f;
}
Example #27
0
rs_result rs_file_copy_cb(void *arg, rs_long_t pos, size_t *len, void **buf)
{
    int        got;
    FILE       *f = (FILE *) arg;

    if (fseek(f, pos, SEEK_SET)) {
        rs_error("seek failed: %s", strerror(errno));
        return RS_IO_ERROR;
    }

    got = fread(*buf, 1, *len, f);
    if (got == -1) {
        rs_error("read error: %s", strerror(errno));
        return RS_IO_ERROR;
    } else if (got == 0) {
        rs_error("unexpected eof on fd%d", fileno(f));
        return RS_INPUT_ENDED;
    } else {
        *len = got;
        return RS_DONE;
    }
}
Example #28
0
int fextract_calc(fextract_t *fex, mx_real_t *features, dsp_sample_t *signal, int maj, int min, ...) {
	va_list  arg_zeiger; 
	if (maj==1 && min==0) {
		return fextract_calc_v1_0(fex,features,signal);
	}
	if (maj==1 && min==1) {
		char *filename=NULL;
		va_start(arg_zeiger,min);
		filename = va_arg(arg_zeiger,char *);
		if (!filename)
			rs_error("No file name given for feature extraction version %d.%d!",maj,min);
		va_end(arg_zeiger);
		return fextract_calc_v1_1(fex,features,signal,filename);
	}
Example #29
0
int mx_scoreset_fwrite(FILE *fp, mx_scoreset_t *scoreset)
	{
	int i, j, tot_scores = 0;
	mx_scorelist_t *scorelist;
	mx_xscore_size_t n;
	mx_xscore_t xscore;

	if (!scoreset || !fp)
		return(-1);

	if (scoreset->n_lists > 1) {
		n = scoreset->n_lists;
		if (fwrite(&n, sizeof(n), 1, fp) != 1)
			rs_error("can't write # of codebooks!");
		}

	for (i = 0; i < scoreset->n_lists; i++) {
		scorelist = scoreset->list[i];

		n = scorelist->n_scores;
		if (fwrite(&n, sizeof(n), 1, fp) != 1)
			rs_error("can't write # of scores!");
		
		for (j = 0; j < scorelist->n_scores; j++) {
			xscore.id = scorelist->score[j].id;
			xscore.score = _score2xscore(scorelist->score[j].score,
						scoreset->type);

			if (fwrite(&xscore, sizeof(xscore), 1, fp) != 1)
				rs_error("can't write score data!");
			}

		tot_scores += scorelist->n_scores;
		}

	return(tot_scores);
	}
Example #30
0
static dsp_vad_state_t _dsp_vad_newstate_1_0(dsp_vad_t *vad, mx_real_t va)
	{
	dsp_vad_state_t state;

	/* ... je nach internem Zustand entscheiden ... */
	state = vad->state;
	switch (state) {
		case dsp_vad_no_decision:
		case dsp_vad_silence:
			if (va > V1_0_VA_THRESH_VOICE)
				state = dsp_vad_voice;
			else if (va > V1_0_VA_THRESH_START)
				state = dsp_vad_starting;
			break;

		case dsp_vad_starting:
			if (va <= V1_0_VA_THRESH_START)
				state = dsp_vad_silence;
			else if (va > V1_0_VA_THRESH_VOICE)
				state = dsp_vad_voice;
			break;

		case dsp_vad_voice:
			if (va <= V1_0_VA_THRESH_VOICE) {
				state = dsp_vad_stopping;
				vad->n_no_va_frames = 0;
				}
			break;

		case dsp_vad_stopping:
			if (va > V1_0_VA_THRESH_VOICE)
				state = dsp_vad_voice;
			else if (va <= V1_0_VA_THRESH_STOP)
				vad->n_no_va_frames++;

			if (vad->n_no_va_frames > V1_0_VA_GAP_LEN)
				state = dsp_vad_starting;
			
			break;

		default:
			rs_error("internal VAD data corrupted!");
		}

	return(state);
	}