tsdp_parser_header_T.rl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_T.c
  21. * @brief SDP "t=" header (Timing).
  22. */
  23. #include "tinysdp/headers/tsdp_header_T.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 tsdp_machine_parser_header_T;
  33. # Includes
  34. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_start_time{
  39. TSK_PARSER_SET_INTEGER_EX(hdr_T->start, uint64_t, atoi64);
  40. }
  41. action parse_stop_time{
  42. TSK_PARSER_SET_INTEGER_EX(hdr_T->stop, uint64_t, atoi64);
  43. }
  44. action parse_repeat_fields{
  45. tsdp_header_R_t* header_R;
  46. if((header_R = tsdp_header_R_parse(tag_start, (p - tag_start)))){
  47. if(!hdr_T->repeat_fields){
  48. hdr_T->repeat_fields = tsk_list_create();
  49. }
  50. tsk_list_push_back_data(hdr_T->repeat_fields, (void**)&header_R);
  51. }
  52. }
  53. start_time = DIGIT+ >tag %parse_start_time;
  54. stop_time = DIGIT+ >tag %parse_stop_time;
  55. repeat_fields = any+ >tag %parse_repeat_fields;
  56. T = 't' SP* "=" SP*<: start_time :>SP stop_time (CRLF <:repeat_fields)?;
  57. # Entry point
  58. main := T :>CRLF?;
  59. }%%
  60. tsdp_header_T_t* tsdp_header_T_create(uint64_t start, uint64_t stop)
  61. {
  62. return tsk_object_new(TSDP_HEADER_T_VA_ARGS(start, stop));
  63. }
  64. tsdp_header_T_t* tsdp_header_T_create_null()
  65. {
  66. return tsdp_header_T_create(0, 0);
  67. }
  68. int tsdp_header_T_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  69. {
  70. if(header)
  71. {
  72. const tsdp_header_T_t *T = (const tsdp_header_T_t *)header;
  73. const tsk_list_item_t *item;
  74. //"t=3034423619 3042462419\r\n"
  75. //"r=7d 1h 0 25h\r\n"
  76. // IMPORTANT: Do not append the last CRLF (because we only print the header value).
  77. tsk_buffer_append_2(output, "%llu %llu",
  78. T->start,
  79. T->stop
  80. );
  81. tsk_list_foreach(item, T->repeat_fields)
  82. {
  83. if(TSK_LIST_IS_FIRST(T->repeat_fields, item)){
  84. tsk_buffer_append(output, "\r\n", 2);
  85. }
  86. tsk_buffer_append_2(output, "%c=", tsdp_header_get_nameex(TSDP_HEADER(item->data)));
  87. TSDP_HEADER(item->data)->tostring(TSDP_HEADER(item->data), output);
  88. //tsdp_header_tostring(TSDP_HEADER(item->data), output);
  89. if(!TSK_LIST_IS_LAST(T->repeat_fields, item)){
  90. tsk_buffer_append(output, "\r\n", 2);
  91. }
  92. }
  93. return 0;
  94. }
  95. return -1;
  96. }
  97. tsdp_header_t* tsdp_header_T_clone(const tsdp_header_t* header)
  98. {
  99. if(header){
  100. const tsdp_header_T_t *T = (const tsdp_header_T_t *)header;
  101. tsdp_header_T_t* clone;
  102. const tsk_list_item_t *item;
  103. if((clone = tsdp_header_T_create(T->start, T->stop))){
  104. if(T->repeat_fields){
  105. clone->repeat_fields = tsk_list_create();
  106. }
  107. tsk_list_foreach(item, T->repeat_fields){
  108. const tsdp_header_t* curr = item->data;
  109. tsdp_header_t* hdr_clone = curr->clone(curr);
  110. tsk_list_push_back_data(clone->repeat_fields, (void**)&hdr_clone);
  111. }
  112. }
  113. return TSDP_HEADER(clone);
  114. }
  115. return tsk_null;
  116. }
  117. tsdp_header_T_t *tsdp_header_T_parse(const char *data, tsk_size_t size)
  118. {
  119. int cs = 0;
  120. const char *p = data;
  121. const char *pe = p + size;
  122. const char *eof = pe;
  123. tsdp_header_T_t *hdr_T = tsdp_header_T_create_null();
  124. const char *tag_start = tsk_null;
  125. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  126. %%write data;
  127. (void)(tsdp_machine_parser_header_T_first_final);
  128. (void)(tsdp_machine_parser_header_T_error);
  129. (void)(tsdp_machine_parser_header_T_en_main);
  130. %%write init;
  131. %%write exec;
  132. TSK_RAGEL_DISABLE_WARNINGS_END()
  133. if( cs < %%{ write first_final; }%% ){
  134. TSK_DEBUG_ERROR("Failed to parse \"t=\" header.");
  135. TSK_OBJECT_SAFE_FREE(hdr_T);
  136. }
  137. return hdr_T;
  138. }
  139. //========================================================
  140. // T header object definition
  141. //
  142. static tsk_object_t* tsdp_header_T_ctor(tsk_object_t *self, va_list * app)
  143. {
  144. tsdp_header_T_t *T = self;
  145. if(T){
  146. TSDP_HEADER(T)->type = tsdp_htype_T;
  147. TSDP_HEADER(T)->tostring = tsdp_header_T_tostring;
  148. TSDP_HEADER(T)->clone = tsdp_header_T_clone;
  149. TSDP_HEADER(T)->rank = TSDP_HTYPE_T_RANK;
  150. }
  151. else{
  152. TSK_DEBUG_ERROR("Failed to create new U header.");
  153. }
  154. return self;
  155. }
  156. static tsk_object_t* tsdp_header_T_dtor(tsk_object_t *self)
  157. {
  158. tsdp_header_T_t *T = self;
  159. if(T){
  160. TSK_OBJECT_SAFE_FREE(T->repeat_fields);
  161. }
  162. else{
  163. TSK_DEBUG_ERROR("Null U header.");
  164. }
  165. return self;
  166. }
  167. static int tsdp_header_T_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  168. {
  169. if(obj1 && obj2){
  170. return tsdp_header_rank_cmp(obj1, obj2);
  171. }
  172. else{
  173. return -1;
  174. }
  175. }
  176. static const tsk_object_def_t tsdp_header_T_def_s =
  177. {
  178. sizeof(tsdp_header_T_t),
  179. tsdp_header_T_ctor,
  180. tsdp_header_T_dtor,
  181. tsdp_header_T_cmp
  182. };
  183. const tsk_object_def_t *tsdp_header_T_def_t = &tsdp_header_T_def_s;