[PATCH weston v2 2/2] editor: Load a file if specified on command line
Bryce Harrington
bryce at osg.samsung.com
Mon Nov 21 18:34:34 UTC 2016
Add support for basic text file loading, to facilitate more expansive
testing of its UTF-8 text editing support.
Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
---
clients/editor.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 63 insertions(+), 5 deletions(-)
diff --git a/clients/editor.c b/clients/editor.c
index 33b43d2..f386a8b 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,10 +1533,58 @@ 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[])
{
struct editor editor;
+ char *text_buffer = NULL;
int i;
parse_options(editor_options, ARRAY_LENGTH(editor_options),
@@ -1544,9 +1593,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 +1626,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 +1663,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
More information about the wayland-devel
mailing list