prepare for the release of rrdtool-1.3.0
[rrdtool.git] / src / rrd_diff.c
1 /*****************************************************************************
2  * RRDtool 1.3.0  Copyright by Tobi Oetiker, 1997-2008
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 rrd_diff(
41     char *a,
42     char *b)
43 {
44     char      res[LAST_DS_LEN + 1], *a1, *b1, *r1, *fix;
45     int       c, x, m;
46     char      a_neg = 0, b_neg = 0;
47     double    result;
48
49     while (!(isdigit((int) *a) || *a == 0)) {
50         if (*a == '-')
51             a_neg = 1;
52         a++;
53     }
54     fix = a;
55     while (isdigit((int) *fix))
56         fix++;
57     *fix = 0;           /* maybe there is some non digit data in the string */
58     while (!(isdigit((int) *b) || *b == 0)) {
59         if (*b == '-')
60             b_neg = 1;
61         b++;
62     }
63     fix = b;
64     while (isdigit((int) *fix))
65         fix++;
66     *fix = 0;           /* maybe there is some non digit data in the string */
67     if (!isdigit((int) *a) || !isdigit((int) *b))
68         return DNAN;
69     if (a_neg + b_neg == 1) /* can not handle numbers with different signs yet */
70         return DNAN;
71     a1 = &a[strlen(a) - 1];
72     m = max(strlen(a), strlen(b));
73     if (m > LAST_DS_LEN)
74         return DNAN;    /* result string too short */
75
76     r1 = &res[m + 1];
77     for (b1 = res; b1 <= r1; b1++)
78         *b1 = ' ';
79     b1 = &b[strlen(b) - 1];
80     r1[1] = 0;          /* Null terminate result */
81     c = 0;
82     for (x = 0; x < m; x++) {
83         if (a1 >= a && b1 >= b) {
84             *r1 = ((*a1 - c) - *b1) + '0';
85         } else if (a1 >= a) {
86             *r1 = (*a1 - c);
87         } else {
88             *r1 = ('0' - *b1 - c) + '0';
89         }
90         if (*r1 < '0') {
91             *r1 += 10;
92             c = 1;
93         } else if (*r1 > '9') { /* 0 - 10 */
94             *r1 -= 10;
95             c = 1;
96         } else {
97             c = 0;
98         }
99         a1--;
100         b1--;
101         r1--;
102     }
103     if (c) {
104         r1 = &res[m + 1];
105         for (x = 0; isdigit((int) *r1) && x < m; x++, r1--) {
106             *r1 = ('9' - *r1 + c) + '0';
107             if (*r1 > '9') {
108                 *r1 -= 10;
109                 c = 1;
110             } else {
111                 c = 0;
112             }
113         }
114         result = -atof(res);
115     } else
116         result = atof(res);
117
118     if (a_neg + b_neg == 2) /* both are negatives, reverse sign */
119         result = -result;
120
121     return result;
122 }