tsdp_parser_header_B.rl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2010-2015 Mamadou Diop.
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. *
  19. */
  20. /**@file tsdp_header_B.c
  21. * @brief SDP "b=" header (Bandwidth).
  22. *
  23. */
  24. #include "tinysdp/headers/tsdp_header_B.h"
  25. #include "tsk_debug.h"
  26. #include "tsk_memory.h"
  27. #include "tsk_string.h"
  28. #include <string.h>
  29. /***********************************
  30. * Ragel state machine.
  31. */
  32. %%{
  33. machine tsdp_machine_parser_header_B;
  34. # Includes
  35. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  36. action tag{
  37. tag_start = p;
  38. }
  39. action parse_bwtype{
  40. TSK_PARSER_SET_STRING(hdr_B->bwtype);
  41. }
  42. action parse_bandwidth{
  43. TSK_PARSER_SET_UINT(hdr_B->bandwidth);
  44. }
  45. bwtype = token>tag %parse_bwtype;
  46. bandwidth = DIGIT+>tag %parse_bandwidth;
  47. B = 'b' SP* "=" SP*<: bwtype ":" bandwidth;
  48. # Entry point
  49. main := B :>CRLF?;
  50. }%%
  51. tsdp_header_B_t* tsdp_header_B_create(const char* bwtype, uint32_t bandwidth)
  52. {
  53. return tsk_object_new(TSDP_HEADER_B_VA_ARGS(bwtype, bandwidth));
  54. }
  55. tsdp_header_B_t* tsdp_header_B_create_null()
  56. {
  57. return tsdp_header_B_create(tsk_null, 0);
  58. }
  59. int tsdp_header_B_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  60. {
  61. if(header){
  62. const tsdp_header_B_t *B = (const tsdp_header_B_t *)header;
  63. return tsk_buffer_append_2(output, "%s:%u",
  64. B->bwtype,
  65. B->bandwidth
  66. );
  67. }
  68. return -1;
  69. }
  70. tsdp_header_t* tsdp_header_B_clone(const tsdp_header_t* header)
  71. {
  72. if(header){
  73. const tsdp_header_B_t *B = (const tsdp_header_B_t *)header;
  74. return (tsdp_header_t*)tsdp_header_B_create(B->bwtype, B->bandwidth);
  75. }
  76. return tsk_null;
  77. }
  78. tsdp_header_B_t *tsdp_header_B_parse(const char *data, tsk_size_t size)
  79. {
  80. int cs = 0;
  81. const char *p = data;
  82. const char *pe = p + size;
  83. const char *eof = pe;
  84. tsdp_header_B_t *hdr_B = tsdp_header_B_create_null();
  85. const char *tag_start = tsk_null;
  86. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  87. %%write data;
  88. (void)(tsdp_machine_parser_header_B_first_final);
  89. (void)(tsdp_machine_parser_header_B_error);
  90. (void)(tsdp_machine_parser_header_B_en_main);
  91. %%write init;
  92. %%write exec;
  93. TSK_RAGEL_DISABLE_WARNINGS_END()
  94. if( cs < %%{ write first_final; }%% ){
  95. TSK_DEBUG_ERROR("Failed to parse \"b=\" header.");
  96. TSK_OBJECT_SAFE_FREE(hdr_B);
  97. }
  98. return hdr_B;
  99. }
  100. //========================================================
  101. // B header object definition
  102. //
  103. static tsk_object_t* tsdp_header_B_ctor(tsk_object_t *self, va_list * app)
  104. {
  105. tsdp_header_B_t *B = self;
  106. if(B){
  107. TSDP_HEADER(B)->type = tsdp_htype_B;
  108. TSDP_HEADER(B)->tostring = tsdp_header_B_tostring;
  109. TSDP_HEADER(B)->clone = tsdp_header_B_clone;
  110. TSDP_HEADER(B)->rank = TSDP_HTYPE_B_RANK;
  111. B->bwtype = tsk_strdup(va_arg(*app, const char*));
  112. B->bandwidth = va_arg(*app, uint32_t);
  113. }
  114. else{
  115. TSK_DEBUG_ERROR("Failed to create new B header.");
  116. }
  117. return self;
  118. }
  119. static tsk_object_t* tsdp_header_B_dtor(tsk_object_t *self)
  120. {
  121. tsdp_header_B_t *B = self;
  122. if(B){
  123. TSK_FREE(B->bwtype);
  124. }
  125. else{
  126. TSK_DEBUG_ERROR("Null B header.");
  127. }
  128. return self;
  129. }
  130. static int tsdp_header_B_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  131. {
  132. if(obj1 && obj2){
  133. return tsdp_header_rank_cmp(obj1, obj2);
  134. }
  135. else{
  136. return -1;
  137. }
  138. }
  139. static const tsk_object_def_t tsdp_header_B_def_s =
  140. {
  141. sizeof(tsdp_header_B_t),
  142. tsdp_header_B_ctor,
  143. tsdp_header_B_dtor,
  144. tsdp_header_B_cmp
  145. };
  146. const tsk_object_def_t *tsdp_header_B_def_t = &tsdp_header_B_def_s;