diff options
author | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-30 13:03:42 -0400 |
---|---|---|
committer | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-30 13:03:42 -0400 |
commit | f68e6dc228980e2d980c6774cda3db120bd5e17b (patch) | |
tree | 816fb3775ba360f57985dcfa68cf587b9a177f82 /string_utils.c | |
parent | 54235051d6a0e46afa8314824fcec3828f90d6f3 (diff) |
try removing some fixed-length strings
Diffstat (limited to 'string_utils.c')
-rw-r--r-- | string_utils.c | 68 |
1 files changed, 32 insertions, 36 deletions
diff --git a/string_utils.c b/string_utils.c index 382cf30..e3cb4f8 100644 --- a/string_utils.c +++ b/string_utils.c @@ -43,12 +43,10 @@ void String_Parameter_To_Text(float Float_To_Convert, int significant_digits, char *start_string,char *units,char *LCD_string,int show_plus_sign) { // FIXME - crappy string func - char out_val[LCD_col_width+1]; gchar *floating_val = NULL; - char String_of_spaces[LCD_col_width+1]; - char unit_mult[10]; /* units multiplier, eg. M, k, u */ + gchar *unit_mult = NULL; /* units multiplier, eg. M, k, u */ - int i; /* just a counter */ + int i; /* just a counter */ 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 chars_processed; /* how many characters are in the parsed number string so far */ @@ -64,26 +62,31 @@ void String_Parameter_To_Text(float Float_To_Convert, int significant_digits, /* if significant_digits is zero, used the supplied integer rather than the floating number */ if (!significant_digits) { - sprintf(out_val, "%d", (int) Float_To_Convert); + gchar *out_val; + out_val = g_strdup_printf ("%d", (int) Float_To_Convert); strcat(LCD_string,out_val); + g_free (out_val); } else { + GString *out_gstr = g_string_new (""); + Float_To_Text(remote_digits_after_decimal,Float_To_Convert,&floating_val); /* -- COPY FIRST ONE OR TWO CHARACTERS -- */ if (floating_val[0]=='-') { /* if it's negative ... */ - decimal_location=2; /* decimal at position two (e.g.: -2.23e-9) */ - chars_processed=2; /* two characters so far (e.g.: -2) */ - out_val[0]='-'; /* copy minus sign */ - out_val[1]=floating_val[1]; /* copy first digit */ + decimal_location=2; /* decimal at position two (e.g.: -2.23e-9) */ + chars_processed=2; /* two characters so far (e.g.: -2) */ + out_gstr = g_string_append_c (out_gstr, '-'); /* copy minus sign */ + out_gstr = g_string_append_c (out_gstr, floating_val[1]); /* copy first digit */ + } else if (show_plus_sign==YES) { /* if it's positive and plus sign required ... */ decimal_location=1; /* decimal at position one (e.g.: 2.23e-9) */ chars_processed=2; /* two characters so far (e.g.: +2) */ - out_val[0]='+'; /* add plus sign */ - out_val[1]=floating_val[0]; /* copy first digit */ + out_gstr = g_string_append_c (out_gstr, '+'); /* copy minus sign */ + out_gstr = g_string_append_c (out_gstr, floating_val[0]); /* copy first digit */ } else { /* if it's positive and plus sign not required ... */ decimal_location=1; /* decimal at position one (e.g.: 2.23e-9) */ chars_processed=1; /* one character so far (e.g.: 2) */ - out_val[0]=floating_val[0]; /* copy first digit */ + out_gstr = g_string_append_c (out_gstr, floating_val[0]); /* copy first digit */ } /* -- FIND EXPONENT -- */ @@ -96,57 +99,49 @@ void String_Parameter_To_Text(float Float_To_Convert, int significant_digits, /* -- PICK UNITS -- */ if (exponent_val<12 && exponent_val>=9) { - strcpy(unit_mult,"G"); + unit_mult = g_strdup("G"); } else if (exponent_val<9 && exponent_val>=6) { - strcpy(unit_mult,"M"); + unit_mult = g_strdup("M"); } else if (exponent_val<6 && exponent_val>=3) { - strcpy(unit_mult,"k"); + unit_mult = g_strdup("k"); } else if (exponent_val<3 && exponent_val>=0) { - strcpy(unit_mult,""); + unit_mult = g_strdup(""); } else if (exponent_val<0 && exponent_val>=-3) { - strcpy(unit_mult,"m"); + unit_mult = g_strdup("m"); } else if (exponent_val<-3 && exponent_val>=-6) { - strcpy(unit_mult,"u"); + unit_mult = g_strdup("u"); } else if (exponent_val<-6 && exponent_val>=-9) { - strcpy(unit_mult,"n"); + unit_mult = g_strdup("n"); } else if (exponent_val<-9 && exponent_val>=-12) { - strcpy(unit_mult,"p"); - } - - if (exponent_val<-12) { - strcpy(unit_mult,""); /* if parameter=0, don't use silly units */ + unit_mult = g_strdup("p"); + } else { + unit_mult = g_strdup(""); /* if parameter=0, don't use silly units */ } /* -- MOVE DIGITS AROUND DECIMAL POINT -- */ /* move the digits that will come before the decimal */ for (i=decimal_location; i<decimal_location+shift_decimal_by; ++i) { - out_val[chars_processed]=floating_val[1+i]; + out_gstr = g_string_append_c (out_gstr, floating_val[1+i]); ++chars_processed; } /* put in the new decimal point */ - out_val[chars_processed]='.'; + out_gstr = g_string_append_c (out_gstr, '.'); ++chars_processed; /* copy the rest of the digits */ for (i=shift_decimal_by+1+decimal_location; i<significant_digits+3; ++i) { - out_val[chars_processed]=floating_val[i]; + out_gstr = g_string_append_c (out_gstr, floating_val[i]); ++chars_processed; /* leave space for minus sign, decimal point, and extra digit on the end too */ } - /* -- TERMINATE STRING -- */ - if ((show_plus_sign==YES) || (floating_val[0]=='-')) { - out_val[significant_digits+3]=0; - } else { - out_val[significant_digits+2]=0; - } - - /* -- FINISH UP -- */ - strcat(LCD_string,out_val); + strcat(LCD_string,out_gstr->str); + g_string_free (out_gstr, TRUE); + LCD_string[strlen(LCD_string)-1]=0; /* remove extra digit */ /* -- CHECK FOR TERMINATING DECIMAL POINT -- */ @@ -160,5 +155,6 @@ void String_Parameter_To_Text(float Float_To_Convert, int significant_digits, strcat(LCD_string,units); g_free (floating_val); + g_free (unit_mult); } |