summaryrefslogtreecommitdiff
path: root/src/tibvar.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tibvar.c')
-rw-r--r--src/tibvar.c182
1 files changed, 91 insertions, 91 deletions
diff --git a/src/tibvar.c b/src/tibvar.c
index f25061a..c3a3fae 100644
--- a/src/tibvar.c
+++ b/src/tibvar.c
@@ -1,6 +1,6 @@
/*
* libtib - Read, write, and evaluate TI BASIC programs
- * Copyright (C) 2015 Delwink, LLC
+ * Copyright (C) 2015, 2017 Delwink, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@@ -24,127 +24,127 @@
struct varlist
{
- tib_Variable *vars;
- int len;
+ tib_Variable *vars;
+ int len;
};
-static struct varlist varlist =
- {
- .len = 0,
- .vars = NULL
- };
+static struct varlist varlist = {
+ .len = 0,
+ .vars = NULL
+};
int
-tib_var_init ()
+tib_var_init()
{
- int rc;
- TIB *t;
-
-#define ADD(K,V) \
- { \
- t = (V); \
- if (t) \
- { \
- rc = tib_var_set ((K), t); \
- tib_decref (t); \
- } \
- else \
- { \
- rc = tib_errno; \
- } \
- \
- if (rc) \
- goto end; \
- }
-
- ADD ('e', tib_new_complex (2.718281828459045235360287471352662498, 0));
- ADD (TIB_CHAR_PI,
- tib_new_complex (3.141592653589793238462643383279502884, 0));
+ int rc;
+ TIB *t;
+
+#define ADD(K,V) \
+ { \
+ t = (V); \
+ if (t) \
+ { \
+ rc = tib_var_set((K), t); \
+ tib_decref(t); \
+ } \
+ else \
+ { \
+ rc = tib_errno; \
+ } \
+ \
+ if (rc) \
+ goto end; \
+ }
+
+ ADD('e', tib_new_complex(2.718281828459045235360287471352662498, 0));
+ ADD(TIB_CHAR_PI,
+ tib_new_complex(3.141592653589793238462643383279502884, 0));
#undef ADD
end:
- return rc;
+ return rc;
}
void
-tib_var_free ()
+tib_var_free()
{
- for (int i = 0; i < varlist.len; ++i)
- tib_decref (varlist.vars[i].value);
+ for (int i = 0; i < varlist.len; ++i)
+ tib_decref(varlist.vars[i].value);
- free (varlist.vars);
+ free(varlist.vars);
- varlist.len = 0;
- varlist.vars = NULL;
+ varlist.len = 0;
+ varlist.vars = NULL;
}
static int
-add_var (int key, const TIB *value)
+add_var(int key, const TIB *value)
{
- tib_Variable *old = varlist.vars;
-
- ++varlist.len;
- varlist.vars = realloc (varlist.vars, varlist.len * sizeof (tib_Variable));
- if (NULL == varlist.vars)
- {
- varlist.vars = old;
- --varlist.len;
- return TIB_EALLOC;
- }
-
- tib_errno = 0;
- tib_Variable *new = varlist.vars + varlist.len - 1;
- new->key = key;
- new->value = tib_copy (value);
- if (tib_errno)
- --varlist.len;
-
- return tib_errno;
+ tib_Variable *old = varlist.vars;
+
+ ++varlist.len;
+ varlist.vars = realloc(varlist.vars,
+ varlist.len * sizeof(tib_Variable));
+ if (NULL == varlist.vars)
+ {
+ varlist.vars = old;
+ --varlist.len;
+ return TIB_EALLOC;
+ }
+
+ tib_errno = 0;
+ tib_Variable *new = varlist.vars + varlist.len - 1;
+ new->key = key;
+ new->value = tib_copy(value);
+ if (tib_errno)
+ --varlist.len;
+
+ return tib_errno;
}
int
-tib_var_set (int key, const TIB *value)
+tib_var_set(int key, const TIB *value)
{
- if (!tib_is_var (key))
- return add_var (key, value);
-
- for (int i = 0; i < varlist.len; ++i)
- {
- if (key == varlist.vars[i].key)
- {
- TIB *old = varlist.vars[i].value;
- varlist.vars[i].value = tib_copy (value);
- if (tib_errno)
- {
- varlist.vars[i].value = old;
- return tib_errno;
- }
-
- tib_decref (old);
- return 0;
- }
- }
-
- return TIB_EINDEX; /* should be unreachable */
+ if (!tib_is_var(key))
+ return add_var(key, value);
+
+ for (int i = 0; i < varlist.len; ++i)
+ {
+ if (key == varlist.vars[i].key)
+ {
+ TIB *old = varlist.vars[i].value;
+ varlist.vars[i].value = tib_copy(value);
+ if (tib_errno)
+ {
+ varlist.vars[i].value = old;
+ return tib_errno;
+ }
+
+ tib_decref(old);
+ return 0;
+ }
+ }
+
+ return TIB_EINDEX; // should be unreachable
}
TIB *
-tib_var_get (int key)
+tib_var_get(int key)
{
- for (int i = 0; i < varlist.len; ++i)
- if (key == varlist.vars[i].key)
- return tib_copy (varlist.vars[i].value);
+ for (int i = 0; i < varlist.len; ++i)
+ if (key == varlist.vars[i].key)
+ return tib_copy(varlist.vars[i].value);
- return tib_new_complex (0, 0);
+ return tib_new_complex(0, 0);
}
bool
-tib_is_var (int key)
+tib_is_var(int key)
{
- for (int i = 0; i < varlist.len; ++i)
- if (key == varlist.vars[i].key)
- return true;
+ for (int i = 0; i < varlist.len; ++i)
+ if (key == varlist.vars[i].key)
+ return true;
- return false;
+ return false;
}