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