Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

qvalue.h

Go to the documentation of this file.
00001 /*
00002  * $Id: qvalue.h,v 1.4 2004/04/27 14:52:57 janakj Exp $
00003  *
00004  * Handling of the q value
00005  *
00006  * Copyright (C) 2004 FhG FOKUS
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License 
00026  * along with this program; if not, write to the Free Software 
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  * History
00030  * ------
00031  * 2004-04-25 created (janakj)
00032  */
00033 
00034 #ifndef _QVALUE_H
00035 #define _QVALUE_H 1
00036 
00037 #include <string.h>
00038 
00039 /*
00040  * The q value expresses the priority of a URI within a set of URIs 
00041  * (Contact header field in the same SIP message or dset array in 
00042  * ser. The higher is the q value of a URI the higher is the priority 
00043  * of the URI.
00044  *
00045  * The q value is usually expressed as a floating point number with 
00046  * limited number of decimal digits, for example 0.346. RFC3261 allows 
00047  * 0-3 decimal digits.
00048  *
00049  * To speed things up we represent the q value as integer number, it 
00050  * is then easier to handle/print the value. To convert float into 
00051  * integer we multiply the q value by 1000, i.e. 
00052  * (float)0.567 == (int)567. In the opposite direction, values 
00053  * higher or equal to 1000 are converted to 1.0 and values below or 
00054  * equal to 0 are converted to 0.
00055  *
00056  * Value Q_UNSPECIFIED (which is in fact -1) has a special meaning, it 
00057  * means that the q value is not known and the parameter should not be 
00058  * printed when printing Contacts, implementations will then use 
00059  * implementation specific pre-defined values.
00060  */
00061 
00062 typedef int qvalue_t;
00063 
00064 /*
00065  * Use this if the value of q is not specified
00066  */
00067 #define Q_UNSPECIFIED ((qvalue_t)-1)
00068 
00069 
00070 #define MAX_Q ((qvalue_t)1000)
00071 #define MIN_Q ((qvalue_t)0)
00072 
00073 #define MAX_Q_STR "1"
00074 #define MAX_Q_STR_LEN (sizeof(MAX_Q_STR) - 1)
00075 
00076 #define MIN_Q_STR "0"
00077 #define MIN_Q_STR_LEN (sizeof(MIN_Q_STR) - 1)
00078 
00079 #define Q_PREFIX "0."
00080 #define Q_PREFIX_LEN (sizeof(Q_PREFIX) - 1)
00081 
00082 
00083 
00084 /*
00085  * Calculate the length of printed q
00086  */
00087 static inline size_t len_q(qvalue_t q)
00088 {
00089         if (q == Q_UNSPECIFIED) {
00090                 return 0;
00091         } else if (q >= MAX_Q) {
00092                 return MAX_Q_STR_LEN;
00093         } else if (q <= MIN_Q) {
00094                 return MIN_Q_STR_LEN;
00095         } else if (q % 100 == 0) {
00096                 return Q_PREFIX_LEN + 1;
00097         } else if (q % 10 == 0) {
00098                 return Q_PREFIX_LEN + 2;
00099         } else {
00100                 return Q_PREFIX_LEN + 3;
00101         }
00102 }
00103 
00104 
00105 /*
00106  * Convert qvalue_t to double
00107  */
00108 static inline double q2double(qvalue_t q)
00109 {
00110         if (q == Q_UNSPECIFIED) {
00111                 return -1;
00112         } else {
00113                 return (double)((double)q / (double)1000);
00114         }
00115 }
00116 
00117 
00118 /*
00119  * Convert double to qvalue_t
00120  */
00121 static inline qvalue_t double2q(double q)
00122 {
00123         if (q == -1) {
00124                 return Q_UNSPECIFIED;
00125         } else {
00126                 return q * 1000;
00127         }
00128 }
00129 
00130 
00131 /*
00132  * Convert q value to string
00133  */
00134 static inline char* q2str(qvalue_t q, unsigned int* len)
00135 {
00136         static char buf[sizeof("0.123")];
00137         char* p;
00138 
00139         p = buf;
00140         if (q == Q_UNSPECIFIED) {
00141                      /* Do nothing */
00142         } else if (q >= MAX_Q) {
00143                 memcpy(p, MAX_Q_STR, MAX_Q_STR_LEN);
00144                 p += MAX_Q_STR_LEN;
00145         } else if (q <= MIN_Q) {
00146                 memcpy(p, MIN_Q_STR, MIN_Q_STR_LEN);
00147                 p += MIN_Q_STR_LEN;
00148         } else {
00149                 memcpy(p, Q_PREFIX, Q_PREFIX_LEN);
00150                 p += Q_PREFIX_LEN;
00151                 
00152                 *p++ = q / 100 + '0';
00153                 q %= 100;
00154                 if (!q) goto end;
00155 
00156                 *p++ = q / 10 + '0';
00157                 q %= 10;
00158                 if (!q) goto end;
00159 
00160                 *p++ = q + '0';
00161         }
00162  end:
00163         *p = '\0';
00164         if (len) {
00165                 *len = p - buf;
00166         }
00167         return buf;
00168 }
00169 
00170 
00171 /*
00172  * Convert string representation of q parameter in qvalue_t
00173  */
00174 int str2q(qvalue_t* q, char* s, int len);
00175 
00176 
00177 #endif /* _QVALUE_H */

Generated on Tue Sep 7 04:16:13 2010 for SIPExpressRouter by  doxygen 1.3.9.1