Bill Allombert on Fri, 11 Sep 2015 18:31:37 +0200


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

Re: Reading a stream from a C string


On Thu, Sep 10, 2015 at 08:07:08PM +0200, Jeroen Demeyer wrote:
> On 2015-09-08 19:22, Bill Allombert wrote:
> >Alas this is not implemented. At minimum one would need to implement an
> >'input_method' which handle C strings.
> 
> This (attached patch) works for me. It adds a function
> gp_read_file_from_str() which is like gp_read_file() but from a
> string.

Maybe it would be better to call it gp_read_str_multiline() ?

> Note that there is a hack involved: a cast of FILE* to char** for
> the third argument of fgets(). Perhaps using a void* in all cases
> would be cleaner.

Agreed.

> I added a helper function gp_read_from_input() which is general enough
> that it could be used to implement gp_read_file() and gp_read_stream()
> also. I didn't do this since I wanted some feedback first.

This is a good idea for a subsequent patch.

> index e9526fb..76d047a 100644
> --- a/src/headers/paripriv.h
> +++ b/src/headers/paripriv.h
> @@ -484,13 +484,13 @@ int path_is_absolute(char *s);
>  
>  typedef struct input_method {
>  /* mandatory */
> -  char * (*fgets)(char *,int,FILE*);
>    char * (*getline)(char**, int f, struct input_method*, filtre_t *F);
>    int free; /* boolean: must we free the output of getline() ? */
>  /* for interactive methods */
>    const char *prompt, *prompt_cont;
>  /* for non-interactive methods */
>    FILE *file;
> +  char * (*fgets)(char *, int, FILE*);
>  } input_method;
>  
>  int input_loop(filtre_t *F, input_method *IM);

Why did you do that ? This cause funny crashes if we omit make clean.

Otherwise, your patch seems to work fine, good job.

Cheers,
Bill.