tsdp_parser_header_E.rl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_E.c
  21. * @brief SDP "e=" header (Session Information).
  22. */
  23. #include "tinysdp/headers/tsdp_header_E.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_E;
  33. # Includes
  34. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_value{
  39. TSK_PARSER_SET_STRING(hdr_E->value);
  40. }
  41. E = 'e' SP* "=" SP*<: any*>tag %parse_value;
  42. # Entry point
  43. main := E :>CRLF?;
  44. }%%
  45. tsdp_header_E_t* tsdp_header_E_create(const char* value)
  46. {
  47. return tsk_object_new(TSDP_HEADER_E_VA_ARGS(value));
  48. }
  49. tsdp_header_E_t* tsdp_header_E_create_null()
  50. {
  51. return tsdp_header_E_create(tsk_null);
  52. }
  53. int tsdp_header_E_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  54. {
  55. if(header){
  56. const tsdp_header_E_t *E = (const tsdp_header_E_t *)header;
  57. if(E->value){
  58. tsk_buffer_append(output, E->value, tsk_strlen(E->value));
  59. }
  60. return 0;
  61. }
  62. return -1;
  63. }
  64. tsdp_header_t* tsdp_header_E_clone(const tsdp_header_t* header)
  65. {
  66. if(header){
  67. const tsdp_header_E_t *E = (const tsdp_header_E_t *)header;
  68. return (tsdp_header_t*)tsdp_header_E_create(E->value);
  69. }
  70. return tsk_null;
  71. }
  72. tsdp_header_E_t *tsdp_header_E_parse(const char *data, tsk_size_t size)
  73. {
  74. int cs = 0;
  75. const char *p = data;
  76. const char *pe = p + size;
  77. const char *eof = pe;
  78. tsdp_header_E_t *hdr_E = tsdp_header_E_create_null();
  79. const char *tag_start = tsk_null;
  80. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  81. %%write data;
  82. (void)(tsdp_machine_parser_header_E_first_final);
  83. (void)(tsdp_machine_parser_header_E_error);
  84. (void)(tsdp_machine_parser_header_E_en_main);
  85. %%write init;
  86. %%write exec;
  87. TSK_RAGEL_DISABLE_WARNINGS_END()
  88. if( cs < %%{ write first_final; }%% ){
  89. TSK_DEBUG_ERROR("Failed to parse \"e=\" header.");
  90. TSK_OBJECT_SAFE_FREE(hdr_E);
  91. }
  92. return hdr_E;
  93. }
  94. //========================================================
  95. // E header object definition
  96. //
  97. static tsk_object_t* tsdp_header_E_ctor(tsk_object_t *self, va_list * app)
  98. {
  99. tsdp_header_E_t *E = self;
  100. if(E){
  101. TSDP_HEADER(E)->type = tsdp_htype_E;
  102. TSDP_HEADER(E)->tostring = tsdp_header_E_tostring;
  103. TSDP_HEADER(E)->clone = tsdp_header_E_clone;
  104. TSDP_HEADER(E)->rank = TSDP_HTYPE_E_RANK;
  105. E->value = tsk_strdup(va_arg(*app, const char*));
  106. }
  107. else{
  108. TSK_DEBUG_ERROR("Failed to create new E header.");
  109. }
  110. return self;
  111. }
  112. static tsk_object_t* tsdp_header_E_dtor(tsk_object_t *self)
  113. {
  114. tsdp_header_E_t *E = self;
  115. if(E){
  116. TSK_FREE(E->value);
  117. }
  118. else{
  119. TSK_DEBUG_ERROR("Null E header.");
  120. }
  121. return self;
  122. }
  123. static int tsdp_header_E_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  124. {
  125. if(obj1 && obj2){
  126. return tsdp_header_rank_cmp(obj1, obj2);
  127. }
  128. else{
  129. return -1;
  130. }
  131. }
  132. static const tsk_object_def_t tsdp_header_E_def_s =
  133. {
  134. sizeof(tsdp_header_E_t),
  135. tsdp_header_E_ctor,
  136. tsdp_header_E_dtor,
  137. tsdp_header_E_cmp
  138. };
  139. const tsk_object_def_t *tsdp_header_E_def_t = &tsdp_header_E_def_s;