On Nov 21, 2016, at 1:26 PM, Bryce Harrington <[email protected]> wrote:
> 
> Add support for basic text file loading, to facilitate more expansive
> testing of its UTF-8 text editing support.
> 
> Signed-off-by: Bryce Harrington <[email protected]>

This patch checks the command line options for a filename, prints the new
usage string if an option is amiss, and assigns the contents of the file
to editor.entry if all else is well. LGTM.

Reviewed-by: Yong Bakos <[email protected]>

yong


> ---
> clients/editor.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 62 insertions(+), 5 deletions(-)
> 
> diff --git a/clients/editor.c b/clients/editor.c
> index b8fc63a..42c7f52 100644
> --- a/clients/editor.c
> +++ b/clients/editor.c
> @@ -25,6 +25,7 @@
> #include "config.h"
> 
> #include <assert.h>
> +#include <errno.h>
> #include <stdint.h>
> #include <stdio.h>
> #include <stdlib.h>
> @@ -1513,7 +1514,7 @@ usage(const char *program_name, int exit_code)
> {
>       unsigned k;
> 
> -     fprintf(stderr, "Usage: %s [OPTIONS]\n\n", program_name);
> +     fprintf(stderr, "Usage: %s [OPTIONS] [FILENAME]\n\n", program_name);
>       for (k = 0; k < ARRAY_LENGTH(editor_options); k++) {
>               const struct weston_option *p = &editor_options[k];
>               if (p->name) {
> @@ -1532,6 +1533,53 @@ usage(const char *program_name, int exit_code)
>       exit(exit_code);
> }
> 
> +/* Load the contents of a file into a UTF-8 text buffer and return it.
> + *
> + * Caller is responsible for freeing the buffer when done.
> + * On error, returns NULL.
> + */
> +static char *
> +read_file(char *filename)
> +{
> +     char *buffer = NULL;
> +     int buf_size, read_size;
> +     FILE *fin;
> +     int errsv;
> +
> +     fin = fopen(filename, "r");
> +     if (fin == NULL)
> +             goto error;
> +
> +     /* Determine required buffer size */
> +     if (fseek(fin, 0, SEEK_END) != 0)
> +             goto error;
> +     buf_size = ftell(fin);
> +     if (buf_size < 0)
> +             goto error;
> +     rewind(fin);
> +
> +     /* Create buffer and read in the text */
> +     buffer = (char*) malloc(sizeof(char) * (buf_size + 1));
> +     if (buffer == NULL)
> +             goto error;
> +     read_size = fread(buffer, sizeof(char), buf_size, fin);
> +     fclose(fin);
> +     if (buf_size != read_size)
> +             goto error;
> +     buffer[buf_size] = '\0';
> +
> +     return buffer;
> +
> +error:
> +     errsv = errno;
> +     if (fin)
> +             fclose(fin);
> +     free(buffer);
> +     errno = errsv || EINVAL;
> +
> +     return NULL;
> +}
> +
> int
> main(int argc, char *argv[])
> {
> @@ -1544,9 +1592,14 @@ main(int argc, char *argv[])
>               usage(argv[0], EXIT_SUCCESS);
> 
>       if (argc > 1) {
> -             usage(argv[0], EXIT_FAILURE);
> -             /* FIXME: Use remaining arguments as a path/filename to load */
> -             return 0;
> +             if (argv[1][0] == '-')
> +                     usage(argv[0], EXIT_FAILURE);
> +
> +             text_buffer = read_file(argv[1]);
> +             if (text_buffer == NULL) {
> +                     fprintf(stderr, "could not read file '%s': %m\n", 
> argv[1]);
> +                     return -1;
> +             }
>       }
> 
>       memset(&editor, 0, sizeof editor);
> @@ -1572,7 +1625,10 @@ main(int argc, char *argv[])
>       editor.window = window_create(editor.display);
>       editor.widget = window_frame_create(editor.window, &editor);
> 
> -     editor.entry = text_entry_create(&editor, "Entry");
> +     if (text_buffer)
> +             editor.entry = text_entry_create(&editor, text_buffer);
> +     else
> +             editor.entry = text_entry_create(&editor, "Entry");
>       editor.entry->click_to_show = opt_click_to_show;
>       if (opt_preferred_language)
>               editor.entry->preferred_language = 
> strdup(opt_preferred_language);
> @@ -1606,6 +1662,7 @@ main(int argc, char *argv[])
>       widget_destroy(editor.widget);
>       window_destroy(editor.window);
>       display_destroy(editor.display);
> +     free(text_buffer);
> 
>       return 0;
> }
> -- 
> 1.9.1
> 
> _______________________________________________
> wayland-devel mailing list
> [email protected]
> https://lists.freedesktop.org/mailman/listinfo/wayland-devel

_______________________________________________
wayland-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/wayland-devel

Reply via email to