Subversion Repositories SvarDOS

Rev

Rev 252 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 252 Rev 255
Line 15... Line 15...
15
 
15
 
16
#include "rtrim.h"
16
#include "rtrim.h"
17
#include "helpers.h"
17
#include "helpers.h"
18
 
18
 
19
 
19
 
20
/* translates a version string into a array of integer values. The array must be 8-position long.
-
 
21
   returns 0 if parsing was successful, non-zero otherwise.
-
 
22
   Accepted formats follow:
-
 
23
    300.12.1
-
 
24
    1
-
 
25
    12.2.34.2-4.5
-
 
26
    1.2c
-
 
27
    1.01
-
 
28
    2013-12-31   */
-
 
29
static int versiontointarr(char *verstr, int *arr) {
-
 
30
  int i, vlen, dotcounter = 1, firstcharaftersep = 0;
-
 
31
  char *digits[8];
-
 
32
  char verstrcopy[16];
-
 
33
 
-
 
34
  /* fill the array with zeroes first */
-
 
35
  for (i = 0; i < 8; i++) arr[i] = 0;
-
 
36
 
-
 
37
  /* first extensively validate the input */
-
 
38
  if (verstr == NULL) return(-1);
-
 
39
  vlen = strlen(verstr);
-
 
40
  if (vlen == 0) return(-1);
-
 
41
  if (vlen > 15) return(-1);
-
 
42
  if ((verstr[0] < '0') || (verstr[0] > '9')) return(-1); /* a version string must start with a 0..9 digit */
-
 
43
  if ((tolower(verstr[vlen - 1]) >= 'a') && (tolower(verstr[vlen - 1]) <= 'z') && (vlen > 1)) { /* remove any letter from the end, and use it as a (very) minor differenciator */
-
 
44
    vlen -= 1;
-
 
45
    arr[7] = tolower(verstr[vlen]);
-
 
46
  }
-
 
47
  if ((verstr[vlen - 1] < '0') || (verstr[vlen - 1] > '9')) return(-1); /* a version string must end with a 0..9 digit */
-
 
48
 
-
 
49
  digits[0] = verstrcopy;
-
 
50
  for (i = 0; i < vlen; i++) {
-
 
51
    verstrcopy[i] = verstr[i];
-
 
52
    switch (verstr[i]) {
-
 
53
      case '.':
-
 
54
      case '-':
-
 
55
        if (i == 0) return(-1);
-
 
56
        if (verstrcopy[i-1] == 0) return(-1); /* do not allow two separators in a row */
-
 
57
        if (dotcounter > 6) return(-1);
-
 
58
        digits[dotcounter++] = &verstrcopy[i + 1];
-
 
59
        verstrcopy[i] = 0;
-
 
60
        firstcharaftersep = 1;
-
 
61
        break;
-
 
62
      case '0':
-
 
63
        /* if this is a zero right after a separator, and trailed with a digit
-
 
64
         * (as in '1.01'), then enforce a separator first */
-
 
65
        if ((firstcharaftersep != 0) && (verstr[i+1] >= '0') && (verstr[i+1] <= '9')) {
-
 
66
          if (dotcounter > 6) return(-1);
-
 
67
          digits[dotcounter++] = &verstrcopy[i + 1];
-
 
68
          verstrcopy[i] = 0;
-
 
69
        }
-
 
70
        break;
-
 
71
      case '1':
-
 
72
      case '2':
-
 
73
      case '3':
-
 
74
      case '4':
-
 
75
      case '5':
-
 
76
      case '6':
-
 
77
      case '7':
-
 
78
      case '8':
-
 
79
      case '9':
-
 
80
        firstcharaftersep = 0;
-
 
81
        break;
-
 
82
      default: /* do not allow any character different than 0..9 or '.' */
-
 
83
        return(-1);
-
 
84
        break;
-
 
85
    }
-
 
86
  }
-
 
87
  verstrcopy[i] = 0;
-
 
88
  /* now that we know the input is sane, let's process it */
-
 
89
  for (i = 0; i < dotcounter; i++) {
-
 
90
    int tmpint;
-
 
91
    tmpint = atoi(digits[i]);
-
 
92
    if ((tmpint < 0) || (tmpint > 32000)) return(-1);
-
 
93
    arr[i] = tmpint;
-
 
94
  }
-
 
95
  return(0);
-
 
96
}
-
 
97
 
-
 
98
 
-
 
99
/* compares version strings v1 and v2. Returns 1 if v2 is newer than v1, 0 otherwise, and -1 if comparison is unable to tell */
-
 
100
int isversionnewer(char *v1, char *v2) {
-
 
101
  int x1[8], x2[8], i;
-
 
102
  if ((v1 == NULL) || (v2 == NULL)) return(-1); /* if input is invalid (NULL), don't continue */
-
 
103
  if (strcasecmp(v1, v2) == 0) return(0);  /* if both versions are the same, don't continue */
-
 
104
  /* check versions of the decimal format 1.23... */
-
 
105
  if ((versiontointarr(v1, x1) != 0) || (versiontointarr(v2, x2) != 0)) return(-1);
-
 
106
  for (i = 0; i < 8; i++) {
-
 
107
    if (x2[i] > x1[i]) return(1);
-
 
108
    if (x2[i] < x1[i]) return(0);
-
 
109
  }
-
 
110
  return(0);
-
 
111
}
-
 
112
 
-
 
113
 
-
 
114
/* change all / to \ in a string */
20
/* change all / to \ in a string */
115
void slash2backslash(char *str) {
21
void slash2backslash(char *str) {
116
  int x;
22
  int x;
117
  for (x = 0; str[x] != 0; x++) {
23
  for (x = 0; str[x] != 0; x++) {
118
    if (str[x] == '/') str[x] = '\\';
24
    if (str[x] == '/') str[x] = '\\';