Browse code

qvalue related functions and types.

Jan Janak authored on 25/04/2004 15:29:31
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,132 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Handling of the q value
4
+ *
5
+ * Copyright (C) 2004 FhG FOKUS
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ *
28
+ * History
29
+ * ------
30
+ * 2004-04-25 created (janakj)
31
+ */
32
+
33
+#ifndef _QVALUE_H
34
+#define _QVALUE_H 1
35
+
36
+#include <string.h>
37
+
38
+/*
39
+ * The q value expresses the priority of a URI within a set of URIs 
40
+ * (Contact header field in the same SIP message or dset array in 
41
+ * ser. The higher is the q value of a URI the higher is the priority 
42
+ * of the URI.
43
+ *
44
+ * The q value is usually expressed as a floating point number with 
45
+ * limited number of decimal digits, for example 0.346. RFC3261 allows 
46
+ * 0-3 decimal digits.
47
+ *
48
+ * To speed things up we represent the q value as integer number, it 
49
+ * is then easier to handle/print the value. To convert float into 
50
+ * integer we multiply the q value by 1000, i.e. 
51
+ * (float)0.567 == (int)567. In the opposite direction, values 
52
+ * higher or equal to 1000 are converted to 1.0 and values below or 
53
+ * equal to 0 are converted to 0.
54
+ *
55
+ * Value Q_UNSPECIFIED (which is in fact -1) has a special meaning, it 
56
+ * means that the q value is not known and the parameter should not be 
57
+ * printed when printing Contacts, implementations will then use 
58
+ * implementation specific pre-defined values.
59
+ */
60
+
61
+typedef int qvalue_t;
62
+
63
+/*
64
+ * Use this if the value of q is not specified
65
+ */
66
+#define Q_UNSPECIFIED ((qvalue_t)-1)
67
+
68
+
69
+#define MAX_Q ((qvalue_t)1000)
70
+#define MIN_Q ((qvalue_t)0)
71
+
72
+#define MAX_Q_STR "1"
73
+#define MAX_Q_STR_LEN (sizeof(MAX_Q_STR) - 1)
74
+
75
+#define MIN_Q_STR "0"
76
+#define MIN_Q_STR_LEN (sizeof(MIN_Q_STR) - 1)
77
+
78
+#define Q_PREFIX "0."
79
+#define Q_PREFIX_LEN (sizeof(Q_PREFIX) - 1)
80
+
81
+
82
+/*
83
+ * Calculate the length of printed q
84
+ */
85
+static inline size_t len_q(qvalue_t q)
86
+{
87
+	if (q == Q_UNSPECIFIED) {
88
+		return 0;
89
+	} else if (q >= MAX_Q) {
90
+		return MAX_Q_STR_LEN;
91
+	} else if (q <= MIN_Q) {
92
+		return MIN_Q_STR_LEN;
93
+	} else if (q % 100 == 0) {
94
+		return Q_PREFIX_LEN + 1;
95
+	} else if (q % 10 == 0) {
96
+		return Q_PREFIX_LEN + 2;
97
+	} else {
98
+		return Q_PREFIX_LEN + 3;
99
+	}
100
+}
101
+
102
+
103
+/*
104
+ * Print the q parameter
105
+ */
106
+static inline size_t print_q(char* p, qvalue_t q)
107
+{
108
+	if (q == Q_UNSPECIFIED) {
109
+		return 0;
110
+	} else if (q >= MAX_Q) {
111
+		memcpy(p, MAX_Q_STR, MAX_Q_STR_LEN);
112
+		return MAX_Q_STR_LEN;
113
+	} else if (q <= MIN_Q) {
114
+		memcpy(p, MIN_Q_STR, MIN_Q_STR_LEN);
115
+		return MIN_Q_STR_LEN;
116
+	}
117
+	
118
+	memcpy(p, Q_PREFIX, Q_PREFIX_LEN);
119
+	p += Q_PREFIX_LEN;
120
+
121
+	*p++ = q / 100 + '0';
122
+	q %= 100;
123
+	if (!q) return Q_PREFIX_LEN + 1;
124
+	*p++ = q / 10 + '0';
125
+	q %= 10;
126
+	if (!q) return Q_PREFIX_LEN + 2;
127
+	*p = q + '0';
128
+	return Q_PREFIX_LEN + 3;
129
+}
130
+
131
+#endif /* _QVALUE_H */