tsdp_parser_header_V.rl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_V.c
  21. * @brief SDP "v=" header (Protocol Version).
  22. */
  23. #include "tinysdp/headers/tsdp_header_V.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_V;
  33. # Includes
  34. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_version{
  39. TSK_PARSER_SET_INT(hdr_V->version);
  40. }
  41. V = 'v' SP* "=" SP*<: DIGIT+>tag %parse_version;
  42. # Entry point
  43. main := V :>CRLF?;
  44. }%%
  45. tsdp_header_V_t* tsdp_header_V_create(int32_t version)
  46. {
  47. return tsk_object_new(TSDP_HEADER_V_VA_ARGS(version));
  48. }
  49. tsdp_header_V_t* tsdp_header_V_create_null()
  50. {
  51. return tsdp_header_V_create(TSDP_HEADER_V_DEFAULT);
  52. }
  53. int tsdp_header_V_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  54. {
  55. if(header)
  56. {
  57. const tsdp_header_V_t *V = (const tsdp_header_V_t *)header;
  58. if(V->version >=0){
  59. tsk_buffer_append_2(output, "%d", V->version);
  60. }
  61. return 0;
  62. }
  63. return -1;
  64. }
  65. tsdp_header_t* tsdp_header_V_clone(const tsdp_header_t* header)
  66. {
  67. if(header){
  68. const tsdp_header_V_t *V = (const tsdp_header_V_t *)header;
  69. return (tsdp_header_t*)tsdp_header_V_create(V->version);
  70. }
  71. return tsk_null;
  72. }
  73. tsdp_header_V_t *tsdp_header_V_parse(const char *data, tsk_size_t size)
  74. {
  75. int cs = 0;
  76. const char *p = data;
  77. const char *pe = p + size;
  78. const char *eof = pe;
  79. tsdp_header_V_t *hdr_V = tsdp_header_V_create_null();
  80. const char *tag_start = tsk_null;
  81. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  82. %%write data;
  83. (void)(tsdp_machine_parser_header_V_first_final);
  84. (void)(tsdp_machine_parser_header_V_error);
  85. (void)(tsdp_machine_parser_header_V_en_main);
  86. %%write init;
  87. %%write exec;
  88. TSK_RAGEL_DISABLE_WARNINGS_END()
  89. if( cs < %%{ write first_final; }%% ){
  90. TSK_DEBUG_ERROR("Failed to parse \"v=\" header.");
  91. TSK_OBJECT_SAFE_FREE(hdr_V);
  92. }
  93. return hdr_V;
  94. }
  95. //========================================================
  96. // V header object definition
  97. //
  98. static tsk_object_t* tsdp_header_V_ctor(tsk_object_t *self, va_list * app)
  99. {
  100. tsdp_header_V_t *V = self;
  101. if(V){
  102. TSDP_HEADER(V)->type = tsdp_htype_V;
  103. TSDP_HEADER(V)->tostring = tsdp_header_V_tostring;
  104. TSDP_HEADER(V)->clone = tsdp_header_V_clone;
  105. TSDP_HEADER(V)->rank = TSDP_HTYPE_V_RANK;
  106. V->version = va_arg(*app, int32_t);
  107. }
  108. else{
  109. TSK_DEBUG_ERROR("Failed to create new V header.");
  110. }
  111. return self;
  112. }
  113. static tsk_object_t* tsdp_header_V_dtor(tsk_object_t *self)
  114. {
  115. tsdp_header_V_t *V = self;
  116. if(V){
  117. }
  118. else{
  119. TSK_DEBUG_ERROR("Null V header.");
  120. }
  121. return self;
  122. }
  123. static int tsdp_header_V_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_V_def_s =
  133. {
  134. sizeof(tsdp_header_V_t),
  135. tsdp_header_V_ctor,
  136. tsdp_header_V_dtor,
  137. tsdp_header_V_cmp
  138. };
  139. const tsk_object_def_t *tsdp_header_V_def_t = &tsdp_header_V_def_s;