D
dolbz.cardiff
Hi,
I would have asked this in the MRTG newsgroups but they all appear to
be dead. Sorry if this is the wrong place for the question.
I have been looking at the MRTG
(http://people.ee.ethz.ch/~oetiker/webtools/mrtg/) source code in order
to gain an understanding of how it works. Everything was going fine
until I ran into the 'diff' function. Could anyone provide a brief
overview of what it is actually doing. I am fairly new to C so cant
really follow what's going on with the pointers everywhere. The a and b
variables are char arrays containing just numbers/digits.
Here it is...
static double diff(a,b)
char *a,*b;
{
char res[MAXL], *a1, *b1, *r1;
int c,x,m;
if (*a == '-' && *b == '-') {
b1 = b + 1;
b = a + 1;
a = b1;
}
while (!isdigit((int)*a))
a++;
while (!isdigit((int)*b))
b++;
a1 = &a[strlen(a)-1];
m = max(strlen(a),strlen(b));
r1 = &res[m+1];
for (b1 = res;b1 <= r1; b1++) *b1 = ' ';
b1 = &b[strlen(b)-1];
r1[1] = 0; /* Null terminate result */
c = 0;
for (x=0; x<m; x++) {
if (a1 >= a && b1 >= b) {
*r1 = ((*a1 - c) - *b1) + '0';
} else if (a1 >= a) {
*r1 = (*a1 - c);
} else {
*r1 = ('0' - *b1 - c) + '0';
}
if (*r1 < '0') {
*r1 += 10;
c=1;
} else {
c=0;
}
a1--;b1--;r1--;
}
if (c) {
r1 = &res[m+1];
for (x=0; isdigit((int)*r1) && x<m; x++,r1--) {
*r1 = ('9' - *r1 + c) + '0';
if (*r1 > '9') {
*r1 -= 10;
c=1;
} else {
c=0;
}
}
return(-atof(res));
} else
return(atof(res));
}
Thanks in advance
Dolbz
I would have asked this in the MRTG newsgroups but they all appear to
be dead. Sorry if this is the wrong place for the question.
I have been looking at the MRTG
(http://people.ee.ethz.ch/~oetiker/webtools/mrtg/) source code in order
to gain an understanding of how it works. Everything was going fine
until I ran into the 'diff' function. Could anyone provide a brief
overview of what it is actually doing. I am fairly new to C so cant
really follow what's going on with the pointers everywhere. The a and b
variables are char arrays containing just numbers/digits.
Here it is...
static double diff(a,b)
char *a,*b;
{
char res[MAXL], *a1, *b1, *r1;
int c,x,m;
if (*a == '-' && *b == '-') {
b1 = b + 1;
b = a + 1;
a = b1;
}
while (!isdigit((int)*a))
a++;
while (!isdigit((int)*b))
b++;
a1 = &a[strlen(a)-1];
m = max(strlen(a),strlen(b));
r1 = &res[m+1];
for (b1 = res;b1 <= r1; b1++) *b1 = ' ';
b1 = &b[strlen(b)-1];
r1[1] = 0; /* Null terminate result */
c = 0;
for (x=0; x<m; x++) {
if (a1 >= a && b1 >= b) {
*r1 = ((*a1 - c) - *b1) + '0';
} else if (a1 >= a) {
*r1 = (*a1 - c);
} else {
*r1 = ('0' - *b1 - c) + '0';
}
if (*r1 < '0') {
*r1 += 10;
c=1;
} else {
c=0;
}
a1--;b1--;r1--;
}
if (c) {
r1 = &res[m+1];
for (x=0; isdigit((int)*r1) && x<m; x++,r1--) {
*r1 = ('9' - *r1 + c) + '0';
if (*r1 > '9') {
*r1 -= 10;
c=1;
} else {
c=0;
}
}
return(-atof(res));
} else
return(atof(res));
}
Thanks in advance
Dolbz