summaryrefslogtreecommitdiff
path: root/string_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'string_utils.c')
-rw-r--r--string_utils.c47
1 files changed, 30 insertions, 17 deletions
diff --git a/string_utils.c b/string_utils.c
index bf17e50..77765d2 100644
--- a/string_utils.c
+++ b/string_utils.c
@@ -5,8 +5,6 @@
#include "globals.h"
#include "string_utils.h"
-#include "lcd.h" // FIXME
-
void Float_To_Text(int decimal_digits,float number_in, gchar ** text_out)
{
@@ -40,7 +38,7 @@ gboolean String_is_it_numeric(char *parameter)
void String_Parameter_To_Text(float Float_To_Convert, int significant_digits,
- char *start_string,char *units,char *LCD_string,int show_plus_sign)
+ char *start_string,char *units,gchar **LCD_string,int show_plus_sign, int width_of_column)
{
gchar *floating_val = NULL;
gchar *unit_mult = NULL; /* units multiplier, eg. M, k, u */
@@ -49,23 +47,18 @@ void String_Parameter_To_Text(float Float_To_Convert, int significant_digits,
/* is annoying. (e.g. 1.000 -> 0.999) */
/* Move the decimal with string manipulations instead. */
- strcpy(LCD_string,start_string);
+ GString *out_gstr = g_string_new (start_string);
/* if significant_digits is zero, used the supplied integer rather than the floating number */
if (!significant_digits) {
- gchar *out_val;
- out_val = g_strdup_printf ("%d", (int) Float_To_Convert);
- strcat(LCD_string,out_val);
- g_free (out_val);
+ g_string_append_printf (out_gstr, "%d", (int) Float_To_Convert);
} else {
int i;
int shift_decimal_by; /* if the exponent isn't a multiple of 3, the decimal point will be moved */
int decimal_location; /* where the decimal is in the number string */
int exponent_val; /* the exponent, in integer form */
- GString *out_gstr = g_string_new ("");
-
Float_To_Text(remote_digits_after_decimal,Float_To_Convert,&floating_val);
/* -- COPY FIRST ONE OR TWO CHARACTERS -- */
@@ -125,29 +118,49 @@ void String_Parameter_To_Text(float Float_To_Convert, int significant_digits,
/* put in the new decimal point */
out_gstr = g_string_append_c (out_gstr, '.');
+ int space_left = width_of_column - strlen(out_gstr->str) - strlen (unit_mult) - strlen (units);
+
/* copy the rest of the digits */
- for (i=shift_decimal_by+1+decimal_location; digits_so_far<significant_digits; ++i) {
+ for (i=shift_decimal_by+1+decimal_location; (digits_so_far<significant_digits) && (space_left>0); ++i) {
out_gstr = g_string_append_c (out_gstr, floating_val[i]);
++digits_so_far;
+ space_left = width_of_column - strlen(out_gstr->str) - strlen (unit_mult) - strlen (units);
/* leave space for minus sign, decimal point, and extra digit on the end too */
}
/* -- FINISH UP -- */
- strcat(LCD_string,out_gstr->str);
- g_string_free (out_gstr, TRUE);
/* -- CHECK FOR TERMINATING DECIMAL POINT -- */
- if (LCD_string[strlen(LCD_string)-1]=='.') {
- LCD_string[strlen(LCD_string)-1]=0;
+ int len = strlen(out_gstr->str);
+ if (out_gstr->str[len-1]=='.') {
+ out_gstr = g_string_erase (out_gstr, len-1, 1);
}
- strcat(LCD_string,unit_mult);
+ out_gstr = g_string_append (out_gstr, unit_mult);
}
- strcat(LCD_string,units);
+ out_gstr = g_string_append (out_gstr, units);
+ *LCD_string = g_strdup (out_gstr->str);
+
+ g_string_free (out_gstr, TRUE);
g_free (floating_val);
g_free (unit_mult);
}
+
+gchar* conditional_regex_replace (gboolean do_it, gchar* in_string, gchar* regex_string, gchar* replace_with)
+{
+ gchar *out;
+
+ if (do_it) {
+ GRegex *regex = g_regex_new (regex_string, 0, 0, NULL);
+ out = g_regex_replace_literal (regex, in_string, -1, 0, replace_with, 0, NULL);
+ g_regex_unref (regex);
+ } else {
+ out = g_strdup (in_string);
+ }
+
+ return out;
+}