WvStreams
verstring.cc
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).
10 */
11#include "wvverstring.h"
12#include <stdio.h>
13#include <ctype.h>
14#include <string.h>
15
16const char *old_ver_to_string(unsigned int ver)
17{
18 static char str[10];
19 unsigned int maj = (ver & 0xFFFF0000) >> 16, min = (ver & 0x0000FFFF);
20
21 sprintf(str, "%x.%04x", maj, min);
22 trim_verstr(str);
23
24 return str;
25}
26
27
28const char *new_ver_to_string(unsigned int ver)
29{
30 static char str[11];
31 unsigned int maj = (ver & 0xFF000000) >> 24, min = (ver & 0x00FF0000) >> 16,
32 rev = (ver & 0x0000FFFF);
33
34 sprintf(str, "%x.%02x.%04x", maj, min, rev);
35
36 return str;
37}
38
39
40const char *ver_to_string(unsigned int ver)
41{
42 if (is_new_ver(ver))
43 return new_ver_to_string(ver);
44
45 return old_ver_to_string(ver);
46}
47
48
49unsigned int string_to_old_ver(const char *str)
50{
51 static char lookup[] = "0123456789abcdef";
52 unsigned int maj = 0, min = 0;
53 unsigned char *cptr, *idx;
54 int bits;
55
56 // do the major number
57 cptr = (unsigned char *)str;
58 for (; *cptr && *cptr != '.' && *cptr != '_'; cptr++)
59 {
60 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
61 if (!idx)
62 continue;
63
64 maj = (maj << 4) | ((char *)idx - lookup);
65 }
66
67 // do the minor number
68 for (bits = 4; *cptr && bits > 0; cptr++)
69 {
70 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
71 if (!idx)
72 continue;
73
74 min = (min << 4) | ((char *)idx - lookup);
75 bits--;
76 }
77
78 return (maj << 16) | (min << (4*bits));
79}
80
81
82unsigned int string_to_new_ver(const char *str)
83{
84 static char lookup[] = "0123456789abcdef";
85 unsigned int maj = 0, min = 0, rev = 0, ver;
86 unsigned char *cptr, *idx;
87 int bits;
88
89 // do the major number
90 cptr = (unsigned char *)str;
91 for (; *cptr; cptr++)
92 {
93 if (*cptr == '.' || *cptr == '_')
94 {
95 cptr++;
96 break;
97 }
98 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
99 if (!idx)
100 continue;
101
102 maj = (maj << 4) | ((char *)idx - lookup);
103 }
104
105 // do the minor number
106 for (bits = 2; *cptr && *cptr != '.' && *cptr != '_' && bits > 0; cptr++)
107 {
108 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
109 if (!idx)
110 continue;
111
112 min = (min << 4) | ((char *)idx - lookup);
113 bits--;
114 }
115
116 // do the revision number
117 for (bits = 4; *cptr && bits > 0; cptr++)
118 {
119 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
120 if (!idx)
121 continue;
122
123 rev = (rev << 4) | ((char *)idx - lookup);
124 bits--;
125 }
126
127 ver = (maj << 24) | (min << 16) | (rev << (4*bits));
128
129 return ver;
130}
131
132
133unsigned int string_to_ver(const char *str)
134{
135 if (is_new_verstr(str))
136 return string_to_new_ver(str);
137
138 return string_to_old_ver(str);
139}
140
141
142bool is_new_ver(unsigned int ver)
143{
144 return (ver & 0xff000000);
145}
146
147
148bool is_new_verstr(const char *str)
149{
150 const char *p = strchr(str, '.');
151 if (p && strchr(p+1, '.'))
152 return true;
153
154 return false;
155}
156
157
158char *trim_verstr(char *verstr)
159{
160 // trim off trailing zeroes
161 char *cptr;
162
163 for (cptr = strchr(verstr, 0); --cptr >= verstr; )
164 {
165 if (*cptr != '0')
166 break;
167
168 if (cptr <= verstr || *(cptr - 1) == '.')
169 break;
170
171 *cptr = 0;
172 }
173 return verstr;
174}
int lookup(const char *str, const char *const *table, bool case_sensitive=false)
Finds a string in an array and returns its index.
Definition: strutils.cc:850