From 80b991816acb29e645fa7e047325942f8f050861 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 31 Dec 1999 19:50:00 -0500 Subject: add new files --- string_utils.c | 237 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 string_utils.c (limited to 'string_utils.c') diff --git a/string_utils.c b/string_utils.c new file mode 100644 index 0000000..b5ec2c2 --- /dev/null +++ b/string_utils.c @@ -0,0 +1,237 @@ +/* START LIBRARY DESCRIPTION ********************************************* +FLOAT.LIB + Copyright (c) 2006, Avtech Electrosystems Ltd. + +DESCRIPTION: + Functions that deal with strings and floating point numbers. + +SUPPORT LIB'S: +END DESCRIPTION **********************************************************/ + + +#include +#include +#include +#include +#include "globals.h" +#include "string_utils.h" + +void Float_To_Text(int decimal_digits,float number_in, gchar ** text_out) +{ + if (fabs(number_in)<1.1*smallest_allowed_number) { + if (number_in<0.0) { + *text_out = g_strdup("-0.000000000"); + *text_out[decimal_digits+3]=0; + } else { + *text_out = g_strdup("0.000000000"); + *text_out[decimal_digits+2]=0; + } + } + + if(*text_out == NULL) *text_out = g_strdup_printf("%.*e", decimal_digits, number_in); + else g_sprintf (*text_out, "%.*e", decimal_digits, number_in); +} + + +int String_trim_excess_digits(char *parameter) +{ + /* this function takes a parameter like "1.2345678901234567890" and reduces it to "1.234567" */ + /* so that atof() will work properly */ + + gchar* new_string = g_strdup(parameter); + memset(new_string, 0, strlen(parameter)); + + int i; + int j; + int sig_digits; /* number of significant digits so far */ + long exp_power; /* append an exponent of this power */ + int sign; /* is the exponent negative? */ + int start_of_exponent; /* location of exponent */ + + i=0; /* location in input string */ + j=0; /* location in output string */ + sig_digits=0; + exp_power=0; + sign=NO; + +#define max_sig_dig 8 + + if (!(isdigit(parameter[0]) || parameter[0]=='+' || parameter[0]=='-' || parameter[0]=='.')) { + return OK; + } + + /* take care of sign */ + if (parameter[0]=='+' || parameter[0]=='-') { + new_string[j]=parameter[i]; + ++i; + ++j; + } + + /* leave in leading zeros */ + while (parameter[i]=='0') { + new_string[j]=parameter[i]; + ++i; + ++j; + } + + /* leave in up to 8 pre-decimal significant digits */ + while (isdigit(parameter[i]) && sig_digits0) { + strcat(new_string+j,"e"); + + //replaced itoa (non standard) with sprintf + char temp[64]; + memset(temp, 0, 64); + sprintf(temp, "%ld", exp_power); + + strcat(new_string,temp); + + strcpy(parameter,new_string); + if (exp_power<38) { + return OK; + } else { + return OutOfRange; + } + } + + /* leave in decimal point */ + if (parameter[i]=='.') { + new_string[j]=parameter[i]; + ++i; + ++j; + } + + /* leave in post-decimal zeros if no significant digits yet */ + while (parameter[i]=='0' && sig_digits==0) { + new_string[j]=parameter[i]; + ++i; + ++j; + } + + /* leave in up to 8 post-decimal significant digits */ + while (isdigit(parameter[i]) && sig_digits37 && sign==NO) { + g_free(new_string); + return OutOfRange; + } + if (exp_power>37 && sign==YES) { + strcpy(new_string,"0.0"); + } + } + + new_string[j]=0; + + strcpy(parameter,new_string); + g_free(new_string); + return OK; +} + +/*----------------------------------------------------------------------------------------------------------*/ +int String_is_it_numeric(char *parameter) +{ + /* this function takes a parameter like "1e+6" or "on" and determines if it is numeric or not */ + /* it is similar to the Parser_get_unit function */ + + int i; + int j; + int is_number; + + is_number=0; + i=0; + + + if (isdigit(parameter[0]) || parameter[0]=='+' || parameter[0] == '-' || parameter[0] == '.') { + for (i=1; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {} + + if (i < strlen(parameter)) + if ( parameter[i]=='.' ) + for (++i; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {} + + /* suck out spaces */ + while ( (i