2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Version number and string manipulations. Version numbers are 32-bit
6 * hexadecimal numbers such as 0x00012a00. The first 16 bits are the major
7 * version, and the second 16 bits are the (fractional) minor version. For
8 * example, the above example corresponds to version "1.2a" (which is the
11 #include "wvverstring.h"
16 const char *old_ver_to_string(unsigned int ver
)
19 unsigned int maj
= (ver
& 0xFFFF0000) >> 16, min
= (ver
& 0x0000FFFF);
21 sprintf(str
, "%x.%04x", maj
, min
);
28 const char *new_ver_to_string(unsigned int ver
)
31 unsigned int maj
= (ver
& 0xFF000000) >> 24, min
= (ver
& 0x00FF0000) >> 16,
32 rev
= (ver
& 0x0000FFFF);
34 sprintf(str
, "%x.%02x.%04x", maj
, min
, rev
);
40 const char *ver_to_string(unsigned int ver
)
43 return new_ver_to_string(ver
);
45 return old_ver_to_string(ver
);
49 unsigned int string_to_old_ver(const char *str
)
51 static char lookup
[] = "0123456789abcdef";
52 unsigned int maj
= 0, min
= 0;
53 unsigned char *cptr
, *idx
;
56 // do the major number
57 cptr
= (unsigned char *)str
;
58 for (; *cptr
&& *cptr
!= '.' && *cptr
!= '_'; cptr
++)
60 idx
= (unsigned char *)strchr(lookup
, tolower(*cptr
));
64 maj
= (maj
<< 4) | ((char *)idx
- lookup
);
67 // do the minor number
68 for (bits
= 4; *cptr
&& bits
> 0; cptr
++)
70 idx
= (unsigned char *)strchr(lookup
, tolower(*cptr
));
74 min
= (min
<< 4) | ((char *)idx
- lookup
);
78 return (maj
<< 16) | (min
<< (4*bits
));
82 unsigned int string_to_new_ver(const char *str
)
84 static char lookup
[] = "0123456789abcdef";
85 unsigned int maj
= 0, min
= 0, rev
= 0, ver
;
86 unsigned char *cptr
, *idx
;
89 // do the major number
90 cptr
= (unsigned char *)str
;
93 if (*cptr
== '.' || *cptr
== '_')
98 idx
= (unsigned char *)strchr(lookup
, tolower(*cptr
));
102 maj
= (maj
<< 4) | ((char *)idx
- lookup
);
105 // do the minor number
106 for (bits
= 2; *cptr
&& *cptr
!= '.' && *cptr
!= '_' && bits
> 0; cptr
++)
108 idx
= (unsigned char *)strchr(lookup
, tolower(*cptr
));
112 min
= (min
<< 4) | ((char *)idx
- lookup
);
116 // do the revision number
117 for (bits
= 4; *cptr
&& bits
> 0; cptr
++)
119 idx
= (unsigned char *)strchr(lookup
, tolower(*cptr
));
123 rev
= (rev
<< 4) | ((char *)idx
- lookup
);
127 ver
= (maj
<< 24) | (min
<< 16) | (rev
<< (4*bits
));
133 unsigned int string_to_ver(const char *str
)
135 if (is_new_verstr(str
))
136 return string_to_new_ver(str
);
138 return string_to_old_ver(str
);
142 bool is_new_ver(unsigned int ver
)
144 return (ver
& 0xff000000);
148 bool is_new_verstr(const char *str
)
150 const char *p
= strchr(str
, '.');
151 if (p
&& strchr(p
+1, '.'))
158 char *trim_verstr(char *verstr
)
160 // trim off trailing zeroes
163 for (cptr
= strchr(verstr
, 0); --cptr
>= verstr
; )
168 if (cptr
<= verstr
|| *(cptr
- 1) == '.')