prepare for the release of rrdtool-1.2.9
[rrdtool.git] / src / rrd_diff.c
1 /*****************************************************************************
2  * RRDtool 1.2.9  Copyright by Tobi Oetiker, 1997-2005
3  * This code is stolen from rateup (mrtg-2.x) by Dave Rand
4  *****************************************************************************
5  * diff calculate the difference between two very long integers available as
6  *      strings
7  *****************************************************************************
8  * $Id$
9  * $Log$
10  * Revision 1.4  2003/03/10 00:30:34  oetiker
11  * handle cases with two negative numbers
12  * --  Sasha Mikheev <sasha@avalon-net.co.il>
13  *
14  * Revision 1.3  2002/04/01 18:31:22  oetiker
15  * "!" takes a higher preference than "||" this means rrd_update N:: would
16  * segfault -- Oliver Cook <ollie@uk.clara.net>
17  *
18  * Revision 1.2  2002/02/01 20:34:49  oetiker
19  * fixed version number and date/time
20  *
21  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
22  * checkin
23  *
24  * Revision 1.1  1998/10/08 18:21:45  oetiker
25  * Initial revision
26  *
27  * Revision 1.3  1998/02/06 21:10:52  oetiker
28  * removed max define .. it is now in rrd_tool.h
29  *
30  * Revision 1.2  1997/12/07 20:38:03  oetiker
31  * ansified
32  *
33  * Revision 1.1  1997/11/28 23:31:59  oetiker
34  * Initial revision
35  *
36  *****************************************************************************/
37
38 #include "rrd_tool.h"
39
40 double
41 rrd_diff(char *a, char *b)
42 {
43     char res[LAST_DS_LEN+1], *a1, *b1, *r1, *fix;
44     int c,x,m;
45     char a_neg=0, b_neg=0;
46     double result;
47    
48     while (!(isdigit((int)*a) || *a==0)) {
49         if(*a=='-') 
50             a_neg = 1;
51         a++;
52     }
53     fix=a;
54     while (isdigit((int)*fix)) 
55         fix++;
56     *fix = 0; /* maybe there is some non digit data in the string */ 
57     while (!(isdigit((int)*b) || *b==0)) {
58         if(*b=='-') 
59             b_neg = 1;  
60         b++;
61     }
62     fix=b;
63     while (isdigit((int)*fix)) 
64         fix++;
65     *fix = 0; /* maybe there is some non digit data in the string */ 
66     if(!isdigit((int)*a) || !isdigit((int)*b))
67         return DNAN;
68     if(a_neg+b_neg == 1) /* can not handle numbers with different signs yet */
69         return DNAN;
70     a1 = &a[strlen(a)-1];
71     m = max(strlen(a),strlen(b));
72     if (m > LAST_DS_LEN) return DNAN; /* result string too short */
73
74     r1 = &res[m+1];
75     for (b1 = res;b1 <= r1; b1++) *b1 = ' ';
76     b1 = &b[strlen(b)-1];
77     r1[1] = 0;  /* Null terminate result */
78     c = 0;
79     for (x=0; x<m; x++) {
80         if (a1 >= a && b1 >= b) {
81             *r1 = ((*a1 - c) - *b1) + '0';
82         } else if (a1 >= a) {
83             *r1 = (*a1 - c);
84         } else {
85             *r1 = ('0' - *b1 - c) + '0';
86         }
87         if (*r1 < '0') {
88             *r1 += 10;
89             c=1;
90         } else
91           if (*r1 > '9') { /* 0 - 10 */
92             *r1 -= 10;
93             c=1;            
94           } else {
95             c=0;
96         }
97         a1--;b1--;r1--;
98     }
99     if (c) {
100         r1 = &res[m+1];
101         for (x=0; isdigit((int)*r1) && x<m; x++,r1--)  {
102             *r1 = ('9' - *r1 + c) + '0';
103             if (*r1 > '9') {
104                 *r1 -= 10;
105                 c=1;
106             } else {
107                 c=0;
108             }
109         }
110         result = -atof(res);
111     } else
112         result = atof(res);
113
114     if(a_neg+b_neg==2) /* both are negatives, reverse sign */
115         result = -result;
116     
117     return result;
118 }