]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[script] Add string library and the first function "CharAt"
authorCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 24 Nov 2009 22:46:27 +0000 (22:46 +0000)
committerCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 24 Nov 2009 22:46:27 +0000 (22:46 +0000)
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

src/plugins/splash/script/Makefile.am
src/plugins/splash/script/plugin.c
src/plugins/splash/script/script-lib-string.c [new file with mode: 0644]
src/plugins/splash/script/script-lib-string.h [new file with mode: 0644]
src/plugins/splash/script/script-lib-string.script [new file with mode: 0644]

index 3fbe8356007eb869a9390a127e986e08f896f00d..c7615192126e76d7a6a3a71525c995506bf40651 100644 (file)
@@ -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'                                                  \
index 1989289cb236306ee7b26a880d7696135be5dfae..95487a0cec19eb680504376a79f88c9f0bc2e3dc 100644 (file)
@@ -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 <linux/kd.h>
 
@@ -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 (file)
index 0000000..c2cd3d0
--- /dev/null
@@ -0,0 +1,89 @@
+/* script-lib-string.c - string script functions library
+ *
+ * Copyright (C) 2009 Charlie Brej <cbrej@cs.man.ac.uk>
+ *
+ * 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 <cbrej@cs.man.ac.uk>
+ */
+#define _GNU_SOURCE
+#include "script.h"
+#include "script-parse.h"
+#include "script-execute.h"
+#include "script-object.h"
+#include "script-lib-string.h"
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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 (file)
index 0000000..1ea6db8
--- /dev/null
@@ -0,0 +1,35 @@
+/* script-lib-string.h - string script functions library
+ *
+ * Copyright (C) 2009 Charlie Brej <cbrej@cs.man.ac.uk>
+ *
+ * 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 <cbrej@cs.man.ac.uk>
+ */
+#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 (file)
index 0000000..4ce791e
--- /dev/null
@@ -0,0 +1,4 @@
+String |= fun(text)
+{
+  return text | global.String;
+};