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