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