From: Charlie Brej Date: Tue, 24 Nov 2009 22:46:27 +0000 (+0000) Subject: [script] Add string library and the first function "CharAt" X-Git-Tag: 0.8.0~107 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f11988e500b4ca7c45d6541c200c87b103098f8f;p=thirdparty%2Fplymouth.git [script] Add string library and the first function "CharAt" Currently the strings must be manually told to adopt from the library functions by using String("string").function(). This is temporary and later it should be possible to apply functions directly to strings. CharAt gives the a single character string at the index specified. The first character is at index zero. Characters beyon the end are empty strings, and errors return NULL. This can be used thusly: str = "something"; letter = String(str).CharAt(7); # letter = "n" letter = String(str).CharAt(12); # letter = "" letter = String(str).CharAt("foo"); # letter = NULL --- diff --git a/src/plugins/splash/script/Makefile.am b/src/plugins/splash/script/Makefile.am index 3fbe8356..c7615192 100644 --- a/src/plugins/splash/script/Makefile.am +++ b/src/plugins/splash/script/Makefile.am @@ -44,12 +44,19 @@ script_la_SOURCES = $(srcdir)/plugin.c \ $(srcdir)/script-lib-plymouth.script \ $(srcdir)/script-lib-math.c \ $(srcdir)/script-lib-math.h \ - $(srcdir)/script-lib-math.script + $(srcdir)/script-lib-math.script \ + $(srcdir)/script-lib-string.c \ + $(srcdir)/script-lib-string.h \ + $(srcdir)/script-lib-string.script MAINTAINERCLEANFILES = Makefile.in CLEANFILES = *.script.h -BUILT_SOURCES = script-lib-image.script.h script-lib-sprite.script.h script-lib-math.script.h script-lib-plymouth.script.h +BUILT_SOURCES = script-lib-image.script.h \ + script-lib-sprite.script.h \ + script-lib-plymouth.script.h \ + script-lib-math.script.h \ + script-lib-string.script.h %.script.h: %.script sed -e 's/\\/\\\\/g' \ diff --git a/src/plugins/splash/script/plugin.c b/src/plugins/splash/script/plugin.c index 1989289c..95487a0c 100644 --- a/src/plugins/splash/script/plugin.c +++ b/src/plugins/splash/script/plugin.c @@ -61,6 +61,7 @@ #include "script-lib-sprite.h" #include "script-lib-plymouth.h" #include "script-lib-math.h" +#include "script-lib-string.h" #include @@ -78,6 +79,7 @@ typedef struct script_lib_image_data_t *script_image_lib; script_lib_plymouth_data_t *script_plymouth_lib; script_lib_math_data_t *script_math_lib; + script_lib_string_data_t *script_string_lib; } view_t; struct _ply_boot_splash_plugin @@ -277,6 +279,7 @@ view_start_animation (view_t *view) view->script_plymouth_lib = script_lib_plymouth_setup (view->script_state, plugin->mode); view->script_math_lib = script_lib_math_setup (view->script_state); + view->script_string_lib = script_lib_string_setup (view->script_state); ply_trace ("executing script file"); script_return_t ret = script_execute (view->script_state, @@ -338,6 +341,7 @@ view_stop_animation (view_t *view) script_lib_image_destroy (view->script_image_lib); script_lib_plymouth_destroy (view->script_plymouth_lib); script_lib_math_destroy (view->script_math_lib); + script_lib_string_destroy (view->script_string_lib); } static void diff --git a/src/plugins/splash/script/script-lib-string.c b/src/plugins/splash/script/script-lib-string.c new file mode 100644 index 00000000..c2cd3d0c --- /dev/null +++ b/src/plugins/splash/script/script-lib-string.c @@ -0,0 +1,89 @@ +/* script-lib-string.c - string script functions library + * + * Copyright (C) 2009 Charlie Brej + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written by: Charlie Brej + */ +#define _GNU_SOURCE +#include "script.h" +#include "script-parse.h" +#include "script-execute.h" +#include "script-object.h" +#include "script-lib-string.h" +#include +#include +#include +#include + +#include "config.h" + +#include "script-lib-string.script.h" + + +static script_return_t script_lib_string_char_at (script_state_t *state, + void *user_data) +{ + char *text = script_obj_as_string (state->this); + int index = script_obj_hash_get_number (state->local, "index"); + int count; + char charstring [2]; + + if (!text || index < 0) + { + free (text); + return script_return_obj(script_obj_new_string ("sdfsd")); + } + for (count = 0; count < index; count++) + { + if (text[count] == '\0') + { + free (text); + return script_return_obj(script_obj_new_string ("sdfasdf asd")); + } + } + charstring[0] = text[index]; + charstring[1] = '\0'; + free (text); + return script_return_obj(script_obj_new_string (charstring)); +} + +script_lib_string_data_t *script_lib_string_setup (script_state_t *state) +{ + script_lib_string_data_t *data = malloc (sizeof (script_lib_string_data_t)); + + script_obj_t *string_hash = script_obj_hash_get_element (state->global, "String"); + script_add_native_function (string_hash, + "CharAt", + script_lib_string_char_at, + NULL, + "index", + NULL); + script_obj_unref (string_hash); + data->script_main_op = script_parse_string (script_lib_string_string, "script-lib-string.script"); + script_return_t ret = script_execute (state, data->script_main_op); + script_obj_unref (ret.object); + + return data; +} + +void script_lib_string_destroy (script_lib_string_data_t *data) +{ + script_parse_op_free (data->script_main_op); + free (data); +} + diff --git a/src/plugins/splash/script/script-lib-string.h b/src/plugins/splash/script/script-lib-string.h new file mode 100644 index 00000000..1ea6db89 --- /dev/null +++ b/src/plugins/splash/script/script-lib-string.h @@ -0,0 +1,35 @@ +/* script-lib-string.h - string script functions library + * + * Copyright (C) 2009 Charlie Brej + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written by: Charlie Brej + */ +#ifndef SCRIPT_LIB_STRING_H +#define SCRIPT_LIB_STRING_H + +#include "script.h" + +typedef struct +{ + script_op_t *script_main_op; +} script_lib_string_data_t; + +script_lib_string_data_t *script_lib_string_setup (script_state_t *state); +void script_lib_string_destroy (script_lib_string_data_t *data); + +#endif /* SCRIPT_LIB_STRING_H */ diff --git a/src/plugins/splash/script/script-lib-string.script b/src/plugins/splash/script/script-lib-string.script new file mode 100644 index 00000000..4ce791ec --- /dev/null +++ b/src/plugins/splash/script/script-lib-string.script @@ -0,0 +1,4 @@ +String |= fun(text) +{ + return text | global.String; +};