Component index
Section index

Component streamp


Search

	/*
	    streamp is a simple component to help split a streamed input into
	    chunks. It does the buffering, filling and processing logic.
	
	    The following example output lines (text lines) but read bytes
	    5 at a time. So fill() is called until process() sees a full line
	    and output it.
	
	    One way to test this sample is to copy a text file in /tmp/foo
	    and do
	
	    /tmp/x streamp </tmp/foo >/tmp/foo.new
	    cmp /tmp/foo /tmp/foo.new && echo ok
	*/
	<mod>
	void sample_streamp()
	{
	    <call streamp>();
	    <f fill>
	        return fread (buf,1,5,stdin);
	    </f>
	    <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>