Component index
Section index

Component streamp


	    STREAMP is an object to handling a stream of bytes and handling
	    that to a processing unit expecting complete records. The object
	    iterate between fill() functag, to grab more bytes and process()
	    functag, to try to process some records. The process functag
	    return how many bytes it used from the buffer, potentially 0 if
	    it does not find a complete record to process. Then fill() is called
	    again to grab more bytes and so on.
	    The simple case is streamp which does everything. streamp is built
	    on top of STREAMP, which is more flexible.
	    The following example uses two STREAMP object, each doing half
	    the job.
	void sample_STREAMP()
	    // This waits for empty line to output paragraphs
	    <obj STREAMP output>();
	    <f process>
	        // Check if there is a end of line
	        const char *text = (const char *)buf;
	        int ret = 0;
	        if (nomore){
	            ret = len;
	            for (int i=0; i<len; i++){
	                if (text[i] == '\n' && text[i+1] == '\n'){
	                    // Ok a paragraph ends here
	                    ret = i+2;
	        // printf ("input ret=%d len=%d\n",ret,len);
	        if (ret > 0){
	            printf ("<paragraph>\n");
	            printf ("%*.*s",ret,ret,text);
	            printf ("</paragraph>\n");
	        return ret;
	    // This inputs 5 bytes at a time, and picks full line as record
	    <obj STREAMP input>();
	    <f fill>
	        // printf ("filling\n");
	        return fread (buf,1,5,stdin);
	    <f validrecord>
	        // 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 + 1;
	        // printf ("validrecord used=%d len=%d\n",used,len);
	        return used;
	        char buf[100000];
	        int len = input.getrecord (buf,sizeof(buf));
	        if (len > 0){
	            output.fill (buf,len);
	    output.eof ();