]>
git.saurik.com Git - apt.git/blob - test/interactive-helper/rpmver.cc
1 #include <apt-pkg/debversion.h>
8 #define xisdigit(x) isdigit(x)
9 #define xisalpha(x) isalpha(x)
10 #define xisalnum(x) (isdigit(x) || isalpha(x))
14 int rpmvercmp(const char * a
, const char * b
)
20 /* easy comparison to see if versions are identical */
21 if (!strcmp(a
, b
)) return 0;
23 str1
= (char *)alloca(strlen(a
) + 1);
24 str2
= (char *)alloca(strlen(b
) + 1);
32 /* loop through each version segment of str1 and str2 and compare them */
33 while (*one
&& *two
) {
34 while (*one
&& !xisalnum(*one
)) one
++;
35 while (*two
&& !xisalnum(*two
)) two
++;
40 /* grab first completely alpha or completely numeric segment */
41 /* leave one and two pointing to the start of the alpha or numeric */
42 /* segment and walk str1 and str2 to end of segment */
43 if (xisdigit(*str1
)) {
44 while (*str1
&& xisdigit(*str1
)) str1
++;
45 while (*str2
&& xisdigit(*str2
)) str2
++;
48 while (*str1
&& xisalpha(*str1
)) str1
++;
49 while (*str2
&& xisalpha(*str2
)) str2
++;
53 /* save character at the end of the alpha or numeric segment */
54 /* so that they can be restored after the comparison */
60 /* take care of the case where the two version segments are */
61 /* different types: one numeric, the other alpha (i.e. empty) */
62 if (one
== str1
) return -1; /* arbitrary */
63 if (two
== str2
) return 1;
66 /* this used to be done by converting the digit segments */
67 /* to ints using atoi() - it's changed because long */
68 /* digit segments can overflow an int - this should fix that. */
70 /* throw away any leading zeros - it's a number, right? */
71 while (*one
== '0') one
++;
72 while (*two
== '0') two
++;
74 /* whichever number has more digits wins */
75 if (strlen(one
) > strlen(two
)) return 1;
76 if (strlen(two
) > strlen(one
)) return -1;
79 /* strcmp will return which one is greater - even if the two */
80 /* segments are alpha or if they are numeric. don't return */
81 /* if they are equal because there might be more segments to */
83 int rc
= strcmp(one
, two
);
86 /* restore character that was replaced by null above */
93 /* this catches the case where all numeric and alpha segments have */
94 /* compared identically but the segment sepparating characters were */
96 if ((!*one
) && (!*two
)) return 0;
98 /* whichever version still has characters left over wins */
99 if (!*one
) return -1; else return 1;
102 int main(int argc
,const char *argv
[])
104 printf("%i\n",strcmp(argv
[1],argv
[2]));
106 printf("'%s' <> '%s': ",argv
[1],argv
[2]);
107 printf("rpm: %i deb: %i\n",rpmvercmp(argv
[1],argv
[2]),
108 debVS
.CmpFragment(argv
[1],argv
[1]+strlen(argv
[1]),
109 argv
[2],argv
[2]+strlen(argv
[2])));
111 printf("'%s' <> '%s': ",argv
[2],argv
[1]);
112 printf("rpm: %i deb: %i\n",rpmvercmp(argv
[2],argv
[1]),
113 debVS
.CmpFragment(argv
[2],argv
[2]+strlen(argv
[2]),
114 argv
[1],argv
[1]+strlen(argv
[1])));