tmsrp_parser_header_To-Path.rl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2009-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 tmsrp_header_To_Path.c
  21. * @brief MSRP "To-Path" header.
  22. */
  23. #include "tinymsrp/headers/tmsrp_header_To-Path.h"
  24. #include "tsk_debug.h"
  25. #include "tsk_memory.h"
  26. #include "tsk_string.h"
  27. #include <string.h>
  28. /***********************************
  29. * Ragel state machine.
  30. */
  31. %%{
  32. machine tmsrp_machine_parser_header_To_Path;
  33. # Includes
  34. include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_uri{
  39. int len = (int)(p - tag_start);
  40. tmsrp_uri_t* uri;
  41. if((uri = tmsrp_uri_parse(tag_start, (tsk_size_t)len))){
  42. if(!header->uri){
  43. header->uri = uri;
  44. }
  45. else{
  46. if(!header->otherURIs){
  47. header->otherURIs = tsk_list_create();
  48. }
  49. tsk_list_push_back_data(header->otherURIs, ((void**) &uri));
  50. }
  51. }
  52. }
  53. MSRP_URI = (any -- SP)* >tag %parse_uri;
  54. #//"To-Path:" SP MSRP-URI *( SP MSRP-URI )
  55. To_Path = "To-Path:"i SP MSRP_URI (SP <:MSRP_URI)*;
  56. # Entry point
  57. main := To_Path :>CRLF?;
  58. }%%
  59. tmsrp_header_To_Path_t* tmsrp_header_To_Path_create(const tmsrp_uri_t* uri)
  60. {
  61. return tsk_object_new(TMSRP_HEADER_TO_PATH_VA_ARGS(uri));
  62. }
  63. tmsrp_header_To_Path_t* tmsrp_header_To_Path_create_null()
  64. {
  65. return tmsrp_header_To_Path_create(tsk_null);
  66. }
  67. int tmsrp_header_To_Path_tostring(const tmsrp_header_t* header, tsk_buffer_t* output)
  68. {
  69. if(header){
  70. const tmsrp_header_To_Path_t *To_Path = (const tmsrp_header_To_Path_t *)header;
  71. const tsk_list_item_t *item;
  72. if(To_Path->uri){
  73. tmsrp_uri_serialize(To_Path->uri, output);
  74. }
  75. tsk_list_foreach(item, To_Path->otherURIs){
  76. tsk_buffer_append(output, " ", 1);
  77. tmsrp_uri_serialize(TMSRP_URI(item->data), output);
  78. }
  79. }
  80. return -1;
  81. }
  82. tmsrp_header_To_Path_t *tmsrp_header_To_Path_parse(const char *data, tsk_size_t size)
  83. {
  84. int cs = 0;
  85. const char *p = data;
  86. const char *pe = p + size;
  87. const char *eof = pe;
  88. tmsrp_header_To_Path_t *header = tmsrp_header_To_Path_create_null();
  89. const char *tag_start = tsk_null;
  90. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  91. %%write data;
  92. (void)(eof);
  93. (void)(tmsrp_machine_parser_header_To_Path_first_final);
  94. (void)(tmsrp_machine_parser_header_To_Path_error);
  95. (void)(tmsrp_machine_parser_header_To_Path_en_main);
  96. %%write init;
  97. %%write exec;
  98. TSK_RAGEL_DISABLE_WARNINGS_END()
  99. if( cs < %%{ write first_final; }%% ){
  100. TSK_DEBUG_ERROR("Failed to parse 'To-Path' header.");
  101. TSK_OBJECT_SAFE_FREE(header);
  102. }
  103. return header;
  104. }
  105. tmsrp_header_To_Path_t *tmsrp_header_To_Path_clone(const tmsrp_header_To_Path_t* To_Path)
  106. {
  107. tmsrp_header_To_Path_t* clone = tsk_null;
  108. if(!To_Path){
  109. goto bail;
  110. }
  111. clone = tmsrp_header_To_Path_create_null();
  112. clone->uri = tmsrp_uri_clone(To_Path->uri);
  113. if(To_Path->otherURIs){
  114. tsk_list_item_t *item;
  115. clone->otherURIs = tsk_list_create();
  116. tsk_list_foreach(item, To_Path->otherURIs){
  117. tmsrp_uri_t *uri = tmsrp_uri_clone(TMSRP_URI(item->data));
  118. tsk_list_push_back_data(clone->otherURIs, (void**)&uri);
  119. }
  120. }
  121. bail:
  122. return clone;
  123. }
  124. //========================================================
  125. // To_Path header object definition
  126. //
  127. static tsk_object_t* tmsrp_header_To_Path_ctor(tsk_object_t *self, va_list * app)
  128. {
  129. tmsrp_header_To_Path_t *To_Path = self;
  130. if(To_Path){
  131. TMSRP_HEADER(To_Path)->type = tmsrp_htype_To_Path;
  132. TMSRP_HEADER(To_Path)->tostring = tmsrp_header_To_Path_tostring;
  133. To_Path->uri = tsk_object_ref((void*)va_arg(*app, const tmsrp_uri_t*));
  134. }
  135. else{
  136. TSK_DEBUG_ERROR("Failed to create new To-Path header.");
  137. }
  138. return self;
  139. }
  140. static tsk_object_t* tmsrp_header_To_Path_dtor(tsk_object_t *self)
  141. {
  142. tmsrp_header_To_Path_t *To_Path = self;
  143. if(To_Path){
  144. TSK_OBJECT_SAFE_FREE(To_Path->uri);
  145. TSK_OBJECT_SAFE_FREE(To_Path->otherURIs);
  146. }
  147. else{
  148. TSK_DEBUG_ERROR("Null To-Path header.");
  149. }
  150. return self;
  151. }
  152. static const tsk_object_def_t tmsrp_header_To_Path_def_s =
  153. {
  154. sizeof(tmsrp_header_To_Path_t),
  155. tmsrp_header_To_Path_ctor,
  156. tmsrp_header_To_Path_dtor,
  157. tsk_null
  158. };
  159. const tsk_object_def_t *tmsrp_header_To_Path_def_t = &tmsrp_header_To_Path_def_s;