File: verstring.cc

package info (click to toggle)
wvstreams 4.6.1-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,420 kB
  • sloc: cpp: 64,196; ansic: 4,154; sh: 4,025; makefile: 546; perl: 402
file content (174 lines) | stat: -rw-r--r-- 3,665 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 * 
 * Version number and string manipulations.  Version numbers are 32-bit
 * hexadecimal numbers such as 0x00012a00.  The first 16 bits are the major
 * version, and the second 16 bits are the (fractional) minor version.  For
 * example, the above example corresponds to version "1.2a" (which is the
 * version string).
 */
#include "wvverstring.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>

const char *old_ver_to_string(unsigned int ver)
{
    static char str[10];
    unsigned int maj = (ver & 0xFFFF0000) >> 16, min = (ver & 0x0000FFFF);

    sprintf(str, "%x.%04x", maj, min);
    trim_verstr(str);
    
    return str;
}


const char *new_ver_to_string(unsigned int ver)
{
    static char str[11];
    unsigned int maj = (ver & 0xFF000000) >> 24, min = (ver & 0x00FF0000) >> 16,
                 rev = (ver & 0x0000FFFF);

    sprintf(str, "%x.%02x.%04x", maj, min, rev);

    return str;
}


const char *ver_to_string(unsigned int ver)
{
    if (is_new_ver(ver))
        return new_ver_to_string(ver);

    return old_ver_to_string(ver);
}


unsigned int string_to_old_ver(const char *str)
{
    static char lookup[] = "0123456789abcdef";
    unsigned int maj = 0, min = 0;
    unsigned char *cptr, *idx;
    int bits;
    
    // do the major number
    cptr = (unsigned char *)str;
    for (; *cptr && *cptr != '.' && *cptr != '_'; cptr++)
    {
        idx = (unsigned char *)strchr(lookup, tolower(*cptr));
        if (!idx)
            continue;
        
        maj = (maj << 4) | ((char *)idx - lookup);
    }
    
    // do the minor number
    for (bits = 4; *cptr && bits > 0; cptr++)
    {
        idx = (unsigned char *)strchr(lookup, tolower(*cptr));
        if (!idx)
            continue;
        
        min = (min << 4) | ((char *)idx - lookup);
        bits--;
    }
    
    return (maj << 16) | (min << (4*bits));
}


unsigned int string_to_new_ver(const char *str)
{
    static char lookup[] = "0123456789abcdef";
    unsigned int maj = 0, min = 0, rev = 0, ver;
    unsigned char *cptr, *idx;
    int bits;

    // do the major number
    cptr = (unsigned char *)str;
    for (; *cptr; cptr++)
    {
	if (*cptr == '.' || *cptr == '_')
	{
	    cptr++;
	    break;
	}
	idx = (unsigned char *)strchr(lookup, tolower(*cptr));
	if (!idx)
	    continue;
	
	maj = (maj << 4) | ((char *)idx - lookup);
    }
    
    // do the minor number
    for (bits = 2; *cptr && *cptr != '.' && *cptr != '_' && bits > 0; cptr++)
    {
	idx = (unsigned char *)strchr(lookup, tolower(*cptr));
	if (!idx)
	    continue;
	
	min = (min << 4) | ((char *)idx - lookup);
	bits--;
    }
    
    // do the revision number
    for (bits = 4; *cptr && bits > 0; cptr++)
    {
	idx = (unsigned char *)strchr(lookup, tolower(*cptr));
	if (!idx)
	    continue;

	rev = (rev << 4) | ((char *)idx - lookup);
	bits--;
    }

    ver = (maj << 24) | (min << 16) | (rev << (4*bits));

    return ver;
}


unsigned int string_to_ver(const char *str)
{
    if (is_new_verstr(str))
        return string_to_new_ver(str);

    return string_to_old_ver(str);
}


bool is_new_ver(unsigned int ver)
{
    return (ver & 0xff000000);
}


bool is_new_verstr(const char *str)
{
    const char *p = strchr(str, '.');
    if (p && strchr(p+1, '.'))
        return true;

    return false;
}


char *trim_verstr(char *verstr)
{
    // trim off trailing zeroes
    char *cptr;

    for (cptr = strchr(verstr, 0); --cptr >= verstr; )
    {
	if (*cptr != '0')
	    break;
	
	if (cptr <= verstr  ||  *(cptr - 1) == '.')
	    break;
	
	*cptr = 0;
    }
    return verstr;
}