summaryrefslogtreecommitdiff
path: root/parser.c
diff options
context:
space:
mode:
authorMichael J. Chudobiak <mjc@avtechpulse.com>2012-08-14 15:15:50 -0400
committerMichael J. Chudobiak <mjc@avtechpulse.com>2012-08-14 15:15:50 -0400
commite1eba453f807db61c55c9092710c9d1f1a358dda (patch)
tree8b03a4519454d70ee551a50c4edac4821d1b93f1 /parser.c
parent7f68698142507faecdf74e0a572f2e526b8ffcd3 (diff)
use g_str functions to extract units
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c74
1 files changed, 17 insertions, 57 deletions
diff --git a/parser.c b/parser.c
index fb01520..ce90c9f 100644
--- a/parser.c
+++ b/parser.c
@@ -19,7 +19,7 @@ static int process_float_param (char *parameter, float *value, float min_val, fl
static int check_channel_ok (int channel, int enabled_channels, char chankey);
static int Parser_id_word(char *id_me, int *channel, int *with_id_code);
static int Parser_find_commands(int commands[], int command_depth);
-static int Parser_get_unit(char *parameter,char *units);
+static int Parser_get_unit(char *parameter, char **units);
static int Parser_channel (int *channel,int with_id_code,int routine_num);
static int Go_freq_32_33(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Handle_Units(float *mult,char *units, char *base);
@@ -466,64 +466,27 @@ static int Parser_find_commands(int commands[], int command_depth)
}
-static int Parser_get_unit(char *parameter,char *units)
+static int Parser_get_unit(char *parameter, char **units)
{
/* this function takes a parameter like "1e+6 ms" and breaks it into parameter="1e+6" and
units="ms" */
- int i;
- int j;
- int units_present;
+ g_assert (*units == NULL);
- units_present=0;
- i=0;
+ gchar *end_ptr;
+ g_strtod (parameter, &end_ptr);
-
- 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<strlen(parameter)) && (isspace(parameter[i])) ) {
- for (j=i; j<strlen(parameter); ++j) {
- parameter[j]=parameter[j+1];
- }
- parameter[j]=0;
- }
-
- if (i < strlen(parameter)) {
- if ( (parameter[i]=='e' && parameter[i+1]=='-')
- || (parameter[i]=='e' && parameter[i+1]=='+') ) {
- for (i+=2; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {}
- } else if (parameter[i]=='e' && isdigit(parameter[i+1]) ) {
- for (i+=2; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {}
- }
- }
-
- /* suck out spaces */
- while ( (i<strlen(parameter)) && (isspace(parameter[i])) ) {
- for (j=i; j<strlen(parameter); ++j) {
- parameter[j]=parameter[j+1];
- }
- parameter[j]=0;
- }
-
- if (i != strlen(parameter)) {
- units_present=1;
- strcpy(units,parameter+i);
- parameter[i]=0;
-
- /* remove trailing spaces */
- for (j=strlen(units)-1; isspace(units[j]); --j) {
- units[j]=0;
- }
-
- }
+ if (end_ptr == parameter) {
+ *units = g_strdup ("");
+ } else {
+ *units = g_strdup (end_ptr);
+ end_ptr[0] = 0; // truncates parameter
}
+ g_strstrip (*units);
+ g_strstrip (parameter);
+
+ int units_present = (*units[0] != 0);
return units_present;
}
@@ -540,7 +503,7 @@ void Parser_main (char *raw_in, int interactive_terminal, void(*cbfunc)(gpointer
int space_found; /* if true, a space was found. parameter may follow */
int parameter_found; /* if true, there is a parameter at the end of the command string */
char parameter[max_input_word_length]; /* identified parameter */
- char units[max_input_word_length]; /* identified units */
+ char *units = NULL;
int semicolon_found; /* to break up lines */
int units_found; /* text found after parameter must be units */
int i; /* counter */
@@ -685,14 +648,10 @@ void Parser_main (char *raw_in, int interactive_terminal, void(*cbfunc)(gpointer
gchar *error_response = NULL;
if (parameter_found) {
- units_found = Parser_get_unit(parameter,units);
+ units_found = Parser_get_unit(parameter,&units);
}
if (!error_num) {
- if (!units_found) {
- units[0]='\0';
- }
- g_strstrip (parameter);
command_type=(is_query<<2) | (parameter_found?2:0) | units_found;
if (commands[0]!=0) {
@@ -746,6 +705,7 @@ void Parser_main (char *raw_in, int interactive_terminal, void(*cbfunc)(gpointer
(*cbfunc)(user_data, response);
}
+ g_free (units);
g_free (response);
g_free (error_response);