Component index
Section index

Component streamp_do


Search

	/*
	    streamp_do is a simple component to help split some bytes into
	    It receives some bytes, add them to an internal buffer (STREAMP_BUF)
	    and tries to extract some chunks/records out of it (using the process
	    functag). Unlike the streamp component, this one does not handle
	    the fill function. It must be called several time, each time with more
	    bytes to process.
	
	    The following example output lines (text lines) but read bytes
	    5 at a time. So the component is called over and over.
	
	    One way to test this sample is to copy a text file in /tmp/foo
	    and do
	
	    /tmp/x streamp_do </tmp/foo >/tmp/foo.new
	    cmp /tmp/foo /tmp/foo.new && echo ok
	*/
	<mod>
	void sample_streamp_do()
	{
	    // We create a very small buffer (1 byte) and let it grow 1 byte
	    // at a time to show it adapts properly
	    STREAMP_BUF ctl (1,1);
	    char buf[5];
	    int n;
	    while ((n=fread(buf,1,5,stdin))> 0){
	        <call streamp_do>(ctl,buf,n);
	        <f process>
	            // Check if there is a end of line
	            const void *pt = memchr (buf,'\n',len);
	            int used = 0;
	            if (pt != NULL){
	                used = (char*)pt - (char*)buf;
	                printf ("%*.*s\n",used,used,(const char *)buf);
	                used++;
	            }else{
	                // fprintf (stderr,"length %d is not enough\n",len);
	            }
	            return used;
	        </f>
	        </call>
	    }
	}
	</mod>