[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

RE: gdk_input_add looping



Thanks for the quick response.

> -----Original Message-----
> From:	Erik Mouw [SMTP:J.A.K.Mouw@its.tudelft.nl]
> Sent:	Thursday, February 10, 2000 4:20 AM
> To:	gtk-app-devel-list@redhat.com
> Subject:	Re: gdk_input_add looping
> 
> On Thu, 10 Feb 2000 02:31:00 -0600, broc.stirton@amd.com wrote:
> >     I am having some trouble with gdk_input_add.  I am trying to watch a
> > log file, and whenever something is appended to it, read it in and
> insert it
> > into a text widget.  I am new to gtk and my C is pretty weak, but here
> is
> > what I am trying:
> > I open the file for reading and do the gdk_input_add on it when the
> program
> > starts:
> > 
> >     if((errlog[0] = open(logname, O_RDONLY)) == -1){
> 
> Use "open(logname, O_RDONLY | O_NONBLOCK)", so your program won't block if
> you can't read() the desired amount of bytes.
> 
	-----------------
	I tried with and without O_NONBLOCK. I wasn't sure which was right,
but that makes sense.
	-----------------

> >         printf("Error: can't open log %s\n", logname);
> 
>   perror("open");
> 
> Will show exactly why the log can't be opened.
> 
	---------------------
	thanks.
	--------------------

> >         exit(1);
> >     }
> >     errlog[1] = gdk_input_add(errlog[0], GDK_INPUT_READ,
> > (GdkInputFunction)read_log, errlog);
> > 
> > Then in read_log:
> > 
> >     readbytes = read(log_tag[0], buffer, MAXPIPE - 1);
> 
> When the file is opened without O_NONBLOCK, read() will block until
> MAXPIPE - 1 bytes are read. Any particular reasons why you're using
> MAXPIPE? Are you using a pipe?
> 
	--------------------------------------
	just re-using it- it is used elsewhere (for a pipe) and is the right
size (512 actually).
	----------------------------------------

> >     lseek(log_tag[0], 0, SEEK_END);
> 
> This shouldn't be necessary. If you read everything, you're already at the
> end of the file; if you didn't read everything, you read_log() will be
> called again. If you're using a pipe, lseek() will fail: you can't seek on
> a pipe.
> 
	----------------------------------
	OK. makes sense.
	-----------------------------------

> >     gtk_text_freeze(GTK_TEXT (log->text));
> >     gtk_text_insert(GTK_TEXT (log->text), NULL, NULL, NULL, buffer,
> > strlen(buffer));
> 
> Watch out! buffer does not contain a null terminated string, so
> strlen(buffer) will return an invalid length!
> 
	-------------------------
	doh! even I knew that.  dumb mistake. thanks.
	------------------------

> >     gtk_text_thaw(GTK_TEXT (log->text));
> 
> I think you should code this like:
> 
>   #define BUFFERLEN 512
> 
>   int stop = FALSE;
>   char buffer[BUFFERLEN + 1];
> 
>   do
>     {
>       numread = read(errlog[0], buffer, BUFFERLEN);
>       if(numread > 0)
>         {
>           buffer[BUFFERLEN]='\0'; /* make buffer null terminated */
	/* should be buffer[numread] = '\0';  right? */
>           /* do whatever you want to do with the buffer */
>         }
>       else
>         {
>           stop = TRUE;
>         }
>     }
>   while(stop == FALSE);
> 
> I didn't test this, of course ;-)
	----------------------------------
	so now I have in main: 

		if((errlog[0] = open(logname, O_RDONLY | O_NONBLOCK)) ==
-1){
			printf("Error: can't open --for read-- log %s\n",
logname);
			exit(1);
		}
		errlog[1] = gdk_input_add(errlog[0], GDK_INPUT_READ,
(GdkInputFunction)read_log, errlog);

	and read_log:

	void read_log(int *errlog, int *source, GdkInputCondition condition)
	{
		int stop = FALSE;
		char buffer[MAXPIPE + 1];
		int buff_length, text_length, numread;

		printf("read_log called\n");
		do
		{
			numread = read(errlog[0], buffer, MAXPIPE);
			if(numread > 0){
				buffer[numread]='\0'; /* make buffer null
terminated */
				/* insert buffer into text widget */
				gtk_text_freeze(GTK_TEXT (log->text));
				gtk_text_insert(GTK_TEXT (log->text), NULL,
NULL, NULL, buffer, strlen(buffer));

				/* If more than MAXLOGTEXT bytes, delete
some...  */
				buff_length = strlen(buffer);
				text_length =
gtk_text_get_length(GTK_TEXT(log->text));
				if( buff_length + text_length > MAXLOGTEXT
){
					gtk_text_set_point
(GTK_TEXT(log->text),0);
					gtk_text_forward_delete
(GTK_TEXT(log->text), (text_length+buff_length) - MAXLOGTEXT );
				}
	  
				gtk_text_set_point (GTK_TEXT(log->text),
gtk_text_get_length(GTK_TEXT(log->text)));
				gtk_text_thaw(GTK_TEXT (log->text));
			}else{
				stop = TRUE;
			}
		}
		while(stop == FALSE);

	}


	BUT IT STILL LOOPS!  Any ideas?  How is gdk_input_add determining
that there is data to be read from the fd?  It relies on the file position
being at the end of the file? (which it is).  
		broc
		



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]