1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2                              McGill University
3 
4 Routine:
5   int STdecDfrac (const char String[], double *Dval1, double *Dval2)
6 
7 Purpose:
8   Decode a ratio specification of two double values
9 
10 Description:
11   This routine decodes a string specifying a ratio of double values.  The
12   ratio is specified in the form "V" or "V1/V2", for example "-23.1 / 45".
13   Optional white-space (as defined by isspace) can surround the values.  For
14   the case of a single value V, this is equivalent to the ratio "V/1".  If an
15   error is encountered, neither output value is set.
16 
17 Parameters:
18   <-  int STdecRfrac
19       Error status, 0 for no error, 1 for error
20    -> const char String[]
21       Input string
22   <-  double *Dval1
23       First value
24   <-  double *Dval2
25       Second value
26 
27 Author / revision:
28   P. Kabal  Copyright (C) 2003
29   $Revision: 1.7 $  $Date: 2003/05/09 03:02:44 $
30 
31 -------------------------------------------------------------------------*/
32 
33 #include <libtsp.h>
34 #include <libtsp/nucleus.h>
35 #include <libtsp/STmsg.h>
36 
37 #define MAXC	23
38 #define DP_EMPTY		0
39 #define DP_LVAL			1
40 #define DP_DELIM		2
41 #define DP_RVAL			4
42 
43 
44 int
STdecDfrac(const char String[],double * Dval1,double * Dval2)45 STdecDfrac (const char String[], double *Dval1, double *Dval2)
46 
47 {
48   int status;
49   double dval1, dval2;
50 
51   /* Decode the ratio values */
52   status = STdecPair (String, "/", 'D', (void *) (&dval1), (void *) (&dval2));
53 
54   if (status == DP_LVAL) {
55     *Dval1 = dval1;
56     *Dval2 = 1.0;
57   }
58   else if (status == (DP_LVAL + DP_DELIM + DP_RVAL)) {
59     *Dval1 = dval1;
60     *Dval2 = dval2;
61   }
62   else if (status >= 0) {
63     UTwarn ("STdecDfrac - %s: \"%s\"", STM_DataErr, STstrDots (String, MAXC));
64     status = -1;
65   }
66 
67   return (status < 0);
68 }
69