]>
git.saurik.com Git - apt.git/blob - test/interactive-helper/rpmver.cc
3 #include <apt-pkg/debversion.h>
10 #define xisdigit(x) isdigit(x)
11 #define xisalpha(x) isalpha(x)
12 #define xisalnum(x) (isdigit(x) || isalpha(x))
16 int rpmvercmp(const char * a
, const char * b
)
22 /* easy comparison to see if versions are identical */
23 if (!strcmp(a
, b
)) return 0;
25 str1
= (char *)alloca(strlen(a
) + 1);
26 str2
= (char *)alloca(strlen(b
) + 1);
34 /* loop through each version segment of str1 and str2 and compare them */
35 while (*one
&& *two
) {
36 while (*one
&& !xisalnum(*one
)) one
++;
37 while (*two
&& !xisalnum(*two
)) two
++;
42 /* grab first completely alpha or completely numeric segment */
43 /* leave one and two pointing to the start of the alpha or numeric */
44 /* segment and walk str1 and str2 to end of segment */
45 if (xisdigit(*str1
)) {
46 while (*str1
&& xisdigit(*str1
)) str1
++;
47 while (*str2
&& xisdigit(*str2
)) str2
++;
50 while (*str1
&& xisalpha(*str1
)) str1
++;
51 while (*str2
&& xisalpha(*str2
)) str2
++;
55 /* save character at the end of the alpha or numeric segment */
56 /* so that they can be restored after the comparison */
62 /* take care of the case where the two version segments are */
63 /* different types: one numeric, the other alpha (i.e. empty) */
64 if (one
== str1
) return -1; /* arbitrary */
65 if (two
== str2
) return 1;
68 /* this used to be done by converting the digit segments */
69 /* to ints using atoi() - it's changed because long */
70 /* digit segments can overflow an int - this should fix that. */
72 /* throw away any leading zeros - it's a number, right? */
73 while (*one
== '0') one
++;
74 while (*two
== '0') two
++;
76 /* whichever number has more digits wins */
77 if (strlen(one
) > strlen(two
)) return 1;
78 if (strlen(two
) > strlen(one
)) return -1;
81 /* strcmp will return which one is greater - even if the two */
82 /* segments are alpha or if they are numeric. don't return */
83 /* if they are equal because there might be more segments to */
85 int rc
= strcmp(one
, two
);
88 /* restore character that was replaced by null above */
95 /* this catches the case where all numeric and alpha segments have */
96 /* compared identically but the segment sepparating characters were */
98 if ((!*one
) && (!*two
)) return 0;
100 /* whichever version still has characters left over wins */
101 if (!*one
) return -1; else return 1;
104 int main(int argc
,const char *argv
[])
106 printf("%i\n",strcmp(argv
[1],argv
[2]));
108 printf("'%s' <> '%s': ",argv
[1],argv
[2]);
109 printf("rpm: %i deb: %i\n",rpmvercmp(argv
[1],argv
[2]),
110 debVS
.CmpFragment(argv
[1],argv
[1]+strlen(argv
[1]),
111 argv
[2],argv
[2]+strlen(argv
[2])));
113 printf("'%s' <> '%s': ",argv
[2],argv
[1]);
114 printf("rpm: %i deb: %i\n",rpmvercmp(argv
[2],argv
[1]),
115 debVS
.CmpFragment(argv
[2],argv
[2]+strlen(argv
[2]),
116 argv
[1],argv
[1]+strlen(argv
[1])));