commit
6529a052d9
|
@ -0,0 +1,9 @@
|
||||||
|
MAINTAINERCLEANFILES = Makefile.in
|
||||||
|
|
||||||
|
SUBDIRS = eina
|
||||||
|
|
||||||
|
examples:
|
||||||
|
@$(MAKE) -C eina examples
|
||||||
|
|
||||||
|
install-examples:
|
||||||
|
@$(MAKE) -C eina install-examples
|
|
@ -0,0 +1,120 @@
|
||||||
|
MAINTAINERCLEANFILES = Makefile.in
|
||||||
|
|
||||||
|
AM_CPPFLAGS = \
|
||||||
|
-I. \
|
||||||
|
-I$(top_srcdir)/src/include/eina \
|
||||||
|
-I$(top_builddir)/src/include/eina
|
||||||
|
|
||||||
|
LDADD = $(top_builddir)/src/lib/eina/libeina.la
|
||||||
|
|
||||||
|
SRCS = \
|
||||||
|
eina_accessor_01.c \
|
||||||
|
eina_array_01.c \
|
||||||
|
eina_array_02.c \
|
||||||
|
eina_error_01.c \
|
||||||
|
eina_file_01.c \
|
||||||
|
eina_hash_01.c \
|
||||||
|
eina_hash_02.c \
|
||||||
|
eina_hash_03.c \
|
||||||
|
eina_hash_04.c \
|
||||||
|
eina_hash_05.c \
|
||||||
|
eina_hash_06.c \
|
||||||
|
eina_hash_07.c \
|
||||||
|
eina_hash_08.c \
|
||||||
|
eina_iterator_01.c \
|
||||||
|
eina_list_01.c \
|
||||||
|
eina_list_02.c \
|
||||||
|
eina_list_03.c \
|
||||||
|
eina_list_04.c \
|
||||||
|
eina_log_01.c \
|
||||||
|
eina_log_02.c \
|
||||||
|
eina_log_03.c \
|
||||||
|
eina_inlist_01.c \
|
||||||
|
eina_inlist_02.c \
|
||||||
|
eina_inlist_03.c \
|
||||||
|
eina_str_01.c \
|
||||||
|
eina_strbuf_01.c \
|
||||||
|
eina_stringshare_01.c \
|
||||||
|
eina_tiler_01.c \
|
||||||
|
eina_simple_xml_parser_01.c \
|
||||||
|
eina_value_01.c \
|
||||||
|
eina_value_02.c \
|
||||||
|
eina_value_03.c \
|
||||||
|
eina_inarray_01.c \
|
||||||
|
eina_inarray_02.c
|
||||||
|
#eina_magic_01.c \
|
||||||
|
#eina_model_01.c \
|
||||||
|
#eina_model_02.c \
|
||||||
|
#eina_model_03.c
|
||||||
|
|
||||||
|
EXTRA_PROGRAMS = \
|
||||||
|
eina_accessor_01 \
|
||||||
|
eina_array_01 \
|
||||||
|
eina_array_02 \
|
||||||
|
eina_error_01 \
|
||||||
|
eina_file_01 \
|
||||||
|
eina_hash_01 \
|
||||||
|
eina_hash_02 \
|
||||||
|
eina_hash_03 \
|
||||||
|
eina_hash_04 \
|
||||||
|
eina_hash_05 \
|
||||||
|
eina_hash_06 \
|
||||||
|
eina_hash_07 \
|
||||||
|
eina_hash_08 \
|
||||||
|
eina_iterator_01 \
|
||||||
|
eina_list_01 \
|
||||||
|
eina_list_02 \
|
||||||
|
eina_list_03 \
|
||||||
|
eina_list_04 \
|
||||||
|
eina_log_01 \
|
||||||
|
eina_log_02 \
|
||||||
|
eina_log_03 \
|
||||||
|
eina_inlist_01 \
|
||||||
|
eina_inlist_02 \
|
||||||
|
eina_inlist_03 \
|
||||||
|
eina_str_01 \
|
||||||
|
eina_strbuf_01 \
|
||||||
|
eina_stringshare_01 \
|
||||||
|
eina_magic_01 \
|
||||||
|
eina_simple_xml_parser_01 \
|
||||||
|
eina_value_01 \
|
||||||
|
eina_value_02 \
|
||||||
|
eina_value_03 \
|
||||||
|
eina_inarray_01 \
|
||||||
|
eina_inarray_02
|
||||||
|
#eina_model_01 \
|
||||||
|
#eina_model_02 \
|
||||||
|
#eina_model_03 \
|
||||||
|
#eina_model_04
|
||||||
|
|
||||||
|
#eina_model_04_SOURCES = \
|
||||||
|
#eina_model_04_animal.c \
|
||||||
|
#eina_model_04_child.c \
|
||||||
|
#eina_model_04_human.c \
|
||||||
|
#eina_model_04_main.c \
|
||||||
|
#eina_model_04_parrot.c \
|
||||||
|
#eina_model_04_whistler.c \
|
||||||
|
#eina_model_04_animal.h \
|
||||||
|
#eina_model_04_child.h \
|
||||||
|
#eina_model_04_human.h \
|
||||||
|
#eina_model_04_parrot.h \
|
||||||
|
#eina_model_04_whistler.h
|
||||||
|
|
||||||
|
if BUILD_TILER_EXAMPLE
|
||||||
|
AM_CPPFLAGS += @ECORE_EVAS_CFLAGS@
|
||||||
|
EXTRA_PROGRAMS += eina_tiler_01
|
||||||
|
eina_tiler_01_LDADD = $(top_builddir)/src/lib/eina/libeina.la @ECORE_EVAS_LIBS@
|
||||||
|
endif
|
||||||
|
|
||||||
|
examples: $(EXTRA_PROGRAMS)
|
||||||
|
|
||||||
|
install-examples:
|
||||||
|
mkdir -p $(pkgdatadir)/examples
|
||||||
|
$(install_sh_DATA) -c $(SRCS) $(pkgdatadir)/examples
|
||||||
|
|
||||||
|
unsinstall-local:
|
||||||
|
for f in $(SRCS) ; do \
|
||||||
|
rm -f $(pkgdatadir)/examples/$$f ; \
|
||||||
|
done
|
||||||
|
|
||||||
|
EXTRA_DIST = addr_book.txt chat.xml
|
|
@ -0,0 +1,235 @@
|
||||||
|
/*
|
||||||
|
* Compile with:
|
||||||
|
* gcc -o eina_model_01 eina_model_01.c `pkg-config --cflags --libs eina`
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This example demonstrates the usage of Eina Model by implementing
|
||||||
|
* Bank Account Class, which is inherited from Base Class;
|
||||||
|
* and Credit Card Class, which is inherited from Bank Account Class.
|
||||||
|
*
|
||||||
|
* Base Class(Eina_Model_Type) --> Bank Account Class --> Credit Card Class
|
||||||
|
*
|
||||||
|
* Bank Account Class implements "bank_account_data_set()" and "print()" methods;
|
||||||
|
* Credit Card Class inherits these two and implements "credit_card_data_set()"
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Bank Account Class::print() calls for "_bank_account_data_print"
|
||||||
|
* Credit Card Class ::print() is reloaded with "_credit_card_data_print()"
|
||||||
|
* which calls for parent function "_bank_account_data_print"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <eina_safety_checks.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Defining type for new model type
|
||||||
|
* Model will have two methods
|
||||||
|
*/
|
||||||
|
typedef struct _Bank_Account_Type
|
||||||
|
{
|
||||||
|
Eina_Model_Type parent_class;
|
||||||
|
void (*bank_account_data_set)(Eina_Model *, const char *name, const char *number);
|
||||||
|
void (*print)(Eina_Model *);
|
||||||
|
} Bank_Account_Type;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Defining type for Bank Account private data
|
||||||
|
*/
|
||||||
|
typedef struct _Bank_Account_Data
|
||||||
|
{
|
||||||
|
char name[30];
|
||||||
|
char number[30];
|
||||||
|
} Bank_Account_Data;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Defining type for Credit Card model type, which will be inherited from Bank Account model type
|
||||||
|
* Model will have two parent's methods and additional one
|
||||||
|
*/
|
||||||
|
typedef struct _Credit_Card_Type
|
||||||
|
{
|
||||||
|
Bank_Account_Type parent_class;
|
||||||
|
void (*credit_card_data_set)(Eina_Model *, const char *, const char *, int) ;
|
||||||
|
} Credit_Card_Type;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Defining type for Credit Card private data
|
||||||
|
*/
|
||||||
|
typedef struct _Credit_Card_Data
|
||||||
|
{
|
||||||
|
char number[30];
|
||||||
|
char expiry_date[30];
|
||||||
|
int pin;
|
||||||
|
} Credit_Card_Data;
|
||||||
|
|
||||||
|
static Bank_Account_Type _BANK_ACCOUNT_TYPE;
|
||||||
|
static Credit_Card_Type _CREDIT_CARD_TYPE;
|
||||||
|
static Eina_Model_Type *BANK_ACCOUNT_TYPE = (Eina_Model_Type *) &_BANK_ACCOUNT_TYPE;
|
||||||
|
static Eina_Model_Type *CREDIT_CARD_TYPE = (Eina_Model_Type *) &_CREDIT_CARD_TYPE;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Defining method for for Bank Account data
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
_bank_account_data_set(Eina_Model *mdl, const char *name, const char *number)
|
||||||
|
{
|
||||||
|
Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE);
|
||||||
|
|
||||||
|
if (!bdata)
|
||||||
|
printf("ERROR\n");
|
||||||
|
|
||||||
|
if (name != NULL)
|
||||||
|
{
|
||||||
|
strncpy(bdata->name, name, sizeof(bdata->name));
|
||||||
|
bdata->name[sizeof(bdata->number) - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (number != NULL)
|
||||||
|
{
|
||||||
|
strncpy(bdata->number, number, sizeof(bdata->number));
|
||||||
|
bdata->number[sizeof(bdata->number) - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
_credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin)
|
||||||
|
{
|
||||||
|
Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE);
|
||||||
|
|
||||||
|
if (!cdata)
|
||||||
|
printf("ERROR\n");
|
||||||
|
|
||||||
|
if (number != NULL)
|
||||||
|
{
|
||||||
|
strncpy(cdata->number, number, sizeof(cdata->number));
|
||||||
|
cdata->number[sizeof(cdata->number) - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expiry_date != NULL)
|
||||||
|
{
|
||||||
|
strncpy(cdata->expiry_date, expiry_date, sizeof(cdata->expiry_date));
|
||||||
|
cdata->expiry_date[sizeof(cdata->expiry_date) - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
cdata->pin = pin;
|
||||||
|
printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_bank_account_data_print(Eina_Model *mdl)
|
||||||
|
{
|
||||||
|
const Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE);
|
||||||
|
|
||||||
|
printf("\n%s :: %s %p \n\tName: %s(%p)\n\tAccount: %s(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl
|
||||||
|
, bdata->name, bdata->name, bdata->number, bdata->number);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_credit_card_data_print(Eina_Model *mdl)
|
||||||
|
{
|
||||||
|
void (*pf)(Eina_Model *);
|
||||||
|
const Eina_Model_Type *ptype = eina_model_type_parent_get(eina_model_type_get(mdl));
|
||||||
|
//const Eina_Model_Type *ptype = eina_model_type_get(mdl);
|
||||||
|
|
||||||
|
pf = eina_model_type_method_resolve(ptype, mdl, Bank_Account_Type, print);
|
||||||
|
if (pf)
|
||||||
|
pf(mdl);
|
||||||
|
else
|
||||||
|
printf("ERROR: %d", __LINE__);
|
||||||
|
const Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE);
|
||||||
|
printf("%s :: %s %p \n\tNumber: %s(%p)\n\tCC Expiry Date: %s(%p)\n\tCC PIN: %d(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl
|
||||||
|
, cdata->number, cdata->number, cdata->expiry_date, cdata->expiry_date, cdata->pin, &cdata->pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define BANK_ACCOUNT(x) ((Bank_Account_Type *) x)
|
||||||
|
#define CREDIT_CARD(x) ((Credit_Card_Type *) x)
|
||||||
|
|
||||||
|
void
|
||||||
|
bank_account_data_set(Eina_Model *mdl, const char *name, char *number)
|
||||||
|
{
|
||||||
|
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *, const char *, const char *);
|
||||||
|
pf = eina_model_method_resolve(mdl, Bank_Account_Type, bank_account_data_set);
|
||||||
|
if (pf)
|
||||||
|
pf(mdl, name, number);
|
||||||
|
else
|
||||||
|
printf("ERROR %d\n", __LINE__);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
data_print(Eina_Model *mdl)
|
||||||
|
{
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *);
|
||||||
|
pf = eina_model_method_resolve(mdl, Bank_Account_Type, print);
|
||||||
|
if (pf)
|
||||||
|
pf(mdl);
|
||||||
|
else
|
||||||
|
printf("ERROR %d\n", __LINE__);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin)
|
||||||
|
{
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, CREDIT_CARD_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *, const char *, const char *, int);
|
||||||
|
pf = eina_model_method_resolve(mdl, Credit_Card_Type, credit_card_data_set);
|
||||||
|
if (pf)
|
||||||
|
pf(mdl, number, expiry_date, pin);
|
||||||
|
else
|
||||||
|
printf("ERROR %d\n", __LINE__);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Eina_Model *b, *cc;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
memset(&_BANK_ACCOUNT_TYPE, 0, sizeof(_BANK_ACCOUNT_TYPE));
|
||||||
|
memset(&_CREDIT_CARD_TYPE, 0, sizeof(_CREDIT_CARD_TYPE));
|
||||||
|
|
||||||
|
BANK_ACCOUNT_TYPE->version = EINA_MODEL_TYPE_VERSION;
|
||||||
|
BANK_ACCOUNT_TYPE->type_size = sizeof(Bank_Account_Type);
|
||||||
|
BANK_ACCOUNT_TYPE->private_size = sizeof(Bank_Account_Data);
|
||||||
|
BANK_ACCOUNT_TYPE->name = "Bank_Account_Model";
|
||||||
|
BANK_ACCOUNT_TYPE->parent = EINA_MODEL_TYPE_GENERIC;
|
||||||
|
BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->bank_account_data_set = _bank_account_data_set;
|
||||||
|
BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->print = _bank_account_data_print;
|
||||||
|
|
||||||
|
CREDIT_CARD_TYPE->version = EINA_MODEL_TYPE_VERSION;
|
||||||
|
CREDIT_CARD_TYPE->type_size = sizeof(Credit_Card_Type);
|
||||||
|
CREDIT_CARD_TYPE->private_size = sizeof(Credit_Card_Data);
|
||||||
|
CREDIT_CARD_TYPE->name = "Credit_Card_Model";
|
||||||
|
CREDIT_CARD_TYPE->parent = BANK_ACCOUNT_TYPE;
|
||||||
|
CREDIT_CARD(CREDIT_CARD_TYPE)->credit_card_data_set = _credit_card_data_set;
|
||||||
|
BANK_ACCOUNT(CREDIT_CARD_TYPE)->print = _credit_card_data_print;
|
||||||
|
|
||||||
|
b = eina_model_new(BANK_ACCOUNT_TYPE); //creating object of bank class
|
||||||
|
cc = eina_model_new(CREDIT_CARD_TYPE); //creating object of credit card class
|
||||||
|
|
||||||
|
bank_account_data_set(b, "Bill Clark", "8569214756");
|
||||||
|
bank_account_data_set(cc, "John Smith", "3154789");
|
||||||
|
|
||||||
|
credit_card_data_set(cc, "5803 6589 4786 3279 9173", "01/01/2015", 1234);
|
||||||
|
|
||||||
|
data_print(b);
|
||||||
|
data_print(cc);
|
||||||
|
|
||||||
|
eina_model_unref(b);
|
||||||
|
eina_model_unref(cc);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_model_02.c -o eina_model_02 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Eina_Model *m;
|
||||||
|
char *s;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
m = eina_model_new(EINA_MODEL_TYPE_GENERIC);
|
||||||
|
|
||||||
|
eina_model_event_callback_add(m, "deleted", _cb_on_deleted, NULL);
|
||||||
|
|
||||||
|
//Adding properties to model
|
||||||
|
for (i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
Eina_Value val;
|
||||||
|
char name[2] = {'a'+ i, 0};
|
||||||
|
eina_value_setup(&val, EINA_VALUE_TYPE_INT);
|
||||||
|
eina_value_set(&val, i);
|
||||||
|
eina_model_property_set(m, name, &val);
|
||||||
|
eina_value_flush(&val);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Adding children to model
|
||||||
|
for (i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
Eina_Value val;
|
||||||
|
Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC);
|
||||||
|
eina_value_setup(&val, EINA_VALUE_TYPE_INT);
|
||||||
|
eina_value_set(&val, i);
|
||||||
|
eina_model_property_set(c, "x", &val);
|
||||||
|
|
||||||
|
eina_model_event_callback_add(c, "deleted", _cb_on_deleted, NULL);
|
||||||
|
|
||||||
|
eina_model_child_append(m, c);
|
||||||
|
//Now that the child has been appended to a model, it's parent will manage it's lifecycle
|
||||||
|
eina_model_unref(c);
|
||||||
|
eina_value_flush(&val);
|
||||||
|
}
|
||||||
|
|
||||||
|
s = eina_model_to_string(m);
|
||||||
|
printf("model as string:\n%s\n", s);
|
||||||
|
|
||||||
|
free(s);
|
||||||
|
eina_model_unref(m);
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info)
|
||||||
|
{
|
||||||
|
printf("deleted %p\n", model);
|
||||||
|
}
|
|
@ -0,0 +1,236 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_model_03.c -o eina_model_03 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
static Eina_Model_Type *ADDRESS_BOOK_TYPE;
|
||||||
|
static Eina_Model_Type *PERSON_TYPE;
|
||||||
|
|
||||||
|
static void address_book_init(void);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Eina_Model *address_book;
|
||||||
|
Eina_Value val;
|
||||||
|
int i, count;
|
||||||
|
char *s;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
address_book_init();
|
||||||
|
|
||||||
|
address_book = eina_model_new(ADDRESS_BOOK_TYPE);
|
||||||
|
|
||||||
|
eina_value_setup(&val, EINA_VALUE_TYPE_STRING);
|
||||||
|
eina_value_set(&val, "addr_book.txt");
|
||||||
|
eina_model_property_set(address_book, "filename", &val);
|
||||||
|
eina_value_flush(&val);
|
||||||
|
|
||||||
|
eina_model_load(address_book);
|
||||||
|
s = eina_model_to_string(address_book);
|
||||||
|
printf("model as string:\n%s\n\n", s);
|
||||||
|
free(s);
|
||||||
|
|
||||||
|
count = eina_model_child_count(address_book);
|
||||||
|
printf("Address Book with %d entries:\n", count);
|
||||||
|
for (i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
Eina_Model *person = eina_model_child_get(address_book, i);
|
||||||
|
Eina_Value nameval, emailval;
|
||||||
|
const char *name, *email;
|
||||||
|
|
||||||
|
eina_model_property_get(person, "name", &nameval);
|
||||||
|
eina_model_property_get(person, "email", &emailval);
|
||||||
|
|
||||||
|
eina_value_get(&nameval, &name);
|
||||||
|
eina_value_get(&emailval, &email);
|
||||||
|
|
||||||
|
printf("%02d \"%s\" <%s>\n", i, name, email);
|
||||||
|
|
||||||
|
// We don't need property values anymore
|
||||||
|
eina_value_flush(&nameval);
|
||||||
|
eina_value_flush(&emailval);
|
||||||
|
|
||||||
|
// We don't need our reference to person anymore
|
||||||
|
eina_model_unref(person);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_model_unref(address_book);
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Structure Descriptions are just used internally in the type constructors:
|
||||||
|
static Eina_Value_Struct_Desc *ADDRESS_BOOK_DESC;
|
||||||
|
static Eina_Value_Struct_Desc *PERSON_DESC;
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_person_constructor(Eina_Model *model)
|
||||||
|
{
|
||||||
|
// call parent type constructor, like "super" in other languages:
|
||||||
|
if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model))
|
||||||
|
return EINA_FALSE;
|
||||||
|
|
||||||
|
// Do specific setup of our internal structure, letting it know about
|
||||||
|
// our description
|
||||||
|
return eina_model_struct_set(model, PERSON_DESC, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_address_book_constructor(Eina_Model *model)
|
||||||
|
{
|
||||||
|
// call parent type constructor, like "super" in other languages:
|
||||||
|
if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model))
|
||||||
|
return EINA_FALSE;
|
||||||
|
|
||||||
|
// Do specific setup of our internal structure, letting it know about
|
||||||
|
// our description
|
||||||
|
return eina_model_struct_set(model, ADDRESS_BOOK_DESC, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_address_book_load(Eina_Model *model)
|
||||||
|
{
|
||||||
|
const char *filename;
|
||||||
|
Eina_Value val;
|
||||||
|
char buf[256];
|
||||||
|
FILE *f;
|
||||||
|
|
||||||
|
// We retrieve filename from property of same name:
|
||||||
|
eina_model_property_get(model, "filename", &val);
|
||||||
|
eina_value_get(&val, &filename);
|
||||||
|
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN_VAL(filename, EINA_FALSE);
|
||||||
|
|
||||||
|
f = fopen(filename, "r");
|
||||||
|
|
||||||
|
// Now that we have used filename, we must free its memory holder:
|
||||||
|
eina_value_flush(&val);
|
||||||
|
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN_VAL(f, EINA_FALSE);
|
||||||
|
|
||||||
|
while (fgets(buf, sizeof(buf), f))
|
||||||
|
{
|
||||||
|
Eina_Model *person;
|
||||||
|
char *name, *email;
|
||||||
|
|
||||||
|
if (strlen(buf) <= 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
name = strtok(buf, "\t");
|
||||||
|
email = strtok(NULL, "\n");
|
||||||
|
|
||||||
|
if ((!name) || (!email)) continue;
|
||||||
|
|
||||||
|
// Create person
|
||||||
|
person = eina_model_new(PERSON_TYPE);
|
||||||
|
|
||||||
|
// Setup value type as string, as our properties are strings:
|
||||||
|
eina_value_setup(&val, EINA_VALUE_TYPE_STRING);
|
||||||
|
|
||||||
|
// Set string properties:
|
||||||
|
eina_value_set(&val, name);
|
||||||
|
eina_model_property_set(person, "name", &val);
|
||||||
|
|
||||||
|
eina_value_set(&val, email);
|
||||||
|
eina_model_property_set(person, "email", &val);
|
||||||
|
|
||||||
|
// Flush value, free string
|
||||||
|
eina_value_flush(&val);
|
||||||
|
|
||||||
|
// Add person to the end of model children
|
||||||
|
eina_model_child_append(model, person);
|
||||||
|
|
||||||
|
// Model already holds its reference to person, we release ours
|
||||||
|
eina_model_unref(person);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
address_book_init(void)
|
||||||
|
{
|
||||||
|
// Declare type for internal struct, this is just used to easily
|
||||||
|
// create Eina_Value_Struct_Member array for Eina_Value_Struct_Desc.
|
||||||
|
//
|
||||||
|
// We don't need this structure outside address_book_init()
|
||||||
|
// as it is managed automatically by Eina_Value_Struct, used by
|
||||||
|
// Eina_Model_Struct! Handy! :-)
|
||||||
|
typedef struct _Person Person;
|
||||||
|
struct _Person
|
||||||
|
{
|
||||||
|
const char *name;
|
||||||
|
const char *email;
|
||||||
|
};
|
||||||
|
static Eina_Value_Struct_Member person_members[] = {
|
||||||
|
// no eina_value_type as they are not constant initializers, see below.
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, Person, name),
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, Person, email)
|
||||||
|
};
|
||||||
|
// Values that cannot be set on static declarations since they are not
|
||||||
|
// constant initializers. It is a nitpick from C that we need to deal with
|
||||||
|
// here and on all our other declarations.
|
||||||
|
person_members[0].type = EINA_VALUE_TYPE_STRING;
|
||||||
|
person_members[1].type = EINA_VALUE_TYPE_STRING;
|
||||||
|
|
||||||
|
static Eina_Value_Struct_Desc person_desc = {
|
||||||
|
EINA_VALUE_STRUCT_DESC_VERSION,
|
||||||
|
NULL, // no special operations
|
||||||
|
person_members,
|
||||||
|
EINA_C_ARRAY_LENGTH(person_members),
|
||||||
|
sizeof(Person)
|
||||||
|
};
|
||||||
|
static Eina_Model_Type person_type = EINA_MODEL_TYPE_INIT_NOPRIVATE
|
||||||
|
("Person_Type",
|
||||||
|
Eina_Model_Type,
|
||||||
|
NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer!
|
||||||
|
NULL, // no extra interfaces
|
||||||
|
NULL // no extra events);
|
||||||
|
);
|
||||||
|
person_type.parent = EINA_MODEL_TYPE_STRUCT;
|
||||||
|
// Set our overloaded methods:
|
||||||
|
person_type.constructor = _person_constructor;
|
||||||
|
|
||||||
|
typedef struct _Address_Book Address_Book;
|
||||||
|
struct _Address_Book
|
||||||
|
{
|
||||||
|
const char *filename;
|
||||||
|
};
|
||||||
|
static Eina_Value_Struct_Member address_book_members[] = {
|
||||||
|
// no eina_value_type as they are not constant initializers, see below.
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, Address_Book, filename)
|
||||||
|
};
|
||||||
|
address_book_members[0].type = EINA_VALUE_TYPE_STRING;
|
||||||
|
static Eina_Value_Struct_Desc address_book_desc = {
|
||||||
|
EINA_VALUE_STRUCT_DESC_VERSION,
|
||||||
|
NULL, // no special operations
|
||||||
|
address_book_members,
|
||||||
|
EINA_C_ARRAY_LENGTH(address_book_members),
|
||||||
|
sizeof(Address_Book)
|
||||||
|
};
|
||||||
|
static Eina_Model_Type address_book_type = EINA_MODEL_TYPE_INIT_NOPRIVATE
|
||||||
|
("Address_Book_Type",
|
||||||
|
Eina_Model_Type,
|
||||||
|
NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer!
|
||||||
|
NULL, // no extra interfaces
|
||||||
|
NULL // no extra events);
|
||||||
|
);
|
||||||
|
address_book_type.parent = EINA_MODEL_TYPE_STRUCT;
|
||||||
|
// Set our overloaded methods:
|
||||||
|
address_book_type.constructor = _address_book_constructor;
|
||||||
|
address_book_type.load = _address_book_load;
|
||||||
|
|
||||||
|
// Expose the configured pointers to public usage:
|
||||||
|
// NOTE: they are static, so they live after this function returns!
|
||||||
|
PERSON_TYPE = &person_type;
|
||||||
|
PERSON_DESC = &person_desc;
|
||||||
|
|
||||||
|
ADDRESS_BOOK_TYPE = &address_book_type;
|
||||||
|
ADDRESS_BOOK_DESC = &address_book_desc;
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* animal.c
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "eina_model_04_animal.h"
|
||||||
|
|
||||||
|
static Eina_Bool initialized = EINA_FALSE;
|
||||||
|
|
||||||
|
static void
|
||||||
|
_animal_eat(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Eat Animal\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_animal_breathe(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Breathe Animal\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *ANIMAL_MODEL_TYPE_NAME = NULL;
|
||||||
|
static Animal_Type _ANIMAL_TYPE;
|
||||||
|
|
||||||
|
const Eina_Model_Type * const ANIMAL_TYPE = (Eina_Model_Type *) &_ANIMAL_TYPE;
|
||||||
|
|
||||||
|
void
|
||||||
|
animal_init(void)
|
||||||
|
{
|
||||||
|
Eina_Model_Type *type;
|
||||||
|
|
||||||
|
if (initialized) return;
|
||||||
|
initialized = EINA_TRUE;
|
||||||
|
|
||||||
|
ANIMAL_MODEL_TYPE_NAME = "Animal_Model_Type";
|
||||||
|
|
||||||
|
type = (Eina_Model_Type *)&_ANIMAL_TYPE;
|
||||||
|
type->version = EINA_MODEL_TYPE_VERSION;
|
||||||
|
type->name = ANIMAL_MODEL_TYPE_NAME;
|
||||||
|
type->private_size = 0;
|
||||||
|
|
||||||
|
eina_model_type_subclass_setup(type, EINA_MODEL_TYPE_GENERIC);
|
||||||
|
|
||||||
|
/* define extra methods */
|
||||||
|
|
||||||
|
type->type_size = sizeof(Animal_Type);
|
||||||
|
ANIMAL_TYPE(type)->breathe = _animal_breathe;
|
||||||
|
ANIMAL_TYPE(type)->eat = _animal_eat;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
animal_breathe(Eina_Model *m)
|
||||||
|
{
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *m);
|
||||||
|
pf = eina_model_method_resolve(m, Animal_Type, breathe);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
animal_eat(Eina_Model *m)
|
||||||
|
{
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *m);
|
||||||
|
pf = eina_model_method_resolve(m, Animal_Type, eat);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* animal.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANIMAL_H_
|
||||||
|
#define ANIMAL_H_
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
extern const char *ANIMAL_MODEL_TYPE_NAME;
|
||||||
|
extern const Eina_Model_Type * const ANIMAL_TYPE;
|
||||||
|
|
||||||
|
#define ANIMAL_TYPE(x) ((Animal_Type *) (eina_model_type_subclass_check((x), ANIMAL_TYPE) ? (x) : NULL))
|
||||||
|
|
||||||
|
typedef struct _Animal_Type
|
||||||
|
{
|
||||||
|
Eina_Model_Type parent_class;
|
||||||
|
void (*eat)(Eina_Model *m);
|
||||||
|
void (*breathe)(Eina_Model *m);
|
||||||
|
} Animal_Type;
|
||||||
|
|
||||||
|
void animal_init(void);
|
||||||
|
void animal_breathe(Eina_Model *m);
|
||||||
|
void animal_eat(Eina_Model *m);
|
||||||
|
|
||||||
|
#endif /* ANIMAL_H_ */
|
|
@ -0,0 +1,81 @@
|
||||||
|
/*
|
||||||
|
* child.c
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "eina_model_04_child.h"
|
||||||
|
#include "eina_model_04_whistler.h"
|
||||||
|
|
||||||
|
static Eina_Bool initialized = EINA_FALSE;
|
||||||
|
|
||||||
|
static void
|
||||||
|
_child_cry(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Cry Child\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_child_dive(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Dive Child\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *CHILD_MODEL_TYPE_NAME = NULL;
|
||||||
|
|
||||||
|
static Child_Type _CHILD_TYPE;
|
||||||
|
const Eina_Model_Type * const CHILD_TYPE = (Eina_Model_Type *) &_CHILD_TYPE;
|
||||||
|
|
||||||
|
static const Diver_Interface _DIVER_INTERFACE;
|
||||||
|
static const Eina_Model_Interface * const DIVER_INTERFACE =
|
||||||
|
(Eina_Model_Interface *) &_DIVER_INTERFACE;
|
||||||
|
|
||||||
|
static const Eina_Model_Interface * CLASS_INTERFACE_ARRAY[] =
|
||||||
|
{ &_DIVER_INTERFACE.base_interface, NULL }; //this array is for model
|
||||||
|
|
||||||
|
void
|
||||||
|
child_init()
|
||||||
|
{
|
||||||
|
Eina_Model_Type *type;
|
||||||
|
|
||||||
|
if (initialized) return;
|
||||||
|
initialized = EINA_TRUE;
|
||||||
|
|
||||||
|
human_init();
|
||||||
|
|
||||||
|
//overriding Diver Interface
|
||||||
|
Eina_Model_Interface * iface = (Eina_Model_Interface *) &_DIVER_INTERFACE;
|
||||||
|
iface->version = EINA_MODEL_INTERFACE_VERSION;
|
||||||
|
iface->interface_size = sizeof(Diver_Interface);
|
||||||
|
iface->name = DIVER_INTERFACE_NAME;
|
||||||
|
DIVER_INTERFACE(iface)->dive = _child_dive;
|
||||||
|
|
||||||
|
//creating instance of Child type
|
||||||
|
CHILD_MODEL_TYPE_NAME = "Child_Model_Type";
|
||||||
|
|
||||||
|
type = (Eina_Model_Type *) &_CHILD_TYPE;
|
||||||
|
type->version = EINA_MODEL_TYPE_VERSION;
|
||||||
|
type->name = CHILD_MODEL_TYPE_NAME;
|
||||||
|
|
||||||
|
eina_model_type_subclass_setup(type, HUMAN_TYPE);
|
||||||
|
|
||||||
|
type->type_size = sizeof(Child_Type);
|
||||||
|
type->interfaces = CLASS_INTERFACE_ARRAY;
|
||||||
|
|
||||||
|
CHILD_TYPE(type)->cry = _child_cry;
|
||||||
|
}
|
||||||
|
|
||||||
|
//call for implemented Child Class function
|
||||||
|
void
|
||||||
|
child_cry(Eina_Model *m)
|
||||||
|
{
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, CHILD_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *m);
|
||||||
|
pf = eina_model_method_resolve(m, Child_Type, cry);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t\t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
/*
|
||||||
|
* child.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CHILD_H_
|
||||||
|
#define CHILD_H_
|
||||||
|
|
||||||
|
#include "eina_model_04_human.h"
|
||||||
|
|
||||||
|
extern const char *CHILD_MODEL_TYPE_NAME;
|
||||||
|
extern const Eina_Model_Type * const CHILD_TYPE;
|
||||||
|
#define CHILD_TYPE(x) ((Child_Type *) (eina_model_type_subclass_check((x), CHILD_TYPE) ? (x) : NULL))
|
||||||
|
|
||||||
|
typedef struct _Child_Type
|
||||||
|
{
|
||||||
|
Human_Type parent_class;
|
||||||
|
void (*cry)(Eina_Model *m);
|
||||||
|
} Child_Type;
|
||||||
|
|
||||||
|
void child_init();
|
||||||
|
void child_cry(Eina_Model *m);
|
||||||
|
|
||||||
|
#endif /* CHILD_H_ */
|
|
@ -0,0 +1,157 @@
|
||||||
|
/*
|
||||||
|
* human.c
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "eina_model_04_human.h"
|
||||||
|
#include "eina_model_04_whistler.h"
|
||||||
|
|
||||||
|
static Eina_Bool initialized = EINA_FALSE;
|
||||||
|
|
||||||
|
static void
|
||||||
|
_human_eat(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Salad\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_human_walk(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Walk\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_human_whistle(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Whistle Human\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_human_swim(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Swim Human\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_human_dive(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Dive Human\n");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* defining Human Model Instance
|
||||||
|
* defining Whistler Interface instance
|
||||||
|
* defining Swimmer Interface instance
|
||||||
|
* defining Diver Interface instance
|
||||||
|
*/
|
||||||
|
|
||||||
|
const char *HUMAN_MODEL_TYPE_NAME = NULL;
|
||||||
|
|
||||||
|
static Human_Type _HUMAN_TYPE;
|
||||||
|
const Eina_Model_Type * const HUMAN_TYPE = (Eina_Model_Type *) &_HUMAN_TYPE;
|
||||||
|
|
||||||
|
static const Whistler_Interface _WHISTLER_INTERFACE;
|
||||||
|
static const Eina_Model_Interface * const WHISTLER_INTERFACE =
|
||||||
|
(Eina_Model_Interface *) &_WHISTLER_INTERFACE;
|
||||||
|
|
||||||
|
static const Swimmer_Interface _SWIMMER_INTERFACE;
|
||||||
|
static const Eina_Model_Interface * const SWIMMER_INTERFACE =
|
||||||
|
(Eina_Model_Interface *) &_SWIMMER_INTERFACE;
|
||||||
|
|
||||||
|
static const Diver_Interface _DIVER_INTERFACE;
|
||||||
|
static const Eina_Model_Interface * const DIVER_INTERFACE =
|
||||||
|
(Eina_Model_Interface *) &_DIVER_INTERFACE;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* defining parent interfaces for Diver Interface instance
|
||||||
|
* defining Interfaces for Human Model instance
|
||||||
|
*/
|
||||||
|
static const Eina_Model_Interface * PARENT_INTERFACES_ARRAY[] =
|
||||||
|
{ &_SWIMMER_INTERFACE.base_interface, NULL }; //this array is for model
|
||||||
|
static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] =
|
||||||
|
{ &_WHISTLER_INTERFACE.base_interface, &_DIVER_INTERFACE.base_interface,
|
||||||
|
NULL }; //this array is for model
|
||||||
|
|
||||||
|
void
|
||||||
|
human_init()
|
||||||
|
{
|
||||||
|
Eina_Model_Type *type;
|
||||||
|
|
||||||
|
if (initialized) return;
|
||||||
|
initialized = EINA_TRUE;
|
||||||
|
|
||||||
|
animal_init();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initializing Whistler Interface Instance
|
||||||
|
*/
|
||||||
|
Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE;
|
||||||
|
iface->version = EINA_MODEL_INTERFACE_VERSION;
|
||||||
|
iface->interface_size = sizeof(Whistler_Interface);
|
||||||
|
iface->name = WHISTLER_INTERFACE_NAME;
|
||||||
|
WHISTLER_INTERFACE(iface)->whistle = _human_whistle;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initializing Swimmer Interface Instance
|
||||||
|
*/
|
||||||
|
iface = (Eina_Model_Interface *) &_SWIMMER_INTERFACE;
|
||||||
|
iface->version = EINA_MODEL_INTERFACE_VERSION;
|
||||||
|
iface->interface_size = sizeof(Swimmer_Interface);
|
||||||
|
iface->name = SWIMMER_INTERFACE_NAME;
|
||||||
|
SWIMMER_INTERFACE(iface)->swim = _human_swim;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initializing Diver Interface Instance
|
||||||
|
* Diver_Interface is inherited from Swimmer
|
||||||
|
*/
|
||||||
|
iface = (Eina_Model_Interface *) &_DIVER_INTERFACE;
|
||||||
|
iface->version = EINA_MODEL_INTERFACE_VERSION;
|
||||||
|
iface->interface_size = sizeof(Diver_Interface);
|
||||||
|
iface->name = DIVER_INTERFACE_NAME;
|
||||||
|
iface->interfaces = PARENT_INTERFACES_ARRAY;
|
||||||
|
DIVER_INTERFACE(iface)->dive = _human_dive;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initializing instance of Human Model
|
||||||
|
*/
|
||||||
|
|
||||||
|
HUMAN_MODEL_TYPE_NAME = "Human_Model_Type";
|
||||||
|
|
||||||
|
type = (Eina_Model_Type *) &_HUMAN_TYPE;
|
||||||
|
type->version = EINA_MODEL_TYPE_VERSION;
|
||||||
|
type->name = HUMAN_MODEL_TYPE_NAME;
|
||||||
|
type->private_size = 0;
|
||||||
|
|
||||||
|
eina_model_type_subclass_setup(type, ANIMAL_TYPE);
|
||||||
|
|
||||||
|
type->type_size = sizeof(Human_Type);
|
||||||
|
type->interfaces = MODEL_INTERFACES_ARRAY;
|
||||||
|
|
||||||
|
ANIMAL_TYPE(type)->eat = _human_eat;
|
||||||
|
HUMAN_TYPE(type)->walk =_human_walk;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call for implemented Human Class function
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
human_walk(Eina_Model *m)
|
||||||
|
{
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, HUMAN_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *m);
|
||||||
|
pf = eina_model_method_resolve(m, Human_Type, walk);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
/*
|
||||||
|
* human.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HUMAN_H_
|
||||||
|
#define HUMAN_H_
|
||||||
|
|
||||||
|
#include "eina_model_04_animal.h"
|
||||||
|
|
||||||
|
extern const char *HUMAN_MODEL_TYPE_NAME;
|
||||||
|
extern const Eina_Model_Type * const HUMAN_TYPE;
|
||||||
|
|
||||||
|
#define HUMAN_TYPE(x) ((Human_Type *) (eina_model_type_subclass_check((x), ANIMAL_TYPE) ? (x) : NULL))
|
||||||
|
|
||||||
|
typedef struct _Human_Type
|
||||||
|
{
|
||||||
|
Animal_Type parent_class;
|
||||||
|
void (*walk)(Eina_Model *m);
|
||||||
|
} Human_Type;
|
||||||
|
|
||||||
|
void human_init();
|
||||||
|
void human_walk(Eina_Model *m);
|
||||||
|
|
||||||
|
#endif /* HUMAN_H_ */
|
|
@ -0,0 +1,110 @@
|
||||||
|
/*
|
||||||
|
* main_animal.c
|
||||||
|
* compile with: gcc eina_model_04_*.c -o eina_model_04 `pkg-config --cflags --libs eina`
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This example demonstrates the extended usage of Eina Model.
|
||||||
|
* Class inheritance and interface implementation
|
||||||
|
*
|
||||||
|
* Animal Class is inherited from BaseClass and implements
|
||||||
|
* "_breathe_animal()" and "_eat_animal()" methods.
|
||||||
|
*
|
||||||
|
* Human Class is inherited from Animal class.
|
||||||
|
* Parrot Class is inherited from Animal class.
|
||||||
|
*
|
||||||
|
* Child Class is inherited from Human class.
|
||||||
|
*
|
||||||
|
* Human Class and Parrot Class implement Whistler Interface.
|
||||||
|
* Human Class implements Diver Interface. Diver Interface inherited from Swimmer Interface
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Animal Class (inherited from Base Class)
|
||||||
|
* + _breathe_animal()
|
||||||
|
* + _eat_animal()
|
||||||
|
* / -------/ \-------------\
|
||||||
|
* / \
|
||||||
|
* Human Class Parrot Class
|
||||||
|
* inherits inherits
|
||||||
|
* + animal_breathe() + animal_breathe()
|
||||||
|
* overrides overrides
|
||||||
|
* + animal_eat(); + animal_eat();
|
||||||
|
* implements implements
|
||||||
|
* + human_walk(); + parrot_fly();
|
||||||
|
*
|
||||||
|
* implements Whistler, Swimmer, implements Whistler,
|
||||||
|
* Diver Interfaces: + whistler_whistle()
|
||||||
|
* + whistler_whistle()
|
||||||
|
* + swimmer_swim()
|
||||||
|
* + diver_dive()
|
||||||
|
*
|
||||||
|
* ----------------------------------------------------------
|
||||||
|
* | Swim_Interface |
|
||||||
|
* | + swim() |
|
||||||
|
* | | |
|
||||||
|
* | | |
|
||||||
|
* | Dive Intarface (inherited from Swim Interface) |
|
||||||
|
* | + dive() |
|
||||||
|
* ---------------------------------------------------------
|
||||||
|
* |
|
||||||
|
* |
|
||||||
|
* Child Class
|
||||||
|
* + inherits all parent's methods
|
||||||
|
* + implements cry_child()
|
||||||
|
* + overrides dive() interface method
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include "eina_model_04_human.h"
|
||||||
|
#include "eina_model_04_parrot.h"
|
||||||
|
#include "eina_model_04_child.h"
|
||||||
|
#include "eina_model_04_whistler.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
Eina_Model *h, *p, *c;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
human_init();
|
||||||
|
parrot_init();
|
||||||
|
child_init();
|
||||||
|
|
||||||
|
h = eina_model_new(HUMAN_TYPE);
|
||||||
|
p = eina_model_new(PARROT_TYPE);
|
||||||
|
c = eina_model_new(CHILD_TYPE);
|
||||||
|
|
||||||
|
animal_breathe(p);
|
||||||
|
animal_eat(p);
|
||||||
|
parrot_fly(p);
|
||||||
|
whistler_whistle(p);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
animal_breathe(h);
|
||||||
|
animal_eat(h);
|
||||||
|
human_walk(h);
|
||||||
|
whistler_whistle(h);
|
||||||
|
swimmer_swim(h);
|
||||||
|
diver_dive(h);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
animal_breathe(c);
|
||||||
|
animal_eat(c);
|
||||||
|
human_walk(c);
|
||||||
|
whistler_whistle(c);
|
||||||
|
swimmer_swim(c);
|
||||||
|
diver_dive(c);
|
||||||
|
child_cry(c);
|
||||||
|
|
||||||
|
eina_model_unref(c);
|
||||||
|
eina_model_unref(h);
|
||||||
|
eina_model_unref(p);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,95 @@
|
||||||
|
/*
|
||||||
|
* parrot.c
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "eina_model_04_parrot.h"
|
||||||
|
#include "eina_model_04_whistler.h"
|
||||||
|
|
||||||
|
static Eina_Bool initialized = EINA_FALSE;
|
||||||
|
|
||||||
|
static void
|
||||||
|
_parrot_fly(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Fly Parrot\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_parrot_eat(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Grain \n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_parrot_whistle(Eina_Model *m)
|
||||||
|
{
|
||||||
|
printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
|
||||||
|
__func__);
|
||||||
|
printf("\t\t Whistle Parrot\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* defining Parrot Model Instance
|
||||||
|
* defining Whistler Interface instance
|
||||||
|
*/
|
||||||
|
const char *PARROT_MODEL_TYPE_NAME = NULL;
|
||||||
|
|
||||||
|
static Parrot_Type _PARROT_TYPE;
|
||||||
|
const Eina_Model_Type * const PARROT_TYPE = (Eina_Model_Type *) &_PARROT_TYPE;
|
||||||
|
|
||||||
|
static const Whistler_Interface _WHISTLER_INTERFACE;
|
||||||
|
static const Eina_Model_Interface * const WHISTLER_INTERFACE =
|
||||||
|
(Eina_Model_Interface *) &_WHISTLER_INTERFACE;
|
||||||
|
|
||||||
|
static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] =
|
||||||
|
{ &_WHISTLER_INTERFACE.base_interface, NULL }; //this array is for model
|
||||||
|
|
||||||
|
void
|
||||||
|
parrot_init()
|
||||||
|
{
|
||||||
|
Eina_Model_Type *type;
|
||||||
|
if (initialized) return;
|
||||||
|
initialized = EINA_TRUE;
|
||||||
|
|
||||||
|
animal_init();
|
||||||
|
/*
|
||||||
|
*overriding Whistler Interface (creating instance of Whistler Interface)
|
||||||
|
*/
|
||||||
|
Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE;
|
||||||
|
iface->version = EINA_MODEL_INTERFACE_VERSION;
|
||||||
|
iface->interface_size = sizeof(Whistler_Interface);
|
||||||
|
iface->name = WHISTLER_INTERFACE_NAME;
|
||||||
|
WHISTLER_INTERFACE(iface)->whistle = _parrot_whistle;
|
||||||
|
|
||||||
|
PARROT_MODEL_TYPE_NAME = "Parrot_Model_Type";
|
||||||
|
|
||||||
|
type = (Eina_Model_Type *)&_PARROT_TYPE;
|
||||||
|
type->version = EINA_MODEL_TYPE_VERSION;
|
||||||
|
type->name = PARROT_MODEL_TYPE_NAME;
|
||||||
|
type->private_size = 0;
|
||||||
|
|
||||||
|
eina_model_type_subclass_setup(type, ANIMAL_TYPE);
|
||||||
|
|
||||||
|
type->type_size = sizeof(Parrot_Type);
|
||||||
|
type->interfaces = MODEL_INTERFACES_ARRAY;
|
||||||
|
|
||||||
|
ANIMAL_TYPE(type)->eat = _parrot_eat;
|
||||||
|
PARROT_TYPE(type)->fly = _parrot_fly;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
parrot_fly(Eina_Model *m)
|
||||||
|
{
|
||||||
|
EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, PARROT_TYPE));
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *m);
|
||||||
|
pf = eina_model_method_resolve(m, Parrot_Type, fly);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
/*
|
||||||
|
* parrot.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PARROT_H_
|
||||||
|
#define PARROT_H_
|
||||||
|
|
||||||
|
#include "eina_model_04_animal.h"
|
||||||
|
|
||||||
|
extern const char *PARROT_MODEL_TYPE_NAME;
|
||||||
|
extern const Eina_Model_Type * const PARROT_TYPE;
|
||||||
|
|
||||||
|
#define PARROT_TYPE(x) ((Parrot_Type *) (eina_model_type_subclass_check((x), PARROT_TYPE) ? (x) : NULL))
|
||||||
|
|
||||||
|
typedef struct _Parrot_Type
|
||||||
|
{
|
||||||
|
Animal_Type parent_class;
|
||||||
|
void (*fly)(Eina_Model *m);
|
||||||
|
} Parrot_Type;
|
||||||
|
|
||||||
|
void parrot_init();
|
||||||
|
void parrot_fly(Eina_Model *m);
|
||||||
|
|
||||||
|
#endif /* PARROT_H_ */
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
* whistler.c
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "eina_model_04_whistler.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
whistler_whistle(Eina_Model *m)
|
||||||
|
{
|
||||||
|
const Eina_Model_Interface *iface = NULL;
|
||||||
|
iface = eina_model_interface_get(m, WHISTLER_INTERFACE_NAME);
|
||||||
|
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(iface);
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *);
|
||||||
|
|
||||||
|
pf = eina_model_interface_method_resolve(iface, m, Whistler_Interface, whistle);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* call for overridden Swimmer Interface function
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
swimmer_swim(Eina_Model *m)
|
||||||
|
{
|
||||||
|
const Eina_Model_Interface *iface = NULL;
|
||||||
|
iface = eina_model_interface_get(m, SWIMMER_INTERFACE_NAME);
|
||||||
|
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(iface);
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *);
|
||||||
|
|
||||||
|
pf = eina_model_interface_method_resolve(iface, m, Swimmer_Interface, swim);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call for overridden Diver Interface function
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
diver_dive(Eina_Model *m)
|
||||||
|
{
|
||||||
|
const Eina_Model_Interface *iface = NULL;
|
||||||
|
iface = eina_model_interface_get(m, DIVER_INTERFACE_NAME);
|
||||||
|
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(iface);
|
||||||
|
|
||||||
|
void (*pf)(Eina_Model *);
|
||||||
|
|
||||||
|
pf = eina_model_interface_method_resolve(iface, m, Diver_Interface, dive);
|
||||||
|
EINA_SAFETY_ON_NULL_RETURN(pf);
|
||||||
|
printf("%s() \t", __func__);
|
||||||
|
pf(m);
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* whistler.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef WHISTLER_H_
|
||||||
|
#define WHISTLER_H_
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <eina_safety_checks.h>
|
||||||
|
|
||||||
|
#define WHISTLER_INTERFACE_NAME "Whistler_Interface"
|
||||||
|
#define SWIMMER_INTERFACE_NAME "Swimmer_Interface"
|
||||||
|
#define DIVER_INTERFACE_NAME "Diver_Interface"
|
||||||
|
|
||||||
|
#define WHISTLER_INTERFACE(x) ((Whistler_Interface *) x)
|
||||||
|
#define SWIMMER_INTERFACE(x) ((Swimmer_Interface *) x)
|
||||||
|
#define DIVER_INTERFACE(x) ((Diver_Interface *) x)
|
||||||
|
|
||||||
|
typedef struct _Whistler_Interface
|
||||||
|
{
|
||||||
|
Eina_Model_Interface base_interface;
|
||||||
|
void (*whistle)(Eina_Model *);
|
||||||
|
|
||||||
|
} Whistler_Interface;
|
||||||
|
|
||||||
|
typedef struct _Swimmer_Interface
|
||||||
|
{
|
||||||
|
Eina_Model_Interface base_interface;
|
||||||
|
void (*swim)(Eina_Model *);
|
||||||
|
|
||||||
|
} Swimmer_Interface;
|
||||||
|
|
||||||
|
//Diver Interface will use Swimmer Interface as a parent
|
||||||
|
typedef struct _Diver_Interface
|
||||||
|
{
|
||||||
|
Eina_Model_Interface base_interface;
|
||||||
|
void (*dive)(Eina_Model *);
|
||||||
|
|
||||||
|
} Diver_Interface;
|
||||||
|
|
||||||
|
void whistler_whistle(Eina_Model *m);
|
||||||
|
void swimmer_swim(Eina_Model *m);
|
||||||
|
void diver_dive(Eina_Model *m);
|
||||||
|
|
||||||
|
#endif /* WHISTLER_H_ */
|
|
@ -0,0 +1,7 @@
|
||||||
|
Kara Thrace starbuck@bsg.com
|
||||||
|
Sharon Valerii boomer@bsg.com
|
||||||
|
Sharon Aghaton athena@bsg.com
|
||||||
|
Karl Aghaton helo@bsg.com
|
||||||
|
Louanne Katraine kat@bsg.com
|
||||||
|
|
||||||
|
Lee Adama apolo@bsg.com
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE board SYSTEM "tp-0.1.dtd">
|
||||||
|
<board site="http://linuxfr.org/">
|
||||||
|
<post id="3211">
|
||||||
|
<login>houplaboom</login>
|
||||||
|
<message>Is this an XML test?</message>
|
||||||
|
</post>
|
||||||
|
<post id="3212">
|
||||||
|
<login>fredx</login>
|
||||||
|
<message>I'm seeing bald people.</message>
|
||||||
|
</post>
|
||||||
|
<post id="3213">
|
||||||
|
<login>NedFlanders</login>
|
||||||
|
<message>Don't call him!</message>
|
||||||
|
</post>
|
||||||
|
<post id="3214">
|
||||||
|
<login>Single</login>
|
||||||
|
<message>I'm not bald!</message>
|
||||||
|
</post>
|
||||||
|
<post id="3215">
|
||||||
|
<login>ngc891</login>
|
||||||
|
<message>It was an XML test.</message>
|
||||||
|
</post>
|
||||||
|
</board>
|
|
@ -0,0 +1,55 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_accessor_01.c -o eina_accessor_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char *strings[] = {
|
||||||
|
"even", "odd", "even", "odd", "even", "odd", "even", "odd", "even", "odd"
|
||||||
|
};
|
||||||
|
const char *more_strings[] = {
|
||||||
|
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
|
||||||
|
};
|
||||||
|
Eina_Array *array;
|
||||||
|
Eina_List *list = NULL;
|
||||||
|
Eina_Accessor *acc;
|
||||||
|
unsigned short int i;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
array = eina_array_new(10);
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
eina_array_push(array, strings[i]);
|
||||||
|
list = eina_list_append(list, more_strings[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
acc = eina_array_accessor_new(array);
|
||||||
|
for(i = 1; i < 10; i += 2)
|
||||||
|
{
|
||||||
|
eina_accessor_data_get(acc, i, &data);
|
||||||
|
printf("%s\n", (const char *)data);
|
||||||
|
}
|
||||||
|
eina_accessor_free(acc);
|
||||||
|
eina_array_free(array);
|
||||||
|
|
||||||
|
acc = eina_list_accessor_new(list);
|
||||||
|
for(i = 1; i < 10; i += 2)
|
||||||
|
{
|
||||||
|
eina_accessor_data_get(acc, i, &data);
|
||||||
|
printf("%s\n", (const char *)data);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_list_free(eina_accessor_container_get(acc));
|
||||||
|
eina_accessor_free(acc);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_array_01.c -o eina_array_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_print(const void *container, void *data, void *fdata)
|
||||||
|
{
|
||||||
|
printf("%s\n", (char *)data);
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char* strings[] = {
|
||||||
|
"helo", "hera", "starbuck", "kat", "boomer",
|
||||||
|
"hotdog", "longshot", "jammer", "crashdown", "hardball",
|
||||||
|
"duck", "racetrack", "apolo", "husker", "freaker",
|
||||||
|
"skulls", "bulldog", "flat top", "hammerhead", "gonzo"
|
||||||
|
};
|
||||||
|
Eina_Array *array;
|
||||||
|
Eina_Array_Iterator iterator;
|
||||||
|
char *item;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
array = eina_array_new(10);
|
||||||
|
eina_array_step_set(array, sizeof(*array), 20);
|
||||||
|
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
eina_array_push(array, strdup(strings[i]));
|
||||||
|
|
||||||
|
printf("array count: %d\n", eina_array_count(array));
|
||||||
|
eina_array_foreach(array, _print, NULL);
|
||||||
|
|
||||||
|
printf("Top gun: %s\n", (char*)eina_array_data_get(array, 2));
|
||||||
|
|
||||||
|
while (eina_array_count(array))
|
||||||
|
free(eina_array_pop(array));
|
||||||
|
|
||||||
|
eina_array_free(array);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_array_02.c -o eina_array_02 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
Eina_Bool keep(void *data, void *gdata)
|
||||||
|
{
|
||||||
|
if (strlen((const char*)data) <= 5)
|
||||||
|
return EINA_TRUE;
|
||||||
|
return EINA_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char* strs[] = {
|
||||||
|
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
|
||||||
|
"ten", "eleven", "twelve", "thirteen", "fourtenn", "fifteen", "sixteen",
|
||||||
|
"seventeen", "eighteen", "nineteen", "twenty"
|
||||||
|
};
|
||||||
|
const char* strings[] = {
|
||||||
|
"helo", "hera", "starbuck", "kat", "boomer",
|
||||||
|
"hotdog", "longshot", "jammer", "crashdown", "hardball",
|
||||||
|
"duck", "racetrack", "apolo", "husker", "freaker",
|
||||||
|
"skulls", "bulldog", "flat top", "hammerhead", "gonzo"
|
||||||
|
};
|
||||||
|
Eina_Array *array;
|
||||||
|
Eina_Array_Iterator iterator;
|
||||||
|
const char *item;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
array = eina_array_new(10);
|
||||||
|
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
eina_array_push(array, strs[i]);
|
||||||
|
|
||||||
|
eina_array_clean(array);
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
eina_array_push(array, strings[i]);
|
||||||
|
|
||||||
|
eina_array_data_set(array, 17, "flattop");
|
||||||
|
|
||||||
|
eina_array_remove(array, keep, NULL);
|
||||||
|
EINA_ARRAY_ITER_NEXT(array, i, item, iterator)
|
||||||
|
printf("item #%d: %s\n", i, item);
|
||||||
|
|
||||||
|
eina_array_free(array);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_error_01.c -o eina_error_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <eina_main.h>
|
||||||
|
#include <eina_error.h>
|
||||||
|
|
||||||
|
Eina_Error MY_ERROR_NEGATIVE;
|
||||||
|
Eina_Error MY_ERROR_NULL;
|
||||||
|
|
||||||
|
void *data_new()
|
||||||
|
{
|
||||||
|
eina_error_set(0);
|
||||||
|
|
||||||
|
eina_error_set(MY_ERROR_NULL);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test(int n)
|
||||||
|
{
|
||||||
|
eina_error_set(0);
|
||||||
|
|
||||||
|
if (n < 0)
|
||||||
|
{
|
||||||
|
eina_error_set(MY_ERROR_NEGATIVE);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
if (!eina_init())
|
||||||
|
{
|
||||||
|
printf ("Error during the initialization of eina_error module\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
MY_ERROR_NEGATIVE = eina_error_msg_static_register("Negative number");
|
||||||
|
MY_ERROR_NULL = eina_error_msg_static_register("NULL pointer");
|
||||||
|
|
||||||
|
data = data_new();
|
||||||
|
if (!data)
|
||||||
|
{
|
||||||
|
Eina_Error err;
|
||||||
|
|
||||||
|
err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error during memory allocation: %s\n",
|
||||||
|
eina_error_msg_get(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!test(0))
|
||||||
|
{
|
||||||
|
Eina_Error err;
|
||||||
|
|
||||||
|
err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error during test function: %s\n",
|
||||||
|
eina_error_msg_get(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!test(-1))
|
||||||
|
{
|
||||||
|
Eina_Error err;
|
||||||
|
|
||||||
|
err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error during test function: %s\n",
|
||||||
|
eina_error_msg_get(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_file_01.c -o eina_file_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
_print_cb(const char *name, const char *path, void *data)
|
||||||
|
{
|
||||||
|
printf("file %s in %s\n", name, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_Iterator *it;
|
||||||
|
const char *f_name;
|
||||||
|
const Eina_File_Direct_Info *f_info;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
eina_file_dir_list("/home/", EINA_FALSE, _print_cb, NULL);
|
||||||
|
|
||||||
|
it = eina_file_ls("/home/");
|
||||||
|
EINA_ITERATOR_FOREACH(it, f_name)
|
||||||
|
{
|
||||||
|
printf("%s\n", f_name);
|
||||||
|
eina_stringshare_del(f_name);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
|
||||||
|
it = eina_file_stat_ls("/home/");
|
||||||
|
EINA_ITERATOR_FOREACH(it, f_info)
|
||||||
|
printf("%s if of type %d\n", f_info->path, f_info->type);
|
||||||
|
eina_iterator_free(it);
|
||||||
|
|
||||||
|
it = eina_file_direct_ls("/home/");
|
||||||
|
EINA_ITERATOR_FOREACH(it, f_info)
|
||||||
|
printf("%s if of type %d\n", f_info->path, f_info->type);
|
||||||
|
eina_iterator_free(it);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,198 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_01.c -o eina_hash_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - phonebook
|
||||||
|
*
|
||||||
|
* This example demonstrate the use of Eina Hash by implementing a phonebook
|
||||||
|
* that stores its contact data into the hash.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with
|
||||||
|
* string keys.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
const char *name; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
|
||||||
|
{ "Ludwig van Beethoven", "+12 34 567-89101" },
|
||||||
|
{ "Richard Georg Strauss", "+23 45 678-91012" },
|
||||||
|
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
}; // _start_entries
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
|
||||||
|
void *data, void *fdata)
|
||||||
|
{
|
||||||
|
const char *name = key;
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
|
||||||
|
// Return EINA_FALSE to stop this callback from being called
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
const char *entry_name = "Heitor Villa-Lobos";
|
||||||
|
char *phone = NULL;
|
||||||
|
Eina_Bool r;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
phone_book = eina_hash_string_superfast_new(_phone_entry_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash
|
||||||
|
for (i = 0; _start_entries[i].name != NULL; i++)
|
||||||
|
{
|
||||||
|
eina_hash_add(phone_book, _start_entries[i].name,
|
||||||
|
strdup(_start_entries[i].number));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a specific entry and get its phone number
|
||||||
|
phone = eina_hash_find(phone_book, entry_name);
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Printing entry.\n");
|
||||||
|
printf("Name: %s\n", entry_name);
|
||||||
|
printf("Number: %s\n\n", phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete this entry
|
||||||
|
r = eina_hash_del(phone_book, entry_name, NULL);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// Modify the pointer data of an entry and free the old one
|
||||||
|
phone = eina_hash_modify(phone_book, "Richard Georg Strauss",
|
||||||
|
strdup("+23 45 111-11111"));
|
||||||
|
free(phone);
|
||||||
|
|
||||||
|
// Modify or add an entry to the hash with eina_hash_set
|
||||||
|
// Let's first add a new entry
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, "Raul Seixas",
|
||||||
|
strdup("+55 01 234-56789"));
|
||||||
|
if (!phone)
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
printf("No previous phone found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Error when setting phone for Raul Seixas\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Old phone for Raul Seixas was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now change the phone number
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, "Raul Seixas",
|
||||||
|
strdup("+55 02 222-22222"));
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
|
||||||
|
printf("Old phone was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error when changing phone for Raul Seixas\n");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("No previous phone found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There are many ways to iterate over our Phone book.
|
||||||
|
// First, iterate showing the names and associated numbers.
|
||||||
|
printf("List of phones:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now iterate using an iterator
|
||||||
|
printf("List of phones:\n");
|
||||||
|
it = eina_hash_iterator_tuple_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
Eina_Hash_Tuple *t = data;
|
||||||
|
const char *name = t->key;
|
||||||
|
const char *number = t->data;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it); // Always free the iterator after its use
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the keys (names)
|
||||||
|
printf("List of names in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_key_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *name = data;
|
||||||
|
printf("%s\n", name);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the data (numbers)
|
||||||
|
printf("List of numbers in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_data_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s\n", number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check how many items are in the phone book
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Change the name (key) on an entry
|
||||||
|
eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
|
||||||
|
printf("List of phones after change:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Empty the phone book, but don't destroy it
|
||||||
|
eina_hash_free_buckets(phone_book);
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Phone book could still be used, but we are freeing it since we are
|
||||||
|
// done for now
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,147 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_02.c -o eina_hash_02 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - Two more types of hash
|
||||||
|
*
|
||||||
|
* This example demonstrate two other types of hash in action - using
|
||||||
|
* eina_hash_stringshared_new and eina_hash_new.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with string
|
||||||
|
* keys, exactly the same as the other example.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
const char *name; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
|
||||||
|
{ "Ludwig van Beethoven", "+12 34 567-89101" },
|
||||||
|
{ "Richard Georg Strauss", "+23 45 678-91012" },
|
||||||
|
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_book_stringshared_free_cb(void *data)
|
||||||
|
{
|
||||||
|
Phone_Entry *e = data;
|
||||||
|
eina_stringshare_del(e->name);
|
||||||
|
eina_stringshare_del(e->number);
|
||||||
|
free(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_stringshared_foreach_cb(const Eina_Hash *phone_book,
|
||||||
|
const void *key, void *data, void *fdata)
|
||||||
|
{
|
||||||
|
Phone_Entry *e = data;
|
||||||
|
const char *name = e->name; // e->name == key
|
||||||
|
const char *number = e->number;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
example_hash_stringshared(void)
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Create the hash as before
|
||||||
|
phone_book = eina_hash_stringshared_new(_phone_book_stringshared_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash, using direct_add
|
||||||
|
for (i = 0; _start_entries[i].name != NULL; i++)
|
||||||
|
{
|
||||||
|
Phone_Entry *e = malloc(sizeof(Phone_Entry));
|
||||||
|
e->name = eina_stringshare_add(_start_entries[i].name);
|
||||||
|
e->number = eina_stringshare_add(_start_entries[i].number);
|
||||||
|
// Since we are storing the key (name) in our struct, we can use
|
||||||
|
// eina_hash_direct_add. It could be used in the previous example
|
||||||
|
// too, since each key is already stored in the _start_entries
|
||||||
|
// static array, but we started it with the default add function.
|
||||||
|
eina_hash_direct_add(phone_book, e->name, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate over the elements
|
||||||
|
printf("List of phones:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_stringshared_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
_phone_book_string_key_length(const char *key)
|
||||||
|
{
|
||||||
|
if (!key)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return (int)strlen(key) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_phone_book_string_key_cmp(const char *key1, int key1_length,
|
||||||
|
const char *key2, int key2_length)
|
||||||
|
{
|
||||||
|
return strcmp(key1, key2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
example_hash_big(void)
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
const char *phone;
|
||||||
|
|
||||||
|
// Create the same hash as used in eina_hash_01.c, but
|
||||||
|
// use 1024 (2 ^ 10) buckets.
|
||||||
|
phone_book = eina_hash_new(EINA_KEY_LENGTH(_phone_book_string_key_length),
|
||||||
|
EINA_KEY_CMP(_phone_book_string_key_cmp),
|
||||||
|
EINA_KEY_HASH(eina_hash_superfast),
|
||||||
|
_phone_entry_free_cb,
|
||||||
|
10);
|
||||||
|
for (i = 0; _start_entries[i].name != NULL; i++)
|
||||||
|
{
|
||||||
|
eina_hash_add(phone_book, _start_entries[i].name,
|
||||||
|
strdup(_start_entries[i].number));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a specific entry and get its phone number
|
||||||
|
phone = eina_hash_find(phone_book, "Heitor Villa-Lobos");
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Printing entry.\n");
|
||||||
|
printf("Name: Heitor Villa-Lobos\n");
|
||||||
|
printf("Number: %s\n\n", phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
example_hash_stringshared();
|
||||||
|
example_hash_big();
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,198 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_03.c -o eina_hash_03 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - phonebook
|
||||||
|
*
|
||||||
|
* This example demonstrate the use of Eina Hash by implementing a phonebook
|
||||||
|
* that stores its contact data into the hash.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with
|
||||||
|
* string keys.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
const char *name; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
|
||||||
|
{ "Ludwig van Beethoven", "+12 34 567-89101" },
|
||||||
|
{ "Richard Georg Strauss", "+23 45 678-91012" },
|
||||||
|
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
}; // _start_entries
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
|
||||||
|
void *data, void *fdata)
|
||||||
|
{
|
||||||
|
const char *name = key;
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
|
||||||
|
// Return EINA_FALSE to stop this callback from being called
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
const char *entry_name = "Heitor Villa-Lobos";
|
||||||
|
char *phone = NULL;
|
||||||
|
Eina_Bool r;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
phone_book = eina_hash_string_small_new(_phone_entry_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash
|
||||||
|
for (i = 0; _start_entries[i].name != NULL; i++)
|
||||||
|
{
|
||||||
|
eina_hash_add(phone_book, _start_entries[i].name,
|
||||||
|
strdup(_start_entries[i].number));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a specific entry and get its phone number
|
||||||
|
phone = eina_hash_find(phone_book, entry_name);
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Printing entry.\n");
|
||||||
|
printf("Name: %s\n", entry_name);
|
||||||
|
printf("Number: %s\n\n", phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete this entry
|
||||||
|
r = eina_hash_del(phone_book, entry_name, NULL);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// Modify the pointer data of an entry and free the old one
|
||||||
|
phone = eina_hash_modify(phone_book, "Richard Georg Strauss",
|
||||||
|
strdup("+23 45 111-11111"));
|
||||||
|
free(phone);
|
||||||
|
|
||||||
|
// Modify or add an entry to the hash with eina_hash_set
|
||||||
|
// Let's first add a new entry
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, "Raul Seixas",
|
||||||
|
strdup("+55 01 234-56789"));
|
||||||
|
if (!phone)
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
printf("No previous phone found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Error when setting phone for Raul Seixas\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Old phone for Raul Seixas was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now change the phone number
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, "Raul Seixas",
|
||||||
|
strdup("+55 02 222-22222"));
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
|
||||||
|
printf("Old phone was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error when changing phone for Raul Seixas\n");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("No previous phone found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There are many ways to iterate over our Phone book.
|
||||||
|
// First, iterate showing the names and associated numbers.
|
||||||
|
printf("List of phones:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now iterate using an iterator
|
||||||
|
printf("List of phones:\n");
|
||||||
|
it = eina_hash_iterator_tuple_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
Eina_Hash_Tuple *t = data;
|
||||||
|
const char *name = t->key;
|
||||||
|
const char *number = t->data;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it); // Always free the iterator after its use
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the keys (names)
|
||||||
|
printf("List of names in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_key_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *name = data;
|
||||||
|
printf("%s\n", name);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the data (numbers)
|
||||||
|
printf("List of numbers in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_data_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s\n", number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check how many items are in the phone book
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Change the name (key) on an entry
|
||||||
|
eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
|
||||||
|
printf("List of phones after change:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Empty the phone book, but don't destroy it
|
||||||
|
eina_hash_free_buckets(phone_book);
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Phone book could still be used, but we are freeing it since we are
|
||||||
|
// done for now
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,198 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_04.c -o eina_hash_04 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - phonebook
|
||||||
|
*
|
||||||
|
* This example demonstrate the use of Eina Hash by implementing a phonebook
|
||||||
|
* that stores its contact data into the hash.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with
|
||||||
|
* string keys.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
const char *name; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
|
||||||
|
{ "Ludwig van Beethoven", "+12 34 567-89101" },
|
||||||
|
{ "Richard Georg Strauss", "+23 45 678-91012" },
|
||||||
|
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
}; // _start_entries
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
|
||||||
|
void *data, void *fdata)
|
||||||
|
{
|
||||||
|
const char *name = key;
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
|
||||||
|
// Return EINA_FALSE to stop this callback from being called
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
const char *entry_name = "Heitor Villa-Lobos";
|
||||||
|
char *phone = NULL;
|
||||||
|
Eina_Bool r;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
phone_book = eina_hash_string_djb2_new(_phone_entry_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash
|
||||||
|
for (i = 0; _start_entries[i].name != NULL; i++)
|
||||||
|
{
|
||||||
|
eina_hash_add(phone_book, _start_entries[i].name,
|
||||||
|
strdup(_start_entries[i].number));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a specific entry and get its phone number
|
||||||
|
phone = eina_hash_find(phone_book, entry_name);
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Printing entry.\n");
|
||||||
|
printf("Name: %s\n", entry_name);
|
||||||
|
printf("Number: %s\n\n", phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete this entry
|
||||||
|
r = eina_hash_del(phone_book, entry_name, NULL);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// Modify the pointer data of an entry and free the old one
|
||||||
|
phone = eina_hash_modify(phone_book, "Richard Georg Strauss",
|
||||||
|
strdup("+23 45 111-11111"));
|
||||||
|
free(phone);
|
||||||
|
|
||||||
|
// Modify or add an entry to the hash with eina_hash_set
|
||||||
|
// Let's first add a new entry
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, "Raul Seixas",
|
||||||
|
strdup("+55 01 234-56789"));
|
||||||
|
if (!phone)
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
printf("No previous phone found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Error when setting phone for Raul Seixas\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Old phone for Raul Seixas was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now change the phone number
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, "Raul Seixas",
|
||||||
|
strdup("+55 02 222-22222"));
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
|
||||||
|
printf("Old phone was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error when changing phone for Raul Seixas\n");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("No previous phone found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There are many ways to iterate over our Phone book.
|
||||||
|
// First, iterate showing the names and associated numbers.
|
||||||
|
printf("List of phones:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now iterate using an iterator
|
||||||
|
printf("List of phones:\n");
|
||||||
|
it = eina_hash_iterator_tuple_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
Eina_Hash_Tuple *t = data;
|
||||||
|
const char *name = t->key;
|
||||||
|
const char *number = t->data;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it); // Always free the iterator after its use
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the keys (names)
|
||||||
|
printf("List of names in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_key_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *name = data;
|
||||||
|
printf("%s\n", name);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the data (numbers)
|
||||||
|
printf("List of numbers in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_data_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s\n", number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check how many items are in the phone book
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Change the name (key) on an entry
|
||||||
|
eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
|
||||||
|
printf("List of phones after change:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Empty the phone book, but don't destroy it
|
||||||
|
eina_hash_free_buckets(phone_book);
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Phone book could still be used, but we are freeing it since we are
|
||||||
|
// done for now
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,201 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_05.c -o eina_hash_05 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - phonebook
|
||||||
|
*
|
||||||
|
* This example demonstrate the use of Eina Hash by implementing a phonebook
|
||||||
|
* that stores its contact data into the hash.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with
|
||||||
|
* string keys.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
int32_t id; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ 1, "+01 23 456-78910" },
|
||||||
|
{ 2, "+12 34 567-89101" },
|
||||||
|
{ 3, "+23 45 678-91012" },
|
||||||
|
{ 4, "+34 56 789-10123" },
|
||||||
|
{ -1, NULL }
|
||||||
|
}; // _start_entries
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
|
||||||
|
void *data, void *fdata)
|
||||||
|
{
|
||||||
|
const int32_t *id = key;
|
||||||
|
const char *number = data;
|
||||||
|
printf("%d: %s\n", *id, number);
|
||||||
|
|
||||||
|
// Return EINA_FALSE to stop this callback from being called
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
int32_t entry_id = 4;
|
||||||
|
char *phone = NULL;
|
||||||
|
Eina_Bool r;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
phone_book = eina_hash_int32_new(_phone_entry_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash
|
||||||
|
for (i = 0; _start_entries[i].id != -1; i++)
|
||||||
|
{
|
||||||
|
eina_hash_add(phone_book, &_start_entries[i].id,
|
||||||
|
strdup(_start_entries[i].number));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a specific entry and get its phone number
|
||||||
|
phone = eina_hash_find(phone_book, &entry_id);
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Printing entry.\n");
|
||||||
|
printf("Id: %d\n", entry_id);
|
||||||
|
printf("Number: %s\n\n", phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete this entry
|
||||||
|
r = eina_hash_del(phone_book, &entry_id, NULL);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// Modify the pointer data of an entry and free the old one
|
||||||
|
int32_t id3 = 3;
|
||||||
|
phone = eina_hash_modify(phone_book, &id3,
|
||||||
|
strdup("+23 45 111-11111"));
|
||||||
|
free(phone);
|
||||||
|
|
||||||
|
// Modify or add an entry to the hash with eina_hash_set
|
||||||
|
// Let's first add a new entry
|
||||||
|
int32_t id5 = 5;
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, &id5,
|
||||||
|
strdup("+55 01 234-56789"));
|
||||||
|
if (!phone)
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
printf("No previous phone found for id5. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Error when setting phone for Raul Seixas\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Old phone for id5 was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now change the phone number
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, &id5,
|
||||||
|
strdup("+55 02 222-22222"));
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Changing phone for id5 to +55 02 222-22222. ");
|
||||||
|
printf("Old phone was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error when changing phone for id5\n");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("No previous phone found for id5. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There are many ways to iterate over our Phone book.
|
||||||
|
// First, iterate showing the names and associated numbers.
|
||||||
|
printf("List of phones:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now iterate using an iterator
|
||||||
|
printf("List of phones:\n");
|
||||||
|
it = eina_hash_iterator_tuple_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
Eina_Hash_Tuple *t = data;
|
||||||
|
const int32_t *id = t->key;
|
||||||
|
const char *number = t->data;
|
||||||
|
printf("%d: %s\n", *id, number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it); // Always free the iterator after its use
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the keys (names)
|
||||||
|
printf("List of ids in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_key_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const int32_t *id = data;
|
||||||
|
printf("%d\n", *id);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the data (numbers)
|
||||||
|
printf("List of numbers in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_data_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s\n", number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check how many items are in the phone book
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Change the name (key) on an entry
|
||||||
|
int32_t id6 = 6;
|
||||||
|
eina_hash_move(phone_book, &id5, &id6);
|
||||||
|
printf("List of phones after change:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Empty the phone book, but don't destroy it
|
||||||
|
eina_hash_free_buckets(phone_book);
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Phone book could still be used, but we are freeing it since we are
|
||||||
|
// done for now
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,201 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_06.c -o eina_hash_06 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - phonebook
|
||||||
|
*
|
||||||
|
* This example demonstrate the use of Eina Hash by implementing a phonebook
|
||||||
|
* that stores its contact data into the hash.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with
|
||||||
|
* string keys.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
int64_t id; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ 1, "+01 23 456-78910" },
|
||||||
|
{ 2, "+12 34 567-89101" },
|
||||||
|
{ 3, "+23 45 678-91012" },
|
||||||
|
{ 4, "+34 56 789-10123" },
|
||||||
|
{ -1, NULL }
|
||||||
|
}; // _start_entries
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
|
||||||
|
void *data, void *fdata)
|
||||||
|
{
|
||||||
|
const int64_t *id = key;
|
||||||
|
const char *number = data;
|
||||||
|
printf("%lld: %s\n", *id, number);
|
||||||
|
|
||||||
|
// Return EINA_FALSE to stop this callback from being called
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
int64_t entry_id = 4;
|
||||||
|
char *phone = NULL;
|
||||||
|
Eina_Bool r;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
phone_book = eina_hash_int64_new(_phone_entry_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash
|
||||||
|
for (i = 0; _start_entries[i].id != -1; i++)
|
||||||
|
{
|
||||||
|
eina_hash_add(phone_book, &_start_entries[i].id,
|
||||||
|
strdup(_start_entries[i].number));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a specific entry and get its phone number
|
||||||
|
phone = eina_hash_find(phone_book, &entry_id);
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Printing entry.\n");
|
||||||
|
printf("Id: %lld\n", entry_id);
|
||||||
|
printf("Number: %s\n\n", phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete this entry
|
||||||
|
r = eina_hash_del(phone_book, &entry_id, NULL);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// Modify the pointer data of an entry and free the old one
|
||||||
|
int64_t id3 = 3;
|
||||||
|
phone = eina_hash_modify(phone_book, &id3,
|
||||||
|
strdup("+23 45 111-11111"));
|
||||||
|
free(phone);
|
||||||
|
|
||||||
|
// Modify or add an entry to the hash with eina_hash_set
|
||||||
|
// Let's first add a new entry
|
||||||
|
int64_t id5 = 5;
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, &id5,
|
||||||
|
strdup("+55 01 234-56789"));
|
||||||
|
if (!phone)
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
printf("No previous phone found for id5. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Error when setting phone for Raul Seixas\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Old phone for id5 was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now change the phone number
|
||||||
|
eina_error_set(0);
|
||||||
|
phone = eina_hash_set(phone_book, &id5,
|
||||||
|
strdup("+55 02 222-22222"));
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
printf("Changing phone for id5 to +55 02 222-22222. ");
|
||||||
|
printf("Old phone was %s\n", phone);
|
||||||
|
free(phone);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error when changing phone for id5\n");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("No previous phone found for id5. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There are many ways to iterate over our Phone book.
|
||||||
|
// First, iterate showing the names and associated numbers.
|
||||||
|
printf("List of phones:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now iterate using an iterator
|
||||||
|
printf("List of phones:\n");
|
||||||
|
it = eina_hash_iterator_tuple_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
Eina_Hash_Tuple *t = data;
|
||||||
|
const int64_t *id = t->key;
|
||||||
|
const char *number = t->data;
|
||||||
|
printf("%lld: %s\n", *id, number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it); // Always free the iterator after its use
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the keys (names)
|
||||||
|
printf("List of ids in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_key_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const int64_t *id = data;
|
||||||
|
printf("%lld\n", *id);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the data (numbers)
|
||||||
|
printf("List of numbers in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_data_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s\n", number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check how many items are in the phone book
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Change the name (key) on an entry
|
||||||
|
int64_t id6 = 6;
|
||||||
|
eina_hash_move(phone_book, &id5, &id6);
|
||||||
|
printf("List of phones after change:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Empty the phone book, but don't destroy it
|
||||||
|
eina_hash_free_buckets(phone_book);
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Phone book could still be used, but we are freeing it since we are
|
||||||
|
// done for now
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,222 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_07.c -o eina_hash_07 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - phonebook
|
||||||
|
*
|
||||||
|
* This example demonstrate the use of Eina Hash by implementing a phonebook
|
||||||
|
* that stores its contact data into the hash.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with
|
||||||
|
* string keys.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
const char *name; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
|
||||||
|
{ "Ludwig van Beethoven", "+12 34 567-89101" },
|
||||||
|
{ "Richard Georg Strauss", "+23 45 678-91012" },
|
||||||
|
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
}; // _start_entries
|
||||||
|
|
||||||
|
static const char *_nicknames[] = {
|
||||||
|
"mozzart",
|
||||||
|
"betho",
|
||||||
|
"george",
|
||||||
|
"hector",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
|
||||||
|
void *data, void *fdata)
|
||||||
|
{
|
||||||
|
Phone_Entry **pe = (Phone_Entry **)key;
|
||||||
|
const char *nick = data;
|
||||||
|
printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick);
|
||||||
|
|
||||||
|
// Return EINA_FALSE to stop this callback from being called
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
Phone_Entry *entry_vl = &_start_entries[3];
|
||||||
|
Phone_Entry *p = NULL;
|
||||||
|
char *nick = NULL;
|
||||||
|
Eina_Bool r;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
phone_book = eina_hash_pointer_new(_phone_entry_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash
|
||||||
|
for (i = 0; _start_entries[i].name != NULL; i++)
|
||||||
|
{
|
||||||
|
p = &_start_entries[i];
|
||||||
|
eina_hash_add(phone_book, &p,
|
||||||
|
strdup(_nicknames[i]));
|
||||||
|
}
|
||||||
|
printf("Phonebook:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Look for a specific entry and get its nickname
|
||||||
|
nick = eina_hash_find(phone_book, &entry_vl);
|
||||||
|
if (nick)
|
||||||
|
{
|
||||||
|
printf("Printing entry.\n");
|
||||||
|
printf("Name: %s\n", entry_vl->name);
|
||||||
|
printf("Number: %s\n", entry_vl->number);
|
||||||
|
printf("Nick: %s\n\n", nick);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete this entry
|
||||||
|
r = eina_hash_del(phone_book, &entry_vl, NULL);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// Modify the pointer data of an entry and free the old one
|
||||||
|
p = &_start_entries[2];
|
||||||
|
nick = eina_hash_modify(phone_book, &p,
|
||||||
|
strdup("el jorge"));
|
||||||
|
free(nick);
|
||||||
|
|
||||||
|
// Modify or add an entry to the hash with eina_hash_set
|
||||||
|
// Let's first add a new entry
|
||||||
|
eina_error_set(0);
|
||||||
|
Phone_Entry *p1 = malloc(sizeof(*p1));
|
||||||
|
p1->name = "Raul Seixas";
|
||||||
|
p1->number = "+55 01 234-56789";
|
||||||
|
nick = eina_hash_set(phone_book, &p1,
|
||||||
|
strdup("raulzito"));
|
||||||
|
if (!nick)
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
printf("No previous nick found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Error when setting nick for Raul Seixas\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Old nick for Raul Seixas was %s\n", nick);
|
||||||
|
free(nick);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now change the nick
|
||||||
|
eina_error_set(0);
|
||||||
|
nick = eina_hash_set(phone_book, &p1,
|
||||||
|
strdup("raulzao"));
|
||||||
|
if (nick)
|
||||||
|
{
|
||||||
|
printf("Changing nick for Raul Seixas to raulzao. ");
|
||||||
|
printf("Old nick was %s\n", nick);
|
||||||
|
free(nick);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Eina_Error err = eina_error_get();
|
||||||
|
if (err)
|
||||||
|
printf("Error when changing nick for Raul Seixas\n");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("No previous nick found for Raul Seixas. ");
|
||||||
|
printf("Creating new entry.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There are many ways to iterate over our Phone book.
|
||||||
|
// First, iterate showing the names, phones and associated nicks.
|
||||||
|
printf("Phonebook:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Now iterate using an iterator
|
||||||
|
printf("Phonebook:\n");
|
||||||
|
it = eina_hash_iterator_tuple_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
Eina_Hash_Tuple *t = data;
|
||||||
|
Phone_Entry **pe = (Phone_Entry **)t->key;
|
||||||
|
nick = t->data;
|
||||||
|
printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it); // Always free the iterator after its use
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the keys (names)
|
||||||
|
printf("List of names/numbers in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_key_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
Phone_Entry **pe = (Phone_Entry **)data;
|
||||||
|
printf("%s: %s\n", (*pe)->name, (*pe)->number);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Just iterate over the data (nicks)
|
||||||
|
printf("List of nicks in the phone book:\n");
|
||||||
|
it = eina_hash_iterator_data_new(phone_book);
|
||||||
|
while (eina_iterator_next(it, &data))
|
||||||
|
{
|
||||||
|
nick = data;
|
||||||
|
printf("%s\n", nick);
|
||||||
|
}
|
||||||
|
eina_iterator_free(it);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check how many items are in the phone book
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Change the name (key) on an entry
|
||||||
|
Phone_Entry *p2 = malloc(sizeof(*p2));
|
||||||
|
p2->name = "Alceu Valenca";
|
||||||
|
p2->number = "000000000000";
|
||||||
|
eina_hash_move(phone_book, p1, p2);
|
||||||
|
printf("List of phones after change:\n");
|
||||||
|
eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Empty the phone book, but don't destroy it
|
||||||
|
eina_hash_free_buckets(phone_book);
|
||||||
|
printf("There are %d items in the hash.\n\n",
|
||||||
|
eina_hash_population(phone_book));
|
||||||
|
|
||||||
|
// Phone book could still be used, but we are freeing it since we are
|
||||||
|
// done for now
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
|
||||||
|
free(p1);
|
||||||
|
free(p2);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,128 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_hash_08.c -o eina_hash_08 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Eina Hash - phonebook
|
||||||
|
*
|
||||||
|
* This example demonstrate the use of Eina Hash by implementing a phonebook
|
||||||
|
* that stores its contact data into the hash.
|
||||||
|
*
|
||||||
|
* It indexes the phone numbers by Contact Full Name, so it's a hash with
|
||||||
|
* string keys.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct _Phone_Entry {
|
||||||
|
const char *name; // Full name.
|
||||||
|
const char *number; // Phone number.
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _Phone_Entry Phone_Entry;
|
||||||
|
|
||||||
|
static Phone_Entry _start_entries[] = {
|
||||||
|
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
|
||||||
|
{ "Ludwig van Beethoven", "+12 34 567-89101" },
|
||||||
|
{ "Richard Georg Strauss", "+23 45 678-91012" },
|
||||||
|
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
}; // _start_entries
|
||||||
|
|
||||||
|
static void
|
||||||
|
_phone_entry_free_cb(void *data)
|
||||||
|
{
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
|
||||||
|
void *data, void *fdata)
|
||||||
|
{
|
||||||
|
const char *name = key;
|
||||||
|
const char *number = data;
|
||||||
|
printf("%s: %s\n", name, number);
|
||||||
|
|
||||||
|
// Return EINA_FALSE to stop this callback from being called
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
Eina_Hash *phone_book = NULL;
|
||||||
|
int i;
|
||||||
|
const char *entry_name = "Heitor Villa-Lobos";
|
||||||
|
int entry_size;
|
||||||
|
const char *saved_entry_name = "Alceu Valenca";
|
||||||
|
int saved_entry_size = sizeof("Alceu Valenca");
|
||||||
|
const char *phone = NULL;
|
||||||
|
Eina_Bool r;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
phone_book = eina_hash_string_superfast_new(_phone_entry_free_cb);
|
||||||
|
|
||||||
|
// Add initial entries to our hash
|
||||||
|
for (i = 0; _start_entries[i].name != NULL; i++)
|
||||||
|
{
|
||||||
|
eina_hash_add(phone_book, _start_entries[i].name,
|
||||||
|
strdup(_start_entries[i].number));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete entries
|
||||||
|
r = eina_hash_del(phone_book, entry_name, NULL);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
int hash = eina_hash_superfast("Ludwig van Beethoven",
|
||||||
|
sizeof("Ludwig van Beethoven"));
|
||||||
|
|
||||||
|
r = eina_hash_del_by_key_hash(phone_book, "Ludwig van Beethoven",
|
||||||
|
sizeof("Ludwig van Beethoven"), hash);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
r = eina_hash_del_by_key(phone_book, "Richard Georg Strauss");
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// add entry by hash
|
||||||
|
entry_name = "Raul_Seixas";
|
||||||
|
entry_size = sizeof("Raul Seixas");
|
||||||
|
phone = strdup("+33 33 333-33333");
|
||||||
|
hash = eina_hash_superfast(entry_name, entry_size);
|
||||||
|
eina_hash_add_by_hash(phone_book, entry_name, entry_size, hash, phone);
|
||||||
|
|
||||||
|
// don't need to free 'phone' after the next del:
|
||||||
|
r = eina_hash_del_by_data(phone_book, phone);
|
||||||
|
printf("Hash entry successfully deleted? %d\n\n", r);
|
||||||
|
|
||||||
|
// add entry by hash directly - no copy of the key will be done
|
||||||
|
hash = eina_hash_superfast(saved_entry_name, saved_entry_size);
|
||||||
|
phone = strdup("+44 44 444-44444");
|
||||||
|
eina_hash_direct_add_by_hash(phone_book, saved_entry_name,
|
||||||
|
saved_entry_size, hash, phone);
|
||||||
|
|
||||||
|
// find the added entry by its hash:
|
||||||
|
phone = eina_hash_find_by_hash(phone_book, saved_entry_name,
|
||||||
|
saved_entry_size, hash);
|
||||||
|
if (phone)
|
||||||
|
{
|
||||||
|
char *newphone = strdup("+55 55 555-55555");
|
||||||
|
phone = eina_hash_modify_by_hash(phone_book, saved_entry_name,
|
||||||
|
saved_entry_size, hash, newphone);
|
||||||
|
if (phone)
|
||||||
|
printf("changing phone to %s, old one was %s\n", newphone, phone);
|
||||||
|
else
|
||||||
|
printf("couldn't modify entry identified by %d\n", hash);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("couldn't find entry identified by %d\n", hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_hash_free(phone_book);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_inarray_01.c -o eina_inarray_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
cmp(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return *(int*)a > *(int*)b;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_Inarray *iarr;
|
||||||
|
char ch, *ch2;
|
||||||
|
int a, *b;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
iarr = eina_inarray_new(sizeof(char), 0);
|
||||||
|
|
||||||
|
ch = 'a';
|
||||||
|
eina_inarray_push(iarr, &ch);
|
||||||
|
ch = 'b';
|
||||||
|
eina_inarray_push(iarr, &ch);
|
||||||
|
ch = 'c';
|
||||||
|
eina_inarray_push(iarr, &ch);
|
||||||
|
ch = 'd';
|
||||||
|
eina_inarray_push(iarr, &ch);
|
||||||
|
|
||||||
|
printf("Inline array of chars:\n");
|
||||||
|
EINA_INARRAY_FOREACH(iarr, ch2)
|
||||||
|
printf("char: %c(pointer: %p)\n", *ch2, ch2);
|
||||||
|
|
||||||
|
eina_inarray_flush(iarr);
|
||||||
|
eina_inarray_step_set(iarr, sizeof(Eina_Inarray), sizeof(int), 4);
|
||||||
|
|
||||||
|
a = 97;
|
||||||
|
eina_inarray_push(iarr, &a);
|
||||||
|
a = 98;
|
||||||
|
eina_inarray_push(iarr, &a);
|
||||||
|
a = 100;
|
||||||
|
eina_inarray_push(iarr, &a);
|
||||||
|
a = 99;
|
||||||
|
eina_inarray_insert_sorted(iarr, &a, cmp);
|
||||||
|
|
||||||
|
printf("Inline array of integers with %d elements:\n", eina_inarray_count(iarr));
|
||||||
|
EINA_INARRAY_FOREACH(iarr, b)
|
||||||
|
printf("int: %d(pointer: %p)\n", *b, b);
|
||||||
|
|
||||||
|
eina_inarray_free(iarr);
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_inarray_02.c -o eina_inarray_02 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char* strings[] = {
|
||||||
|
"helo", "hera", "starbuck", "kat", "boomer",
|
||||||
|
"hotdog", "longshot", "jammer", "crashdown", "hardball",
|
||||||
|
"duck", "racetrack", "apolo", "husker", "freaker",
|
||||||
|
"skulls", "bulldog", "flat top", "hammerhead", "gonzo"
|
||||||
|
};
|
||||||
|
char **str, **str2;
|
||||||
|
Eina_Inarray *iarr;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
iarr = eina_inarray_new(sizeof(char *), 0);
|
||||||
|
|
||||||
|
for (i = 0; i < 20; i++){
|
||||||
|
str = &strings[i];
|
||||||
|
eina_inarray_push(iarr, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Inline array of strings:\n");
|
||||||
|
EINA_INARRAY_FOREACH(iarr, str2)
|
||||||
|
printf("string: %s(pointer: %p)\n", *str2, str2);
|
||||||
|
|
||||||
|
eina_inarray_free(iarr);
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,98 @@
|
||||||
|
// Compile with:
|
||||||
|
// gcc -g eina_inlist_01.c -o eina_inlist_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct my_struct {
|
||||||
|
EINA_INLIST;
|
||||||
|
int a, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
sort_cb(const void *d1, const void *d2)
|
||||||
|
{
|
||||||
|
const Eina_Inlist *l1, *l2;
|
||||||
|
const struct my_struct *x1, *x2;
|
||||||
|
|
||||||
|
l1 = d1;
|
||||||
|
l2 = d2;
|
||||||
|
|
||||||
|
x1 = EINA_INLIST_CONTAINER_GET(l1, struct my_struct);
|
||||||
|
x2 = EINA_INLIST_CONTAINER_GET(l2, struct my_struct);
|
||||||
|
|
||||||
|
return x1->a - x2->a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
struct my_struct *d, *cur;
|
||||||
|
Eina_Inlist *list, *itr, *tmp;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
d = malloc(sizeof(*d));
|
||||||
|
d->a = 1;
|
||||||
|
d->b = 10;
|
||||||
|
list = eina_inlist_append(NULL, EINA_INLIST_GET(d));
|
||||||
|
|
||||||
|
d = malloc(sizeof(*d));
|
||||||
|
d->a = 2;
|
||||||
|
d->b = 20;
|
||||||
|
list = eina_inlist_append(list, EINA_INLIST_GET(d));
|
||||||
|
|
||||||
|
d = malloc(sizeof(*d));
|
||||||
|
d->a = 3;
|
||||||
|
d->b = 30;
|
||||||
|
list = eina_inlist_prepend(list, EINA_INLIST_GET(d));
|
||||||
|
|
||||||
|
printf("list=%p\n", list);
|
||||||
|
EINA_INLIST_FOREACH(list, cur)
|
||||||
|
printf("\ta=%d, b=%d\n", cur->a, cur->b);
|
||||||
|
|
||||||
|
list = eina_inlist_promote(list, EINA_INLIST_GET(d));
|
||||||
|
|
||||||
|
d = malloc(sizeof(*d));
|
||||||
|
d->a = 4;
|
||||||
|
d->b = 40;
|
||||||
|
list = eina_inlist_append_relative(list, EINA_INLIST_GET(d), list);
|
||||||
|
|
||||||
|
list = eina_inlist_demote(list, EINA_INLIST_GET(d));
|
||||||
|
|
||||||
|
list = eina_inlist_sort(list, sort_cb);
|
||||||
|
|
||||||
|
printf("list after sort=%p\n", list);
|
||||||
|
EINA_INLIST_FOREACH(list, cur)
|
||||||
|
printf("\ta=%d, b=%d\n", cur->a, cur->b);
|
||||||
|
|
||||||
|
tmp = eina_inlist_find(list, EINA_INLIST_GET(d));
|
||||||
|
if (tmp)
|
||||||
|
cur = EINA_INLIST_CONTAINER_GET(tmp, struct my_struct);
|
||||||
|
else
|
||||||
|
cur = NULL;
|
||||||
|
|
||||||
|
if (d != cur)
|
||||||
|
printf("wrong node! cur=%p\n", cur);
|
||||||
|
|
||||||
|
list = eina_inlist_remove(list, EINA_INLIST_GET(d));
|
||||||
|
free(d);
|
||||||
|
printf("list=%p\n", list);
|
||||||
|
for (itr = list; itr != NULL; itr = itr->next)
|
||||||
|
{
|
||||||
|
cur = EINA_INLIST_CONTAINER_GET(itr, struct my_struct);
|
||||||
|
printf("\ta=%d, b=%d\n", cur->a, cur->b);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (list)
|
||||||
|
{
|
||||||
|
struct my_struct *aux = EINA_INLIST_CONTAINER_GET(list,
|
||||||
|
struct my_struct);
|
||||||
|
list = eina_inlist_remove(list, list);
|
||||||
|
free(aux);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
// Compile with:
|
||||||
|
// gcc -g eina_inlist_02.c -o eina_inlist_02 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct my_struct {
|
||||||
|
EINA_INLIST;
|
||||||
|
int a, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
struct my_struct *d, *cur;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
Eina_Inlist *inlist = NULL;
|
||||||
|
Eina_List *list = NULL, *l_itr, *l_next;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
for (i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
d = malloc(sizeof(*d));
|
||||||
|
d->a = i;
|
||||||
|
d->b = i * 10;
|
||||||
|
inlist = eina_inlist_append(inlist, EINA_INLIST_GET(d));
|
||||||
|
if ((i % 2) == 0)
|
||||||
|
list = eina_list_prepend(list, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("inlist=%p\n", inlist);
|
||||||
|
EINA_INLIST_FOREACH(inlist, cur)
|
||||||
|
printf("\ta=%d, b=%d\n", cur->a, cur->b);
|
||||||
|
|
||||||
|
printf("list=%p\n", list);
|
||||||
|
EINA_LIST_FOREACH(list, l_itr, cur)
|
||||||
|
printf("\ta=%d, b=%d\n", cur->a, cur->b);
|
||||||
|
|
||||||
|
printf("inlist count=%d\n", eina_inlist_count(inlist));
|
||||||
|
printf("list count=%d\n\n", eina_list_count(list));
|
||||||
|
|
||||||
|
EINA_LIST_FOREACH_SAFE(list, l_itr, l_next, cur)
|
||||||
|
{
|
||||||
|
if ((cur->a % 3) == 0)
|
||||||
|
list = eina_list_remove_list(list, l_itr);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("inlist count=%d\n", eina_inlist_count(inlist));
|
||||||
|
printf("list count=%d\n\n", eina_list_count(list));
|
||||||
|
|
||||||
|
eina_list_free(list);
|
||||||
|
|
||||||
|
while (inlist)
|
||||||
|
{
|
||||||
|
struct my_struct *aux = EINA_INLIST_CONTAINER_GET(inlist,
|
||||||
|
struct my_struct);
|
||||||
|
inlist = eina_inlist_remove(inlist, inlist);
|
||||||
|
free(aux);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,75 @@
|
||||||
|
// Compile with:
|
||||||
|
// gcc -g eina_inlist_03.c -o eina_inlist_03 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct my_struct {
|
||||||
|
EINA_INLIST;
|
||||||
|
Eina_Inlist even;
|
||||||
|
int a, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define EVEN_INLIST_GET(Inlist) (& ((Inlist)->even))
|
||||||
|
|
||||||
|
#define EVEN_INLIST_CONTAINER_GET(ptr, type) \
|
||||||
|
((type *)((char *)ptr - offsetof(type, even)))
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
struct my_struct *d, *cur;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
Eina_Inlist *list = NULL, *list_even = NULL, *itr;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
for (i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
d = malloc(sizeof(*d));
|
||||||
|
d->a = i;
|
||||||
|
d->b = i * 10;
|
||||||
|
list = eina_inlist_append(list, EINA_INLIST_GET(d));
|
||||||
|
if ((i % 2) == 0)
|
||||||
|
list_even = eina_inlist_prepend(list_even, EVEN_INLIST_GET(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("list=%p\n", list);
|
||||||
|
EINA_INLIST_FOREACH(list, cur)
|
||||||
|
printf("\ta=%d, b=%d\n", cur->a, cur->b);
|
||||||
|
|
||||||
|
printf("list_even=%p\n", list_even);
|
||||||
|
for (itr = list_even; itr != NULL; itr = itr->next)
|
||||||
|
{
|
||||||
|
cur = EVEN_INLIST_CONTAINER_GET(itr, struct my_struct);
|
||||||
|
printf("\ta=%d, b=%d\n", cur->a, cur->b);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("list count=%d\n", eina_inlist_count(list));
|
||||||
|
printf("list_even count=%d\n\n", eina_inlist_count(list_even));
|
||||||
|
|
||||||
|
itr = list_even;
|
||||||
|
while (itr)
|
||||||
|
{
|
||||||
|
Eina_Inlist *next = itr->next;
|
||||||
|
cur = EVEN_INLIST_CONTAINER_GET(itr, struct my_struct);
|
||||||
|
if ((cur->a % 3) == 0)
|
||||||
|
list_even = eina_inlist_remove(list_even, itr);
|
||||||
|
itr = next;
|
||||||
|
}
|
||||||
|
printf("list count=%d\n", eina_inlist_count(list));
|
||||||
|
printf("list_even count=%d\n\n", eina_inlist_count(list_even));
|
||||||
|
|
||||||
|
while (list)
|
||||||
|
{
|
||||||
|
struct my_struct *aux = EINA_INLIST_CONTAINER_GET(list,
|
||||||
|
struct my_struct);
|
||||||
|
list = eina_inlist_remove(list, list);
|
||||||
|
free(aux);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_iterator_01.c -o eina_iterator_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
print_one(const void *container, void *data, void *fdata)
|
||||||
|
{
|
||||||
|
printf("%s\n", (char*)data);
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_eina_container(Eina_Iterator *it)
|
||||||
|
{
|
||||||
|
eina_iterator_foreach(it, print_one, NULL);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char *strings[] = {
|
||||||
|
"unintersting string", "husker", "starbuck", "husker"
|
||||||
|
};
|
||||||
|
const char *more_strings[] = {
|
||||||
|
"very unintersting string",
|
||||||
|
"what do your hear?",
|
||||||
|
"nothing but the rain",
|
||||||
|
"then grab your gun and bring the cat in"
|
||||||
|
};
|
||||||
|
Eina_Array *array;
|
||||||
|
Eina_List *list = NULL;
|
||||||
|
Eina_Iterator *it;
|
||||||
|
unsigned short int i;
|
||||||
|
char *uninteresting;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
array = eina_array_new(4);
|
||||||
|
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
eina_array_push(array, strings[i]);
|
||||||
|
list = eina_list_append(list, more_strings[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
it = eina_array_iterator_new(array);
|
||||||
|
eina_iterator_next(it, &uninteresting);
|
||||||
|
print_eina_container(it);
|
||||||
|
eina_array_free(eina_iterator_container_get(it));
|
||||||
|
eina_iterator_free(it);
|
||||||
|
|
||||||
|
it = eina_list_iterator_new(list);
|
||||||
|
eina_iterator_next(it, &uninteresting);
|
||||||
|
print_eina_container(it);
|
||||||
|
eina_iterator_free(it);
|
||||||
|
|
||||||
|
eina_list_free(list);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_list_01.c -o eina_list_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_List *list = NULL;
|
||||||
|
Eina_List *l;
|
||||||
|
void *list_data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
list = eina_list_append(list, "tigh");
|
||||||
|
list = eina_list_append(list, "adar");
|
||||||
|
list = eina_list_append(list, "baltar");
|
||||||
|
list = eina_list_append(list, "roslin");
|
||||||
|
|
||||||
|
EINA_LIST_FOREACH(list, l, list_data)
|
||||||
|
printf("%s\n", (char*)list_data);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
l = eina_list_nth_list(list, 1);
|
||||||
|
list = eina_list_append_relative_list(list, "cain", l);
|
||||||
|
|
||||||
|
list = eina_list_append_relative(list, "zarek", "cain");
|
||||||
|
|
||||||
|
list = eina_list_prepend(list, "adama");
|
||||||
|
|
||||||
|
list = eina_list_prepend_relative(list, "gaeta", "cain");
|
||||||
|
|
||||||
|
list = eina_list_prepend_relative_list(list, "lampkin", l);
|
||||||
|
|
||||||
|
EINA_LIST_FOREACH(list, l, list_data)
|
||||||
|
printf("%s\n", (char*)list_data);
|
||||||
|
|
||||||
|
eina_list_free(list);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_list_02.c -o eina_list_02 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_List *list = NULL, *other_list = NULL;
|
||||||
|
Eina_List *l;
|
||||||
|
void *data;
|
||||||
|
int cmp_result;
|
||||||
|
Eina_Compare_Cb cmp_func = (Eina_Compare_Cb)strcmp;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
list = eina_list_append(list, "starbuck");
|
||||||
|
list = eina_list_append(list, "appolo");
|
||||||
|
list = eina_list_append(list, "boomer");
|
||||||
|
|
||||||
|
data = eina_list_search_unsorted(list, cmp_func, "boomer");
|
||||||
|
l = eina_list_search_unsorted_list(list, cmp_func, "boomer");
|
||||||
|
if (l->data != data)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
list = eina_list_sort(list, 0, cmp_func);
|
||||||
|
|
||||||
|
data = eina_list_search_sorted(list, cmp_func, "starbuck");
|
||||||
|
l = eina_list_search_sorted_list(list, cmp_func, "starbuck");
|
||||||
|
if (l->data != data)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
list = eina_list_sorted_insert(list, cmp_func, "helo");
|
||||||
|
|
||||||
|
l = eina_list_search_sorted_near_list(list, cmp_func, "hera", &cmp_result);
|
||||||
|
if (cmp_result > 0)
|
||||||
|
list = eina_list_prepend_relative_list(list, "hera", l);
|
||||||
|
else if (cmp_result < 0)
|
||||||
|
list = eina_list_append_relative_list(list, "hera", l);
|
||||||
|
|
||||||
|
l = eina_list_search_sorted_list(list, cmp_func, "boomer");
|
||||||
|
list = eina_list_split_list(list, l, &other_list);
|
||||||
|
|
||||||
|
other_list = eina_list_sort(other_list, 0, cmp_func);
|
||||||
|
|
||||||
|
list = eina_list_sorted_merge(list, other_list, cmp_func);
|
||||||
|
|
||||||
|
eina_list_free(list);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_list_03.c -o eina_list_03 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_List *list = NULL, *r_list;
|
||||||
|
Eina_List *l;
|
||||||
|
Eina_Iterator *itr;
|
||||||
|
void *list_data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
list = eina_list_append(list, "caprica");
|
||||||
|
list = eina_list_append(list, "sagitarius");
|
||||||
|
list = eina_list_append(list, "aerilon");
|
||||||
|
list = eina_list_append(list, "gemenon");
|
||||||
|
|
||||||
|
list = eina_list_promote_list(list, eina_list_nth_list(list, 2));
|
||||||
|
list = eina_list_demote_list(list, eina_list_nth_list(list, 2));
|
||||||
|
|
||||||
|
list = eina_list_remove(list, "sagitarius");
|
||||||
|
|
||||||
|
l = eina_list_data_find_list(list, "aerilon");
|
||||||
|
eina_list_data_set(l, "aquarius");
|
||||||
|
|
||||||
|
printf("size: %d\n", eina_list_count(list));
|
||||||
|
|
||||||
|
r_list = eina_list_reverse_clone(list);
|
||||||
|
|
||||||
|
itr = eina_list_iterator_new(r_list);
|
||||||
|
EINA_ITERATOR_FOREACH(itr, list_data)
|
||||||
|
printf("%s\n", (char*)list_data);
|
||||||
|
eina_iterator_free(itr);
|
||||||
|
|
||||||
|
eina_list_free(list);
|
||||||
|
eina_list_free(r_list);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_list_04.c -o eina_list_04 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_List *list = NULL;
|
||||||
|
Eina_List *l;
|
||||||
|
void *list_data;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
list = eina_list_append(list, eina_stringshare_add("calvin"));
|
||||||
|
list = eina_list_append(list, eina_stringshare_add("Leoben"));
|
||||||
|
list = eina_list_append(list, eina_stringshare_add("D'Anna"));
|
||||||
|
list = eina_list_append(list, eina_stringshare_add("Simon"));
|
||||||
|
list = eina_list_append(list, eina_stringshare_add("Doral"));
|
||||||
|
list = eina_list_append(list, eina_stringshare_add("Six"));
|
||||||
|
list = eina_list_append(list, eina_stringshare_add("Sharon"));
|
||||||
|
|
||||||
|
for(l = list; l; l = eina_list_next(l))
|
||||||
|
printf("%s\n", (char*)l->data);
|
||||||
|
|
||||||
|
for(l = eina_list_last(list); l; l = eina_list_prev(l))
|
||||||
|
printf("%s\n", (char*)eina_list_data_get(l));
|
||||||
|
|
||||||
|
EINA_LIST_FREE(list, list_data)
|
||||||
|
eina_stringshare_del(list_data);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -Wall -o eina_log_01 eina_log_01.c `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
void test_warn(void)
|
||||||
|
{
|
||||||
|
EINA_LOG_WARN("Here is a warning message");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (!eina_init())
|
||||||
|
{
|
||||||
|
printf("log during the initialization of Eina_Log module\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
test_warn();
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -Wall -o eina_log_02 eina_log_02.c `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
void test(int i)
|
||||||
|
{
|
||||||
|
EINA_LOG_DBG("Entering test");
|
||||||
|
|
||||||
|
if (i < 0)
|
||||||
|
{
|
||||||
|
EINA_LOG_ERR("Argument is negative");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EINA_LOG_INFO("argument non negative");
|
||||||
|
|
||||||
|
EINA_LOG_DBG("Exiting test");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (!eina_init())
|
||||||
|
{
|
||||||
|
printf("log during the initialization of Eina_Log module\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
test(-1);
|
||||||
|
test(0);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -Wall -o eina_log_03 eina_log_03.c `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
#define log(fmt, ...) \
|
||||||
|
eina_log_print(EINA_LOG_LEVEL_ERR, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
|
||||||
|
|
||||||
|
typedef struct _Data Data;
|
||||||
|
|
||||||
|
struct _Data
|
||||||
|
{
|
||||||
|
int to_stderr;
|
||||||
|
};
|
||||||
|
|
||||||
|
void print_cb(const Eina_Log_Domain *domain,
|
||||||
|
Eina_Log_Level level,
|
||||||
|
const char *file,
|
||||||
|
const char *fnc,
|
||||||
|
int line,
|
||||||
|
const char *fmt,
|
||||||
|
void *data,
|
||||||
|
va_list args)
|
||||||
|
{
|
||||||
|
Data *d;
|
||||||
|
FILE *output;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
d = (Data*)data;
|
||||||
|
if (d->to_stderr)
|
||||||
|
{
|
||||||
|
output = stderr;
|
||||||
|
str = "stderr";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
output = stdout;
|
||||||
|
str = "stdout";
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(output, "%s:%s:%s (%d) %s: ",
|
||||||
|
domain->domain_str, file, fnc, line, str);
|
||||||
|
vfprintf(output, fmt, args);
|
||||||
|
putc('\n', output);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test(Data *data, int i)
|
||||||
|
{
|
||||||
|
if (i < 0)
|
||||||
|
data->to_stderr = 0;
|
||||||
|
else
|
||||||
|
data->to_stderr = 1;
|
||||||
|
|
||||||
|
EINA_LOG_INFO("Log message...");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Data data;
|
||||||
|
|
||||||
|
if (!eina_init())
|
||||||
|
{
|
||||||
|
printf("log during the initialization of Eina_Log module\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_log_print_cb_set(print_cb, &data);
|
||||||
|
|
||||||
|
test(&data, -1);
|
||||||
|
test(&data, 0);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_magic_01.c -o eina_magic_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
#define BASETYPE_MAGIC 0x12345
|
||||||
|
struct _person {
|
||||||
|
EINA_MAGIC;
|
||||||
|
char *name;
|
||||||
|
};
|
||||||
|
typedef struct _person person;
|
||||||
|
|
||||||
|
#define SUBTYPE_MAGIC 0x3333
|
||||||
|
struct _pilot {
|
||||||
|
person base;
|
||||||
|
EINA_MAGIC;
|
||||||
|
char *callsign;
|
||||||
|
};
|
||||||
|
typedef struct _pilot pilot;
|
||||||
|
|
||||||
|
person *
|
||||||
|
person_new(const char *name)
|
||||||
|
{
|
||||||
|
person *ptr = malloc(sizeof(person));
|
||||||
|
EINA_MAGIC_SET(ptr, BASETYPE_MAGIC);
|
||||||
|
ptr->name = strdup(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
person_free(person *ptr) {
|
||||||
|
if (!EINA_MAGIC_CHECK(ptr, BASETYPE_MAGIC))
|
||||||
|
{
|
||||||
|
EINA_MAGIC_FAIL(ptr, BASETYPE_MAGIC);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
EINA_MAGIC_SET(ptr, EINA_MAGIC_NONE);
|
||||||
|
free(ptr->name);
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
pilot *
|
||||||
|
pilot_new(const char *name, const char *callsign)
|
||||||
|
{
|
||||||
|
pilot *ptr = malloc(sizeof(pilot));
|
||||||
|
EINA_MAGIC_SET(ptr, SUBTYPE_MAGIC);
|
||||||
|
EINA_MAGIC_SET(&ptr->base, BASETYPE_MAGIC);
|
||||||
|
ptr->base.name = strdup(name);
|
||||||
|
ptr->callsign = strdup(callsign);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
pilot_free(pilot *ptr) {
|
||||||
|
if (!EINA_MAGIC_CHECK(ptr, SUBTYPE_MAGIC))
|
||||||
|
{
|
||||||
|
EINA_MAGIC_FAIL(ptr, SUBTYPE_MAGIC);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
EINA_MAGIC_SET(ptr, EINA_MAGIC_NONE);
|
||||||
|
EINA_MAGIC_SET(&ptr->base, EINA_MAGIC_NONE);
|
||||||
|
free(ptr->base.name);
|
||||||
|
free(ptr->callsign);
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
print_person(person *ptr)
|
||||||
|
{
|
||||||
|
if (!EINA_MAGIC_CHECK(ptr, BASETYPE_MAGIC)){
|
||||||
|
EINA_MAGIC_FAIL(ptr, BASETYPE_MAGIC);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("name: %s\n", ptr->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
print_pilot(pilot *ptr)
|
||||||
|
{
|
||||||
|
if (!EINA_MAGIC_CHECK(ptr, SUBTYPE_MAGIC)) {
|
||||||
|
EINA_MAGIC_FAIL(ptr, SUBTYPE_MAGIC);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
print_person(&ptr->base);
|
||||||
|
printf("callsign: %s\n", ptr->callsign);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
person *base;
|
||||||
|
pilot *sub;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
eina_magic_string_set(BASETYPE_MAGIC, "person");
|
||||||
|
eina_magic_string_static_set(SUBTYPE_MAGIC, "pilot");
|
||||||
|
|
||||||
|
base = person_new("Tyrol");
|
||||||
|
sub = pilot_new("thrace", "starbuck");
|
||||||
|
|
||||||
|
print_person(base);
|
||||||
|
print_person((person *)sub);
|
||||||
|
|
||||||
|
print_pilot(base); //BAD
|
||||||
|
print_pilot(sub);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,127 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -Wall -o eina_simple_xml_01 eina_simple_xml_01.c `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static Eina_Bool _xml_attr_cb(void *data, const char *key, const char *value);
|
||||||
|
static Eina_Bool _xml_tag_cb(void *data, Eina_Simple_XML_Type type,
|
||||||
|
const char *content, unsigned offset, unsigned length);
|
||||||
|
static Eina_Bool _print(const void *container, void *data, void *fdata);
|
||||||
|
|
||||||
|
Eina_Bool tag_login = EINA_FALSE;
|
||||||
|
Eina_Bool tag_message = EINA_FALSE;
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
FILE *file;
|
||||||
|
long size;
|
||||||
|
char *buffer;
|
||||||
|
Eina_Array *array;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
if ((file = fopen("chat.xml", "rb")))
|
||||||
|
{
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
size = ftell(file);
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
|
if ((buffer = malloc(size)))
|
||||||
|
{
|
||||||
|
fread(buffer, 1, size, file);
|
||||||
|
|
||||||
|
array = eina_array_new(10);
|
||||||
|
eina_simple_xml_parse(buffer, size, EINA_TRUE,
|
||||||
|
_xml_tag_cb, array);
|
||||||
|
|
||||||
|
eina_array_foreach(array, _print, NULL);
|
||||||
|
|
||||||
|
eina_array_free(array);
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EINA_LOG_ERR("Can't allocate memory!");
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EINA_LOG_ERR("Can't open chat.xml!");
|
||||||
|
}
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_xml_tag_cb(void *data, Eina_Simple_XML_Type type, const char *content,
|
||||||
|
unsigned offset, unsigned length)
|
||||||
|
{
|
||||||
|
char buffer[length+1];
|
||||||
|
Eina_Array *array = data;
|
||||||
|
char str[512];
|
||||||
|
|
||||||
|
if (type == EINA_SIMPLE_XML_OPEN)
|
||||||
|
{
|
||||||
|
if(!strncmp("post", content, strlen("post")))
|
||||||
|
{
|
||||||
|
const char *tags = eina_simple_xml_tag_attributes_find(content,
|
||||||
|
length);
|
||||||
|
eina_simple_xml_attributes_parse(tags, length - (tags - content),
|
||||||
|
_xml_attr_cb, str);
|
||||||
|
}
|
||||||
|
else if (!strncmp("login>", content, strlen("login>")))
|
||||||
|
{
|
||||||
|
tag_login = EINA_TRUE;
|
||||||
|
}
|
||||||
|
else if (!strncmp("message>", content, strlen("message>")))
|
||||||
|
{
|
||||||
|
tag_message = EINA_TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (type == EINA_SIMPLE_XML_DATA)
|
||||||
|
{
|
||||||
|
if (tag_login == EINA_TRUE)
|
||||||
|
{
|
||||||
|
snprintf(buffer, sizeof(buffer), content);
|
||||||
|
strncat(str, "<", 1);
|
||||||
|
strncat(str, buffer, sizeof(buffer));
|
||||||
|
strncat(str, "> ", 2);
|
||||||
|
tag_login = EINA_FALSE;
|
||||||
|
}
|
||||||
|
else if (tag_message == EINA_TRUE)
|
||||||
|
{
|
||||||
|
snprintf(buffer, sizeof(buffer), content);
|
||||||
|
strncat(str, buffer, sizeof(buffer));
|
||||||
|
tag_message = EINA_FALSE;
|
||||||
|
eina_array_push(array, strdup(str));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_xml_attr_cb(void *data, const char *key, const char *value)
|
||||||
|
{
|
||||||
|
char *str = data;
|
||||||
|
|
||||||
|
if(!strcmp("id", key))
|
||||||
|
{
|
||||||
|
snprintf(str, sizeof(value) + 3, "(%s) ", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_print(const void *container, void *data, void *fdata)
|
||||||
|
{
|
||||||
|
printf("%s\n", (char *)data);
|
||||||
|
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -Wall -o eina_str_01 eina_str_01.c `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
char *names = "Calvin;Leoben;D'anna;Simon;Doral;Six;Daniel;Sharon";
|
||||||
|
char *str;
|
||||||
|
char *tmp;
|
||||||
|
char *prologue;
|
||||||
|
char *part1 = "The Cylons were created by man. They evolved. They rebelled.";
|
||||||
|
char *part2 = "There are many copies. And they have a plan.";
|
||||||
|
char **arr;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
arr = eina_str_split(names, ";", 0);
|
||||||
|
for (i = 0; arr[i]; i++)
|
||||||
|
printf("%s\n", arr[i]);
|
||||||
|
|
||||||
|
free(arr[0]);
|
||||||
|
free(arr);
|
||||||
|
|
||||||
|
str = malloc(sizeof(char) * 4);
|
||||||
|
strcpy(str, "bsd");
|
||||||
|
|
||||||
|
eina_str_toupper((char **)&str);
|
||||||
|
printf("%s\n", str);
|
||||||
|
eina_str_tolower(&str);
|
||||||
|
printf("%s\n", str);
|
||||||
|
|
||||||
|
if (eina_str_has_prefix(names, "Calvin"))
|
||||||
|
printf("Starts with 'Calvin'\n");
|
||||||
|
if (eina_str_has_suffix(names, "sharon"))
|
||||||
|
printf("Ends with 'sharon'\n");
|
||||||
|
if (eina_str_has_extension(names, "sharon"))
|
||||||
|
printf("Has extension 'sharon'\n");
|
||||||
|
|
||||||
|
tmp = eina_str_escape("They'll start going ripe on us pretty soon.");
|
||||||
|
printf("%s\n", tmp);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
prologue = malloc(sizeof(char) * 106);
|
||||||
|
eina_str_join_len(prologue, 106, ' ', part1, strlen(part1), part2, strlen(part2));
|
||||||
|
printf("%s\n", prologue);
|
||||||
|
|
||||||
|
eina_strlcpy(str, prologue, 4);
|
||||||
|
printf("%s\n", str);
|
||||||
|
|
||||||
|
free(prologue);
|
||||||
|
free(str);
|
||||||
|
|
||||||
|
str = malloc(sizeof(char) * 14);
|
||||||
|
sprintf(str, "%s", "cylons+");
|
||||||
|
eina_strlcat(str, "humans", 14);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -Wall -o eina_strbuf_01 eina_strbuf_01.c `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_Strbuf *buf;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
buf = eina_strbuf_new();
|
||||||
|
|
||||||
|
eina_strbuf_append_length(buf, "buffe", 5);
|
||||||
|
eina_strbuf_append_char(buf, 'r');
|
||||||
|
printf("%s\n", eina_strbuf_string_get(buf));
|
||||||
|
|
||||||
|
eina_strbuf_insert_escaped(buf, "my ", 0);
|
||||||
|
printf("%s\n", eina_strbuf_string_get(buf));
|
||||||
|
eina_strbuf_reset(buf);
|
||||||
|
|
||||||
|
eina_strbuf_append_escaped(buf, "my buffer");
|
||||||
|
printf("%s\n", eina_strbuf_string_get(buf));
|
||||||
|
eina_strbuf_reset(buf);
|
||||||
|
|
||||||
|
eina_strbuf_append_printf(buf, "%s%c", "buffe", 'r');
|
||||||
|
eina_strbuf_insert_printf(buf, " %s: %d", 6, "length", eina_strbuf_length_get(buf));
|
||||||
|
printf("%s\n", eina_strbuf_string_get(buf));
|
||||||
|
|
||||||
|
eina_strbuf_remove(buf, 0, 7);
|
||||||
|
printf("%s\n", eina_strbuf_string_get(buf));
|
||||||
|
|
||||||
|
eina_strbuf_replace_all(buf, "length", "size");
|
||||||
|
printf("%s\n", eina_strbuf_string_get(buf));
|
||||||
|
|
||||||
|
eina_strbuf_free(buf);
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -g eina_stringshare_01.c -o eina_stringshare_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char *str, *str2;
|
||||||
|
const char *prologe = "The Cylons were created by man. They rebelled. They "
|
||||||
|
"evolved.";
|
||||||
|
const char *prologe2 = "%d Cylon models. %d are known. %d live in secret. "
|
||||||
|
"%d will be revealed.";
|
||||||
|
const char *prologe3 = "There are many copies. And they have a plan.";
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
str = eina_stringshare_add_length(prologe, 31);
|
||||||
|
printf("%s\n", str);
|
||||||
|
printf("length: %d\n", eina_stringshare_strlen(str));
|
||||||
|
eina_stringshare_del(str);
|
||||||
|
|
||||||
|
str = eina_stringshare_printf(prologe2, 12, 7, 4, 1);
|
||||||
|
printf("%s\n", str);
|
||||||
|
eina_stringshare_del(str);
|
||||||
|
|
||||||
|
str = eina_stringshare_nprintf(45, "%s", prologe3);
|
||||||
|
printf("%s\n", str);
|
||||||
|
str2 = eina_stringshare_add(prologe3);
|
||||||
|
printf("%s\n", str2);
|
||||||
|
eina_stringshare_ref(str2);
|
||||||
|
eina_stringshare_del(str2);
|
||||||
|
printf("%s\n", str2);
|
||||||
|
|
||||||
|
eina_stringshare_replace(&str, prologe);
|
||||||
|
printf("%s\n", str);
|
||||||
|
|
||||||
|
eina_stringshare_del(str);
|
||||||
|
eina_stringshare_del(str2);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,316 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc eina_tiler_01.c -o eina_tiler_01 `pkg-config --cflags --libs ecore-evas ecore evas eina`
|
||||||
|
|
||||||
|
#include <Ecore_Evas.h>
|
||||||
|
#include <Ecore.h>
|
||||||
|
#include <Evas.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
#define WINDOW_PAD (20)
|
||||||
|
|
||||||
|
static Eina_Tiler *tiler;
|
||||||
|
static Eina_Rectangle *input_rects;
|
||||||
|
static unsigned int input_count;
|
||||||
|
static unsigned int input_idx = 0, input_color_idx = 0, output_color_idx = 0;
|
||||||
|
static Eina_List *output_objs = NULL;
|
||||||
|
static Evas_Coord maxw, maxh, winw, winh;
|
||||||
|
static Evas *evas;
|
||||||
|
|
||||||
|
static const struct color {
|
||||||
|
unsigned char r, g, b;
|
||||||
|
} colors[] = {
|
||||||
|
{255, 0, 0},
|
||||||
|
{0, 255, 0},
|
||||||
|
{0, 0, 255},
|
||||||
|
|
||||||
|
{255, 128, 0},
|
||||||
|
{0, 255, 128},
|
||||||
|
{128, 0, 255},
|
||||||
|
|
||||||
|
{255, 255, 0},
|
||||||
|
{0, 255, 255},
|
||||||
|
{255, 0, 255},
|
||||||
|
|
||||||
|
{255, 0, 128},
|
||||||
|
{128, 255, 0},
|
||||||
|
{0, 128, 255},
|
||||||
|
|
||||||
|
{128, 128, 0},
|
||||||
|
{0, 128, 128},
|
||||||
|
{128, 0, 128},
|
||||||
|
|
||||||
|
{128, 0, 0},
|
||||||
|
{0, 128, 0},
|
||||||
|
{0, 0, 128},
|
||||||
|
|
||||||
|
{255, 128, 0},
|
||||||
|
{0, 255, 128},
|
||||||
|
{128, 0, 255},
|
||||||
|
|
||||||
|
{64, 64, 0},
|
||||||
|
{0, 64, 64},
|
||||||
|
{64, 0, 64},
|
||||||
|
|
||||||
|
{128, 128, 0},
|
||||||
|
{0, 128, 128},
|
||||||
|
{128, 0, 128},
|
||||||
|
|
||||||
|
{255, 0, 128},
|
||||||
|
{128, 255, 0},
|
||||||
|
{0, 128, 255},
|
||||||
|
|
||||||
|
{128, 64, 0},
|
||||||
|
{0, 128, 64},
|
||||||
|
{64, 0, 128},
|
||||||
|
|
||||||
|
{128, 0, 64},
|
||||||
|
{64, 128, 0},
|
||||||
|
{0, 64, 128}
|
||||||
|
};
|
||||||
|
|
||||||
|
#define MAX_COLORS (sizeof(colors) / sizeof(colors[0]))
|
||||||
|
|
||||||
|
static void
|
||||||
|
add_text(const char *text, int x, int y, int w)
|
||||||
|
{
|
||||||
|
Evas_Object *o = evas_object_text_add(evas);
|
||||||
|
evas_object_color_set(o, 0, 0, 0, 255);
|
||||||
|
evas_object_move(o, x, y);
|
||||||
|
evas_object_resize(o, w, WINDOW_PAD);
|
||||||
|
evas_object_text_font_set(o, "Sans", 10);
|
||||||
|
evas_object_text_text_set(o, text);
|
||||||
|
evas_object_show(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
output_rects_reset(void)
|
||||||
|
{
|
||||||
|
Evas_Object *o;
|
||||||
|
EINA_LIST_FREE(output_objs, o)
|
||||||
|
evas_object_del(o);
|
||||||
|
output_color_idx = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
add_input_rect(const Eina_Rectangle *r)
|
||||||
|
{
|
||||||
|
Evas_Object *o;
|
||||||
|
Evas_Coord bx, by;
|
||||||
|
|
||||||
|
bx = WINDOW_PAD;
|
||||||
|
by = WINDOW_PAD;
|
||||||
|
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
#define C(comp) (((int)colors[input_color_idx].comp * 128) / 255)
|
||||||
|
evas_object_color_set(o, C(r), C(g), C(b), 128);
|
||||||
|
#undef C
|
||||||
|
evas_object_move(o, r->x + bx, r->y + by);
|
||||||
|
evas_object_resize(o, r->w, r->h);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
input_color_idx = (input_color_idx + 1) % MAX_COLORS;
|
||||||
|
|
||||||
|
bx += maxw + WINDOW_PAD;
|
||||||
|
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
evas_object_color_set(o, 32, 32, 32, 128);
|
||||||
|
evas_object_move(o, r->x + bx, r->y + by);
|
||||||
|
evas_object_resize(o, r->w, 1);
|
||||||
|
evas_object_layer_set(o, EVAS_LAYER_MAX);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
evas_object_color_set(o, 32, 32, 32, 128);
|
||||||
|
evas_object_move(o, r->x + bx, r->y + by);
|
||||||
|
evas_object_resize(o, 1, r->h);
|
||||||
|
evas_object_layer_set(o, EVAS_LAYER_MAX);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
evas_object_color_set(o, 32, 32, 32, 128);
|
||||||
|
evas_object_move(o, r->x + bx, r->y + by + r->h);
|
||||||
|
evas_object_resize(o, r->w, 1);
|
||||||
|
evas_object_layer_set(o, EVAS_LAYER_MAX);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
evas_object_color_set(o, 32, 32, 32, 128);
|
||||||
|
evas_object_move(o, r->x + bx + r->w, r->y + by);
|
||||||
|
evas_object_resize(o, 1, r->h);
|
||||||
|
evas_object_layer_set(o, EVAS_LAYER_MAX);
|
||||||
|
evas_object_show(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
add_output_rect(const Eina_Rectangle *r)
|
||||||
|
{
|
||||||
|
Evas_Object *o = evas_object_rectangle_add(evas);
|
||||||
|
#define C(comp) (((int)colors[output_color_idx].comp * 128) / 255)
|
||||||
|
evas_object_color_set(o, C(r), C(g), C(b), 128);
|
||||||
|
#undef C
|
||||||
|
evas_object_move(o, r->x + maxw + 2 * WINDOW_PAD, r->y + WINDOW_PAD);
|
||||||
|
evas_object_resize(o, r->w, r->h);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
output_color_idx = (output_color_idx + 1) % MAX_COLORS;
|
||||||
|
|
||||||
|
output_objs = eina_list_append(output_objs, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
process_input(void *data)
|
||||||
|
{
|
||||||
|
Eina_Iterator *itr;
|
||||||
|
Eina_Rectangle r, *r1;
|
||||||
|
unsigned int out = 0;
|
||||||
|
|
||||||
|
if (input_idx == input_count)
|
||||||
|
{
|
||||||
|
add_text("Done. Close the window to exit",
|
||||||
|
WINDOW_PAD, winh - WINDOW_PAD, winw - 2 * WINDOW_PAD);
|
||||||
|
return EINA_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
output_rects_reset();
|
||||||
|
|
||||||
|
r = input_rects[input_idx];
|
||||||
|
printf("Iteration #%u: %dx%d%+d%+d\n", input_idx, r.w, r.h, r.x, r.y);
|
||||||
|
input_idx++;
|
||||||
|
add_input_rect(&r);
|
||||||
|
|
||||||
|
eina_tiler_rect_add(tiler, &r);
|
||||||
|
itr = eina_tiler_iterator_new(tiler);
|
||||||
|
EINA_ITERATOR_FOREACH(itr, r1)
|
||||||
|
{
|
||||||
|
printf("\tOutput #%u: %dx%d%+d%+d\n", out, r1->w, r1->h, r1->x, r1->y);
|
||||||
|
add_output_rect(r1);
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
eina_iterator_free(itr);
|
||||||
|
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(const char *progname)
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"Usage:\n\n"
|
||||||
|
"\t%s <rect1> ... <rectN>\n\n"
|
||||||
|
"with rectangles being in the format:\n"
|
||||||
|
"\tWIDTHxHEIGHT<+->X<+->Y\n"
|
||||||
|
"examples:\n"
|
||||||
|
"\t100x100+10+10 - width=100, height=100 at x=10, y=10\n"
|
||||||
|
"\t150x50+5+6 - width=150, height=50 at x=5, y=6\n",
|
||||||
|
progname);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
Ecore_Evas *ee;
|
||||||
|
Evas_Object *o;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
usage(argv[0]);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
input_rects = calloc(argc - 1, sizeof(Eina_Rectangle));
|
||||||
|
input_count = 0;
|
||||||
|
maxw = 0;
|
||||||
|
maxh = 0;
|
||||||
|
for (i = 1; i < argc; i++)
|
||||||
|
{
|
||||||
|
Eina_Rectangle *r = input_rects + input_count;
|
||||||
|
char sx, sy;
|
||||||
|
|
||||||
|
if (sscanf(argv[i], "%dx%d%c%d%c%d",
|
||||||
|
&(r->w), &(r->h), &sx, &(r->x), &sy, &(r->y)) == 6)
|
||||||
|
{
|
||||||
|
if (sx == '-') r->x *= -1;
|
||||||
|
if (sy == '-') r->y *= -1;
|
||||||
|
|
||||||
|
if (maxw < r->x + r->w) maxw = r->x + r->w;
|
||||||
|
if (maxh < r->y + r->h) maxh = r->y + r->h;
|
||||||
|
input_count++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
fprintf(stderr, "ERROR: invalid rectangle ignored: %s\n", argv[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input_count == 0)
|
||||||
|
{
|
||||||
|
fputs("ERROR: Could not find any valid rectangle. Exit!\n", stderr);
|
||||||
|
usage(argv[0]);
|
||||||
|
free(input_rects);
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((maxw == 0) || (maxh == 0))
|
||||||
|
{
|
||||||
|
fputs("ERROR: All rectangles with size 0x0. Exit!\n", stderr);
|
||||||
|
usage(argv[0]);
|
||||||
|
free(input_rects);
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
|
||||||
|
ecore_evas_init();
|
||||||
|
ecore_init();
|
||||||
|
evas_init();
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
winw = 2 * maxw + 3 * WINDOW_PAD;
|
||||||
|
winh = maxh + 2 * WINDOW_PAD;
|
||||||
|
|
||||||
|
ee = ecore_evas_new(NULL, 0, 0, winw, winh, NULL);
|
||||||
|
if (!ee)
|
||||||
|
{
|
||||||
|
fputs("ERROR: Could not create window. Check ecore-evas install.\n",
|
||||||
|
stderr);
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
evas = ecore_evas_get(ee);
|
||||||
|
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
evas_object_color_set(o, 255, 255, 255, 255);
|
||||||
|
evas_object_resize(o, winw, winh);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
add_text("Input", WINDOW_PAD, 0, maxw);
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
evas_object_color_set(o, 200, 200, 200, 255);
|
||||||
|
evas_object_move(o, WINDOW_PAD, WINDOW_PAD);
|
||||||
|
evas_object_resize(o, maxw, maxh);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
add_text("Output", maxw + 2 * WINDOW_PAD, 0, maxw);
|
||||||
|
o = evas_object_rectangle_add(evas);
|
||||||
|
evas_object_color_set(o, 200, 200, 200, 255);
|
||||||
|
evas_object_move(o, maxw + 2 * WINDOW_PAD, WINDOW_PAD);
|
||||||
|
evas_object_resize(o, maxw, maxh);
|
||||||
|
evas_object_show(o);
|
||||||
|
|
||||||
|
tiler = eina_tiler_new(maxw, maxh);
|
||||||
|
ecore_timer_add(2.0, process_input, NULL);
|
||||||
|
|
||||||
|
ecore_evas_show(ee);
|
||||||
|
ecore_main_loop_begin();
|
||||||
|
|
||||||
|
eina_list_free(output_objs);
|
||||||
|
eina_tiler_free(tiler);
|
||||||
|
ecore_evas_free(ee);
|
||||||
|
|
||||||
|
end:
|
||||||
|
free(input_rects);
|
||||||
|
|
||||||
|
eina_shutdown();
|
||||||
|
evas_shutdown();
|
||||||
|
ecore_shutdown();
|
||||||
|
ecore_evas_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc eina_value_01.c -o eina_value_01 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_Value v;
|
||||||
|
int i;
|
||||||
|
char *newstr;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
eina_value_setup(&v, EINA_VALUE_TYPE_INT);
|
||||||
|
eina_value_set(&v, 123);
|
||||||
|
eina_value_get(&v, &i);
|
||||||
|
printf("v=%d\n", i);
|
||||||
|
|
||||||
|
newstr = eina_value_to_string(&v);
|
||||||
|
printf("v as string: %s\n", newstr);
|
||||||
|
free(newstr); // it was allocated by eina_value_to_string()
|
||||||
|
eina_value_flush(&v); // destroy v contents, will not use anymore
|
||||||
|
|
||||||
|
const char *s;
|
||||||
|
eina_value_setup(&v, EINA_VALUE_TYPE_STRING);
|
||||||
|
eina_value_set(&v, "My string");
|
||||||
|
eina_value_get(&v, &s);
|
||||||
|
printf("v=%s (pointer: %p)\n", s, s);
|
||||||
|
|
||||||
|
newstr = eina_value_to_string(&v);
|
||||||
|
printf("v as string: %s (pointer: %p)\n", newstr, newstr);
|
||||||
|
free(newstr); // it was allocated by eina_value_to_string()
|
||||||
|
eina_value_flush(&v); // destroy v contents, string 's' is not valid anymore!
|
||||||
|
|
||||||
|
Eina_Value otherv;
|
||||||
|
eina_value_setup(&otherv, EINA_VALUE_TYPE_STRING);
|
||||||
|
eina_value_setup(&v, EINA_VALUE_TYPE_INT);
|
||||||
|
|
||||||
|
// convert from int to string:
|
||||||
|
eina_value_set(&v, 123);
|
||||||
|
eina_value_convert(&v, &otherv);
|
||||||
|
eina_value_get(&otherv, &s);
|
||||||
|
printf("otherv=%s\n", s);
|
||||||
|
|
||||||
|
// and the other way around!
|
||||||
|
eina_value_set(&otherv, "33");
|
||||||
|
eina_value_convert(&otherv, &v);
|
||||||
|
eina_value_get(&v, &i);
|
||||||
|
printf("v=%d\n", i);
|
||||||
|
|
||||||
|
eina_value_flush(&otherv);
|
||||||
|
eina_value_flush(&v);
|
||||||
|
}
|
|
@ -0,0 +1,100 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc eina_value_02.c -o eina_value_02 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
static Eina_Value_Struct_Desc *V1_DESC = NULL;
|
||||||
|
static Eina_Value_Struct_Desc *V2_DESC = NULL;
|
||||||
|
|
||||||
|
void value_init(void)
|
||||||
|
{
|
||||||
|
typedef struct _My_Struct_V1 {
|
||||||
|
int param1;
|
||||||
|
char param2;
|
||||||
|
} My_Struct_V1;
|
||||||
|
|
||||||
|
|
||||||
|
static Eina_Value_Struct_Member v1_members[] = {
|
||||||
|
// no eina_value_type as they are not constant initializers, see below.
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V1, param1),
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V1, param2)
|
||||||
|
};
|
||||||
|
v1_members[0].type = EINA_VALUE_TYPE_INT;
|
||||||
|
v1_members[1].type = EINA_VALUE_TYPE_CHAR;
|
||||||
|
static Eina_Value_Struct_Desc v1_desc = {
|
||||||
|
EINA_VALUE_STRUCT_DESC_VERSION,
|
||||||
|
NULL, // no special operations
|
||||||
|
v1_members,
|
||||||
|
EINA_C_ARRAY_LENGTH(v1_members),
|
||||||
|
sizeof(My_Struct_V1)
|
||||||
|
};
|
||||||
|
V1_DESC = &v1_desc;
|
||||||
|
|
||||||
|
typedef struct _My_Struct_V2 {
|
||||||
|
int param1;
|
||||||
|
char param2;
|
||||||
|
int param3;
|
||||||
|
} My_Struct_V2;
|
||||||
|
static Eina_Value_Struct_Member v2_members[] = {
|
||||||
|
// no eina_value_type as they are not constant initializers, see below.
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param1),
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param2),
|
||||||
|
EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param3)
|
||||||
|
};
|
||||||
|
v2_members[0].type = EINA_VALUE_TYPE_INT;
|
||||||
|
v2_members[1].type = EINA_VALUE_TYPE_CHAR;
|
||||||
|
v2_members[2].type = EINA_VALUE_TYPE_INT;
|
||||||
|
static Eina_Value_Struct_Desc v2_desc = {
|
||||||
|
EINA_VALUE_STRUCT_DESC_VERSION,
|
||||||
|
NULL, // no special operations
|
||||||
|
v2_members,
|
||||||
|
EINA_C_ARRAY_LENGTH(v2_members),
|
||||||
|
sizeof(My_Struct_V2)
|
||||||
|
};
|
||||||
|
V2_DESC = &v2_desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rand_init(Eina_Value *v)
|
||||||
|
{
|
||||||
|
if (v->type != EINA_VALUE_TYPE_STRUCT)
|
||||||
|
return;
|
||||||
|
|
||||||
|
eina_value_struct_set(v, "param1", rand());
|
||||||
|
eina_value_struct_set(v, "param2", rand() % 256);
|
||||||
|
eina_value_struct_set(v, "param3", rand());
|
||||||
|
}
|
||||||
|
|
||||||
|
void my_struct_use(Eina_Value *params)
|
||||||
|
{
|
||||||
|
int p1, p3;
|
||||||
|
char p2;
|
||||||
|
|
||||||
|
eina_value_struct_get(params, "param1", &p1);
|
||||||
|
eina_value_struct_get(params, "param2", &p2);
|
||||||
|
printf("param1: %d\nparam2: %c\n", p1, p2);
|
||||||
|
|
||||||
|
if (eina_value_struct_get(params, "param3", &p3))
|
||||||
|
printf("param3: %d\n", p3);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_Value *v1, *v2;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
value_init();
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
v1 = eina_value_struct_new(V1_DESC);
|
||||||
|
v2 = eina_value_struct_new(V2_DESC);
|
||||||
|
|
||||||
|
rand_init(v1);
|
||||||
|
my_struct_use(v1);
|
||||||
|
|
||||||
|
rand_init(v2);
|
||||||
|
my_struct_use(v2);
|
||||||
|
|
||||||
|
eina_value_free(v1);
|
||||||
|
eina_value_free(v2);
|
||||||
|
eina_shutdown();
|
||||||
|
}
|
|
@ -0,0 +1,178 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc eina_value_03.c -o eina_value_03 `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <Eina.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_setup(const Eina_Value_Type *type, void *mem)
|
||||||
|
{
|
||||||
|
memset(mem, 0, type->value_size);
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_flush(const Eina_Value_Type *type, void *mem)
|
||||||
|
{
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_copy(const Eina_Value_Type *type, const void *src, void * dst)
|
||||||
|
{
|
||||||
|
struct timezone *tzsrc = src;
|
||||||
|
struct timezone *tzdst = dst;
|
||||||
|
*tzdst = *tzsrc;
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_compare(const Eina_Value_Type *type, const void *a, const void *b)
|
||||||
|
{
|
||||||
|
struct timezone tza = *(struct timezone*)a;
|
||||||
|
struct timezone tzb = *(struct timezone*)b;
|
||||||
|
|
||||||
|
if (tza.tz_minuteswest < tzb.tz_minuteswest)
|
||||||
|
return -1;
|
||||||
|
else if (tza.tz_minuteswest > tzb.tz_minuteswest)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_pset(const Eina_Value_Type *type, void *mem, const void *ptr)
|
||||||
|
{
|
||||||
|
*(struct timezone*)mem = *(struct timezone*)ptr;
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_vset(const Eina_Value_Type *type, void *mem, va_list args)
|
||||||
|
{
|
||||||
|
const struct timezone tz = va_arg(args, struct timezone);
|
||||||
|
return _tz_pset(type, mem, &tz);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_pget(const Eina_Value_Type *type, const void *mem, void *ptr)
|
||||||
|
{
|
||||||
|
memcpy(ptr, mem, type->value_size);
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_tz_convert_to(const Eina_Value_Type *type, const Eina_Value_Type *convert, const void *type_mem, void *convert_mem)
|
||||||
|
{
|
||||||
|
struct timezone v = *(struct timezone*)type_mem;
|
||||||
|
|
||||||
|
eina_error_set(0);
|
||||||
|
|
||||||
|
if (convert == EINA_VALUE_TYPE_UCHAR)
|
||||||
|
{
|
||||||
|
unsigned char other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_USHORT)
|
||||||
|
{
|
||||||
|
unsigned short other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_UINT)
|
||||||
|
{
|
||||||
|
unsigned int other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if ((convert == EINA_VALUE_TYPE_ULONG) || (convert == EINA_VALUE_TYPE_TIMESTAMP))
|
||||||
|
{
|
||||||
|
unsigned long other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_UINT64)
|
||||||
|
{
|
||||||
|
uint64_t other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_CHAR)
|
||||||
|
{
|
||||||
|
char other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_SHORT)
|
||||||
|
{
|
||||||
|
short other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_INT)
|
||||||
|
{
|
||||||
|
int other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_LONG)
|
||||||
|
{
|
||||||
|
long other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_INT64)
|
||||||
|
{
|
||||||
|
int64_t other_mem = v.tz_minuteswest;
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
else if (convert == EINA_VALUE_TYPE_FLOAT)
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &v.tz_minuteswest);
|
||||||
|
else if (convert == EINA_VALUE_TYPE_DOUBLE)
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &v.tz_minuteswest);
|
||||||
|
else if (convert == EINA_VALUE_TYPE_STRINGSHARE ||
|
||||||
|
convert == EINA_VALUE_TYPE_STRING)
|
||||||
|
{
|
||||||
|
const char *other_mem;
|
||||||
|
char buf[64];
|
||||||
|
snprintf(buf, sizeof(buf), "%d", v.tz_minuteswest);
|
||||||
|
other_mem = buf; /* required due &buf == buf */
|
||||||
|
return eina_value_type_pset(convert, convert_mem, &other_mem);
|
||||||
|
}
|
||||||
|
|
||||||
|
eina_error_set(EINA_ERROR_VALUE_FAILED);
|
||||||
|
return EINA_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Eina_Value_Type TZ_TYPE = {
|
||||||
|
EINA_VALUE_TYPE_VERSION,
|
||||||
|
sizeof(struct timezone),
|
||||||
|
"struct timezone",
|
||||||
|
_tz_setup,
|
||||||
|
_tz_flush,
|
||||||
|
_tz_copy,
|
||||||
|
_tz_compare,
|
||||||
|
_tz_convert_to,
|
||||||
|
NULL, //No convert from
|
||||||
|
_tz_vset,
|
||||||
|
_tz_pset,
|
||||||
|
_tz_pget
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Eina_Value vtv, vtz;
|
||||||
|
struct timeval tv;
|
||||||
|
struct timezone tz;
|
||||||
|
char *s;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
eina_value_setup(&vtv, EINA_VALUE_TYPE_TIMEVAL);
|
||||||
|
eina_value_setup(&vtz, &TZ_TYPE);
|
||||||
|
|
||||||
|
gettimeofday(&tv, &tz);
|
||||||
|
eina_value_set(&vtv, tv);
|
||||||
|
eina_value_set(&vtz, tz);
|
||||||
|
|
||||||
|
s = eina_value_to_string(&vtv);
|
||||||
|
printf("time: %s\n", s);
|
||||||
|
free(s);
|
||||||
|
s = eina_value_to_string(&vtz);
|
||||||
|
printf("timezone: %s\n", s);
|
||||||
|
free(s);
|
||||||
|
|
||||||
|
eina_value_flush(&vtz);
|
||||||
|
eina_value_flush(&vtv);
|
||||||
|
}
|
Loading…
Reference in New Issue