/*
* @(#)version_comp.c 1.10 06/02/25
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "jni.h"
#include "jli_util.h"
#include "version_comp.h"
/*
* A collection of useful strings. One should think of these as #define
* entries, but actual strings can be more efficient (with many compilers).
*/
static const char *separators = ".-_";
static const char *zero_string = "0";
/*
* Validate a string as parsable as a "Java int". If so parsable,
* return true (non-zero) and store the numeric value at the address
* passed in as "value"; otherwise return false (zero).
*
* Note that the maximum allowable value is 2147483647 as defined by
* the "Java Language Specification" which precludes the use of native
* conversion routines which may have other limits.
*
* Also note that we don't have to worry about the alternate maximum
* allowable value of 2147483648 because it is only allowed after
* the unary negation operator and this grammar doesn't have one
* of those.
*
* Finally, note that a value which exceeds the maximum jint value will
* return false (zero). This results in the otherwise purely numeric
* string being compared as a string of characters (as per the spec.)
*/
static int
isjavaint(const char *s, jint *value)
{
jlong sum = 0;
jint digit;
while (*s != '\0')
if (isdigit(*s)) {
digit = (jint)((int)(*s++) - (int)('0'));
sum = (sum * 10) + digit;
if (sum > 2147483647)
return (0); /* Overflows jint (but not jlong) */
} else
return (0);
*value = (jint)sum;
return (1);
}
/*
* Modeled after strcmp(), compare two strings (as in the grammar defined
* in Appendix A of JSR 56). If both strings can be interpreted as
* Java ints, do a numeric comparison, else it is strcmp().
*/
static int
comp_string(const char *s1, const char *s2)
{
jint v1, v2;
if (isjavaint(s1, &v1) && isjavaint(s2, &v2))
return ((int)(v1 - v2));
else
return (strcmp(s1, s2));
}
/*
* Modeled after strcmp(), compare two version-ids for a Prefix
* Match as defined in JSR 56.
*/
int
JLI_PrefixVersionId(char *id1, char *id2)
{
char *s1 = JLI_StringDup(id1);
char *s2 = JLI_StringDup(id2);
char *m1 = s1;
char *m2 = s2;
char *end1 = NULL;
char *end2 = NULL;
int res = 0;
do {
if ((s1 != NULL) && ((end1 = strpbrk(s1, ".-_")) != NULL))
*end1 = '\0';
if ((s2 != NULL) && ((end2 = strpbrk(s2, ".-_")) != NULL))
*end2 = '\0';
res = comp_string(s1, s2);
if (end1 != NULL)
s1 = end1 + 1;
else
s1 = NULL;
=1= |