[PATCH weston 3/3] editor: Load a file if specified on command line

Bryce Harrington bryce at osg.samsung.com
Sun Nov 20 22:00:25 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 | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 110 insertions(+), 5 deletions(-)

diff --git a/clients/editor.c b/clients/editor.c
index ead7b6f..de50ef7 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>
@@ -1523,7 +1524,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) {
@@ -1542,10 +1543,95 @@ usage(const char *program_name, int exit_code)
 	exit(exit_code);
 }
 
+/* Join remaining arguments in argv as a space-delimited string.
+ *
+ * Caller is responsible for freeing the returned string.
+ * If there are no remaining arguments, returns a blank string.
+ * Returns NULL on out of memory error.
+*/
+char *
+join_argv(int argc, char *argv[]) {
+	char *filename;
+	int buf_size = 0;
+	int offset = 0;
+	int i;
+
+	for (i = 0; i < argc; i++)
+		buf_size += strlen(argv[i]) + 1;
+
+	filename = (char*) malloc(sizeof(char) * (buf_size + 1));
+	if (filename == NULL)
+		return NULL;
+	filename[0] = '\0';
+
+	while (argv++, --argc) {
+		int num = buf_size - offset;
+		int written = 0;
+		char space = ' ';
+
+		if (argc == 1)
+			space = '\0';
+		written = snprintf(filename+offset, num,
+				   "%s%c", *argv, space);
+		if (num < written)
+			break;
+		offset += written;
+	}
+	return filename;
+}
+
+/* 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.
+ */
+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),
@@ -1556,9 +1642,24 @@ main(int argc, char *argv[])
 		version(argv[0], EXIT_SUCCESS);
 
 	if (argc > 1) {
-		usage(argv[0], EXIT_FAILURE);
-		/* FIXME: Use remaining arguments as a path/filename to load */
-		return 0;
+		char *filename = NULL;
+
+		if (argv[1][0] == '-')
+			usage(argv[0], EXIT_FAILURE);
+
+		filename = join_argv(argc, argv);
+		if (filename == NULL || strlen(filename) < 1) {
+			fprintf(stderr, "could not process command line arguments: %m\n");
+			return -1;
+		}
+
+		text_buffer = read_file(filename);
+		if (text_buffer == NULL) {
+			fprintf(stderr, "could not read '%s': %m\n", filename);
+			free(filename);
+			return -1;
+		}
+		free(filename);
 	}
 
 	memset(&editor, 0, sizeof editor);
@@ -1584,7 +1685,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);
@@ -1618,6 +1722,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