core: Add Efl_Io_File example into efl-core reference
Refactor the copier code into a separate method for clarity.
This commit is contained in:
parent
c41c0cc319
commit
bc275959dd
|
@ -2,6 +2,7 @@
|
|||
#define EFL_BETA_API_SUPPORT 1
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <Eina.h>
|
||||
#include <Efl_Core.h>
|
||||
|
@ -9,12 +10,76 @@
|
|||
/*
|
||||
* Efl.IO examples.
|
||||
*
|
||||
* This IO example simply copies data from a source (stdin) to a
|
||||
* destination (stdout) using an Efl.Io.Copier.
|
||||
* This IO example shows the use of various IO APIs. First we use Efl.Io.File
|
||||
* to read and write a standard file. Then we set up a copier to copy data from
|
||||
* a source (stdin) to a destination (stdout) using an Efl.Io.Copier.
|
||||
*/
|
||||
|
||||
Eo *_copier = NULL;
|
||||
|
||||
static void
|
||||
_io_write(const char *filename)
|
||||
{
|
||||
Eina_Slice content = EINA_SLICE_STR("1. Some dummy content\n2. \n3. With more...\n");
|
||||
Efl_Io_File *file;
|
||||
|
||||
file = efl_add(EFL_IO_FILE_CLASS, NULL,
|
||||
efl_file_set(efl_added, filename, NULL), // mandatory
|
||||
efl_io_file_flags_set(efl_added, O_WRONLY | O_CREAT), // write and create - default is read
|
||||
efl_io_file_mode_set(efl_added, 0644), // neccessary if we use O_CREAT
|
||||
efl_io_closer_close_on_destructor_set(efl_added, EINA_TRUE)); // recommended
|
||||
|
||||
if (!file)
|
||||
return;
|
||||
|
||||
printf("Loaded file %s for read on fd %d\n", filename, efl_io_reader_fd_get(file));
|
||||
|
||||
if (efl_io_writer_write(file, &content, NULL) != EINA_ERROR_NO_ERROR)
|
||||
fprintf(stderr, "Failed to write test file\n");
|
||||
else
|
||||
printf("Wrote content to file\n");
|
||||
|
||||
efl_del(file);
|
||||
}
|
||||
|
||||
static void
|
||||
_io_read(const char *filename)
|
||||
{
|
||||
EINA_RW_SLICE_DECLARE(content, 1024);
|
||||
Efl_Io_File *file;
|
||||
|
||||
file = efl_add(EFL_IO_FILE_CLASS, NULL,
|
||||
efl_file_set(efl_added, filename, NULL), // mandatory
|
||||
efl_io_closer_close_on_destructor_set(efl_added, EINA_TRUE)); // recommended
|
||||
|
||||
if (!file)
|
||||
return;
|
||||
|
||||
printf("Loaded file %s for write on fd %d\n", filename, efl_io_reader_fd_get(file));
|
||||
|
||||
if (efl_io_reader_read(file, &content) != EINA_ERROR_NO_ERROR)
|
||||
fprintf(stderr, "Failed to read test file\n");
|
||||
else
|
||||
{
|
||||
char *string = eina_rw_slice_strdup(content);
|
||||
printf("Read content from file: %s\n", string);
|
||||
free(string);
|
||||
}
|
||||
|
||||
efl_del(file);
|
||||
}
|
||||
|
||||
static void
|
||||
_io_test()
|
||||
{
|
||||
const char *filename = "/tmp/efl_core_io_test.tmp";
|
||||
|
||||
_io_write(filename);
|
||||
_io_read(filename);
|
||||
|
||||
eina_file_unlink(filename);
|
||||
}
|
||||
|
||||
static void
|
||||
_copier_done(void *data EINA_UNUSED, const Efl_Event *event)
|
||||
{
|
||||
|
@ -38,17 +103,10 @@ EFL_CALLBACKS_ARRAY_DEFINE(copier_cbs,
|
|||
{ EFL_IO_COPIER_EVENT_DONE, _copier_done },
|
||||
{ EFL_IO_COPIER_EVENT_ERROR, _copier_error });
|
||||
|
||||
EAPI_MAIN void
|
||||
efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
|
||||
static void
|
||||
_copier_test(Efl_Loop *loop)
|
||||
{
|
||||
Eo *input, *output, *loop;
|
||||
|
||||
/*
|
||||
* Some objects such as the Efl.Io.Copier require a main loop,
|
||||
* thus their parent must be a loop provider.
|
||||
* We use the application's main loop itself.
|
||||
*/
|
||||
loop = ev->object;
|
||||
Eo *input, *output;
|
||||
|
||||
// set up our objects to copy, use stdin and stdout
|
||||
input = efl_add(EFL_IO_STDIN_CLASS, loop);
|
||||
|
@ -69,5 +127,21 @@ efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
|
|||
printf("Type something here and press enter, it will be copied to stdout...\n");
|
||||
printf(" (press Ctrl-D to exit)\n");
|
||||
}
|
||||
|
||||
EAPI_MAIN void
|
||||
efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
|
||||
{
|
||||
Efl_Loop *loop;
|
||||
|
||||
_io_test();
|
||||
|
||||
/*
|
||||
* Some objects such as the Efl.Io.Copier require a main loop,
|
||||
* thus their parent must be a loop provider.
|
||||
* We use the application's main loop itself.
|
||||
*/
|
||||
loop = ev->object;
|
||||
_copier_test(loop);
|
||||
}
|
||||
EFL_MAIN()
|
||||
|
||||
|
|
Loading…
Reference in New Issue