editor: Load a file if specified on command line
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@osg.samsung.com> Reviewed-by: Yong Bakos <ybakos@humanoriented.com>
This commit is contained in:
parent
3d90da21fe
commit
411ffabbb5
@ -25,6 +25,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -1513,7 +1514,7 @@ usage(const char *program_name, int exit_code)
|
|||||||
{
|
{
|
||||||
unsigned k;
|
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++) {
|
for (k = 0; k < ARRAY_LENGTH(editor_options); k++) {
|
||||||
const struct weston_option *p = &editor_options[k];
|
const struct weston_option *p = &editor_options[k];
|
||||||
if (p->name) {
|
if (p->name) {
|
||||||
@ -1532,10 +1533,58 @@ usage(const char *program_name, int exit_code)
|
|||||||
exit(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
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct editor editor;
|
struct editor editor;
|
||||||
|
char *text_buffer = NULL;
|
||||||
|
|
||||||
parse_options(editor_options, ARRAY_LENGTH(editor_options),
|
parse_options(editor_options, ARRAY_LENGTH(editor_options),
|
||||||
&argc, argv);
|
&argc, argv);
|
||||||
@ -1543,9 +1592,14 @@ main(int argc, char *argv[])
|
|||||||
usage(argv[0], EXIT_SUCCESS);
|
usage(argv[0], EXIT_SUCCESS);
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
|
if (argv[1][0] == '-')
|
||||||
usage(argv[0], EXIT_FAILURE);
|
usage(argv[0], EXIT_FAILURE);
|
||||||
/* FIXME: Use remaining arguments as a path/filename to load */
|
|
||||||
return 0;
|
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);
|
memset(&editor, 0, sizeof editor);
|
||||||
@ -1571,6 +1625,9 @@ main(int argc, char *argv[])
|
|||||||
editor.window = window_create(editor.display);
|
editor.window = window_create(editor.display);
|
||||||
editor.widget = window_frame_create(editor.window, &editor);
|
editor.widget = window_frame_create(editor.window, &editor);
|
||||||
|
|
||||||
|
if (text_buffer)
|
||||||
|
editor.entry = text_entry_create(&editor, text_buffer);
|
||||||
|
else
|
||||||
editor.entry = text_entry_create(&editor, "Entry");
|
editor.entry = text_entry_create(&editor, "Entry");
|
||||||
editor.entry->click_to_show = opt_click_to_show;
|
editor.entry->click_to_show = opt_click_to_show;
|
||||||
if (opt_preferred_language)
|
if (opt_preferred_language)
|
||||||
@ -1605,6 +1662,7 @@ main(int argc, char *argv[])
|
|||||||
widget_destroy(editor.widget);
|
widget_destroy(editor.widget);
|
||||||
window_destroy(editor.window);
|
window_destroy(editor.window);
|
||||||
display_destroy(editor.display);
|
display_destroy(editor.display);
|
||||||
|
free(text_buffer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user