fixed version number and date/time
[rrdtool.git] / src / rrd_diff.c
1 /*****************************************************************************
2  * RRDtool 1.1.x  Copyright Tobias Oetiker, 1999
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.2  2002/02/01 20:34:49  oetiker
11  * fixed version number and date/time
12  *
13  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
14  * checkin
15  *
16  * Revision 1.1  1998/10/08 18:21:45  oetiker
17  * Initial revision
18  *
19  * Revision 1.3  1998/02/06 21:10:52  oetiker
20  * removed max define .. it is now in rrd_tool.h
21  *
22  * Revision 1.2  1997/12/07 20:38:03  oetiker
23  * ansified
24  *
25  * Revision 1.1  1997/11/28 23:31:59  oetiker
26  * Initial revision
27  *
28  *****************************************************************************/
29
30 #include "rrd_tool.h"
31
32 double
33 rrd_diff(char *a, char *b)
34 {
35     char res[LAST_DS_LEN+1], *a1, *b1, *r1, *fix;
36     int c,x,m;
37     
38     while (!isdigit((int)*a) || *a==0)
39         a++;
40     fix=a;
41     while (isdigit((int)*fix)) 
42         fix++;
43     *fix = 0; /* maybe there is some non digit data in the string */ 
44     while (!isdigit((int)*b) || *b==0)
45         b++;
46     fix=b;
47     while (isdigit((int)*fix)) 
48         fix++;
49     *fix = 0; /* maybe there is some non digit data in the string */ 
50     if(!isdigit((int)*a) || !isdigit((int)*b))
51         return DNAN;
52     a1 = &a[strlen(a)-1];
53     m = max(strlen(a),strlen(b));
54     if (m > LAST_DS_LEN) return DNAN; /* result string too short */
55
56     r1 = &res[m+1];
57     for (b1 = res;b1 <= r1; b1++) *b1 = ' ';
58     b1 = &b[strlen(b)-1];
59     r1[1] = 0;  /* Null terminate result */
60     c = 0;
61     for (x=0; x<m; x++) {
62         if (a1 >= a && b1 >= b) {
63             *r1 = ((*a1 - c) - *b1) + '0';
64         } else if (a1 >= a) {
65             *r1 = (*a1 - c);
66         } else {
67             *r1 = ('0' - *b1 - c) + '0';
68         }
69         if (*r1 < '0') {
70             *r1 += 10;
71             c=1;
72         } else
73           if (*r1 > '9') { /* 0 - 10 */
74             *r1 -= 10;
75             c=1;            
76           } else {
77             c=0;
78         }
79         a1--;b1--;r1--;
80     }
81     if (c) {
82         r1 = &res[m+1];
83         for (x=0; isdigit((int)*r1) && x<m; x++,r1--)  {
84             *r1 = ('9' - *r1 + c) + '0';
85             if (*r1 > '9') {
86                 *r1 -= 10;
87                 c=1;
88             } else {
89                 c=0;
90             }
91         }
92         return(-atof(res));
93     } else
94         return(atof(res));
95 }