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; }
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_free(postfix, TRUE); return 0; }
static void read_sensor_labels(gchar * driver) { FILE *conf; gchar buf[256], *line, *p; gboolean lock = FALSE; gint i; sensor_labels = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); sensor_compute = g_hash_table_new(g_str_hash, g_str_equal); /* Try to open lm-sensors config file sensors3.conf */ conf = fopen("/etc/sensors3.conf", "r"); /* If it fails, try to open sensors.conf */ if (!conf) conf = fopen("/etc/sensors.conf", "r"); if (!conf) { /* Cannot open config file. */ return; } while (fgets(buf, 256, conf)) { line = buf; remove_linefeed(line); strend(line, '#'); if (*line == '\0') { continue; } else if (lock && strstr(line, "label")) { /* label lines */ gchar **names = g_strsplit(strstr(line, "label") + 5, " ", 0); gchar *name = NULL, *value = NULL; for (i = 0; names[i]; i++) { if (names[i][0] == '\0') continue; if (!name) name = g_strdup(names[i]); else if (!value) value = g_strdup(names[i]); else value = g_strconcat(value, " ", names[i], NULL); } remove_quotes(value); g_hash_table_insert(sensor_labels, name, value); g_strfreev(names); } else if (lock && strstr(line, "ignore")) { /* ignore lines */ p = strstr(line, "ignore") + 6; if (!strchr(p, ' ')) continue; while (*p == ' ') p++; g_hash_table_insert(sensor_labels, g_strdup(p), "ignore"); } else if (lock && strstr(line, "compute")) { /* compute lines */ gchar **formulas = g_strsplit(strstr(line, "compute") + 7, " ", 0); gchar *name = NULL, *formula = NULL; for (i = 0; formulas[i]; i++) { if (formulas[i][0] == '\0') continue; if (formulas[i][0] == ',') break; if (!name) name = g_strdup(formulas[i]); else if (!formula) formula = g_strdup(formulas[i]); else formula = g_strconcat(formula, formulas[i], NULL); } g_strfreev(formulas); g_hash_table_insert(sensor_compute, name, math_string_to_postfix(formula)); } else if (g_str_has_prefix(line, "chip")) { /* chip lines (delimiter) */ if (lock == FALSE) { gchar **chips = g_strsplit(line, " ", 0); for (i = 1; chips[i]; i++) { strend(chips[i], '*'); if (g_str_has_prefix(chips[i] + 1, driver)) { lock = TRUE; break; } } g_strfreev(chips); } else { break; } } } fclose(conf); }