aboutsummaryrefslogtreecommitdiffstats
path: root/extension/ordchr.c
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2012-05-24 15:34:17 -0400
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2012-05-24 15:34:17 -0400
commit577c3fc31a2718461fba2e599d162de96fe838fa (patch)
tree1f45346053a5ae9db3c62761ef214a8a94095d30 /extension/ordchr.c
parentc62b9d773bc064bc1dd5d8db35207fd4e6d42f1e (diff)
downloadegawk-577c3fc31a2718461fba2e599d162de96fe838fa.tar.gz
egawk-577c3fc31a2718461fba2e599d162de96fe838fa.tar.bz2
egawk-577c3fc31a2718461fba2e599d162de96fe838fa.zip
First working version of new API mechanism (probably has memory leaks).
Diffstat (limited to 'extension/ordchr.c')
-rw-r--r--extension/ordchr.c70
1 files changed, 37 insertions, 33 deletions
diff --git a/extension/ordchr.c b/extension/ordchr.c
index 6abda181..95401650 100644
--- a/extension/ordchr.c
+++ b/extension/ordchr.c
@@ -28,71 +28,75 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
-#include "awk.h"
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "config.h"
+#include "gawkapi.h"
+
+static const gawk_api_t *api; /* for convenience macros to work */
+static awk_ext_id_t *ext_id;
int plugin_is_GPL_compatible;
/* do_ord --- return numeric value of first char of string */
-static NODE *
-do_ord(int nargs)
+static awk_value_t *
+do_ord(int nargs, awk_value_t *result)
{
- NODE *str;
- int ret = -1;
+ awk_value_t str;
+ double ret = -1;
if (do_lint && nargs > 1)
- lintwarn("ord: called with too many arguments");
+ lintwarn(ext_id, "ord: called with too many arguments");
- str = get_scalar_argument(0, false);
- if (str != NULL) {
- (void) force_string(str);
- ret = str->stptr[0];
+ if (get_curfunc_param(0, AWK_STRING, &str) != NULL) {
+ ret = str.str_value.str[0];
} else if (do_lint)
- lintwarn("ord: called with no arguments");
-
+ lintwarn(ext_id, "ord: called with no arguments");
/* Set the return value */
- return make_number((AWKNUM) ret);
+ return make_number(ret, result);
}
/* do_chr --- turn numeric value into a string */
-static NODE *
-do_chr(int nargs)
+static awk_value_t *
+do_chr(int nargs, awk_value_t *result)
{
- NODE *num;
+ awk_value_t num;
unsigned int ret = 0;
- AWKNUM val = 0.0;
+ double val = 0.0;
char str[2];
str[0] = str[1] = '\0';
if (do_lint && nargs > 1)
- lintwarn("chr: called with too many arguments");
+ lintwarn(ext_id, "chr: called with too many arguments");
- num = get_scalar_argument(0, false);
- if (num != NULL) {
- val = get_number_d(num);
+ if (get_curfunc_param(0, AWK_NUMBER, &num) != NULL) {
+ val = num.num_value;
ret = val; /* convert to int */
ret &= 0xff;
str[0] = ret;
str[1] = '\0';
} else if (do_lint)
- lintwarn("chr: called with no arguments");
+ lintwarn(ext_id, "chr: called with no arguments");
/* Set the return value */
- return make_string(str, 1);
+ return dup_string(str, 1, result);
}
-/* dlload --- load new builtins in this library */
+static awk_ext_func_t func_table[] = {
+ { "ord", do_ord, 1 },
+ { "chr", do_chr, 1 },
+};
-NODE *
-dlload(tree, dl)
-NODE *tree;
-void *dl;
-{
- make_builtin("ord", do_ord, 1);
- make_builtin("chr", do_chr, 1);
+/* define the dl_load function using the boilerplate macro */
- return make_number((AWKNUM) 0);
-}
+dl_load_func(func_table, ord_chr, "")