qvalue.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * [email protected]
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * History
  30. * ------
  31. * 2004-04-25 created (janakj)
  32. */
  33. #ifndef _QVALUE_H
  34. #define _QVALUE_H 1
  35. #include <string.h>
  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. typedef int qvalue_t;
  59. /*
  60. * Use this if the value of q is not specified
  61. */
  62. #define Q_UNSPECIFIED ((qvalue_t)-1)
  63. #define MAX_Q ((qvalue_t)1000)
  64. #define MIN_Q ((qvalue_t)0)
  65. #define MAX_Q_STR "1"
  66. #define MAX_Q_STR_LEN (sizeof(MAX_Q_STR) - 1)
  67. #define MIN_Q_STR "0"
  68. #define MIN_Q_STR_LEN (sizeof(MIN_Q_STR) - 1)
  69. #define Q_PREFIX "0."
  70. #define Q_PREFIX_LEN (sizeof(Q_PREFIX) - 1)
  71. /*
  72. * Calculate the length of printed q
  73. */
  74. static inline size_t len_q(qvalue_t q)
  75. {
  76. if (q == Q_UNSPECIFIED) {
  77. return 0;
  78. } else if (q >= MAX_Q) {
  79. return MAX_Q_STR_LEN;
  80. } else if (q <= MIN_Q) {
  81. return MIN_Q_STR_LEN;
  82. } else if (q % 100 == 0) {
  83. return Q_PREFIX_LEN + 1;
  84. } else if (q % 10 == 0) {
  85. return Q_PREFIX_LEN + 2;
  86. } else {
  87. return Q_PREFIX_LEN + 3;
  88. }
  89. }
  90. /*
  91. * Convert qvalue_t to double
  92. */
  93. static inline double q2double(qvalue_t q)
  94. {
  95. if (q == Q_UNSPECIFIED) {
  96. return -1;
  97. } else {
  98. return (double)((double)q / (double)1000);
  99. }
  100. }
  101. /*
  102. * Convert double to qvalue_t
  103. */
  104. static inline qvalue_t double2q(double q)
  105. {
  106. if (q == -1) {
  107. return Q_UNSPECIFIED;
  108. } else {
  109. return q * 1000;
  110. }
  111. }
  112. /*
  113. * Convert q value to string
  114. */
  115. static inline char* q2str(qvalue_t q, unsigned int* len)
  116. {
  117. static char buf[sizeof("0.123")];
  118. char* p;
  119. p = buf;
  120. if (q == Q_UNSPECIFIED) {
  121. /* Do nothing */
  122. } else if (q >= MAX_Q) {
  123. memcpy(p, MAX_Q_STR, MAX_Q_STR_LEN);
  124. p += MAX_Q_STR_LEN;
  125. } else if (q <= MIN_Q) {
  126. memcpy(p, MIN_Q_STR, MIN_Q_STR_LEN);
  127. p += MIN_Q_STR_LEN;
  128. } else {
  129. memcpy(p, Q_PREFIX, Q_PREFIX_LEN);
  130. p += Q_PREFIX_LEN;
  131. *p++ = q / 100 + '0';
  132. q %= 100;
  133. if (!q) goto end;
  134. *p++ = q / 10 + '0';
  135. q %= 10;
  136. if (!q) goto end;
  137. *p++ = q + '0';
  138. }
  139. end:
  140. *p = '\0';
  141. if (len) {
  142. *len = p - buf;
  143. }
  144. return buf;
  145. }
  146. /*
  147. * Convert string representation of q parameter in qvalue_t
  148. */
  149. int str2q(qvalue_t* q, char* s, int len);
  150. #endif /* _QVALUE_H */