00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "verstring.h"
00012 #include <stdio.h>
00013 #include <ctype.h>
00014 #include <string.h>
00015
00016 const char *ver_to_string(unsigned int ver)
00017 {
00018 static char str[10];
00019 unsigned int maj = (ver & 0xFFFF0000) >> 16, min = (ver & 0x0000FFFF);
00020 char *cptr;
00021
00022 sprintf(str, "%x.%04x", maj, min);
00023
00024
00025 for (cptr = strchr(str, 0); --cptr >= str; )
00026 {
00027 if (*cptr != '0')
00028 break;
00029
00030 if (cptr <= str || *(cptr - 1) == '.')
00031 break;
00032
00033 *cptr = 0;
00034 }
00035
00036 return str;
00037 }
00038
00039
00040 unsigned int string_to_ver(const char *str)
00041 {
00042 static char lookup[] = "0123456789abcdef";
00043 unsigned int maj = 0, min = 0;
00044 unsigned char *cptr, *idx;
00045 int bits;
00046
00047
00048 cptr = (unsigned char *)str;
00049 for (; *cptr && *cptr != '.' && *cptr != '_'; cptr++)
00050 {
00051 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
00052 if (!idx)
00053 continue;
00054
00055 maj = (maj << 4) | ((char *)idx - lookup);
00056 }
00057
00058
00059 for (bits = 4; *cptr && bits > 0; cptr++)
00060 {
00061 idx = (unsigned char *)strchr(lookup, tolower(*cptr));
00062 if (!idx)
00063 continue;
00064
00065 min = (min << 4) | ((char *)idx - lookup);
00066 bits--;
00067 }
00068
00069 return (maj << 16) | (min << (4*bits));
00070 }