From cdc40b11f4d7a3050bda164b2d92afc23616b7f9 Mon Sep 17 00:00:00 2001 From: "Leandro A. F. Pereira" Date: Thu, 5 Jul 2007 19:21:21 +0000 Subject: Cleanups. Plug most of the memleaks. --- hardinfo2/expr.c | 62 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'hardinfo2/expr.c') diff --git a/hardinfo2/expr.c b/hardinfo2/expr.c index 3f4707f5..d204859d 100644 --- a/hardinfo2/expr.c +++ b/hardinfo2/expr.c @@ -14,7 +14,7 @@ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ + */ /* * This is only used to compute sensor values, hence the only variable supported is '@'. * The '`' operator (ln(x)) is not available, nor multi-line formulas. @@ -71,11 +71,11 @@ static inline gint priority(char operation) case '(': return 0; } - + return 0; } -GSList *math_infix_to_postfix(GSList *infix) +GSList *math_infix_to_postfix(GSList * infix) { MathToken *stack[500]; gint t_sp = 0; @@ -89,7 +89,8 @@ GSList *math_infix_to_postfix(GSList *infix) if (t->type == TOKEN_OPERATOR && t->val.op == '(') { stack[++t_sp] = t; } else if (t->type == TOKEN_OPERATOR && t->val.op == ')') { - for (top = stack[t_sp]; t_sp != 0 && top->val.op != '('; top = stack[t_sp]) + for (top = stack[t_sp]; t_sp != 0 && top->val.op != '('; + top = stack[t_sp]) postfix = g_slist_append(postfix, stack[t_sp--]); t_sp--; } else if (t->type != TOKEN_OPERATOR) { @@ -97,7 +98,8 @@ GSList *math_infix_to_postfix(GSList *infix) } else if (t_sp == 0) { stack[++t_sp] = t; } else { - while (t_sp != 0 && priority(t->val.op) <= priority(stack[t_sp]->val.op)) + while (t_sp != 0 + && priority(t->val.op) <= priority(stack[t_sp]->val.op)) postfix = g_slist_append(postfix, stack[t_sp--]); stack[++t_sp] = t; } @@ -113,7 +115,7 @@ static inline gfloat __result(gfloat op1, gfloat op2, gchar operation) { switch (operation) { case '^': - return powf(op1, op2); + return powf(op1, op2); case '+': return op1 + op2; case '-': @@ -123,18 +125,18 @@ static inline gfloat __result(gfloat op1, gfloat op2, gchar operation) case '*': return op1 * op2; } - + return 0; } -gfloat math_postfix_eval(GSList *postfix, gfloat at_value) +gfloat math_postfix_eval(GSList * postfix, gfloat at_value) { GSList *p; gfloat stack[500]; gint sp = 0; - + memset(stack, 0, sizeof(gfloat) * 500); - + for (p = postfix; p; p = p->next) { MathToken *t = (MathToken *) p->data; @@ -155,7 +157,7 @@ gfloat math_postfix_eval(GSList *postfix, gfloat at_value) return stack[sp]; } -GSList *math_string_to_infix(gchar *string) +GSList *math_string_to_infix(gchar * string) { GSList *infix = NULL; gchar *expr = string; @@ -180,43 +182,43 @@ GSList *math_string_to_infix(gchar *string) return infix; } -void math_infix_free(GSList *infix, gboolean free_tokens) +void math_infix_free(GSList * infix, gboolean free_tokens) { GSList *p; - + if (!free_tokens) - for (p = infix; p; p = g_slist_delete_link(p, p)); - else - for (p = infix; p; p = g_slist_delete_link(p, p)) { - MathToken *t = (MathToken *)p->data; - g_free(t); - } + for (p = infix; p; p = g_slist_delete_link(p, p)); + else + for (p = infix; p; p = g_slist_delete_link(p, p)) { + MathToken *t = (MathToken *) p->data; + g_free(t); + } } -GSList *math_string_to_postfix(gchar *string) +GSList *math_string_to_postfix(gchar * string) { GSList *infix; GSList *postfix; - + infix = math_string_to_infix(string); if (!infix) - return NULL; - + return NULL; + postfix = math_infix_to_postfix(infix); math_infix_free(infix, FALSE); - + return postfix; } -gfloat math_string_eval(gchar *string, gfloat at_value) +gfloat math_string_eval(gchar * string, gfloat at_value) { GSList *postfix; gfloat val; - + postfix = math_string_to_postfix(string); val = math_postfix_eval(postfix, at_value); math_postfix_free(postfix, TRUE); - + return val; } @@ -224,14 +226,14 @@ gfloat math_string_eval(gchar *string, gfloat at_value) int main(void) { GSList *postfix; - + gchar *expr = "0.9*(@+(5.2*0.923+3*(2.0)))"; postfix = math_string_to_postfix(expr); g_print("%s = %f (must be 18.71964)\n", expr, - math_postfix_eval(postfix, 10)); + math_postfix_eval(postfix, 10)); math_postfix_free(postfix, TRUE); - + return 0; } #endif -- cgit v1.2.3