tsdp_parser_header_R.rl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_R.c
  21. * @brief SDP "r=" header (Repeat Times).
  22. *
  23. */
  24. #include "tinysdp/headers/tsdp_header_R.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_R;
  34. # Includes
  35. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  36. action tag{
  37. tag_start = p;
  38. }
  39. action parse_repeat_interval{
  40. TSK_PARSER_SET_STRING(hdr_R->repeat_interval);
  41. }
  42. action parse_typed_time{
  43. if(!hdr_R->typed_time){
  44. TSK_PARSER_SET_STRING(hdr_R->typed_time);
  45. }
  46. else{
  47. TSK_PARSER_ADD_STRING(hdr_R->typed_times);
  48. }
  49. }
  50. fixed_len_time_unit = "d" | "h" | "m" | "s";
  51. repeat_interval = (DIGIT+ fixed_len_time_unit?) >tag %parse_repeat_interval;
  52. typed_time = (DIGIT+ fixed_len_time_unit?) >tag %parse_typed_time;
  53. R = 'r' SP* "=" SP*<: repeat_interval :>SP typed_time (SP<: typed_time)+;
  54. # Entry point
  55. main := R :>CRLF?;
  56. }%%
  57. tsdp_header_R_t* tsdp_header_R_create()
  58. {
  59. return tsk_object_new(TSDP_HEADER_R_VA_ARGS());
  60. }
  61. tsdp_header_R_t* tsdp_header_R_create_null()
  62. {
  63. return tsdp_header_R_create();
  64. }
  65. int tsdp_header_R_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  66. {
  67. if(header){
  68. const tsdp_header_R_t *R = (const tsdp_header_R_t *)header;
  69. const tsk_list_item_t* item;
  70. // r=7d 1h 0 25h
  71. tsk_buffer_append_2(output, "%s %s",
  72. R->repeat_interval,
  73. R->typed_time
  74. );
  75. tsk_list_foreach(item, R->typed_times){
  76. tsk_string_t* string = item->data;
  77. tsk_buffer_append_2(output, " %s", TSK_STRING_STR(string));
  78. }
  79. return 0;
  80. }
  81. return -1;
  82. }
  83. tsdp_header_t* tsdp_header_R_clone(const tsdp_header_t* header)
  84. {
  85. if(header){
  86. const tsdp_header_R_t *R = (const tsdp_header_R_t *)header;
  87. tsdp_header_R_t* clone;
  88. const tsk_list_item_t* item;
  89. if((clone = tsdp_header_R_create_null())){
  90. clone->repeat_interval = tsk_strdup(R->repeat_interval);
  91. clone->typed_time = tsk_strdup(R->typed_time);
  92. if(R->typed_times){
  93. clone->typed_times = tsk_list_create();
  94. }
  95. tsk_list_foreach(item, R->typed_times){
  96. tsk_string_t* string = tsk_string_create(TSK_STRING_STR(item->data));
  97. tsk_list_push_back_data(clone->typed_times, (void**)&string);
  98. }
  99. }
  100. return TSDP_HEADER(clone);
  101. }
  102. return tsk_null;
  103. }
  104. tsdp_header_R_t *tsdp_header_R_parse(const char *data, tsk_size_t size)
  105. {
  106. int cs = 0;
  107. const char *p = data;
  108. const char *pe = p + size;
  109. const char *eof = pe;
  110. tsdp_header_R_t *hdr_R = tsdp_header_R_create_null();
  111. const char *tag_start = tsk_null;
  112. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  113. %%write data;
  114. (void)(tsdp_machine_parser_header_R_first_final);
  115. (void)(tsdp_machine_parser_header_R_error);
  116. (void)(tsdp_machine_parser_header_R_en_main);
  117. %%write init;
  118. %%write exec;
  119. TSK_RAGEL_DISABLE_WARNINGS_END()
  120. if( cs < %%{ write first_final; }%% ){
  121. TSK_DEBUG_ERROR("Failed to parse \"r=\" header.");
  122. TSK_OBJECT_SAFE_FREE(hdr_R);
  123. }
  124. return hdr_R;
  125. }
  126. //========================================================
  127. // E header object definition
  128. //
  129. static tsk_object_t* tsdp_header_R_ctor(tsk_object_t *self, va_list * app)
  130. {
  131. tsdp_header_R_t *R = self;
  132. if(R){
  133. TSDP_HEADER(R)->type = tsdp_htype_R;
  134. TSDP_HEADER(R)->tostring = tsdp_header_R_tostring;
  135. TSDP_HEADER(R)->clone = tsdp_header_R_clone;
  136. TSDP_HEADER(R)->rank = TSDP_HTYPE_R_RANK;
  137. }
  138. else{
  139. TSK_DEBUG_ERROR("Failed to create new E header.");
  140. }
  141. return self;
  142. }
  143. static tsk_object_t* tsdp_header_R_dtor(tsk_object_t *self)
  144. {
  145. tsdp_header_R_t *R = self;
  146. if(R){
  147. TSK_FREE(R->repeat_interval);
  148. TSK_FREE(R->typed_time);
  149. TSK_OBJECT_SAFE_FREE(R->typed_times);
  150. }
  151. else{
  152. TSK_DEBUG_ERROR("Null R header.");
  153. }
  154. return self;
  155. }
  156. static int tsdp_header_R_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  157. {
  158. if(obj1 && obj2){
  159. return tsdp_header_rank_cmp(obj1, obj2);
  160. }
  161. else{
  162. return -1;
  163. }
  164. }
  165. static const tsk_object_def_t tsdp_header_R_def_s =
  166. {
  167. sizeof(tsdp_header_R_t),
  168. tsdp_header_R_ctor,
  169. tsdp_header_R_dtor,
  170. tsdp_header_R_cmp
  171. };
  172. const tsk_object_def_t *tsdp_header_R_def_t = &tsdp_header_R_def_s;