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