Make WvStreams compile with gcc 4.4.
[wvstreams.git] / utils / verstring.cc
blob7d17349926a8a9d19b0f6d88ad1ece58e53a7de1
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
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
9 * version string).
11 #include "wvverstring.h"
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
16 const char *old_ver_to_string(unsigned int ver)
18 static char str[10];
19 unsigned int maj = (ver & 0xFFFF0000) >> 16, min = (ver & 0x0000FFFF);
21 sprintf(str, "%x.%04x", maj, min);
22 trim_verstr(str);
24 return str;
28 const char *new_ver_to_string(unsigned int ver)
30 static char str[11];
31 unsigned int maj = (ver & 0xFF000000) >> 24, min = (ver & 0x00FF0000) >> 16,
32 rev = (ver & 0x0000FFFF);
34 sprintf(str, "%x.%02x.%04x", maj, min, rev);
36 return str;
40 const char *ver_to_string(unsigned int ver)
42 if (is_new_ver(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;
54 int bits;
56 // do the major number
57 cptr = (unsigned char *)str;
58 for (; *cptr && *cptr != '.' && *cptr != '_'; cptr++)
60 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
61 if (!idx)
62 continue;
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));
71 if (!idx)
72 continue;
74 min = (min << 4) | ((char *)idx - lookup);
75 bits--;
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;
87 int bits;
89 // do the major number
90 cptr = (unsigned char *)str;
91 for (; *cptr; cptr++)
93 if (*cptr == '.' || *cptr == '_')
95 cptr++;
96 break;
98 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
99 if (!idx)
100 continue;
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));
109 if (!idx)
110 continue;
112 min = (min << 4) | ((char *)idx - lookup);
113 bits--;
116 // do the revision number
117 for (bits = 4; *cptr && bits > 0; cptr++)
119 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
120 if (!idx)
121 continue;
123 rev = (rev << 4) | ((char *)idx - lookup);
124 bits--;
127 ver = (maj << 24) | (min << 16) | (rev << (4*bits));
129 return ver;
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, '.'))
152 return true;
154 return false;
158 char *trim_verstr(char *verstr)
160 // trim off trailing zeroes
161 char *cptr;
163 for (cptr = strchr(verstr, 0); --cptr >= verstr; )
165 if (*cptr != '0')
166 break;
168 if (cptr <= verstr || *(cptr - 1) == '.')
169 break;
171 *cptr = 0;
173 return verstr;