std.stream.File help required (and classes)

akaz nemo at utopia.com
Tue Mar 27 14:57:29 PDT 2012


OK, I converted into using the std.stdio.File. Without success, 
the programs till crashes.

However, in the meantime:

A) why there is no parameter-less constructor for std.stdio.File? 
I would like to have into my "init" function: s.filedesc=new 
File() and, then, in my setter "open" method 
s.filedesc.open(name,mode). Because I have no such constructor, I 
am forced to have in the "init": s.filedesc=null; and in the 
"open": s.filedesc=new File(name,mode). However, in that case, 
what use for the std.stdio.File.open method? If the name and the 
mode *must* be known at the construction time, then why ask those 
once more for the std.stdio.File.open() method? I am forced to 
open, first, a dummy file (in the constructor), only to be able 
to call, later, the std.stdio.File.open() method with the correct 
file name?

B) In my function:
private int msf_open(MSFilter* f, void* arg){ //a setter
     
printf("msf_binfile_open-start\n============================\n");
	MSF_State* s=cast(MSF_State*)f.data;
	ms_mutex_lock(&(f.lock));
	s.filedesc = new File(*(cast(string*)arg),"w+b");
	printf("s.filedesc=%p+++++++++++++++++++++\n",s.filedesc);
	writef("s.filedesc.isOpen=%b+++++++++++++++++++++\n",s.filedesc.isOpen);
     
writef("s.filedesc.name=%s+++++++++++++++++++++\n",s.filedesc.name);
     
writef("s.filedesc.size=%d+++++++++++++++++++++\n",s.filedesc.size);
     ms_mutex_unlock(&(f.lock));
	printf("msf_binfile_open-stop\n============================\n");
	return 0;
}

can you tell me if the line s.filedesc=new 
File(*(cast(string*)arg),"w+b"); PROPAGATES the change into 
f.data? (recall that MSF_State* s=cast(MSF_State*)f.data;). I 
should also force, at the end of the function (just before 
return), something like: free(f.data); f.data=s; to propagate 
this?

As dumb as it may seem: is my "s" variable in MSF_State* 
s=cast(MSF_State*)f.data; a POINTER or a VALUE? I should write 
s->filedesc? Or, maybe, (*s).filedesc? I am a bit lost between 
pointers (s->x or (*s).x) and values (s.x). For structures there 
are pointers, for classes there are no pointers?

Finally, the program crash on the same line, when trying to write 
something in the file. Irony is that the s.filedesc.isOpen 
shows... 1.

Here is my new "msf_sinker_binaryfile.d file:

=======================msf_sinker_binaryfile.d======================
module msf_sinker_binaryfile;
import std.stdio;

import mediastreamer2_layer;
import msf_commons;

const uint MSF_SINKER_BINARYFILE_OPEN = 
MS_FILTER_METHOD!(byte)(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,0);
const uint MSF_SINKER_BINARYFILE_CLOSE = 
MS_FILTER_METHOD_NO_ARG(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,1);

private struct _MSF_State{
	File* filedesc; //file descriptor
};
private alias _MSF_State MSF_State;

private void msf_init(MSFilter* f){ //a constructor
     
printf("msf_binfile_init-start\n============================\n");
	MSF_State* s=ms_new!(MSF_State)(1);
	s.filedesc = null;
	f.data=s;
	printf("msf_binfile_init-stop\n============================\n");
}

private int msf_open(MSFilter* f, void* arg){ //a setter
     
printf("msf_binfile_open-start\n============================\n");
	MSF_State* s=cast(MSF_State*)f.data;
	ms_mutex_lock(&(f.lock));
	s.filedesc = new File(*(cast(string*)arg),"w+b");
	printf("s.filedesc=%p+++++++++++++++++++++\n",s.filedesc);
	writef("s.filedesc.isOpen=%b+++++++++++++++++++++\n",s.filedesc.isOpen);
     
writef("s.filedesc.name=%s+++++++++++++++++++++\n",s.filedesc.name);
     
writef("s.filedesc.size=%d+++++++++++++++++++++\n",s.filedesc.size);
     ms_mutex_unlock(&(f.lock));
	printf("msf_binfile_open-stop\n============================\n");
	return 0;
}

private void msf_process(MSFilter* f){ //a run() method
     
printf("msf_binfile_process-start\n============================\n");
	MSF_State* s=cast(MSF_State*)f.data;
	mblk_t* m;
	while((m=ms_queue_get(f.inputs[0]))!=null){ //reading samples 
from input
		ms_mutex_lock(&(f.lock));
		int len=cast(int)((m.b_wptr-m.b_rptr)); //length of message 
data block
		byte* p=cast(byte*)m.b_rptr;
		/*printf("----------------------------\n");
         for(int idx=0; idx<len; idx++){
             printf("d_data[%d]=%d\n",idx,d_data[idx]);
         }
		printf("----------------------------\n");*/
		//File filedesc;
		//filedesc.open("newfile.bin","w+b");
		printf("s.filedesc=%p+++++++++++++++++++++\n",s.filedesc);
         
writef("s.filedesc.isOpen=%b+++++++++++++++++++++\n",s.filedesc.isOpen);
         
//writef("s.filedesc.name=%s+++++++++++++++++++++\n",s.filedesc.name); 
//SEGFAULT
         
//writef("s.filedesc.size=%d+++++++++++++++++++++\n",s.filedesc.size); 
//SEGFAULT
		s.filedesc.writefln("This writes a string to the file."); 
//SEGFAULT
		//s.filedesc.writeBlock(p,len);
		//filedesc.close();
		ms_mutex_unlock(&(f.lock));
		freemsg(m);
	}
     
printf("msf_binfile_process-start\n============================\n");
}

private int msf_close(MSFilter* f, void* arg){ //an unsetter
	MSF_State* s=cast(MSF_State*)f.data;
	ms_mutex_lock(&(f.lock));
     s.filedesc.close();
	ms_mutex_unlock(&(f.lock));
	return 0;
}

private void msf_uninit(MSFilter* f){ //a destructor
	MSF_State* s=cast(MSF_State*)f.data;
     msf_close(f, null);
	ms_free(s);
}

private immutable MSFilterMethod[3] msf_methods=[
	{id:MSF_SINKER_BINARYFILE_OPEN,method:cast(MSFilterMethodFunc)&msf_open},
	{id:MSF_SINKER_BINARYFILE_CLOSE,method:cast(MSFilterMethodFunc)&msf_close},
	{0,null}
];

immutable MSFilterDesc msf_sinker_binaryfile_desc={
	id:cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,
	name:"MSFileSink",
	text:"File sink",
	category:MSFilterCategory.MS_FILTER_OTHER,
	ninputs:1,
	noutputs:0,
	init:cast(MSFilterFunc)&msf_init,
	process:cast(MSFilterFunc)&msf_process,
	uninit:cast(MSFilterFunc)&msf_uninit,
	methods:cast(MSFilterMethod*)&msf_methods[0]
};
==============================================================

and here is the output of the program:

-------START PROGRAM-------
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 0
ALSA lib control.c:951:(snd_ctl_open_noupdate) Invalid CTL 
default:0
ortp-warning-Could not attach mixer to card: Invalid argument
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 0
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:0
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 0
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:0
ortp-warning-Strange, sound card HDA Intel does not seems to be 
capable of anything, retrying with plughw...
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 1
ALSA lib control.c:951:(snd_ctl_open_noupdate) Invalid CTL 
default:1
ortp-warning-Could not attach mixer to card: Invalid argument
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 1
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:1
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 1
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:1
ortp-warning-Strange, sound card HDA ATI HDMI does not seems to 
be capable of anything, retrying with plughw...
ortp-warning-Strange, sound card HDA ATI HDMI seems totally 
unusable.
msf_binfile_init-start
============================
msf_binfile_init-stop
============================
msf_set_freq-start
============================
msf_set_freq-stop
============================
msf_set_rate-start
============================
msf_set_rate-stop
============================
msf_binfile_open-start
============================
s.filedesc=0x7ff83baacff0+++++++++++++++++++++
s.filedesc.isOpen=1+++++++++++++++++++++
s.filedesc.name=file_givenname.bin+++++++++++++++++++++
s.filedesc.size=0+++++++++++++++++++++
msf_binfile_open-stop
============================
msf_sinusoid_process-start
============================
s.rate=4000.000000============================
msf_sinusoid_process-stop
============================
msf_split_process-start
============================
msf_split_process-stop
============================
msf_dbl2int16_process-start
============================
msf_dbl2int16_process-stop
============================
ortp-warning-alsa_set_params: periodsize:0 Using 64
ortp-warning-alsa_set_params: period:8 Using 8
msf_binfile_process-start
============================
s.filedesc=0x7ff83baacff0+++++++++++++++++++++
s.filedesc.isOpen=1+++++++++++++++++++++
Segmentation fault (core dumped)

Any help?


More information about the Digitalmars-d-learn mailing list