1 /*
2   Copyright (c) <2007-2012> <Barbara Philippot - Olivier Courtin>
3 
4   Permission is hereby granted, free of charge, to any person obtaining a copy
5   of this software and associated documentation files (the "Software"), to deal
6   in the Software without restriction, including without limitation the rights
7   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8   copies of the Software, and to permit persons to whom the Software is
9   furnished to do so, subject to the following conditions:
10 
11   The above copyright notice and this permission notice shall be included in
12   all copies or substantial portions of the Software.
13 
14   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20   IN THE SOFTWARE.
21 */
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26 
27 #include "ows.h"
28 
29 
30 /*
31  * Initialize version structure
32  */
ows_version_init()33 ows_version *ows_version_init()
34 {
35   ows_version *v;
36 
37   v = malloc(sizeof(ows_version));
38   assert(v);
39 
40   v->major = -1;
41   v->minor = -1;
42   v->release = -1;
43 
44   return v;
45 }
46 
47 
48 /*
49  * Fill a version structure
50  */
ows_version_set(ows_version * v,int major,int minor,int release)51 void ows_version_set(ows_version * v, int major, int minor, int release)
52 {
53   assert(v);
54 
55   v->major = major;
56   v->minor = minor;
57   v->release = release;
58 }
59 
60 
ows_version_set_str(ows_version * v,char * str)61 bool ows_version_set_str(ows_version * v, char *str)
62 {
63   int i, major, minor, release;
64   char *p;
65 
66   assert(v);
67   assert(str);
68 
69   /* Nota: We don't handle version on 2 digits like X.YY.Z */
70   if (!check_regexp(str, "[0-9].[0-9].[0-9]")) return false;
71 
72   i = major = minor = release = -1;
73   for ( p = str; *p ; p++, i = *(p - 1)) {
74 
75     /* 48 means 0 in ASCII */
76     if (*(p) == '.') {
77       if (major < 0) major = i - 48;
78       else if (major >= 0 && minor < 0) minor = i - 48;
79     }
80   }
81   release = *(p - 1) - 48;
82 
83   v->major = major;
84   v->minor = minor;
85   v->release = release;
86 
87   return true;
88 }
89 
90 
91 /*
92  * Free a version structure
93  */
ows_version_free(ows_version * v)94 void ows_version_free(ows_version * v)
95 {
96   assert(v);
97 
98   free(v);
99   v = NULL;
100 }
101 
102 
103 /*
104  * Return an int related to an ows_version
105  */
ows_version_get(ows_version * v)106 int ows_version_get(ows_version * v)
107 {
108   assert(v);
109 
110   return v->major * 100 + v->minor * 10 + v->release;
111 }
112 
113 
ows_version_check(ows_version * v)114 bool ows_version_check(ows_version *v)
115 {
116   assert(v);
117   if (v->major == -1 || v->minor == -1 || v->release == -1) return false;
118   return true;
119 }
120 
121 #ifdef OWS_DEBUG
ows_version_flush(ows_version * v,FILE * output)122 void ows_version_flush(ows_version * v, FILE * output)
123 {
124   assert(v);
125   assert(output);
126 
127   fprintf(output, "version: [%i,%i,%i]\n", v->major, v->minor, v->release);
128 }
129 #endif
130 
131 /*
132  * vim: expandtab sw=4 ts=4
133  */
134