tsdp_parser_header_Z.rl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_Z.c
  21. * @brief SDP "z=" header (Time Zones).
  22. *
  23. */
  24. #include "tinysdp/headers/tsdp_header_Z.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_Z;
  34. # Includes
  35. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  36. action tag{
  37. tag_start = p;
  38. }
  39. action create_zone{
  40. if(!zone){
  41. zone = tsdp_zone_create_null();
  42. }
  43. }
  44. action add_zone{
  45. if(zone){
  46. tsk_list_push_back_data(hdr_Z->zones,(void**)&zone);
  47. }
  48. }
  49. action parse_time{
  50. if(zone){
  51. TSK_PARSER_SET_INTEGER_EX(zone->time, uint64_t, atoi64);
  52. }
  53. }
  54. action parse_typed_time{
  55. if(zone){
  56. TSK_PARSER_SET_STRING(zone->typed_time);
  57. }
  58. }
  59. action shifted{
  60. if(zone){
  61. zone->shifted_back = tsk_true;
  62. }
  63. }
  64. fixed_len_time_unit = "d" | "h" | "m" | "s";
  65. time = DIGIT+ >tag %parse_time;
  66. typed_time = (DIGIT+ fixed_len_time_unit?) >tag %parse_typed_time;
  67. Z = 'z' SP* "=" (time SP ("-"%shifted)? typed_time) >create_zone %add_zone
  68. (SP time SP ("-"%shifted)? typed_time)* >create_zone %add_zone;
  69. # Entry point
  70. main := Z :>CRLF?;
  71. }%%
  72. tsdp_header_Z_t* tsdp_header_Z_create(uint64_t time, tsk_bool_t shifted_back, const char* typed_time)
  73. {
  74. return tsk_object_new(TSDP_HEADER_Z_VA_ARGS(time, shifted_back, typed_time));
  75. }
  76. tsdp_header_Z_t* tsdp_header_Z_create_null()
  77. {
  78. return tsdp_header_Z_create(0L, tsk_false, tsk_null);
  79. }
  80. tsdp_zone_t* tsdp_zone_create(uint64_t time, tsk_bool_t shifted_back, const char* typed_time)
  81. {
  82. return tsk_object_new(tsdp_zone_def_t, time, shifted_back, typed_time);
  83. }
  84. tsdp_zone_t* tsdp_zone_create_null()
  85. {
  86. return tsdp_zone_create(0L, tsk_false, tsk_null);
  87. }
  88. int tsdp_header_Z_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  89. {
  90. if(header){
  91. const tsdp_header_Z_t *Z = (const tsdp_header_Z_t *)header;
  92. const tsk_list_item_t *item;
  93. const tsdp_zone_t* zone;
  94. tsk_list_foreach(item, Z->zones){
  95. zone = (const tsdp_zone_t*)item->data;
  96. // time SP ["-"] typed-time
  97. tsk_buffer_append_2(output, "%s%llu %s%s",
  98. TSK_LIST_IS_FIRST(Z->zones, item) ? "" : " ",
  99. zone->time,
  100. zone->shifted_back ? "-" : "",
  101. zone->typed_time
  102. );
  103. }
  104. }
  105. return -1;
  106. }
  107. tsdp_header_Z_t *tsdp_header_Z_parse(const char *data, tsk_size_t size)
  108. {
  109. int cs = 0;
  110. const char *p = data;
  111. const char *pe = p + size;
  112. const char *eof = pe;
  113. tsdp_header_Z_t *hdr_Z = tsdp_header_Z_create_null();
  114. tsdp_zone_t* zone = tsk_null;
  115. const char *tag_start = tsk_null;
  116. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  117. %%write data;
  118. (void)(tsdp_machine_parser_header_Z_first_final);
  119. (void)(tsdp_machine_parser_header_Z_error);
  120. (void)(tsdp_machine_parser_header_Z_en_main);
  121. %%write init;
  122. %%write exec;
  123. TSK_RAGEL_DISABLE_WARNINGS_END()
  124. if(zone){
  125. TSK_OBJECT_SAFE_FREE(zone);
  126. }
  127. if( cs < %%{ write first_final; }%% ){
  128. TSK_DEBUG_ERROR("Failed to parse \"z=\" header.");
  129. TSK_OBJECT_SAFE_FREE(hdr_Z);
  130. }
  131. return hdr_Z;
  132. }
  133. //========================================================
  134. // Z header object definition
  135. //
  136. static tsk_object_t* tsdp_header_Z_ctor(tsk_object_t *self, va_list * app)
  137. {
  138. tsdp_header_Z_t *Z = self;
  139. if(Z){
  140. TSDP_HEADER(Z)->type = tsdp_htype_Z;
  141. TSDP_HEADER(Z)->tostring = tsdp_header_Z_tostring;
  142. TSDP_HEADER(Z)->rank = TSDP_HTYPE_Z_RANK;
  143. if((Z->zones = tsk_list_create())){
  144. uint64_t time = va_arg(*app, uint64_t);
  145. unsigned shifted_back = va_arg(*app, unsigned);
  146. const char* typed_time = va_arg(*app, const char*);
  147. if(typed_time){
  148. tsdp_zone_t *zone;
  149. if((zone = tsdp_zone_create(time, shifted_back, typed_time))){
  150. tsk_list_push_back_data(Z->zones,(void**)&zone);
  151. }
  152. }
  153. }
  154. }
  155. else{
  156. TSK_DEBUG_ERROR("Failed to create new Z header.");
  157. }
  158. return self;
  159. }
  160. static tsk_object_t* tsdp_header_Z_dtor(tsk_object_t *self)
  161. {
  162. tsdp_header_Z_t *Z = self;
  163. if(Z){
  164. TSK_OBJECT_SAFE_FREE(Z->zones);
  165. }
  166. else{
  167. TSK_DEBUG_ERROR("Null Z header.");
  168. }
  169. return self;
  170. }
  171. static int tsdp_header_Z_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  172. {
  173. if(obj1 && obj2){
  174. return tsdp_header_rank_cmp(obj1, obj2);
  175. }
  176. else{
  177. return -1;
  178. }
  179. }
  180. static const tsk_object_def_t tsdp_header_Z_def_s =
  181. {
  182. sizeof(tsdp_header_Z_t),
  183. tsdp_header_Z_ctor,
  184. tsdp_header_Z_dtor,
  185. tsdp_header_Z_cmp
  186. };
  187. const tsk_object_def_t *tsdp_header_Z_def_t = &tsdp_header_Z_def_s;
  188. //========================================================
  189. // Zone object definition
  190. //
  191. static tsk_object_t* tsdp_zone_ctor(tsk_object_t *self, va_list * app)
  192. {
  193. tsdp_zone_t *zone = self;
  194. if(zone){
  195. zone->time = va_arg(*app, uint64_t);
  196. zone->shifted_back = va_arg(*app, tsk_bool_t);
  197. zone->typed_time = tsk_strdup( va_arg(*app, const char*) );
  198. }
  199. else{
  200. TSK_DEBUG_ERROR("Failed to create new zone object.");
  201. }
  202. return self;
  203. }
  204. static tsk_object_t* tsdp_zone_dtor(tsk_object_t *self)
  205. {
  206. tsdp_zone_t *zone = self;
  207. if(zone){
  208. TSK_FREE(zone->typed_time);
  209. }
  210. else{
  211. TSK_DEBUG_ERROR("Null zone object.");
  212. }
  213. return self;
  214. }
  215. static const tsk_object_def_t tsdp_zone_def_s =
  216. {
  217. sizeof(tsdp_zone_t),
  218. tsdp_zone_ctor,
  219. tsdp_zone_dtor,
  220. tsk_null
  221. };
  222. const tsk_object_def_t *tsdp_zone_def_t = &tsdp_zone_def_s;