/src/kamailio/src/core/qvalue.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Handling of the q value |
3 | | * |
4 | | * Copyright (C) 2004 FhG FOKUS |
5 | | * |
6 | | * This file is part of Kamailio, a free SIP server. |
7 | | * |
8 | | * Kamailio is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU General Public License as published by |
10 | | * the Free Software Foundation; either version 2 of the License, or |
11 | | * (at your option) any later version |
12 | | * |
13 | | * Kamailio is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | * |
22 | | */ |
23 | | /*! |
24 | | * \file |
25 | | * \brief Kamailio core :: Handling of the Q value |
26 | | * \author janakj |
27 | | * \ingroup core |
28 | | * Module: \ref core |
29 | | */ |
30 | | |
31 | | #ifndef _QVALUE_H |
32 | | #define _QVALUE_H 1 |
33 | | |
34 | | #include <string.h> |
35 | | |
36 | | /* |
37 | | * The q value expresses the priority of a URI within a set of URIs |
38 | | * (Contact header field in the same SIP message or dset array in |
39 | | * ser. The higher is the q value of a URI the higher is the priority |
40 | | * of the URI. |
41 | | * |
42 | | * The q value is usually expressed as a floating point number with |
43 | | * limited number of decimal digits, for example 0.346. RFC3261 allows |
44 | | * 0-3 decimal digits. |
45 | | * |
46 | | * To speed things up we represent the q value as integer number, it |
47 | | * is then easier to handle/print the value. To convert float into |
48 | | * integer we multiply the q value by 1000, i.e. |
49 | | * (float)0.567 == (int)567. In the opposite direction, values |
50 | | * higher or equal to 1000 are converted to 1.0 and values below or |
51 | | * equal to 0 are converted to 0. |
52 | | * |
53 | | * Value Q_UNSPECIFIED (which is in fact -1) has a special meaning, it |
54 | | * means that the q value is not known and the parameter should not be |
55 | | * printed when printing Contacts, implementations will then use |
56 | | * implementation specific pre-defined values. |
57 | | */ |
58 | | |
59 | | typedef int qvalue_t; |
60 | | |
61 | | /* |
62 | | * Use this if the value of q is not specified |
63 | | */ |
64 | 0 | #define Q_UNSPECIFIED ((qvalue_t)-1) |
65 | | |
66 | | |
67 | 0 | #define MAX_Q ((qvalue_t)1000) |
68 | 0 | #define MIN_Q ((qvalue_t)0) |
69 | | |
70 | 0 | #define MAX_Q_STR "1" |
71 | 0 | #define MAX_Q_STR_LEN (sizeof(MAX_Q_STR) - 1) |
72 | | |
73 | 0 | #define MIN_Q_STR "0" |
74 | 0 | #define MIN_Q_STR_LEN (sizeof(MIN_Q_STR) - 1) |
75 | | |
76 | 0 | #define Q_PREFIX "0." |
77 | 0 | #define Q_PREFIX_LEN (sizeof(Q_PREFIX) - 1) |
78 | | |
79 | | |
80 | | /* |
81 | | * Calculate the length of printed q |
82 | | */ |
83 | | static inline size_t len_q(qvalue_t q) |
84 | 0 | { |
85 | 0 | if(q == Q_UNSPECIFIED) { |
86 | 0 | return 0; |
87 | 0 | } else if(q >= MAX_Q) { |
88 | 0 | return MAX_Q_STR_LEN; |
89 | 0 | } else if(q <= MIN_Q) { |
90 | 0 | return MIN_Q_STR_LEN; |
91 | 0 | } else if(q % 100 == 0) { |
92 | 0 | return Q_PREFIX_LEN + 1; |
93 | 0 | } else if(q % 10 == 0) { |
94 | 0 | return Q_PREFIX_LEN + 2; |
95 | 0 | } else { |
96 | 0 | return Q_PREFIX_LEN + 3; |
97 | 0 | } |
98 | 0 | } Unexecuted instantiation: main.c:len_q Unexecuted instantiation: receive.c:len_q Unexecuted instantiation: select_core.c:len_q Unexecuted instantiation: action.c:len_q Unexecuted instantiation: cfg.tab.c:len_q Unexecuted instantiation: dset.c:len_q Unexecuted instantiation: fmsg.c:len_q Unexecuted instantiation: kemi.c:len_q |
99 | | |
100 | | |
101 | | /* |
102 | | * Convert qvalue_t to double |
103 | | */ |
104 | | static inline double q2double(qvalue_t q) |
105 | 0 | { |
106 | 0 | if(q == Q_UNSPECIFIED) { |
107 | 0 | return -1; |
108 | 0 | } else { |
109 | 0 | return (double)((double)q / (double)1000); |
110 | 0 | } |
111 | 0 | } Unexecuted instantiation: main.c:q2double Unexecuted instantiation: receive.c:q2double Unexecuted instantiation: select_core.c:q2double Unexecuted instantiation: action.c:q2double Unexecuted instantiation: cfg.tab.c:q2double Unexecuted instantiation: dset.c:q2double Unexecuted instantiation: fmsg.c:q2double Unexecuted instantiation: kemi.c:q2double |
112 | | |
113 | | |
114 | | /* |
115 | | * Convert double to qvalue_t |
116 | | */ |
117 | | static inline qvalue_t double2q(double q) |
118 | 0 | { |
119 | 0 | if(q == -1) { |
120 | 0 | return Q_UNSPECIFIED; |
121 | 0 | } else { |
122 | 0 | return q * 1000; |
123 | 0 | } |
124 | 0 | } Unexecuted instantiation: main.c:double2q Unexecuted instantiation: receive.c:double2q Unexecuted instantiation: select_core.c:double2q Unexecuted instantiation: action.c:double2q Unexecuted instantiation: cfg.tab.c:double2q Unexecuted instantiation: dset.c:double2q Unexecuted instantiation: fmsg.c:double2q Unexecuted instantiation: kemi.c:double2q |
125 | | |
126 | | |
127 | | /* |
128 | | * Convert q value to string |
129 | | */ |
130 | | static inline char *q2str(qvalue_t q, unsigned int *len) |
131 | 0 | { |
132 | 0 | static char buf[sizeof("0.123")]; |
133 | 0 | char *p; |
134 | |
|
135 | 0 | p = buf; |
136 | 0 | if(q == Q_UNSPECIFIED) { |
137 | | /* Do nothing */ |
138 | 0 | } else if(q >= MAX_Q) { |
139 | 0 | memcpy(p, MAX_Q_STR, MAX_Q_STR_LEN); |
140 | 0 | p += MAX_Q_STR_LEN; |
141 | 0 | } else if(q <= MIN_Q) { |
142 | 0 | memcpy(p, MIN_Q_STR, MIN_Q_STR_LEN); |
143 | 0 | p += MIN_Q_STR_LEN; |
144 | 0 | } else { |
145 | 0 | memcpy(p, Q_PREFIX, Q_PREFIX_LEN); |
146 | 0 | p += Q_PREFIX_LEN; |
147 | |
|
148 | 0 | *p++ = q / 100 + '0'; |
149 | 0 | q %= 100; |
150 | 0 | if(!q) |
151 | 0 | goto end; |
152 | | |
153 | 0 | *p++ = q / 10 + '0'; |
154 | 0 | q %= 10; |
155 | 0 | if(!q) |
156 | 0 | goto end; |
157 | | |
158 | 0 | *p++ = q + '0'; |
159 | 0 | } |
160 | 0 | end: |
161 | 0 | *p = '\0'; |
162 | 0 | if(len) { |
163 | 0 | *len = p - buf; |
164 | 0 | } |
165 | 0 | return buf; |
166 | 0 | } Unexecuted instantiation: main.c:q2str Unexecuted instantiation: receive.c:q2str Unexecuted instantiation: select_core.c:q2str Unexecuted instantiation: action.c:q2str Unexecuted instantiation: cfg.tab.c:q2str Unexecuted instantiation: dset.c:q2str Unexecuted instantiation: fmsg.c:q2str Unexecuted instantiation: kemi.c:q2str |
167 | | |
168 | | |
169 | | /* |
170 | | * Convert string representation of q parameter in qvalue_t |
171 | | */ |
172 | | int str2q(qvalue_t *q, char *s, int len); |
173 | | |
174 | | |
175 | | #endif /* _QVALUE_H */ |