tsdp_parser_header_A.rl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_A.c
  21. * @brief SDP "a=" header (Attributes).
  22. *
  23. */
  24. #include "tinysdp/headers/tsdp_header_A.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_A;
  34. # Includes
  35. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  36. action tag{
  37. tag_start = p;
  38. }
  39. action parse_field{
  40. TSK_PARSER_SET_STRING(hdr_A->field);
  41. }
  42. action parse_value{
  43. TSK_PARSER_SET_STRING(hdr_A->value);
  44. }
  45. field = token>tag %parse_field;
  46. value = any*>tag %parse_value;
  47. A = 'a' SP* "=" SP*<: ((field ":" value) | (field));
  48. # Entry point
  49. main := A :>CRLF?;
  50. }%%
  51. tsdp_header_A_t* tsdp_header_A_create(const char* field, const char* value)
  52. {
  53. return tsk_object_new(TSDP_HEADER_A_VA_ARGS(field, value));
  54. }
  55. tsdp_header_A_t* tsdp_header_A_create_null()
  56. {
  57. return tsdp_header_A_create(tsk_null, tsk_null);
  58. }
  59. int tsdp_header_A_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  60. {
  61. if(header){
  62. const tsdp_header_A_t *A = (const tsdp_header_A_t *)header;
  63. return tsk_buffer_append_2(output, "%s%s%s",
  64. A->field,
  65. A->value ? ":" : "",
  66. A->value ? A->value : ""
  67. );
  68. }
  69. return -1;
  70. }
  71. tsdp_header_t* tsdp_header_A_clone(const tsdp_header_t* header)
  72. {
  73. if(header){
  74. const tsdp_header_A_t *A = (const tsdp_header_A_t *)header;
  75. return (tsdp_header_t*)tsdp_header_A_create(A->field, A->value);
  76. }
  77. return tsk_null;
  78. }
  79. tsdp_header_A_t *tsdp_header_A_parse(const char *data, tsk_size_t size)
  80. {
  81. int cs = 0;
  82. const char *p = data;
  83. const char *pe = p + size;
  84. const char *eof = pe;
  85. tsdp_header_A_t *hdr_A = tsdp_header_A_create_null();
  86. const char *tag_start = tsk_null;
  87. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  88. %%write data;
  89. %%write init;
  90. (void)(tsdp_machine_parser_header_A_first_final);
  91. (void)(tsdp_machine_parser_header_A_error);
  92. (void)(tsdp_machine_parser_header_A_en_main);
  93. %%write exec;
  94. TSK_RAGEL_DISABLE_WARNINGS_END()
  95. if( cs < %%{ write first_final; }%% ){
  96. TSK_DEBUG_ERROR("Failed to parse \"a=\" header.");
  97. TSK_OBJECT_SAFE_FREE(hdr_A);
  98. }
  99. return hdr_A;
  100. }
  101. int tsdp_header_A_removeAll_by_field(tsdp_headers_A_L_t *attributes, const char* field)
  102. {
  103. tsk_list_item_t* item;
  104. const tsdp_header_A_t* A;
  105. if(!attributes || !field){
  106. TSK_DEBUG_ERROR("Invalid parameter");
  107. return -1;
  108. }
  109. again:
  110. tsk_list_foreach(item, attributes){
  111. if(!(A = item->data) || TSDP_HEADER(A)->type != tsdp_htype_A){
  112. continue;
  113. }
  114. if(tsk_striequals(field, A->field)){
  115. tsk_list_remove_item(attributes, item);
  116. goto again;
  117. }
  118. }
  119. return 0;
  120. }
  121. int tsdp_header_A_removeAll_by_fields(tsdp_headers_A_L_t *attributes, const char** fields, tsk_size_t fields_count)
  122. {
  123. tsk_size_t i;
  124. if(!attributes || !fields){
  125. TSK_DEBUG_ERROR("Invalid parameter");
  126. return -1;
  127. }
  128. for(i = 0; i < fields_count; ++i){
  129. if(!fields[i]){
  130. continue;
  131. }
  132. tsdp_header_A_removeAll_by_field(attributes, fields[i]);
  133. }
  134. return 0;
  135. }
  136. //========================================================
  137. // A header object definition
  138. //
  139. static tsk_object_t* tsdp_header_A_ctor(tsk_object_t *self, va_list * app)
  140. {
  141. tsdp_header_A_t *A = self;
  142. if(A)
  143. {
  144. TSDP_HEADER(A)->type = tsdp_htype_A;
  145. TSDP_HEADER(A)->tostring = tsdp_header_A_tostring;
  146. TSDP_HEADER(A)->clone = tsdp_header_A_clone;
  147. TSDP_HEADER(A)->rank = TSDP_HTYPE_A_RANK;
  148. A->field = tsk_strdup(va_arg(*app, const char*));
  149. A->value = tsk_strdup(va_arg(*app, const char*));
  150. }
  151. else{
  152. TSK_DEBUG_ERROR("Failed to create new A header.");
  153. }
  154. return self;
  155. }
  156. static tsk_object_t* tsdp_header_A_dtor(tsk_object_t *self)
  157. {
  158. tsdp_header_A_t *A = self;
  159. if(A){
  160. TSK_FREE(A->field);
  161. TSK_FREE(A->value);
  162. }
  163. else{
  164. TSK_DEBUG_ERROR("Null A header.");
  165. }
  166. return self;
  167. }
  168. static int tsdp_header_A_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  169. {
  170. if(obj1 && obj2){
  171. return tsdp_header_rank_cmp(obj1, obj2);
  172. }
  173. else{
  174. return -1;
  175. }
  176. }
  177. static const tsk_object_def_t tsdp_header_A_def_s =
  178. {
  179. sizeof(tsdp_header_A_t),
  180. tsdp_header_A_ctor,
  181. tsdp_header_A_dtor,
  182. tsdp_header_A_cmp
  183. };
  184. const tsk_object_def_t *tsdp_header_A_def_t = &tsdp_header_A_def_s;