static char *
config_init(char *file_name, char *backup_file, char *resin_home)
{
	FILE *is;
	FILE *os;
	char buf[4096];
	
	is = fopen(file_name, "r");

	if (! is)
		return strdup(rsprintf(buf, "Can't find Netscape's %s", file_name));

	os = fopen(backup_file, "w+");

	if (! os) {
		fclose(is);
		return strdup(rsprintf(buf, "Can't write Netscape's %s", backup_file));
	}
	
	int lastInitModule = -1;
	int hasCaucho = 0;
	int line = 0;
	while (fgets(buf, sizeof(buf), is)) {
		fputs(buf, os);
		line++;
		char cmd[1024];
		int args = sscanf(buf, "%s", cmd);
		
		if (strstr(buf, "caucho_status")) {
			hasCaucho = 1;
		}

		if (args >= 1 && ! strcmp(cmd, "Init"))
			lastInitModule = line;
	}

	fclose(is);
	fclose(os);

	if (hasCaucho || lastInitModule < 0)
		return 0;

	is = fopen(backup_file, "r");
	os = fopen(file_name, "w+");

	line = 0;
	int isFirst = 1;
	while (fgets(buf, sizeof(buf), is)) {
		fputs(buf, os);

		line++;
		if (line == lastInitModule) {
			fprintf(os, "Init fn=\"load-modules\" shlib=\"%s/libexec/nsapi.dll\" "
				"funcs=\"caucho_service,caucho_filter,caucho_status\"\n",
				resin_home);
		}
	}

	return 0;
}
Example #2
0
File: word.c Project: AIDman/Kaldi
void *append_WORD_no_NULL(void *p1, void *p2){
    WORD *tw, *w1 = (WORD *)p1, *w2 = (WORD *)p2;

    tw = get_WORD(); 
    tw->use = 1;
    tw->T1 = w1->T1;
    tw->T2 = w2->T2;
    tw->T_dur = tw->T2 - tw->T1; 
    tw->weight = w1->weight + w2->weight; 
    tw->conf = (w1->conf + w2->conf) / 2.0;
    tw->frag_corr = (w1->frag_corr && w2->frag_corr);

    if (w1->opt_del && w2->opt_del){
      tw->intern_value =
	TEXT_strdup((TEXT*)rsprintf("%s%s",
			      (! w1->opt_del) ? w1->value : w1->intern_value,
			      (! w2->opt_del) ? w2->value : w2->intern_value));
      tw->value = 
      	TEXT_strdup((TEXT*)rsprintf(WORD_OPT_DEL_PRE_STR "%s"
				    WORD_OPT_DEL_POST_STR, tw->intern_value));

      tw->opt_del = TRUE;
    } else {
      if (TEXT_strcmp(w1->value, (TEXT *)"@") == 0)
	tw->value = TEXT_strdup((! w2->opt_del) ? w2->value : w2->intern_value);
      else if (TEXT_strcmp(w2->value,(TEXT *)"@") == 0)
	tw->value = TEXT_strdup((! w1->opt_del) ? w1->value : w1->intern_value);
      else
	tw->value = TEXT_add((! w1->opt_del) ? w1->value : w1->intern_value,
			     (! w2->opt_del) ? w2->value : w2->intern_value);
    }

    return(tw);
}
Example #3
0
int sp_compute_checksum(SP_FILE *sp, SP_CHECKSUM *comp_chksum){
    char *proc = "sp_compute_checksum";
    void *buff;
    SP_INTEGER cur_pos;

    if (sp == SPNULL)
	return_err(proc,101,101,"Null SPFILE structure");
    if (sp->open_mode != SP_mode_read) 
	return_err(proc,102,102,"File must be opened for read");
    if (! sp->read_spifr->status->is_disk_file)
	return_err(proc,103,103,"File must be a disk file");
    if (sp_error(sp) != 0)
	return_err(proc,104,104,"Sphere file already has an error");

    /* save the current position so we can go back to it */
    if ((cur_pos = sp_tell(sp)) < 0)
	return_err(proc,110,110,
		   rsprintf("sp_tell() failed, returning: %s",
			    get_return_status_message()));
    /* rewind the file */
    if (sp_seek(sp,0,0) != 0)
	return_err(proc,111,111,
		   rsprintf("sp_seek() to sample 0 failed, returning: %s",
			    get_return_status_message()));
    
    /* allocate some memory */
    if ((buff = (void *)sp_data_alloc(sp,4096)) == (void *)0)
	return_err(proc,112,112,
		   rsprintf("can not alloc waveform buffer, returning: %s",
			    get_return_status_message()));

    /* read in the data */
    while (sp_read_data(buff,4096,sp) > 0)
	;

    /* dealloc memory */
    sp_data_free(sp,buff);

    if (sp_error(sp) != 0)
	return_err(proc,120,120,
		   rsprintf("sp_error() indicates an error, returning: %s",
			    get_return_status_message()));

    *comp_chksum = sp->read_spifr->waveform->checksum;

    if (sp_seek(sp,cur_pos,0) != 0)
	return_err(proc,130,130,
		   rsprintf("sp_seek() to return the file to it's initial"
			    "state failed, returning: %s",
			    get_return_status_message()));

    return_success(proc,0,0,"ok");
}
Example #4
0
int verify_file_checksum(char *filename)
{
    char *proc="verify_file_checksum " SPHERE_VERSION_STR;
    SP_FILE *sp;
    char buf[1024];

    if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc);
    if (filename == CNULL) return_err(proc,100,100,"Null filename");
    
    if ((sp = sp_open(filename,"rv")) == SPNULL)
        return_err(proc,101,101,
		   rsprintf("Unable to open SPHERE file '%s'",filename));
    if (sp->read_spifr->status->user_sample_count > 0){
	if (sp_read_data(buf,1,sp) != 1){
	    sp_close(sp);
	    return_err(proc,200,200,"Verification of checksum failed");
	}
    } else {
	sp_close(sp);
	return_err(proc,300,300,"No data in file to check");
    }
	
    sp_close(sp);
    if (sp_verbose > 11) fprintf(spfp,"Proc %s: Returning 0\n",proc);
    return_success(proc,0,0,"Checksum verification passed");
}
Example #5
0
File: sine.c Project: EQ4/noise
int nz_instrument_sine_init(struct nz_node * node) {
    int rc = nz_instr_init(node, sizeof(struct state), &sine_render);
    if (rc != 0) return rc;

    node->node_name = rsprintf("Sine Instrument");
    return 0;
}
Example #6
0
int parse_sample_byte_format(char *str, enum SP_sample_byte_fmt *sbf)
{
    char *proc="parse_sample_byte_format " SPHERE_VERSION_STR;

    if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc);
    if (str == CNULL) 
	return_err(proc,100,100,"Null sample_byte_format_string");
    if (sbf == (enum SP_sample_byte_fmt *)0)
	return_err(proc,101,101,"Null sbf pointer");

    if (strsame(str,"01")) *sbf = SP_sbf_01;
    else if (strsame(str,"10")) *sbf = SP_sbf_10;
    else if (strsame(str,"1")) *sbf = SP_sbf_1;
    else if (strsame(str,"0123")) *sbf = SP_sbf_0123;
    else if (strsame(str,"1032")) *sbf = SP_sbf_1032;
    else if (strsame(str,"2301")) *sbf = SP_sbf_2301;
    else if (strsame(str,"3210")) *sbf = SP_sbf_3210;
    else if (strsame(str,"N")) *sbf = SP_sbf_N;
    if (strstr(str,"shortpack") != CNULL) {
	/* this return value must remain 1000, other functions depend on it*/
	return_err(proc,1000,1000,
	   rsprintf("Unknown sample_byte_format value '%s' in header",str));
    }
    if (sp_verbose > 11) fprintf(spfp,"Proc %s: Returning 0\n",proc);
    return_success(proc,0,0,"ok");
}    
Example #7
0
static int OnEditEaClick(HWND hDlg)
{
    TFileTestData * pData = GetDialogData(hDlg);
    TCHAR szEaInfo[128];

    // Invoke the editor of the extended attributes
    if(ExtendedAtributesEditorDialog(hDlg, pData) == IDOK)
    {
        // Update the info about extended attributes
        rsprintf(szEaInfo, _maxchars(szEaInfo), IDS_EA_INFO, pData->pFileEa, pData->dwEaSize);
        SetDlgItemText(hDlg, IDC_EXTENDED_ATTRIBUTES, szEaInfo);
    }

    return TRUE;
}
Example #8
0
SP_INTEGER sp_header_size(struct header_t *h)
{
    FILE *fp;
    SP_INTEGER header_size, data_size;    

    if (h == HDRNULL)
	return -1;

    fp = fopen(rsprintf("%s/%s.sz",TEMP_DIR,TEMP_BASE_NAME),"w");
    if (fp == FPNULL){
	if (sp_verbose > 15)
	    fprintf(spfp,"Can't open file '%s'\n",
		   rsprintf("%s/%s.sz",TEMP_DIR,TEMP_BASE_NAME));
	return -1;
    }
    if (sp_write_header(fp,h,&header_size,&data_size) < 0){
	fclose(fp);
	unlink(rsprintf("%s/%s.sz",TEMP_DIR,TEMP_BASE_NAME));
	return(-1);
    }
    fclose(fp);
    unlink(rsprintf("%s/%s.sz",TEMP_DIR,TEMP_BASE_NAME));
    return(header_size);
}
static void GetFileTestAppTitle(LPTSTR szTitle, int nMaxChars)
{
    TCHAR szUserName[256] = _T("");
    DWORD dwSize = _maxchars(szUserName);
    UINT nIDTitle = IDS_APP_TITLE;
    BOOL bElevated = FALSE;

    // Get the elevation flags. Note that this returns FALSE on pre-Vista
    if(GetTokenElevation(&bElevated))
    {
        nIDTitle = bElevated ? IDS_APP_TITLE_VISTA1 : IDS_APP_TITLE_VISTA2;
    }

    GetUserName(szUserName, &dwSize);
    rsprintf(szTitle, nMaxChars, nIDTitle, szUserName);
}
Example #10
0
static int OnRelativeFileHelp(HWND hDlg)
{
    TFileTestData * pData = GetDialogData(hDlg);
    TCHAR szMsgBuff[512];
    LPTSTR szBuffer = szMsgBuff;
    int nLength = 0;

    // Load both parts of the message
    nLength += LoadString(g_hInst, IDS_RELATIVE_FILE1, szBuffer + nLength, _tsize(szMsgBuff) - nLength);
    nLength += LoadString(g_hInst, IDS_RELATIVE_FILE2, szBuffer + nLength, _tsize(szMsgBuff) - nLength);
    nLength += rsprintf(szBuffer + nLength, IDS_RELATIVE_FILE3, pData->dwDesiredAccessRF,
                                                                pData->dwOpenOptionsRF,
                                                                pData->dwShareAccessRF);
    // Show the message
    MessageBoxRc(hDlg, IDS_RELATIVE_FILE_TITLE, (UINT_PTR)szMsgBuff);
    return TRUE;
}
Example #11
0
static int OnSetActive(HWND hDlg)
{
    TFileTestData * pData = GetDialogData(hDlg);
    TCHAR szEaInfo[128];
    BOOL bEnabled = FALSE;
    int nChecked;

    // Set directory name and file name
    SetDlgItemText(hDlg, IDC_DIRECTORY_NAME, pData->szDirName);
    SetDlgItemText(hDlg, IDC_FILE_NAME, pData->szFileName1);

    // Convert both to NT name.
    if(GetWindowTextLength(GetDlgItem(hDlg, IDC_DIRECTORY_NAME)) == 0)
        ConvertToNtName(hDlg, IDC_FILE_NAME);
    ConvertToNtName(hDlg, IDC_DIRECTORY_NAME);

    // Set the various create options
    Hex2DlgText32(hDlg, IDC_OBJ_ATTR_FLAGS, pData->dwObjAttrFlags);
    Hex2DlgText32(hDlg, IDC_DESIRED_ACCESS, pData->dwDesiredAccess);
    Hex2DlgText64(hDlg, IDC_ALLOCATION_SIZE, pData->AllocationSize);
    Hex2DlgText32(hDlg, IDC_FILE_ATTRIBUTES, pData->dwFileAttributes);
    Hex2DlgText32(hDlg, IDC_SHARE_ACCESS, pData->dwShareAccess);
    Hex2DlgText32(hDlg, IDC_CREATE_OPTIONS, pData->dwCreateOptions);

    // Update the info about extended attributes
    rsprintf(szEaInfo, IDS_EA_INFO, pData->pFileEa, pData->dwEaSize);
    SetDlgItemText(hDlg, IDC_EXTENDED_ATTRIBUTES, szEaInfo);

    // Enable/disable transaction
    if(pfnRtlSetCurrentTransaction != NULL && IsHandleValid(pData->hTransaction))
        bEnabled = TRUE;
    EnableDlgItems(hDlg, bEnabled, IDC_TRANSACTED, 0);
    if(bEnabled)
        CheckDlgButton(hDlg, IDC_TRANSACTED, pData->bUseTransaction);

    // Check/uncheck virtualization
    nChecked = TokenVirtualization(TOKEN_VIRT_QUERY, 0);
    CheckDlgButton(hDlg, IDC_VIRTUALIZATION, nChecked);

    // Enable/disable "NtClose"
    bEnabled = IsHandleValid(pData->hFile) ? TRUE : FALSE;
    EnableDlgItems(hDlg, bEnabled, IDC_CLOSE_HANDLE, 0);
    return TRUE;
}
Example #12
0
char * sptemp_dirfile(void)
{
    int max_attempt=999, attempt=0;
    char * s, *n;
    static int call=0;
    struct stat fileinfo;


    do {
	s = rsprintf("%s/%s%d.sph",TEMP_DIR,TEMP_BASE_NAME,call++);
	if (attempt++ >= max_attempt)
	    return(CNULL);
	if (call > 9999)
	    call=0;
    }  while (stat(s,&fileinfo) == 0);
    if ((n = mtrf_malloc(strlen(s) + 1)) == CNULL)
	return(CNULL);
    strcpy(n,s);
    return(n);
}
Example #13
0
nz_rc tee_block_create(const struct nz_context * context_p, const char * string, nz_block_state ** state_pp, struct nz_block_info * info_p) {
    nz_arg * args[2];
    nz_rc rc = arg_parse("required int n_outputs, required generic type", string, args);
    if(rc != NZ_SUCCESS) return rc;
    long n_outputs = *(long *)args[0];
    char * type_str = (char *)args[1];
    free(args[0]);

    const struct nz_typeclass * typeclass_p;
    nz_type * type_p;

    if(n_outputs < 0) {
        free(type_str);
        NZ_RETURN_ERR_MSG(NZ_ARG_VALUE, rsprintf("%ld", n_outputs));
    }

    rc = nz_context_create_type(context_p, &typeclass_p, &type_p, type_str);
    free(type_str);
    if(rc != NZ_SUCCESS) return rc;

    return tee_block_create_args(n_outputs, typeclass_p, type_p, state_pp, info_p);
}
Example #14
0
static nz_rc tee_block_create_args(size_t n_outputs, const struct nz_typeclass * typeclass_p, nz_type * type_p, nz_block_state ** state_pp, struct nz_block_info * info_p) {
    struct tee_block_state * state_p = calloc(1, sizeof(struct tee_block_state));
    if(state_p == NULL) NZ_RETURN_ERR(NZ_NOT_ENOUGH_MEMORY);

    if(n_outputs < 1) NZ_RETURN_ERR(NZ_ARG_VALUE);

    // This function assumes ownership of typeclass_p and type_p
    nz_rc rc;
    nz_obj * obj_p;
    rc = typeclass_p->type_create_obj(type_p, &obj_p);
    if(rc != NZ_SUCCESS) {
        typeclass_p->type_destroy(type_p);
        free(state_p);
        return rc;
    }

    state_p->typeclass_p = typeclass_p;
    state_p->type_p = type_p;
    state_p->obj_p = obj_p;

    if((rc = nz_block_info_set_n_io(info_p, 1, n_outputs)) == NZ_SUCCESS &&
            (rc = nz_block_info_set_input(info_p, 0, strdup("in"), typeclass_p, type_p)) == NZ_SUCCESS &&
            (rc = nz_block_info_set_output(info_p, 0, strdup("main"), typeclass_p, type_p, tee_main_pull_fn)) == NZ_SUCCESS) {
        for(size_t i = 1; i < n_outputs; i++) {
            if((rc = nz_block_info_set_output(info_p, i, rsprintf("aux %lu", i), typeclass_p, type_p, tee_aux_pull_fn)) != NZ_SUCCESS) break;
        }
    }

    if(rc != NZ_SUCCESS) {
        typeclass_p->type_destroy_obj(type_p, obj_p);
        typeclass_p->type_destroy(type_p);
        free(state_p);
        return rc;
    }

    *(struct tee_block_state **)(state_pp) = state_p;
    return NZ_SUCCESS;
}
Example #15
0
int sp_close(SP_FILE *sp)
{
    char *proc="sp_close " SPHERE_VERSION_STR;
    char *write_name;
    char *read_name;
    SP_INTEGER lint=0, header_size=0, data_size=0;
    int header_changed=FALSE;
    SPIFR *spifr;
    SPSTATUS *w_spstat, *r_spstat;
    int ret, verify_checksum=FALSE;

    if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc);
    if (sp == SPNULL)
	return_err(proc,100,100,"Null SPFILE pointer");

    w_spstat = sp->write_spifr->status;
    r_spstat = sp->read_spifr->status;
    write_name = (w_spstat->external_filename == CNULL) ? CNULL :
	mtrf_strdup(sp->write_spifr->status->external_filename);
    read_name =  (r_spstat->external_filename == CNULL) ? CNULL : 
	mtrf_strdup(r_spstat->external_filename);
    if (sp->open_mode == SP_mode_update) {
	if (sp_verbose > 10) fprintf(spfp,"Proc %s:  Mode SP_update\n",proc);
	if (w_spstat->write_occured_flag) {
	    /* if there has been a spwrite, then the waveform is written     */
	    /* as if it were a file opened for write */
	    /* Step 1: recursively call sp_close, changing the mode to write */
	    /* Step 2: delete the previous file                              */
	    /* Step 3: rename the temporary file                             */

	    if (sp_verbose > 15)
		fprintf(spfp,"Proc %s: Overwriting the original waveform\n",
		       proc);
	    sp->open_mode = SP_mode_write;
	    if ((ret=sp_close(sp)) != 0){
		unlink(write_name);
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		return_child(proc,int,ret);
	    }
	    unlink(read_name);
	    rename(write_name,read_name);
	    if (write_name != CNULL) mtrf_free(write_name);
	    if (read_name != CNULL) mtrf_free(read_name);
	    return_success(proc,0,0,"ok");
	} else {
	    /* the header has been changed and the data mode of the waveform */
	    /* COULD BE CHANGED the waveform has not been modified in any    */
	    /* way, only the header has changed */

	    /* Step 1: write the header into the temporary file           */
	    /* Step 2: If the header changed in size OR the waveform      */
            /*         format has changed                                 */
	    /*            A: copy the waveform into the temp file         */
	    /*            B: close the files                              */
	    /*            C: delete the old file in favor of the new file */
	    /*         Else the header has not changed in size.           */
	    /*            A: write the header into the original file      */
	    /*            B: Close both files                             */
	    /*            C: delete the temporary file                    */
	    FILE *fp;
	    int samples_read, samples_written;

	    /* Step 1: */
	    spifr = sp->write_spifr;
	    fp = ((spifr->waveform->sp_fp != FPNULL) ?
		  (spifr->waveform->sp_fp) : 
		  ((spifr->waveform->sp_fob->fp != FPNULL) ?
		   (spifr->waveform->sp_fob->fp) : FPNULL));
	    if (sp_verbose > 15)
		fprintf(spfp,
			"Proc %s: Writing header to temp file.  position %d\n",
		       proc,ftell(fp));
	    if (fp == FPNULL){
		free_sphere_t(sp);
		unlink(write_name);
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		return_err(proc,3000,3000,"Internal Error");
	    }
	    /* Write the header into the temporary file to compute the size  */
	    /* of the header and then rewind back over the just written header*/
	    rewind(fp);
	    if (sp_write_header(fp,spifr->status->file_header,
				&header_size,&data_size) < 0){
		free_sphere_t(sp);
		unlink(write_name);
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		return_err(proc,3001,3001,
			   "Unable to update header in file");
	    }
	    rewind(fp);
	    /* Step 2   -  -  if the header size or waveform has changed */
	    if ((sp->read_spifr->waveform->header_data_size != header_size) || 
		(w_spstat->file_encoding != r_spstat->file_encoding) ||
		(w_spstat->file_compress != r_spstat->file_compress) ||
		(w_spstat->file_sbf != r_spstat->file_sbf) ||
		(w_spstat->channels != CHANNELSNULL)){
		char *buff;
		int ns, nc, in_nspb, out_nspb;
		if (sp_verbose > 15) {
		    fprintf(spfp,"Proc %s:   output header and/or",proc);
		    fprintf(spfp,"data has changed, copying file.\n");
		    fprintf(spfp,"Proc %s:       from %d to %d\n",proc, 
			   sp->read_spifr->waveform->header_data_size,
			   header_size);
		}
		ns = r_spstat->user_sample_count;
		nc = r_spstat->user_channel_count;
		in_nspb  = r_spstat->user_sample_n_bytes * 
		    r_spstat->user_channel_count;
		out_nspb = w_spstat->user_sample_n_bytes *
		     w_spstat->user_channel_count;
		if ((buff=mtrf_malloc(nc * in_nspb * 4096)) == CNULL) {
		    free_sphere_t(sp);
		    unlink(write_name);

		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    return_err(proc,3010,3010,
			       "Unable to malloc transfer buffer space");
		}
		/* A: */
		do {
		    sp->open_mode = SP_mode_read;	
		    samples_read = sp_read_data(buff,4096,sp);
		    if (samples_read > 0) {
			sp->open_mode = SP_mode_write;
			samples_written = sp_write_data(buff,samples_read,sp);
			if (samples_written != samples_read){
			    free_sphere_t(sp);
			    unlink(write_name);
			    if (write_name != CNULL) mtrf_free(write_name);
			    if (read_name != CNULL) mtrf_free(read_name);
			    return_err(proc,3012,3012,
				       "Copy of waveform data failed");
			}
		    } else { 
			if (sp_eof(sp) == 0) {
			    free_sphere_t(sp);
			    unlink(write_name);
			    if (write_name != CNULL) mtrf_free(write_name);
			    if (read_name != CNULL) mtrf_free(read_name);
			    return_err(proc,3013,3013,
				  "Error: Zero samples read while not at EOF");
			}
			if (sp_error(sp) >= 100) {
			    /* a checksum error occured, close the sp and */
			    /* delete the temp file */
			    sp->open_mode = SP_mode_update;
			    sp_print_return_status(spfp);
			    free_sphere_t(sp);
			    unlink(write_name);
			    if (write_name != CNULL) mtrf_free(write_name);
			    if (read_name != CNULL) mtrf_free(read_name);
			    mtrf_free(buff);
			    return_err(proc,3011,3011,
				       "Error copying waveform data");
			}
		    }
		    sp->open_mode = SP_mode_update;
		} while (samples_read > 0);
		mtrf_free(buff);
		/* make sure the file is at eof (as if it were opened for   */
		/* read) */
		sp->open_mode = SP_mode_read;
		if (! sp_eof(sp)){
		    sp->open_mode = SP_mode_update;
		    free_sphere_t(sp);
		    unlink(write_name);
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    return_err(proc,3012,3012,"Error copying waveform data");
		}
		sp->open_mode = SP_mode_write;
		if ((ret=sp_close(sp)) != 0){
		    unlink(write_name);
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    return_child(proc,int,ret);
		}
		/* C: */
		unlink(read_name);
		rename(write_name,read_name);
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		return_success(proc,0,0,"ok");
	    } else { 
		/* A: */
		spifr = sp->read_spifr;
		fp = ((spifr->waveform->sp_fp != FPNULL) ? 
		      (spifr->waveform->sp_fp) : 
		      ((spifr->waveform->sp_fob->fp != FPNULL) ?
		       (spifr->waveform->sp_fob->fp) : FPNULL));
		if (fp == FPNULL)
		    return_err(proc,3002,3002,"Internal Error");
		rewind(fp);
		if (sp_verbose > 15)
		    fprintf(spfp,
			 "Proc %s:   header size not changed.  position %d\n",
			   proc,ftell(fp));
		if (sp_write_header(fp,w_spstat->file_header,
				    &header_size,&data_size) < 0){
		    free_sphere_t(sp);
		    unlink(write_name);
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    return_err(proc,3003,3003,
			       "Unable to update header in file");
		}
		/* B: */
		free_sphere_t(sp);
		/* C: */
		unlink(write_name);
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		return_success(proc,0,0,"ok");
	    }
	}
    }

    /*  END of update mode file */

    if (sp->open_mode == SP_mode_write) {
	if (sp_verbose > 10) fprintf(spfp,
				     "Proc %s:  Mode SP_mode_write\n",proc);
	spifr = sp->write_spifr;
	/* flush the data to the file */
	if (spifr->status->is_disk_file)
	    fob_fflush(spifr->waveform->sp_fob);

	/* if the mode is write, update the sample_count and checksum */
	/* field if needed.  If the checksum field exists, verify it, */
	/* and warn if it's not the same */
	/************ ONLY UPDATE FIELDS IF THE FILE IS NOT A STREAM *********/
	if (spifr->status->is_disk_file){
	    h_get_field(w_spstat->file_header, SAMPLE_COUNT_FIELD,
			T_INTEGER, (void *)&lint);
	    if (spifr->waveform->samples_written != lint) {
		/* then update the field */
		lint = (SP_INTEGER) spifr->waveform->samples_written;
		spifr->status->file_sample_count = lint;
		/* temporarily reset the write occured flag to allow header  */
		/* modifications */
		w_spstat->write_occured_flag = FALSE;
		if (sp_h_set_field(sp,SAMPLE_COUNT_FIELD,T_INTEGER,&lint) !=0){
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    free_sphere_t(sp);
		    return_err(proc,200,200,"Unable to update sample_count");
		}
		/* Reset the write occured flag */
		w_spstat->write_occured_flag = TRUE;
		header_changed = TRUE;
	    }
	    if (h_get_field(spifr->status->file_header,SAMPLE_CHECKSUM_FIELD,
			    T_INTEGER,(void *)&lint) != 0){
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		free_sphere_t(sp);
		return_err(proc,201,201,
			   "Unable to get sample_checksum for file on disk");
	    }
	    if (lint != spifr->status->file_checksum) {
		/* then the checksum was just computed, so install it */
		lint = (SP_INTEGER)spifr->waveform->checksum;
		/* temporarily reset the write occured flag to allow header  */
		/* modifications */
		w_spstat->write_occured_flag = FALSE;
		if (sp_h_set_field(sp,SAMPLE_CHECKSUM_FIELD,
				   T_INTEGER,&lint) >= 100){
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    free_sphere_t(sp);
		    return_err(proc,202,202,"Unable to update checksum");
		}
		/* Reset the write occured flag */
		w_spstat->write_occured_flag = TRUE;
		header_changed = TRUE;
	    } else 
		if (lint != spifr->waveform->checksum) {
		    spifr->waveform->failed_checksum = TRUE;	    
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    free_sphere_t(sp);
		  return_err(proc,203,203,
		    rsprintf("Write verification of checksum failed on file %s",
			     spifr->status->external_filename));
		}
	}

	/* flush the updated header to the file  */
	if (header_changed) {
	    FILE *fp;
	    if (! spifr->status->is_disk_file) {
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		free_sphere_t(sp);
		return_err(proc,301,301,
		   "Internal Error, header changed size on write to stdout");
	    }
	    fp = ((spifr->waveform->sp_fp != FPNULL) ? 
		  (spifr->waveform->sp_fp) : 
		  ((spifr->waveform->sp_fob->fp != FPNULL) ?
		   (spifr->waveform->sp_fob->fp) : FPNULL));
	    if (fp == FPNULL) {
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);
		free_sphere_t(sp);
		return_err(proc,300,300,"Internal Error");
	    }
	    rewind(fp);
	    if (sp_write_header(fp,spifr->status->file_header,
				&header_size,&data_size) < 0) {
		if (write_name != CNULL) mtrf_free(write_name);
		if (read_name != CNULL) mtrf_free(read_name);		
		free_sphere_t(sp);
		return_err(proc,204,204,"Unable to update header in file");
	    }
        }
	
	if ((spifr->status->is_temp_file == FALSE) &&
	    fob_is_fp(spifr->waveform->sp_fob)) { 
	    /* check to make sure the blocking has not changed */
	    if (header_changed)
		if (((data_size + PAD_MULT) / PAD_MULT) !=
		    ((spifr->waveform->header_data_size + PAD_MULT) /PAD_MULT)){
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    free_sphere_t(sp);
		    return_err(proc,205,205,
			       "Header size has changed on update");    
		}
	} else {
	    if (spifr->status->user_compress == spifr->status->file_compress){
		if (fob_flush_to_fp(spifr->waveform->sp_fob,
				    spifr->waveform->sp_fp) != 0){
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    free_sphere_t(sp);
		    return_err(proc,206,206,"Unable to flush data to disk");
		}
	    } else { /* do some compression */
		FOB *comp_fob;
		/* 1. rewind the data */
		/* 2. alloc FOB to compress into */
		/* 3. compress the file */
		/* 4. free the allocated FOB */
		fob_rewind(spifr->waveform->sp_fob);
		if  ((comp_fob = fob_create(spifr->waveform->sp_fp)) ==
		     FOBPNULL) {
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    free_sphere_t(sp);
		    return_err(proc,207,207,"Unable to setup for compression");
		}
		spifr->waveform->sp_fp = FPNULL;
		switch (spifr->status->file_compress){
                  char message[70];
		  case SP_wc_shorten:
		    /* optimize the compression */
		   shorten_set_channel_count(spifr->status->file_channel_count);
		    if (spifr->status->file_encoding == SP_se_ulaw)
			shorten_set_ftype("au");
		    else if (spifr->status->file_encoding == SP_se_pcm1)
			shorten_set_ftype("s8");
		    else if (spifr->status->file_encoding == SP_se_pcm2)
			if (spifr->status->file_sbf == SP_sbf_01)
			    shorten_set_ftype("s16lh");
			else
			    shorten_set_ftype("s16hl");
		    if (sp_verbose > 15) shorten_dump_flags(spfp);
  
	            if(setjmp(exitenv) == 0){
			if (shorten_compress(spifr->waveform->sp_fob, 
					     comp_fob, message) < 0){
			    fob_destroy(comp_fob);
			    if (write_name != CNULL) mtrf_free(write_name);
			    if (read_name != CNULL) mtrf_free(read_name);
			    free_sphere_t(sp);
			    return_err(proc,208,208,
				  rsprintf("Shorten Compression Failed - %s",
					   message));
			}
		    } else
			return_err(proc,213,0,"Shorten Compression Aborted");
		  
		    fob_fflush(comp_fob);
		    break;
		  case SP_wc_wavpack:
		    if(setjmp(exitenv) == 0){
			/* optimize the compression */
			wavpack_set_progname( "wavpack" );
			if (spifr->status->file_channel_count == 1)
			    wavpack_set_monoflg(TRUE);
			else
			    wavpack_set_monoflg(FALSE);
			wavpack_set_byteflg(spifr->status->file_sbf ==SP_sbf_1);
			if (sp_verbose > 15) wavpack_dump_interface(spfp);
			if (wavpack_pack(spifr->waveform->sp_fob, comp_fob)<0){
			    fob_destroy(comp_fob);
			    if (write_name != CNULL) mtrf_free(write_name);
			    if (read_name != CNULL) mtrf_free(read_name);
			    free_sphere_t(sp);
			    return_err(proc,209,209,
				       "Wavpack Compression Failed");
			}
			wavpack_free_progname();
			fob_fflush(comp_fob);
		    } else {
			return_err(proc,212,0,"Wavpack Compression Aborted");
		    }
		    break;
		  case SP_wc_shortpack:
		    return_err(proc,211,211,
			       "Unable to Compress using shortpack\n");
		  default:
		    if (write_name != CNULL) mtrf_free(write_name);
		    if (read_name != CNULL) mtrf_free(read_name);
		    free_sphere_t(sp);
		    return_err(proc,210,210,
			       "Unable to Compress the requested format\n");
		}
		spifr->waveform->sp_fp = comp_fob->fp;
		fob_destroy(comp_fob);
	    }
	}
	if ((sp->open_mode == SP_mode_write) ||
	    (sp->open_mode == SP_mode_update))
	    if (w_spstat->extra_checksum_verify)
		verify_checksum = TRUE;

    }


    free_sphere_t(sp);
    /*************************************************/
    /* The file is now completely written and closed */
    /*************************************************/

    /**************************************************/
    /*  If the write verification is requested, do it */
    if (verify_checksum) {
	if (strsame(write_name,"-")) {
	    if (write_name != CNULL) mtrf_free(write_name);
	    if (read_name != CNULL) mtrf_free(read_name);
	    return_warn(proc,1,1,
			"Unable to verify checksum, file went to STDOUT");
	}
	if (verify_file_checksum(write_name) != 0){
	    sp_print_return_status(spfp);
	    if (write_name != CNULL) mtrf_free(write_name);
	    if (read_name != CNULL) mtrf_free(read_name);
	    return_err(proc,1000,1000,
		       "Read Verification of written file failed");
	}
    }

    if (write_name != CNULL) mtrf_free(write_name);
    if (read_name != CNULL) mtrf_free(read_name);
    return_success(proc,0,0,"ok");
}
Example #16
0
static int decompress_waveform(SP_FILE *sp)
{
    int decomp_into_memory=TRUE;
    int wav_bytes=0;
    SPIFR *spifr = sp->read_spifr;
    FOB *fob_in, *fob_out;
    char *buff;
    int blen;
    char *proc="decompress_waveform " SPHERE_VERSION_STR;
    
    wav_bytes = spifr->status->user_sample_count * 
	spifr->status->user_channel_count * 
	    spifr->status->user_sample_n_bytes;
    
    /* the file must be decompressed, Question: Should it be done in memory? */
    if (wav_bytes > MAX_INTERNAL_WAVFORM)
	decomp_into_memory = FALSE;
    
    /* The file needs to be de_compressed into memory !!!! */
    /* 1. make an FOB struct for the uncompressed waveform to be read into d */
    /*    and the original file */
    /* 2. allocate memory for the entire waveform */
    /* 3. decompress the file */
    /* 4. Clean up the FOB struct for the file, moving the fp from the FOB */
    /* 5. reset the uncompressed FOB struct to the beginning of the memory */
		

    if (decomp_into_memory) {
	if (sp_verbose > 15)
	    fprintf(spfp,"Proc %s: Pre-buffering compressed data into memory\n",
		   proc);
	if (fob_create2(spifr->waveform->sp_fp, FPNULL, &fob_in, &fob_out) < 0)
	    return_err(proc,200,0,"Unable to setup for decompression");
	blen = spifr->status->file_channel_count * 
	    spifr->status->file_sample_count * 
		spifr->status->file_sample_n_bytes;
	if ((buff=mtrf_malloc(blen)) == CNULL){
	    fob_destroy(fob_in);
	    fob_destroy(fob_out);
	    return_err(proc,201,0,"Unable to malloc memory to decompress into");
	}
	fob_bufinit(fob_out, buff, blen);
    } else { /* decompress into a disk file */
	FILE *temp_fp;
	if (sp_verbose > 15)
	    fprintf(spfp,"Proc %s: Pre-buffering compressed data %s\n",
		   proc,"into a temporary file");
	spifr->status->temp_filename = sptemp_dirfile();
	if (spifr->status->temp_filename == CNULL)
	    return_err(proc,400,0,"Unable to create usable temporary file");
	if (sp_verbose > 15)
	    fprintf(spfp,"Proc %s: %s%d bytes long, using temp file %s\n",
		   proc, "Attempting to read a big file ",wav_bytes,
		   spifr->status->temp_filename);
	if ((temp_fp=fopen(spifr->status->temp_filename,TRUNCATE_UPDATEMODE)) 
	    == FPNULL) 
	    return_err(proc,401,0,
		       rsprintf("Unable to open temporary file %s",
				spifr->status->temp_filename));
	if (fob_create2(spifr->waveform->sp_fp, temp_fp, &fob_in, &fob_out) <0)
	    return_err(proc,402,0,"Unable to setup for decompression");
	
	/* the FILE pointer will be closed after completion of the           */
	/* decompression directly from the fob_in FOB pointer.  this is the  */
	/* bug which Francis caught.                                         */
	
	/* Note:  Do NOT set the waveform file to FPNULL here (see bugfix    */
	/* below.)  ***PSI***  24-Sep-1993                                   */
	/* spifr->waveform->sp_fp = FPNULL; */
	spifr->status->is_temp_file = TRUE;
    }
    if (sp_verbose > 15)
	fprintf(spfp,
		"Proc %s: before decompression, input file at position %d\n",
	       proc,fob_ftell(fob_in));
    switch (spifr->status->file_compress){
	char message[80];
      case SP_wc_shorten:
	if (sp_verbose > 15)
	    fprintf(spfp,"Proc %s: Executing Shorten Decompression\n",proc);
	if (setjmp(exitenv) == 0){
	    if (shorten_uncompress(fob_in, fob_out, message) < 0){
		fob_destroy(fob_in);
		fob_destroy(fob_out);
		return_err(proc,202,0,
			   rsprintf("Shorten Decompression Failed",message));
	    }
	} else {
	    fob_destroy(fob_in);
	    fob_destroy(fob_out);
	    return_err(proc,207,0,"Shorten Decompression aborted");
	}
	break;
      case SP_wc_wavpack:
	if(setjmp(exitenv) == 0){
	    if (sp_verbose > 15) fprintf(spfp,"before set progname\n");
	    wavpack_set_progname( "wavpack" );
	    if (sp_verbose > 15)
		fprintf(spfp,
			"Proc %s: Executing Wavpack Decompression\n",proc);
	    if (sp_verbose > 15)
		wavpack_dump_interface(spfp);
	    if (wavpack_unpack(fob_in, fob_out) < 0){
		fob_destroy(fob_in);
		fob_destroy(fob_out);
		return_err(proc,203,0,"Wavpack Decompression Failed");
	    }
	    wavpack_free_progname();
	} else {
	    fob_destroy(fob_in);
	    fob_destroy(fob_out);
	    return_err(proc,206,0,"Wavpack Decompression aborted");
	}
	break;
      case SP_wc_shortpack:
	if(setjmp(exitenv) == 0){
	    if (sp_verbose > 15)
		fprintf(spfp,
			"Proc %s: Executing Shortpack Decompression\n",proc);
	    if (shortpack_uncompress(fob_in, fob_out,
				     spifr->status->file_header) < 0){
		fob_destroy(fob_in);
		fob_destroy(fob_out);
		return_err(proc,203,0,"Shortpack Decompression Failed");
	    }
	} else {
	    fob_destroy(fob_in);
	    fob_destroy(fob_out);
	    return_err(proc,208,0,"Shortpack Decompression aborted");
	}		    
	break;
      default:
	return_err(proc,209,0,"Unable to decompress the requested format\n");
    }
    fob_rewind(fob_out);
    
    
    /**** Begin SRI bugfix. ****/
    
    /* If a temporary file is being used, close the waveform file BEFORE    */
    /* setting it to FPNULL.  ***PSI***  24-Sep-1993                        */
    
    if (spifr->status->is_temp_file) {
	if (sp_verbose > 15)
	    fprintf(spfp,"Proc %s: Closing waveform file \"%s\" (%d)\n",
		   proc, spifr->status->external_filename,
		   fileno(spifr->waveform->sp_fp));
	
	if (fclose(spifr->waveform->sp_fp))
	    return_err(proc, 403, 0,
		       rsprintf("Unable to close waveform file \"%s\" (%d)",
				spifr->status->external_filename,
				fileno(spifr->waveform->sp_fp)));
	spifr->waveform->sp_fp = FPNULL;
    }
    
    /**** End SRI bugfix. ****/
    
    if (! decomp_into_memory) { 
	/* Close the original file pointer to the SPHERE file */
	fclose(fob_in->fp);
    }
    fob_destroy(fob_in);	    
    
    spifr->waveform->sp_fob = fob_out;
    return_success(proc,0,0,"ok");
}
Example #17
0
/*
 *
 *  sp_read_data()
 *
 */
int sp_mc_read_data(void *buffer, size_t num_sample, SP_FILE *sp)
{
    char *proc="sp_read_data " SPHERE_VERSION_STR;
    SPIFR *spifr;
    int ret;
    int do_conversions, do_channel_selections;

    if (buffer == (void *)0) 
	return_err(proc,100,0,"Null memory buffer"); 
    if (sp == SPNULL)
	return_err(proc,101,0,"Null SPFILE structure");
    if (sp->open_mode != SP_mode_read) 
	return_err(proc,104,104,"Read on a file not opened for read");
#ifdef isnotansi
    if (num_sample < 0)
	return_err(proc,103,0,
		   rsprintf("Negative sample count %d",num_sample));
#endif
    spifr = sp->read_spifr;
    if (sp_verbose > 10) 
       fprintf(spfp,
	       "Proc %s: file %s, %d bytes/sample, %d channels, %d samples\n",
	       proc,spifr->status->external_filename,
	       spifr->status->user_sample_n_bytes,
	       spifr->status->user_channel_count,
	       num_sample);

    if (spifr->waveform->failed_checksum)
	return_err(proc,1001,0,"Pre-Read Checksum Test Failed");


    /***********************************************************************/
    /*     Perform all initializations to the sphere file to begin reading */

    if (spifr->status->read_occured_flag == FALSE) {
	spifr->status->read_occured_flag = TRUE;

	if (sp_verbose > 15) fprintf(spfp,
				     "Proc %s: Initializing read of data\n",
				     proc);

	if ((spifr->status->user_compress == SP_wc_none) &&
	    (spifr->status->file_compress != SP_wc_none)){
	    decompress_waveform(sp) ; 
	    if ((ret = sp_get_return_status()) != 0)
		return_err(proc,ret,0,
			   rsprintf("decompress_waveform failed, %s",
				    get_return_status_message()));
	}
	else {
	    /* The following code assumes that no pre-reading of the       */
	    /* waveform is needed                                          */

	    if ((spifr->waveform->sp_fob = 
		 fob_create(spifr->waveform->sp_fp)) == FOBPNULL)
		return_err(proc,300,0,
			"Unable to allocate a FOB 'File or Buffer' struct.");
	    spifr->waveform->sp_fp = FPNULL;
	}
	
    	/****************************************************/
	/**            INVARIANT ASSERTION:                **/
	/** The data is now in it's natural (decomp) form  **/
	/****************************************************/

	/************ Set up the file conversions ***********/
	/****************************************************/
	/*        Set up byte format the conversions        */
	if (spifr->status->user_sbf != spifr->status->file_sbf) 
	    if (((spifr->status->user_sbf == SP_sbf_01) &&
		 (spifr->status->file_sbf == SP_sbf_10)) ||
		((spifr->status->user_sbf == SP_sbf_10) &&
		 (spifr->status->file_sbf == SP_sbf_01)))
		fob_read_byte_swap(spifr->waveform->sp_fob);

	/********************************************************/
	/*  set up a translation buffer, for sample conversions */
	/*  and channel selections                              */

	/* are sample encodings necessary ????? */
	do_conversions = FALSE;
	if (spifr->status->user_encoding != spifr->status->file_encoding) {
	    if (((spifr->status->file_encoding == SP_se_ulaw) &&
		 (spifr->status->user_encoding == SP_se_pcm2)) || 
		((spifr->status->file_encoding == SP_se_pcm2) &&
		 (spifr->status->user_encoding == SP_se_ulaw))) 
		do_conversions = TRUE;
	    else
		return_err(proc,400,0,
			   "Unable to convert sample types ... for now\n");
	}

	/* are channel selection requested ????? */
	do_channel_selections = FALSE;
	if (spifr->status->channels != CHANNELSNULL) 
	    do_channel_selections = TRUE;

	if (do_conversions || do_channel_selections || 
	    (spifr->status->user_data_fmt == SP_df_array)){
	    /* allocate the memory for the file data buffer   */
	    /*  IF it's a legal transformation                */
	    spifr->waveform->file_data_buffer_len = TRANSLATION_LEN *
		spifr->status->file_sample_n_bytes *
		    spifr->status->file_channel_count;
	    if (sp_verbose > 15)
		fprintf(spfp,"Proc %s: Alloc %d (%d*%d*%d) bytes%s\n",
		       proc,	
		       spifr->waveform->file_data_buffer_len,TRANSLATION_LEN,
		       spifr->status->file_sample_n_bytes,
		       spifr->status->file_channel_count,
		       " for file data buffer");
	    if ((spifr->waveform->file_data_buffer = 
		 (void *)
		       mtrf_malloc(spifr->waveform->file_data_buffer_len)) ==
	        (void *)0)
		return_err(proc,500,0,
			"Unable to alloc memory for the translation buffer");
	} 
	if (do_conversions && 
	    (do_channel_selections ||
	     (spifr->status->user_data_fmt == SP_df_array))){
	    spifr->waveform->converted_buffer_len = TRANSLATION_LEN *
		spifr->status->user_sample_n_bytes * 
		    spifr->status->file_channel_count;
	    if (sp_verbose > 15)
		fprintf(spfp,"Proc %s: Alloc %d (%d*%d*%d) bytes %s\n",
		       proc,	
		       spifr->waveform->converted_buffer_len,TRANSLATION_LEN,
		       spifr->status->user_sample_n_bytes,
		       spifr->status->file_channel_count,
		       "for converted data buffer");
	    if ((spifr->waveform->converted_buffer = 
		 (void *)
		       mtrf_malloc(spifr->waveform->converted_buffer_len)) ==
		(void *)0)
		return_err(proc,550,0,
			"Unable to alloc memory for the translation buffer");
	}
	if (spifr->status->user_data_fmt == SP_df_array){
	    spifr->waveform->interleave_buffer_len = TRANSLATION_LEN *
		spifr->status->user_sample_n_bytes * 
		    spifr->status->user_channel_count;
	    if (sp_verbose > 15)
		fprintf(spfp,"Proc %s: Alloc %d (%d*%d*%d) bytes %s\n",
			proc,	
			spifr->waveform->interleave_buffer_len,TRANSLATION_LEN,
			spifr->status->user_sample_n_bytes,
			spifr->status->user_channel_count,
			"for interleave data buffer");
	    if ((spifr->waveform->interleave_buffer = 
		 (void *)
		       mtrf_malloc(spifr->waveform->interleave_buffer_len)) ==
		(void *)0)
		return_err(proc,600,0,
			"Unable to alloc memory for the interleave buffer");
	}
	    

	/* pre-verify the waveform data */
	if (spifr->status->extra_checksum_verify){
	    pre_verify_checksum(sp);
	    if ((ret = sp_get_return_status()) != 0)
		return_err(proc,ret,0,
			   rsprintf("pre_verify_checksum failed, %s",
				    get_return_status_message()));
	}
    }

    if (sp_verbose > 15) fprintf(spfp,
				 "Proc %s: current file position %d\n",proc,
				fob_ftell(spifr->waveform->sp_fob));

    ret = read_data_in(buffer, num_sample, sp);
    if (sp_get_return_type() == RETURN_TYPE_ERROR)
	return_err(proc,sp_get_return_status(),0,
		   rsprintf("read_data_in failed, %s",
			    get_return_status_message()));
    return_success(proc,0,ret,"ok");
}
Example #18
0
static int read_data_in(void *buffer, size_t num_sample, SP_FILE *sp){ 
    SPIFR *spifr;
    char *proc="    read_data_in " SPHERE_VERSION_STR;
    int samples_read = 0;
    int in_samples=0, o, oc, s, c, ret;
    int block=0;
    char *in_block=(char *)0, *out_block=(char *)0;
    char *current_data_location = (char *)0;
    char *out_conversion_block=(char *)0,*in_conversion_block=(char *)0;
    char *next_out_block=(char *)0;
    short *sh_data=(short *)0;
    unsigned char *ulaw_data = (unsigned char *)0;
    SP_CHECKSUM checksum=0, block_checksum=0;
    int file_record_size;
    char **arr_buff = (char **)0, *arr;
       
    spifr = sp->read_spifr;

    file_record_size = spifr->status->file_sample_n_bytes *
	spifr->status->file_channel_count;
    
    while ((samples_read < num_sample) &&
	   (! fob_feof(spifr->waveform->sp_fob))){
	if (sp_verbose > 16)
	    fprintf(spfp,"Proc %s:  Beginning block %d\n",proc,block);
	
	/* read in either a block or the whole chunk if data */
	if (spifr->waveform->file_data_buffer != CNULL) {
	    if (sp_verbose > 15) 
		fprintf(spfp,
			"Proc %s: reading a block into temporary storage\n",
			proc);
	    in_block = (void *)spifr->waveform->file_data_buffer;
	    current_data_location = in_block;
	    if ((in_samples = (num_sample - samples_read))>
		TRANSLATION_LEN)
		in_samples = TRANSLATION_LEN;
	    ret = fob_fread(in_block,  file_record_size,
			    in_samples, spifr->waveform->sp_fob);
	    if (ret < 0)
		return_err(proc,105,0,"Unable to read data");
	} else { 
	    /* there was no change in the sample coding so just read    */
	    /* in the data */
	    if (sp_verbose > 15)
		fprintf(spfp,
			"Proc %s: read a block WITHOUT coding conversions\n",
			proc);
	    ret = fob_fread(buffer, file_record_size,
			    num_sample, spifr->waveform->sp_fob);
	    if (ret < 0)
		return_err(proc,107,0,"Unable to read data");
	    in_block = (void *)buffer;
	    current_data_location = in_block;
	}

	if (sp_verbose > 16)
	    fprintf(spfp,
		    "Proc %s: block read of %d Samples, expected %d\n",
		    proc,ret,num_sample);
	in_samples = ret;
	/**** ret is the number of samples per time period read in from */
	/**** the file */
	
	/**** Do the checksum computation before format changes occur ***/
	switch (spifr->status->file_encoding){
	  case SP_se_pcm2:
	    
	    /* DON'T SWAP if the coding type hasn't changed, and the    */
	    /* output SBF == natural_sbf                                */
	    /*    OR         if the coding changed and the file SBF     */
	    /*               is == natural SBF */
	    if (((spifr->status->user_encoding == SP_se_pcm2) &&
		 (spifr->status->natural_sbf == spifr->status->user_sbf))
		||
		((spifr->status->user_encoding != SP_se_pcm2) &&
		 (spifr->status->natural_sbf == spifr->status->file_sbf))){
		if (sp_verbose > 16)
		    fprintf(spfp,
			    "Proc %s: Not Swapping for checksum\n",proc);
		checksum =
		    sp_compute_short_checksum((void *)in_block,in_samples*
				      spifr->status->file_channel_count, 
					      FALSE);
	    } else {
		if (sp_verbose > 16)
		    fprintf(spfp,"Proc %s: Swapping for checksum\n",proc);
		checksum = 
		    sp_compute_short_checksum((void *)in_block,
		       in_samples*spifr->status->file_channel_count, TRUE);
	    }
	    break;
	  default: {
	      int n;
	      n = in_samples * spifr->status->file_channel_count *
		  spifr->status->file_sample_n_bytes;
	      if (sp_verbose > 16)
		  fprintf(spfp,"Proc %s: Computing char checksum %d bytes\n",
			  proc,n);
	      checksum = sp_compute_char_checksum(in_block, n);
	      break;
	  }
	}
	
	spifr->waveform->checksum = 
	    sp_add_checksum(spifr->waveform->checksum,checksum);
	block_checksum = checksum;
	
	/***  FINISHED WITH THE CHECKSUMS ***/
	samples_read += in_samples;
	spifr->waveform->samples_read += in_samples;
	
	if (sp_eof(sp))
	    /* only check this if the user_sample count is real */
	    /* Added June 22, 1994 by JGF                       */
	    if (spifr->status->user_sample_count != 999999999){
		if (spifr->waveform->samples_read !=
		    spifr->status->user_sample_count){
		    spifr->waveform->read_premature_eof = TRUE;
		    return_err(proc,500,0,
			       rsprintf("Premature End-of-File %d read != %d",
					spifr->waveform->samples_read,
					spifr->status->user_sample_count));

		}
	    }
	if (spifr->waveform->samples_read ==
	    spifr->status->user_sample_count){
	    if ((! spifr->status->ignore_checksum) &&
		(spifr->waveform->checksum !=
		 spifr->status->file_checksum)){
		spifr->waveform->failed_checksum = TRUE;	    
		return_err(proc,1000,0,
			   rsprintf("%sComputed %d != Expected %d",
				    "Checksum Test Failed ",
				    spifr->waveform->checksum,
				    spifr->status->file_checksum));
	    }
	}
	
	/****   Do the sample coding conversions  ****/
	if (((spifr->status->file_encoding == SP_se_ulaw) &&
	     (spifr->status->user_encoding == SP_se_pcm2)) || 
	    ((spifr->status->file_encoding == SP_se_pcm2) &&
	     (spifr->status->user_encoding == SP_se_ulaw))) {
	    int samples_to_change = 
		in_samples*spifr->status->file_channel_count;

	    if ((spifr->status->channels != CHANNELSNULL) ||
		(spifr->status->user_data_fmt == SP_df_array)){
		out_conversion_block = next_out_block = 
		    (char *)spifr->waveform->converted_buffer;
		if (sp_verbose > 16)
		    fprintf(spfp,"Proc %s: using converted buffer output\n",
			    proc);
	    }
	    else {
		out_conversion_block = (char *)buffer;
		if (sp_verbose > 16)
			fprintf(spfp,"Proc %s: using buffer output argument\n",
				proc);
		if (next_out_block == (char *)0)
		    next_out_block = out_conversion_block;
	    }
	    
	    if (sp_verbose > 16)
		fprintf(spfp,
			"Proc %s: converting %d (%d*%d) samples\n",proc,
			samples_to_change,in_samples,
			spifr->status->file_channel_count);
	    if ((spifr->status->file_encoding == SP_se_ulaw) &&
		(spifr->status->user_encoding == SP_se_pcm2)) {
		/* now convert the block into the buffer */
		if (sp_verbose > 16)
		    fprintf(spfp,"Proc %s: Converting ULAW to PCM", proc);
		
		/* set up some pointers */
		ulaw_data =
		    (unsigned char *)spifr->waveform->file_data_buffer;
		sh_data = (short *)next_out_block ;
		
	       /*** This was a bug, it used to compare to sp_sbf_10 ***/
		if (spifr->status->user_sbf != get_natural_sbf(2)){
		    if (sp_verbose > 16)
			fprintf(spfp," outputting 01 format bytes\n");
		    for (o=0; o < samples_to_change; o++) {
			*sh_data = ulaw2linear_01_sbf_lut[*ulaw_data];
			sh_data ++; ulaw_data ++;
		    }
		} else {
		    if (sp_verbose > 16)
			fprintf(spfp," outputting 10 format bytes\n");
		    for (o=0; o < samples_to_change; o++) {
			*sh_data = ulaw2linear_10_sbf_lut[*ulaw_data];
			sh_data ++; ulaw_data ++;
		    }
		}
		next_out_block = (char *)sh_data;
	    } else if ((spifr->status->file_encoding == SP_se_pcm2) &&
		       (spifr->status->user_encoding == SP_se_ulaw)) {
		if (sp_verbose > 16)
		    fprintf(spfp,"Proc %s: Performing read translation%s\n",
			    proc," from PCM2 to ULAW");
		/* set up some pointers */
		sh_data = (short *)spifr->waveform->file_data_buffer;
		ulaw_data = (unsigned char *)next_out_block;
		

		if (spifr->status->file_sbf != spifr->status->natural_sbf){
		    char *p , temp; short odata;
		    if (sp_verbose > 16)
			fprintf(spfp,
				"Proc %s: Swapping input shorts\n", proc);
		    for (o=0; o < samples_to_change; o++) {
			/* Pre swap the bytes */
			
			odata = *sh_data;
			p = (char *) &odata;
			temp = *p;
			*p = *(p + 1);
			*(p + 1) = temp;
			/* fprintf(spfp,
			   "  %d -> %x -> %x\n",o,*sh_data,odata); */
			
			*ulaw_data = linear2ulaw(odata);
			sh_data ++; ulaw_data ++;
		    }
		}else
		    for (o=0; o < samples_to_change; o++) {
			*ulaw_data =linear2ulaw(*sh_data);
			sh_data ++; ulaw_data ++;
		    }
		next_out_block = (char *)ulaw_data;
	    } else 
		return_err(proc,109,0,"Internal Error");
	    
	    current_data_location = out_conversion_block;
	}

	/* Do the channel conversions */
	if (spifr->status->channels != CHANNELSNULL){
	    int in_recsize, out_recsize, in_chancnt, out_chancnt;
	    
	    if (sp_verbose > 15)
		fprintf(spfp,"Proc %s: Converting channels\n",proc);
	    if (spifr->status->user_data_fmt == SP_df_array)
		out_block = spifr->waveform->interleave_buffer;
	    else
		if (out_block == (void *)0)
		    out_block = (void *)buffer;
	    current_data_location = out_block;
		
	    if (spifr->status->file_encoding !=	spifr->status->user_encoding)
		in_conversion_block = 
		    (void *)spifr->waveform->converted_buffer;
	    else 
		if (in_conversion_block == (void *)0) 
		    in_conversion_block =
			(void *)spifr->waveform->file_data_buffer;

	    
	    in_chancnt = spifr->status->file_channel_count;
	    in_recsize = spifr->status->user_sample_n_bytes *  in_chancnt;
	    out_chancnt = spifr->status->user_channel_count;
	    out_recsize = spifr->status->user_sample_n_bytes * out_chancnt;
	    
	    for (oc=0; oc<spifr->status->channels->num_chan; oc++)
		if (spifr->status->channels->ochan[oc].num_origin == 1){
		    char *in_ptr, *out_ptr;
		    in_ptr = in_conversion_block + 
                        ((spifr->status->channels->ochan[oc].orig_channel[0]-1)
			 * spifr->status->user_sample_n_bytes);
		    out_ptr = out_block + 
			(oc * spifr->status->user_sample_n_bytes);
		    
		    for (s=0; s<in_samples; s++){
			memcpy(out_ptr,in_ptr,
			      spifr->status->user_sample_n_bytes);
			in_ptr += in_recsize;
			out_ptr += out_recsize;
		    }
		} else {
		    if (spifr->status->user_encoding == SP_se_pcm2) {
			/* do a straight add */
			ORIGINATION_CHAN *ochn;
			short *in_ptr, *out_ptr;
			int sum, ch;
			if (sp_verbose > 15)
			    fprintf(spfp,
				    "Proc %s: Adding PCM data %s %d\n",
				    proc, "channel",oc);
			in_ptr = (short *)in_conversion_block;
			out_ptr = (short *)out_block;
			ochn = &(spifr->status->channels->ochan[oc]);
			
			if (spifr->status->user_sbf== get_natural_sbf(2)){
			    for (s=0; s<in_samples; s++){
				for (ch=0, sum=0; ch < ochn->num_origin; 
				     ch++){
				    sum +=*(in_ptr+ochn->orig_channel[ch]-1);
				}
				if (sum > 32767) sum = 32767;
				else if (sum < -32768) sum = -32768;
				*(out_ptr + oc) = (short)sum;
				in_ptr += in_chancnt;
				out_ptr += out_chancnt;
			    }
			} else {
			    short x;
			    if (sp_verbose > 15)
				fprintf(spfp,"Proc %s: %s %d\n", proc,
					"Adding Swapped PCM data channel",oc);
			    for (s=0; s<in_samples; s++){
				/*    fprintf(spfp,"sample %d\n",s);  */
				for (ch=0, sum=0; ch < ochn->num_origin; 
				     ch++){
				    x = *(in_ptr+ochn->orig_channel[ch]-1);
				    swap_bytes(x);
				    sum += x;
				    /* fprintf(spfp,"   %d  =  %d\n",x,sum); */
				}
				if (sum > 32767) x = 32767;
				else if (sum < -32768) x = -32768;
				else x = sum;
				swap_bytes(x);
				*(out_ptr + oc) = (short)x;
				in_ptr += in_chancnt;
				out_ptr += out_chancnt;
			    }
			    
			}			   
		    } else if (spifr->status->user_encoding == SP_se_ulaw){
			/* do an add on ulaw data */
			ORIGINATION_CHAN *ochn;
			unsigned char *in_ptr, *out_ptr;
			int sum, ch;
			if (sp_verbose > 15)
			    fprintf(spfp,
				    "Proc %s: Adding ULAW data %s %d\n",
				    proc, "channel",oc);
			in_ptr = (unsigned char *)in_conversion_block;
			out_ptr = (unsigned char *)out_block;
			ochn = &(spifr->status->channels->ochan[oc]);
			
			for (s=0; s<in_samples; s++){
			    for (ch=0, sum=0; ch < ochn->num_origin;
				 ch++){
				sum +=
				    ulaw2linear_10_sbf_lut[
					*(in_ptr+ochn->orig_channel[ch]-1)];
			    }
			    if (sum > 32767) sum = 32767;
			    else if (sum < -32768) sum = -32768;
			    *(out_ptr + oc) =  linear2ulaw(sum);
			    in_ptr += in_chancnt;
			    out_ptr += out_chancnt;
			}
		    }
		}
	    out_block += out_recsize * in_samples;
	}
	
	if (spifr->status->user_data_fmt == SP_df_array){
	    /* convert the data from it's interleaved form */
	    /* to an array format                          */
	    int snb = spifr->status->user_sample_n_bytes;
	    int chcnt = spifr->status->user_channel_count;
	    int recsize = snb * chcnt;
	    char *in_dat;
	    
	    if (sp_verbose > 15)
		fprintf(spfp,"Proc %s: Converting data to an array\n",
			proc);
	    
	    if (arr_buff == (char  **)0)
		arr_buff = (char **)buffer;
	    for (c=0; c<chcnt; c++){
		in_dat =  current_data_location + (snb * c) ;
		arr = arr_buff[c] + ((samples_read-in_samples) * snb);
		
		for (s=0; s<in_samples; s++,arr+=snb,in_dat+=recsize)
		    memcpy(arr,in_dat,snb);
	    }
	}
	if (sp_verbose > 11){
	    fprintf(spfp,"Proc %s: Block %d: Requested %d samples",
		    proc,block++,num_sample);
	    fprintf(spfp," read %d total %d, Checksum %d (BC %d)\n",
		    ret,spifr->waveform->samples_read,
		    block_checksum,
		    spifr->waveform->checksum);
	}
    }
    return_success(proc,0,samples_read,"ok");
}
Example #19
0
void find_minimal_paths(NET_ALIGN *net_ali, int a, int b, int *rlev,float wwd(void *, void *, int (*cmp)(void *, void *)),NETWORK *net_a, PATH *path,int *count)
{

    ARC_LIST_ATOM *oarc_a, *oarc_b;
    ARC *arc_a, *arc_b;
    int la, lb, cost;
    int arc_a_id, arc_b_id,d;
    int db = 0;
    char buf[100];

    sprintf(buf,rsprintf("%%0%ds",*rlev),"");
    if (a == 0 && b == 0){
	if (db > 5) printf("END recursion\n");
	if (db > 2) {
	    printf("Count = %d\n",*count);
	    PATH_print(path,stdout,100);
	}
	(*count)++;
	return;
    }

    arc_a = net_ali->arcset_a.arcs[a];
    arc_b = net_ali->arcset_b.arcs[b];
    cost  = net_ali->cell[a][b].min_d;

    if (db > 5){
	printf("%sCurrent position: D(%d,%d) = %d\n",buf,a,b,cost);
	printf("%s    ",buf); print_arc(arc_a,0);
	printf("%s    ",buf); print_arc(arc_b,0);
    }

    /* loop through the net A in_arcs, seaching for possible paths */
    for (oarc_a = arc_a->from_node->in_arcs; oarc_a != NULL;
	 oarc_a=oarc_a->next){
	if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),oarc_a->arc,
				       a)) <0){
	    fprintf(stderr,"Error: find_arcset_id for Net A failed\n");
	    exit(1);
	}
	if (db > 5) {
	    printf("%s    Checking deletion arcs:\n",buf);
	    printf("%s        A:",buf); print_arc(oarc_a->arc,0);
	}
	if (net_ali->cell[arc_a_id][b].min_d <= cost && 
	    (wwd(arc_a->data,NULL_WORD,net_a->arc_func.equal) +
	     net_ali->cell[arc_a_id][b].min_d) <= cost){
	    if (db > 5) printf("%s            Possible PATH\n",buf);
	    if (db > 5) printf("%s            *****  Delete %s\n",buf,
			       ((WORD*)(arc_b->data))->value);
	    *rlev += 2;
	    PATH_append(path,arc_a->data,0,P_DEL);
	    find_minimal_paths(net_ali,arc_a_id,b,rlev,wwd,net_a,path,count);
	    PATH_remove(path);
	    *rlev -= 2;
	}
    }
    for (oarc_b = arc_b->from_node->in_arcs; oarc_b != NULL;
	 oarc_b = oarc_b->next){
	if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),oarc_b->arc,
				       b)) < 0){
	    fprintf(stderr,"Error: find_arcset_id for Net B failed\n");
	    exit(1);
	}
	if (db > 5) {
	    printf("%s    Checking insertion arcs:\n",buf);
	    printf("%s        B:",buf); print_arc(oarc_b->arc,0);
	}
	if (net_ali->cell[a][arc_b_id].min_d <= cost &&
	    (wwd(NULL_WORD,arc_b->data,net_a->arc_func.equal) +
	     net_ali->cell[a][arc_b_id].min_d) <= cost){
	    if (db > 5) printf("%s            Possible PATH\n",buf);
	    if (db > 5) printf("%s            *****  Insert %s\n",buf,
			       ((WORD*)(arc_b->data))->value);
	    *rlev += 2;
	    PATH_append(path,0,arc_b->data,P_INS);
	    find_minimal_paths(net_ali,a,arc_b_id,rlev,wwd,net_a,path,count);
	    PATH_remove(path);
	    *rlev -= 2;
	}
    }


    for (oarc_a = arc_a->from_node->in_arcs; oarc_a != NULL;
	 oarc_a=oarc_a->next){
	if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),oarc_a->arc,
				       a)) <0){
	    fprintf(stderr,"Error: find_arcset_id for Net A failed\n");
	    exit(1);
	}
	for (oarc_b = arc_b->from_node->in_arcs; oarc_b != NULL;
	     oarc_b = oarc_b->next){
	    if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),oarc_b->arc,
					   b)) < 0){
		fprintf(stderr,"Error: find_arcset_id for Net B failed\n");
		exit(1);
	    }
	    if (db > 5) {
		printf("%s    Checking substitution arcs: (%d,%d)\n",buf,
		       arc_a_id,arc_b_id);
		printf("%s        A:",buf); print_arc(oarc_a->arc,0);
		printf("%s        B:",buf); print_arc(oarc_b->arc,0);
	    }
	    if (net_ali->cell[arc_a_id][arc_b_id].min_d <= cost &&
		((d=wwd(arc_a->data,arc_b->data,net_a->arc_func.equal)) + 
		 net_ali->cell[arc_a_id][arc_b_id].min_d) <= cost){
		if (db > 5){ 
		    printf("%s            Possible PATH\n",buf);
		    printf("%s            *****  Substitute %s and %s\n",buf,
			   ((WORD*)(arc_a->data))->value,
			   ((WORD*)(arc_b->data))->value);
		}
		*rlev += 2;
		PATH_append(path,arc_a->data,arc_b->data,(d==0)?P_CORR:P_SUB);
		find_minimal_paths(net_ali,arc_a_id,arc_b_id,rlev,wwd,net_a,path,count);
		PATH_remove(path);
		*rlev -= 2;
	    }
	}	
    }
    la = a;	lb = b;
    a = net_ali->cell[la][lb].back_a;
    b = net_ali->cell[la][lb].back_b;
}
Example #20
0
int sp_copy_header(SP_FILE *spin, SP_FILE *spout)
{
    char *proc_name="sp_copy_header " SPHERE_VERSION_STR;
    struct header_t *h;
    int i;
    SP_INTEGER l_int;
    SP_REAL real;

    if (spin->open_mode == SP_mode_read)
	h = spin->read_spifr->header;
    else if (spin->open_mode == SP_mode_write)
	h = spin->write_spifr->header;
    else
	return_err(proc_name,100,100,"Unable to dup header opened for update");

    /* just loop through all the names, adding each field */
    for (i=0; i < h->fc ; i++){
	switch (h->fv[i]->type){
	  case T_STRING:
	    if (sp_h_set_field(spout,h->fv[i]->name,
			       h->fv[i]->type,h->fv[i]->data) != 0){
		sp_print_return_status(spfp);
		return_err(proc_name,200,200,
			   rsprintf("Unable to copy STRING field '%s'",
				    h->fv[i]->name));
	    }
	    break;
	  case T_INTEGER:
	    l_int=atol(h->fv[i]->data);
	    if (sp_h_set_field(spout,h->fv[i]->name,
			       h->fv[i]->type,&l_int) != 0){
		sp_print_return_status(spfp);
		return_err(proc_name,200,200,
			   rsprintf("Unable to copy INTEGER field '%s'",
				    h->fv[i]->name));
	    }
	    break;
	  case T_REAL:
	    real=atof(h->fv[i]->data);
	    if (sp_h_set_field(spout,h->fv[i]->name,h->fv[i]->type,&real) != 0){
		sp_print_return_status(spfp);
		return_err(proc_name,200,200,
			   rsprintf("Unable to copy REAL field '%s'",
				    h->fv[i]->name));
	    }
	    break;
	}
    }
    /* Special Check, if the input file is a pipe and the sample_count  */
    /* field is missing, (this is legal), AND the output is a disk file */
    /* add a dummy sample_count field to the output header              */
    /*   Added June 22, 1994 */
    {   int type, size, is_disk_file, out_is_disk_file;
	is_disk_file = (spin->open_mode == SP_mode_read) ? 
	    spin->read_spifr->status->is_disk_file :
		spin->write_spifr->status->is_disk_file;
	out_is_disk_file = (spout->open_mode == SP_mode_read) ? 
	    spout->read_spifr->status->is_disk_file :
		spout->write_spifr->status->is_disk_file;
	if (! is_disk_file)
	    if (sp_get_field(h,SAMPLE_COUNT_FIELD,&type,&size) < 0){
		/* add the field to the header */
		l_int = 999999999;
		if (sp_h_set_field(spout,SAMPLE_COUNT_FIELD,
				   T_INTEGER,&l_int) != 0){
		    sp_print_return_status(spfp);
		    return_err(proc_name,400,400,
			       rsprintf("Unable to copy INTEGER field '%s'",
				    h->fv[i]->name));
		}
	    }
    }
    if (sp_set_default_operations(spout) != 0)
	return_err(proc_name,300,300,
		   "Unable to set default operations duplicated file");
    return_success(proc_name,0,0,"ok");
}
Example #21
0
int sp_set_default_operations(SP_FILE *sp)
{
    SP_INTEGER l_int;
    char *str, *proc="sp_set_default_operations " SPHERE_VERSION_STR;
    SPIFR *spifr;
    int ret;

    if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc);
    if (sp == SPNULL) return_err(proc,100,100,"Null SPFILE pointer");
    if ((sp->open_mode == SP_mode_read) || (sp->open_mode == SP_mode_update))
        spifr = sp->read_spifr;
    else if (sp->open_mode == SP_mode_write)
        spifr = sp->write_spifr;
    else
        return_err(proc,100,100,"Unknown File Mode");

    /**********************************************************************/
    /*    The following fields are REQUIRED for Read operations,          */
    /*                      NO Exceptions                                 */

    if (sp_h_get_field(sp,SAMPLE_COUNT_FIELD,T_INTEGER,(void *)&l_int) != 0){
	/* ----------  OLD CODE  ---------------------
	       return_err(proc,101,101,rsprintf("Missing '%s' header field",
	                                        SAMPLE_COUNT_FIELD));
	    spifr->status->user_sample_count =
		spifr->status->file_sample_count = (int)l_int;*/
        /*  added a condition to permit a pipe to NOT have a sample_count */
	/*  field.  ADDED June 22, 1993   */ 
	if (spifr->status->is_disk_file){
	    return_err(proc,101,101,rsprintf("Missing '%s' header field",
					     SAMPLE_COUNT_FIELD));
	} else { /* Sample counts may be missing from piped files */
	    spifr->status->user_sample_count =
		spifr->status->file_sample_count = 999999999;	    
	} 
    } else
	spifr->status->user_sample_count =
	    spifr->status->file_sample_count = (int)l_int;
    if (l_int <= 0)
	return_err(proc,108,108,
		   rsprintf("Field '%s' value out of range, %d <= 0",
			    SAMPLE_COUNT_FIELD,l_int));

    if (sp_h_get_field(sp,SAMPLE_N_BYTES_FIELD,
		       T_INTEGER,(void *)&l_int) != 0)
	return_err(proc,104,104,
		   rsprintf("Missing '%s' header field",
			    SAMPLE_N_BYTES_FIELD));
    spifr->status->user_sample_n_bytes = 
	spifr->status->file_sample_n_bytes = (int)l_int;
    if (l_int <= 0)
	return_err(proc,108,108,
		   rsprintf("Field '%s' value out of range, %d <= 0",
			    SAMPLE_N_BYTES_FIELD,l_int));

    if (sp_h_get_field(sp,CHANNEL_COUNT_FIELD,T_INTEGER,(void *)&l_int) != 0)
	return_err(proc,105,105,
		   rsprintf("Missing '%s' header field",
			    CHANNEL_COUNT_FIELD));
    spifr->status->user_channel_count = spifr->status->file_channel_count =
	(int)l_int;
    if (l_int <= 0)
	return_err(proc,108,108,
		   rsprintf("Field '%s' value out of range, > 0",
			    CHANNEL_COUNT_FIELD,l_int));

    /**********************************************************************/
    /* The following fields may exist, if they do not, there is a default */

    /***** NOTE:  only set the file_sbf, Sp_set_data_mode is called to    */
    /***** set the user_sbf                                               */
    if ((ret=sp_h_get_field(sp,SAMPLE_BF_FIELD,T_STRING,(void *)&str)) != 0){
	if ((spifr->status->file_sbf = 
	     get_natural_sbf(spifr->status->user_sample_n_bytes)) == 
	    SP_sbf_null)
	    return_err(proc,107,107,
		       rsprintf("Unable to read sample sizes of %d bytes",
				spifr->status->user_sample_n_bytes));
    } else {  /* str holds the sample_byte_format_value */
	ret = parse_sample_byte_format(str,&(spifr->status->file_sbf));
	if (ret == 1000) { 
	    /* then the file was compressed using change_wav_format */
	    spifr->status->file_sbf =
		get_natural_sbf(spifr->status->user_sample_n_bytes);
	    spifr->status->file_compress = SP_wc_shortpack;

	    if ((h_set_field(spifr->header,SAMPLE_BF_FIELD,
			     T_STRING,get_natural_byte_order(2)) != 0) ||
		(h_set_field(spifr->status->file_header,SAMPLE_BF_FIELD,
			     T_STRING,get_natural_byte_order(2)) != 0)){
		sp_print_return_status(spfp);
		mtrf_free(str);
		return_err(proc,110,110,
			   rsprintf("Unable to re-set sample byte format%s",
				    "field for a shortpacked file"));
	    }
	    if ((h_set_field(spifr->header,SAMPLE_CODING_FIELD,T_STRING,
			     rsprintf("pcm,embedded-%s",str)) != 0) ||
		(h_set_field(spifr->status->file_header,SAMPLE_CODING_FIELD,
			     T_STRING,rsprintf("pcm,embedded-%s",str)) !=0)){
		sp_print_return_status(spfp);
		mtrf_free(str);
		return_err(proc,111,111,
			   rsprintf("Unable to re-set sample coding field%s",
				    "for a shortpacked file"));
	    }
	} else if (ret != 0) { /* there really was an error */
	    mtrf_free(str);
	    return_err(proc,106,106,
		       "Unable to parse the 'sample_byte_format' field");
	}
	mtrf_free(str);
    }
              /***** field break *****/	    
    if (sp_h_get_field(sp,SAMPLE_CODING_FIELD,T_STRING,(void *)&str) != 0)
	if (spifr->status->user_sample_n_bytes == 1)
	    str = mtrf_strdup("ulaw");
	else
	    /* the default, since old Corpora are missing this field */
	    str = mtrf_strdup("pcm"); 
    
    if (parse_sample_coding(str,spifr->status->file_sample_n_bytes,
			    &(spifr->status->file_encoding),
			    &(spifr->status->file_compress)) != 0){
	mtrf_free(str);
	print_return_status(spfp);
	return_err(proc,107,107,
	   rsprintf("Unable to parse sample_coding value '%s' header field",
			    str));
    }
    mtrf_free(str);

    /*********************************************************************/
    /*    The following fields are conditionally required.               */
    ret = sp_h_get_field(sp,SAMPLE_RATE_FIELD,T_INTEGER,(void *)&l_int);
    switch (spifr->status->file_encoding) {
      case SP_se_pcm1:
      case SP_se_pcm2:
      case SP_se_ulaw:
	if (ret != 0)
	    return_err(proc,102,102,
		       rsprintf("Header field '%s' missing, but required%s",
				SAMPLE_RATE_FIELD," for waveform data"));
	spifr->status->user_sample_rate = spifr->status->file_sample_rate =
	    (int)l_int;
	break;
      case SP_se_raw:
      default:
	spifr->status->user_sample_rate = spifr->status->file_sample_rate =0;
    }
	  
    /***********************************************************************/
    /*    The following fields are OPTIONAL, but if they exist, there is a */
    /*    special purpose for them                                         */

    if (sp_h_get_field(sp,SAMPLE_CHECKSUM_FIELD,T_INTEGER,(void *)&l_int)==0)
	spifr->status->file_checksum = l_int;
    else {
	spifr->status->ignore_checksum = TRUE;
    }

    /*********************/
    /* Consitency checks */
    /*********************/

    if (spifr->status->file_encoding == SP_se_ulaw &&
	spifr->status->file_sample_n_bytes != 1) 
	return_err(proc,120,120,
		   rsprintf("Ulaw encoding requires a 1 byte sample,%s %d",
			    " however the header value is",
			    spifr->status->file_sample_n_bytes));
    if (spifr->status->file_encoding == SP_se_pcm1 &&
	spifr->status->file_sample_n_bytes != 1) 
	return_err(proc,120,120,
		   rsprintf("PCM1 encoding requires a 1 byte sample, %s %d",
			    "however the header value is",
			    spifr->status->file_sample_n_bytes));
    if (spifr->status->file_encoding == SP_se_pcm1 &&
	spifr->status->file_sample_n_bytes != 2) 
	return_err(proc,120,120,
		   rsprintf("PCM2 encoding requires a 2 byte sample, %s %d",
			    "however the header value is",
			    spifr->status->file_sample_n_bytes));

    /********************************/
    /* set up the default decodings */

    if (sp->open_mode == SP_mode_read)
	if (sp_set_data_mode(sp,"") != 0){
	    print_return_status(spfp);
	    return_err(proc,110,110,
		       rsprintf("Unable to set up default encodings %s",
				"on file opened for read"));
	}
    else if (sp->open_mode == SP_mode_update)
	if (sp_set_data_mode(sp,"SE_ORIG:SBF_ORIG") != 0){
	    print_return_status(spfp);
	    return_err(proc,111,111,
	     "Unable to set up default encodings on file opened for update");
	}

    if (sp_verbose > 11) fprintf(spfp,"Proc %s: Returning 0\n",proc);
    return_success(proc,0,0,"ok");
}
Example #22
0
/*
 *  sp_open
 *
 */
SP_FILE *sp_open(char *filename, char *mode)
{
    SP_FILE *tsp;
    char *errmsg, *proc="sp_open " SPHERE_VERSION_STR, *fopen_mode;
    enum SP_file_open_mode current_mode;

    if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc);
    if (filename == CNULL) 
	return_err(proc,101,SPNULL,"Null filename string");
    if (mode == CNULL) 
	return_err(proc,101,SPNULL,"Null file mode string");

    if (sp_verbose > 10) fprintf(spfp,"Proc %s: file '%s' mode '%s'\n",
				 proc,filename,mode);

    if ((tsp=sp_alloc_and_init_sphere_t()) == SPNULL)
        return_err(proc,102,SPNULL,"Unable to malloc SPFILE memory");

    /* set the file open mode in the status structure */
    if (strsame(mode,"r"))
	current_mode = tsp->open_mode = SP_mode_read;
    else if (strsame(mode,"w"))
	current_mode = tsp->open_mode = SP_mode_write;
    else if (strsame(mode,"rv")){
	current_mode = tsp->open_mode = SP_mode_read;
	tsp->read_spifr->status->extra_checksum_verify = TRUE;
    }
    else if (strsame(mode,"wv")) {
	current_mode = tsp->open_mode = SP_mode_write;
	tsp->write_spifr->status->extra_checksum_verify = TRUE;
    }
    else if (strsame(mode,"u")) {
	tsp->open_mode = SP_mode_read;
	current_mode = SP_mode_update;
    }
    else {
	free_sphere_t(tsp);
        return_err(proc,103,SPNULL,
		   rsprintf("Illegal SPFILE open mode '%s'",mode));
    }

    /* just open the file, either for reading or writing.  If the      */
    /* mode was SP_mode_writing, and the file exists, change the mode  */ 
    /* to SP_mode_update.                                              */
    switch (tsp->open_mode) {
	case (SP_mode_read): {
	    if (! strsame(filename,"-")) {
		fopen_mode = (current_mode == SP_mode_read) ? READMODE : 
		    UPDATEMODE;
		if ((tsp->read_spifr->waveform->sp_fp = 
		     fopen(filename,fopen_mode)) == (FILE *)0){
		    free_sphere_t(tsp);
		    return_err(proc,111,SPNULL,
			       rsprintf("%s'%s' for reading fopen mode %s",
					"Unable to open SPHERE file ",
					filename,fopen_mode));
		}
		tsp->read_spifr->status->is_disk_file = TRUE;
	    } else {
		tsp->read_spifr->waveform->sp_fp = stdin;
		tsp->read_spifr->status->is_disk_file = FALSE;
	    }
	    tsp->read_spifr->status->external_filename =
		mtrf_strdup(filename);
	    break;
	}
	case (SP_mode_write):{ 
	    if (! strsame(filename,"-")) {
		/* open the file, truncating if the file exists */
		fopen_mode = (current_mode == SP_mode_write) ? WRITEMODE : 
		    UPDATEMODE;
		if ((tsp->write_spifr->waveform->sp_fp =
		     fopen(filename,fopen_mode)) == (FILE *)0){
		    free_sphere_t(tsp);
		    return_err(proc,105,SPNULL,
			rsprintf("Unable to open SPHERE file '%s' for %s %s",
				 filename,"writing, fopen mode",fopen_mode));
		}
		tsp->write_spifr->status->is_disk_file = TRUE;
	    } else {
		tsp->write_spifr->waveform->sp_fp = stdout;
		tsp->write_spifr->status->is_disk_file = FALSE;
	    }
	    tsp->write_spifr->status->external_filename = 
		mtrf_strdup(filename);
	    break;
	}
	default: {
	    return_err(proc,200,SPNULL,"Internal error");
	}
    }

    /* now that the file is opened, load the header if it exist, */
    /* otherwise alloc an empty header for the user              */
    switch (tsp->open_mode) {
	case (SP_mode_read): {
	    /* read the header */
	    tsp->read_spifr->header =
	       sp_open_header(tsp->read_spifr->waveform->sp_fp,TRUE,&errmsg);
	    if ( tsp->read_spifr->header == HDRNULL ) {
		free_sphere_t(tsp);				
		return_err(proc,104,SPNULL,
		   rsprintf("Unable to open SPHERE header of file '%s', %s",
			    filename,errmsg));
	    }
	    /* get the size of the header */
	    if (! strsame(filename,"-")) {
		if ((tsp->read_spifr->waveform->header_data_size = 
		     sp_file_header_size(filename)) < 0){
		    free_sphere_t(tsp);
		    return_err(proc,110,SPNULL,
		    rsprintf("Unable to get SPHERE header size of file '%s'",
				filename));
		}
	    } else {
		if ((tsp->read_spifr->waveform->header_data_size =
		     sp_header_size(tsp->read_spifr->header)) < 0){
		    free_sphere_t(tsp);
		    return_err(proc,111,SPNULL,
		    rsprintf("Unable to get SPHERE header size of file '%s'",
			filename));
		}
	    }
	    /****** Remove the sample_count field if it's       ******/
	    /****** value is 999999999 and the file is a stream ******/
	    /****** Added by JGF June 22, 1994                  ******/
	    if (! tsp->read_spifr->status->is_disk_file){
		int type, size;
		SP_INTEGER l_int;
		if (sp_get_field(tsp->read_spifr->header,
				 SAMPLE_COUNT_FIELD,&type,&size) == 0){
		  if (sp_get_data(tsp->read_spifr->header,SAMPLE_COUNT_FIELD,
				  (char *)&l_int,&size) == 0){
		    if (l_int == 999999999){
		      if (sp_verbose > 10)
			fprintf(spfp,
				"Proc %s: file '%s' deleting %s field\n",
				proc,filename,SAMPLE_COUNT_FIELD);
		      if (sp_delete_field(tsp->read_spifr->header,
					  SAMPLE_COUNT_FIELD) < 0)
			return_err(proc,112,SPNULL,
				   rsprintf("Unable to delete fake '%s' field",
					    SAMPLE_COUNT_FIELD));
			
		    }
		  }
	      }
	    }	    

	    /****** Correct any out-of-date headers right NOW ******/
	    if (correct_out_of_date_headers(tsp) != 0){
		fprintf(spfp,"Warning: correction of ");
		fprintf(spfp,"out-of-date headers failed\n");
		sp_print_return_status(spfp);
	    }

            /* duplicate the header for the file interface */
            if ((tsp->read_spifr->status->file_header = 
		 sp_dup_header(tsp->read_spifr->header)) == HDRNULL){
		fprintf(spfp,"Error: sp_open_header unable ");
		fprintf(spfp,"to dup header for file '%s'\n",filename);
		free_sphere_t(tsp);		
		return_err(proc,106,SPNULL,
		   rsprintf("Unable to duplicate the SPHERE header of file",
			    filename));
	    }
            /* set the default operation settings */
	    if (sp_set_default_operations(tsp) != 0){
	      print_return_status(spfp);
	      return_err(proc,107,SPNULL,
	       rsprintf("Unable to interpret the SPHERE header of file '%s'",
			filename));
	    }
	    break;	
	}
	case (SP_mode_write):{ 
	    tsp->write_spifr->header = sp_create_header();
	    if ( tsp->write_spifr->header == HDRNULL ) {
		free_sphere_t(tsp);		
		return_err(proc,108,SPNULL,
		   rsprintf("Unable to allocate SPHERE header for file '%s'",
			    filename));
	    }
	    tsp->write_spifr->status->file_header = sp_create_header();
	    if ( tsp->write_spifr->status->file_header == HDRNULL ) {
	     free_sphere_t(tsp);		
	     return_err(proc,109,SPNULL,
 	     rsprintf("Unable to allocate hidden SPHE. header for file '%s'",
				    filename));
	    }
	}
	default: 
	  ;
    }

    /* the file was actually opened for update, so make a temp file, and */
    /* duplicate the read in file. */
    if (current_mode == SP_mode_update){ 
	SP_FILE *tsp2;
	SPIFR *tspifr;
	char *temp_file;
	char data_mode[100];
	temp_file = sptemp(tsp->read_spifr->status->external_filename);
	if (temp_file == CNULL)
	   return_err(proc,300,SPNULL,"Unable to create temporary filename");
	if ((tsp2 = sp_open(temp_file,WRITEMODE)) == SPNULL) {
	    free_sphere_t(tsp);		
	    mtrf_free(temp_file);
	    return_err(proc,301,SPNULL,
		       rsprintf("Unable to open temporary file",temp_file));
	}
	sp_set_data_mode(tsp,"SE-ORIG:SBF-ORIG");
	/* now copy the header into the update file */
	if (sp_copy_header(tsp,tsp2) != 0){
	    free_sphere_t(tsp);		
	    free_sphere_t(tsp2);		
	    unlink(temp_file);
	    mtrf_free(temp_file);
	    return_err(proc,302,SPNULL,"Unable to duplicate output header");
	}
	*data_mode = '\0';
	/*now set the data mode to match the output format of the read file*/
	switch (tsp->read_spifr->status->file_compress){
	  case SP_wc_shorten: strcat(data_mode,"SE-SHORTEN:"); break;
	  case SP_wc_wavpack: strcat(data_mode,"SE-WAVPACK:"); break;
	  case SP_wc_shortpack: strcat(data_mode,"SE-SHORTPACK:"); break;
	  default: ;
	}
	switch (tsp->read_spifr->status->file_sbf){
	  case SP_sbf_01: strcat(data_mode,"SBF-01"); break;
	  case SP_sbf_10: strcat(data_mode,"SBF-10"); break;
	  case SP_sbf_1: strcat(data_mode,"SBF-1"); break;
	  case SP_sbf_N: strcat(data_mode,"SBF-N"); break;
	  default: ;
	}
	if (sp_set_data_mode(tsp2,data_mode) >= 100){
	    free_sphere_t(tsp);		
	    free_sphere_t(tsp2);		
	    unlink(temp_file);
	    mtrf_free(temp_file);
	    return_err(proc,303,SPNULL,
	       rsprintf("Unable to set_data_mode '%s' for update file",
			data_mode));
	}
	/* now merge the two SPFILE pointers into a single structure */
	/* and free the residual */
	tspifr = tsp->write_spifr;
	tsp->write_spifr = tsp2->write_spifr;
	tsp2->write_spifr = tspifr;
	free_sphere_t(tsp2);
	mtrf_free(temp_file);	
	tsp->open_mode = current_mode;
    }

/*    sp_file_dump(tsp,spfp);*/
    if (sp_verbose > 17) sp_file_dump(tsp,spfp);
    if (sp_verbose > 11)
	fprintf(spfp,"Proc %s: Returning Sphere-file pointer\n",proc);
    return_success(proc,0,tsp,"ok");
}
Example #23
0
char *
configure_apache(HWND hDlg, char *resin_home, char *apache_home)
{
	char buf[1024];
    char esc_resin_home[1024];
	FILE *is;
	FILE *os;
    int i, j;
	int isApache2 = 0;
	char *apache_version = "apache-2.0";

	isApache2 = (strstr(apache_home, "Apache2") != 0);

	if (isApache2)
		apache_version = "apache-2.0";
	else
		apache_version = "apache-1.3";
	
    j = 0;
    for (i = 0; resin_home[i]; i++) {
	if (resin_home[i] == '\\')
		esc_resin_home[j++] = '/';
        else
                esc_resin_home[j++] = resin_home[i];
    }
    esc_resin_home[j] = 0;

	int isEtc = 0;
	
	is = fopen(rsprintf(buf, "%s/etc/httpd.conf", apache_home), "r");
	if (is) {
		isEtc = 1;
		os = fopen(rsprintf(buf, "%s/etc/httpd.conf.bak", apache_home), "w+");
	}
	else {
		is = fopen(rsprintf(buf, "%s/conf/httpd.conf", apache_home), "r");
		if (is)
			os = fopen(rsprintf(buf, "%s/conf/httpd.conf.bak", apache_home), "w+");
	}

	if (! is)
		return "Can't find Apache httpd.conf";
	if (! os) {
		fclose(os);
		return "Can't write Apache httpd.conf.bak";
	}

	int lastAddModule = 0;
	int lastLoadModule = 0;
	int hasCaucho = 0;
	int line = 0;
	while (fgets(buf, sizeof(buf), is)) {
		fputs(buf, os);
		line++;
		char cmd[1024];
		char module[1024];
		char file[1024];
		int args = sscanf(buf, "%s%s%s", cmd, module, file);
		
		if (args >= 2 && ! strcmp(cmd, "LoadModule") && ! strcmp(module, "caucho_module"))
			hasCaucho = 1;

		if (args >= 3 && (! strcmp(cmd, "LoadModule") || ! strcmp(cmd, "#LoadModule")))
			lastLoadModule = line;

		if (args >= 2 && (! strcmp(cmd, "AddModule") || ! strcmp(cmd, "#AddModule")))
			lastAddModule = line;
	}

	if (lastAddModule < lastLoadModule)
	  lastAddModule = lastLoadModule;

	fclose(is);
	fclose(os);

	if (hasCaucho)
		return 0;

	if (isEtc) {
		is = fopen(rsprintf(buf, "%s/etc/httpd.conf.bak", apache_home), "r");
		os = fopen(rsprintf(buf, "%s/etc/httpd.conf", apache_home), "w+");
	}
	else {
		is = fopen(rsprintf(buf, "%s/conf/httpd.conf.bak", apache_home), "r");
		os = fopen(rsprintf(buf, "%s/conf/httpd.conf", apache_home), "w+");
	}

	line = 0;
	while (fgets(buf, sizeof(buf), is)) {
		fputs(buf, os);

		line++;
		if (line == lastLoadModule)
			fprintf(os, "LoadModule caucho_module \"%s/win32/%s/mod_caucho.dll\"\n", 
				esc_resin_home, apache_version);
		if (line == lastAddModule && ! isApache2)
			fprintf(os, "AddModule mod_caucho.c\n");
	}

	if (! lastLoadModule)
		fprintf(os, "LoadModule caucho_module \"%s/win32/%s/mod_caucho.dll\"\n", 
				esc_resin_home, apache_version);
	/*
	if (! lastAddModule && ! isApache2)
		fprintf(os, "AddModule mod_caucho.c\n");
	*/

	fprintf(os, "<IfModule mod_caucho.c>\n");
	/*
	fprintf(os, "  CauchoConfigFile \"%s/conf/resin.conf\"\n", esc_resin_home);
	*/
	fprintf(os, "  ResinConfigServer localhost 6800\n");
	fprintf(os, "  CauchoStatus yes\n");
	fprintf(os, "</IfModule>\n");
	fclose(is);
	fclose(os);

	return 0;
}
Example #24
0
static void print_gnu_range_graph(RANK *rank, char *percent_desc, char *testname, char *base, int for_blks, int feedback)
{
    int b,t;
    FILE *fp_dat, *fp_mean, *fp_plt, *fp_median;
    double sum;
    TEXT *mean_name = (TEXT *)0, *dat_name = (TEXT *)0;
    TEXT *plt_name = (TEXT *)0, *basename = (TEXT *)0;
    TEXT *median_name = (TEXT *)0;
    double *pctt, *rnkk;
    int *ind;

    alloc_singarr(pctt,MAX( rank->n_blk , rank->n_trt ),double);
    alloc_singarr(rnkk,MAX( rank->n_blk , rank->n_trt ),double);
    alloc_singarr(ind,MAX( rank->n_blk , rank->n_trt ),int);

    basename = TEXT_strdup((TEXT *)rsprintf("%s%s",(strcmp(base,"-")==0) ? "STATS" : base,
	    (for_blks) ? ".spk" : ".sys"));
    dat_name = TEXT_strdup((TEXT *)rsprintf("%s%s",basename,".dat"));
    /* make the datafiles for the treatements */
    if ((fp_dat = fopen(dat_name,"w")) == (FILE *)0){
	fprintf(stderr,"Error: unable to open GNUPLOT data file %s\n",
		dat_name);
	exit(1);
    } else
	if (feedback >= 1) printf("        Output written to '%s.*'\n",basename);

    mean_name = TEXT_strdup((TEXT *)rsprintf("%s%s",basename,".mean"));
    if ((fp_mean = fopen(mean_name,"w")) == (FILE *)0){
	fprintf(stderr,"Error: unable to open GNUPLOT data file %s\n",
		mean_name);
	exit(1);
    }
    median_name = TEXT_strdup((TEXT *)rsprintf("%s%s",basename,".median"));
    if ((fp_median = fopen(median_name,"w")) == (FILE *)0){
	fprintf(stderr,"Error: unable to open GNUPLOT data file %s\n",
		median_name);
	exit(1);
    }
    plt_name = TEXT_strdup((TEXT *)rsprintf("%s%s",basename,".plt"));
    if ((fp_plt = fopen(plt_name,"w")) == (FILE *)0){
	fprintf(stderr,"Error: unable to open GNUPLOT file %s\n",
		plt_name);
	exit(1);
    } 
    fprintf(fp_plt,"set yrange [%d:0]\n",
	    1 + ((for_blks) ?rank->n_blk  :rank->n_trt ));
    fprintf(fp_plt,"set xrange [0:100]\n");
    fprintf(fp_plt,"set title \"%s\"\n",testname);
    fprintf(fp_plt,"set key\n");
    fprintf(fp_plt,"set ylabel \"%s\"\n",(for_blks) ? "Speaker ID" :"Systems");
    fprintf(fp_plt,"set xlabel \"%s\"\n",percent_desc);
    fprintf(fp_plt,"set ytics (");

    if (for_blks){
	for (b=0;b< rank->n_blk ;b++){
	    double medianval=0.0;
	    sum = 0.0;
	    for (t=0;t< rank->n_trt ;t++){
		fprintf(fp_dat,"%d %f\n",
			b+1,rank->pcts [ rank->trt_srt_ind [ b ]  ][ t ] );
		pctt[t] = rank->pcts [ rank->trt_srt_ind [ b ]  ][ t ] ;
		sum += rank->pcts [ rank->trt_srt_ind [ b ]  ][ t ] ;
	    }

	    rank_double_arr(pctt, rank->n_trt ,ind,rnkk,INCREASING);
	    if ( rank->n_trt  % 2 == 0) { /* handle the even arr len */
		medianval = (pctt[ind[ rank->n_trt  / 2]] + 
			     pctt[ind[( rank->n_trt  / 2) - 1]]) / 2.0;
	    } else { /* handle the odd arr len */
		medianval = pctt[ind[ rank->n_trt  / 2]];
	    }
	    fprintf(fp_mean,"%d %f\n",b+1,sum / (double)( rank->n_trt ));
	    fprintf(fp_median,"%d %f\n",b+1,medianval);
	    fprintf(fp_plt,"\"%s\" %d",rank->blk_name[rank->trt_srt_ind[b]],
		    b+1);
	    if (b !=rank->n_blk -1)
		fprintf(fp_plt,",");
	}
	fprintf(fp_plt,")\n");
    } else {
	for (t=0;t< rank->n_trt ;t++){
	    double medianval=0.0;
	    sum = 0.0;

	    for (b=0;b< rank->n_blk ;b++){
		fprintf(fp_dat,"%d %f\n",
			t+1,rank->pcts [ b ][ rank->blk_srt_ind [ t ]  ] );
		pctt[b] = rank->pcts [ b ][ rank->blk_srt_ind [ t ]  ] ;
		sum += rank->pcts [ b ][ rank->blk_srt_ind [ t ]  ] ;
	    }
    rank_double_arr(pctt, rank->n_blk ,ind,rnkk,INCREASING);
	    if ( rank->n_blk  % 2 == 0) { /* handle the even arr len */
		medianval = (pctt[ind[ rank->n_blk  / 2]] + 
			     pctt[ind[( rank->n_blk  / 2) - 1]]) / 2.0;
	    } else { /* handle the odd arr len */
		medianval = pctt[ind[ rank->n_blk  / 2]];
	    }
	    fprintf(fp_mean,"%d %f\n",t+1,sum / (double)(rank->n_blk ));
	    fprintf(fp_median,"%d %f\n",t+1,medianval);
	    fprintf(fp_plt,"\"%s\" %d",rank->trt_name[rank->blk_srt_ind[t]],
		    t+1);
	    if (t !=rank->n_trt -1)
		fprintf(fp_plt,",");
	}
	fprintf(fp_plt,")\n");
    }

    fprintf(fp_plt,"plot \"%s\" using 2:1 title \"Mean %s\" with lines,",
	    mean_name, percent_desc);
    fprintf(fp_plt,"\"%s\" using 2:1 title \"Median %s\" with lines,",
	    median_name, percent_desc);
    fprintf(fp_plt,"\"%s\" using 2:1 title \"Individual %s\"\n",dat_name,
	    percent_desc);
    fclose(fp_dat);
    fclose(fp_mean);
    fclose(fp_median);
    fclose(fp_plt);

    free_singarr(mean_name  , TEXT);
    free_singarr(dat_name   , TEXT);
    free_singarr(plt_name   , TEXT);
    free_singarr(basename   , TEXT);
    free_singarr(median_name, TEXT);

    free_singarr(pctt,double);
    free_singarr(rnkk,double);
    free_singarr(ind,int);
}
Example #25
0
void run_multi_sys(SCORES *scor[],int nscor,char *out_dir,int stats,int stats_verbose,int stats_unified, int stats_graphs, char *stats_out_name, char *stats_formula, char *stats_test_name, int reports, int feedback, int linewidth)
{
    RANK rank;
    int nwin=0, **winner[15];
    double **confidence[15];
    char *win_desc[15], *win_id[15], *stat_form_str=formula_str(stats_formula);
    char outroot[200], *sign_str=(char *)0, *wilc_str=(char *)0;
    char outname[200];

    if (feedback >= 1)
	printf("Beginning Multi-System comparisons and reports\n");

    if (strcmp(stats_out_name,"-") == 0)
	strcpy(outroot,stats_out_name);
    else
	sprintf(outroot,"%s%s%s",
		((out_dir != (char *)0) ? out_dir : ""),
		((out_dir != (char *)0) ? "/" : ""),
		stats_out_name);
    /* printf("Out Root = %s\n",outroot); */
    if (strcmp("-",outroot) == 0)
	strcpy(outname,outroot);
    else
	sprintf(outname,"%s.%s",outroot,"stats");


    if (stats > 0 || stats_graphs > 0)
	init_RANK_struct_from_SCORES(&rank, scor, nscor, stats_formula);

    /*    dump_full_RANK_struct(&rank, "Systems", "Speakers", "", "",stats_formula,
			  "test name", "Speaker ranks for the system",
			  "System ranks for the speaker");  */

    if ((stats & STAT_MAPSSWE) > 0){
	win_desc[nwin] = "Matched Pair Sentence Segment (Word Error)";
	win_id[nwin] = "MP";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	do_mtch_pairs(scor, nscor, "2", stats_test_name, 
		      !stats_unified, stats_verbose, &winner[nwin],
		      outname, feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_MAPSSWE_SEG) > 0){
        do_mtch_pairs_seg_analysis(scor, nscor, stats_test_name, 1, 1);
    }
    
    if ((stats & STAT_SIGN) > 0){
	win_desc[nwin] = (char *)TEXT_strdup((TEXT*)rsprintf("%s (%s)",
					     "Signed Paired Comparison",
						      stat_form_str));
	sign_str = win_desc[nwin];
	win_id[nwin] = "SI";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	perform_signtest(&rank, stats_verbose, !stats_unified, 
			 stat_form_str, *stats_formula, &winner[nwin],
			 outname, feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_WILC) > 0){
	win_desc[nwin] = (char*)TEXT_strdup((TEXT *)rsprintf("%s (%s)",
					 "Wilcoxon Signed Rank",
					 stat_form_str));
	wilc_str = win_desc[nwin];
	win_id[nwin] = "WI";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	perform_wilcoxon(&rank, stats_verbose, !stats_unified,
			 stat_form_str, *stats_formula, &winner[nwin],
			 outname, feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_MCN) > 0){
	win_desc[nwin] = "McNemar (Sentence Error)"; win_id[nwin] = "MN";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	McNemar_sent(scor, nscor, &winner[nwin], stats_test_name,
		     !stats_unified, stats_verbose,outname,
		     feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_ANOVAR) > 0){
	win_desc[nwin] = "Analysis of Variance by Ranks";
	win_id[nwin] = "AN";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	compute_anovar(&rank, stats_verbose, !stats_unified, 
		       &winner[nwin],outname,feedback,&confidence[nwin]);
	nwin++;
    }

    if (stats_unified){
	if (feedback >= 1) 
	    printf("    Printing Unified Statistical Test Reports\n");
#define new
#ifdef new
	print_composite_significance2(&rank, 80, nwin, winner, confidence,
				      win_desc, win_id,
				      1 /*matrix*/, 1 /*int report*/,
				      stats_test_name,outname, feedback,
				      (out_dir != (char *)0) ? out_dir : "");
#endif
#ifdef old
	print_composite_significance(&rank, 80, nwin, winner,
				      win_desc, win_id,
				      1 /*matrix*/, 1 /*int report*/,
				      stats_test_name,outname,feedback,
				      (out_dir != (char *)0) ? out_dir : "");
#endif
    }

    
    if ((stats_graphs & GRAPH_RANGE) > 0){
	if (strcmp("-",outroot) == 0)
	    strcpy(outname,outroot);
	else
	    sprintf(outname,"%s.%s",outroot,"range");
	if (feedback >= 1) printf("    Printing Range Graphs\n");
	print_rank_ranges(&rank, stat_form_str,stats_test_name, outname, feedback);
    }

    if ((stats_graphs & GRAPH_GRANGE) > 0){
	if (strcmp("-",outroot) == 0)
	    strcpy(outname,outroot);
	else
	    sprintf(outname,"%s.%s",outroot,"grange");
	if (feedback >= 1) printf("    Printing GNUPLOT Range Graphs\n");
	print_gnu_rank_ranges(&rank, stat_form_str,stats_test_name, outname, feedback);
    }
    if ((stats_graphs & GRAPH_GRANGE2) > 0){
	if (strcmp("-",outroot) == 0)
	    strcpy(outname,outroot);
	else
	    sprintf(outname,"%s.%s",outroot,"grange2");
	if (feedback >= 1) printf("    Printing GNUPLOT Range Graphs V2\n");
	print_gnu_rank_ranges2(&rank, stat_form_str,stats_test_name, outname, feedback);
    }

    if ((stats_graphs & GRAPH_DET) > 0){
      if (strcmp("-",outroot) != 0){
	if (make_SCORES_DET_curve(scor, nscor,outroot,feedback,stats_test_name)
	    != 0)
	  exit(1);
      } else {
	fprintf(scfp,"Warning: DET plot can not go to STDOUT\n");
      }
    }

    strcpy(outname,outroot);

    if ((reports & REPORT_SUM) > 0)
	print_N_system_summary(scor, nscor, outname, stats_test_name, 0, feedback);

    if ((reports & REPORT_RSUM) > 0)
	print_N_system_summary(scor, nscor, outname, stats_test_name, 1,
			       feedback);
    if ((reports & REPORT_ESUM) > 0)
	print_N_system_executive_summary(scor, nscor, outname,
					 stats_test_name, 0, feedback);
    if ((reports & REPORT_RESUM) > 0)
	print_N_system_executive_summary(scor, nscor, outname,
					 stats_test_name, 1, feedback);
    if ((reports & REPORT_PRN) > 0)
	print_N_SCORE(scor, nscor, outname, linewidth, feedback, 0);

    if ((reports & REPORT_LUR) > 0)
	print_N_lur(scor, nscor, outname, stats_test_name, feedback);

    if (stats > 0 || stats_graphs > 0){
	int i;
	for (i=0; i<nwin; i++)
	    free_2dimarr(winner[i],rank.n_trt,int);
	free_RANK(&rank);
    }
Example #26
0
int pattern_init(struct pattern * pattern, const char * prefix) {
    GLenum e;

    memset(pattern, 0, sizeof *pattern);

    pattern->intensity = 0;
    pattern->intensity_integral = 0;
    pattern->name = strdup(prefix);
    if(pattern->name == NULL) ERROR("Could not allocate memory");

    int n = 0;
    for(;;) {
        char * filename;
        struct stat statbuf;
        filename = rsprintf("%s%s.%d.glsl", config.pattern.dir, prefix, n);
        if(filename == NULL) MEMFAIL();

        int rc = stat(filename, &statbuf);
        free(filename);

        if (rc != 0 || S_ISDIR(statbuf.st_mode)) {
            break;
        }
        n++;
    }

    if(n == 0) {
        ERROR("Could not find any shaders for %s", prefix);
        return 1;
    }
    pattern->n_shaders = n;

    pattern->shader = calloc(pattern->n_shaders, sizeof *pattern->shader);
    if(pattern->shader == NULL) MEMFAIL();
    pattern->tex = calloc(pattern->n_shaders, sizeof *pattern->tex);
    if(pattern->tex == NULL) MEMFAIL();

    bool success = true;
    for(int i = 0; i < pattern->n_shaders; i++) {
        char * filename;
        filename = rsprintf("%s%s.%d.glsl", config.pattern.dir, prefix, i);
        if(filename == NULL) MEMFAIL();

        GLhandleARB h = load_shader(filename);

        if (h == 0) {
            fprintf(stderr, "%s", load_shader_error);
            WARN("Unable to load shader %s", filename);
            success = false;
        } else {
            pattern->shader[i] = h;
            DEBUG("Loaded shader #%d", i);
        }
        free(filename);
    }
    if(!success) {
        ERROR("Failed to load some shaders.");
        return 2;
    }

    if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e));

    // Render targets
    glGenFramebuffersEXT(1, &pattern->fb);
    glGenTextures(pattern->n_shaders + 1, pattern->tex);

    if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e));

    for(int i = 0; i < pattern->n_shaders + 1; i++) {
        glBindTexture(GL_TEXTURE_2D, pattern->tex[i]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config.pattern.master_width, config.pattern.master_height, 0, 
                     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    }
    glBindTexture(GL_TEXTURE_2D, 0);

    if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e));

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, pattern->fb);
    for(int i = 0; i < pattern->n_shaders + 1; i++) {
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,
                                  pattern->tex[i], 0);
        glClear(GL_COLOR_BUFFER_BIT);
    }

    if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e));

    // Some OpenGL API garbage
    pattern->uni_tex = calloc(pattern->n_shaders, sizeof *pattern->uni_tex);
    if(pattern->uni_tex == NULL) MEMFAIL();
    for(int i = 0; i < pattern->n_shaders; i++) {
        pattern->uni_tex[i] = i + 1;
    }

    return 0;
}
char *
configure_netscape(HWND hDlg, char *resin_home_raw, char *netscape_home)
{
	char obj_name[1024];
	char bak_name[1024];
	char resin_home[1024];
	char buf[1024];
	char *status;
	FILE *is;
	FILE *os;
	int i;

	// netscape needs forward slashes
	for (i = 0; resin_home_raw[i]; i++) {
		if (resin_home_raw[i] == '\\')
			resin_home[i] = '/';
		else
			resin_home[i] = resin_home_raw[i];
	}
	resin_home[i] = 0;

	int isEtc = 0;
	
	wsprintf(obj_name, "%s/config/magnus.conf", netscape_home);
	wsprintf(bak_name, "%s/config/magnus.conf.bak", netscape_home);

	status = config_init(obj_name, bak_name, resin_home);
	if (status)
		return status;

	wsprintf(obj_name, "%s/config/obj.conf", netscape_home);
	wsprintf(bak_name, "%s/config/obj.conf.bak", netscape_home);

	status = config_init(obj_name, bak_name, resin_home);
	if (status)
		return status;

	is = fopen(obj_name, "r");

	if (! is)
		return strdup(rsprintf(buf, "Can't find Netscape's %s", obj_name));

	os = fopen(bak_name, "w+");

	if (! os) {
		fclose(is);
		return "Can't write Netscape obj.conf.bak";
	}

	int hasCaucho = 0;
	int line = 0;
	while (fgets(buf, sizeof(buf), is)) {
		fputs(buf, os);
		line++;
		char cmd[1024];
		int args = sscanf(buf, "%s", cmd);
		
		if (strstr(buf, "caucho-status")) {
			hasCaucho = 1;
		}
	}

	fclose(is);
	fclose(os);

	if (hasCaucho)
		return 0;

	is = fopen(bak_name, "r");
	os = fopen(obj_name, "w+");

	line = 0;
	int isFirst = 1;
	while (fgets(buf, sizeof(buf), is)) {
		char cmd[1024];
		int args = sscanf(buf, "%s", cmd);
		if (isFirst && args > 0 && ! strcmp(cmd, "NameTrans")) {
			isFirst = 0;
			fprintf(os, "NameTrans fn=\"caucho_filter\" conf=\"%s/conf/resin.conf\" name=\"resin\"\n",
				resin_home);
			fprintf(os, "NameTrans fn=\"assign-name\" from=\"/caucho-status\" name=\"caucho-status\"\n");
		}

		fputs(buf, os);

		line++;
	}

	fprintf(os, "\n");
	fprintf(os, "<Object name=\"resin\">\n");
	fprintf(os, "Service fn=\"caucho_service\"\n");
	fprintf(os, "</Object>\n\n");
	fprintf(os, "<Object name=\"caucho-status\">\n");
	fprintf(os, "Service fn=\"caucho_status\"\n");
	fprintf(os, "</Object>\n");
	fclose(is);
	fclose(os);

	return 0;
}