tsdp_parser_header_U.rl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_U.c
  21. * @brief SDP "u=" header (URI).
  22. */
  23. #include "tinysdp/headers/tsdp_header_U.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_U;
  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_U->value);
  40. }
  41. U = 'u' SP* "=" SP*<: any*>tag %parse_value;
  42. # Entry point
  43. main := U :>CRLF?;
  44. }%%
  45. tsdp_header_U_t* tsdp_header_U_create(const char* value)
  46. {
  47. return tsk_object_new(TSDP_HEADER_U_VA_ARGS(value));
  48. }
  49. tsdp_header_U_t* tsdp_header_U_create_null()
  50. {
  51. return tsdp_header_U_create(tsk_null);
  52. }
  53. int tsdp_header_U_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  54. {
  55. if(header)
  56. {
  57. const tsdp_header_U_t *U = (const tsdp_header_U_t *)header;
  58. if(U->value){
  59. tsk_buffer_append(output, U->value, tsk_strlen(U->value));
  60. }
  61. return 0;
  62. }
  63. return -1;
  64. }
  65. tsdp_header_t* tsdp_header_U_clone(const tsdp_header_t* header)
  66. {
  67. if(header){
  68. const tsdp_header_U_t *U = (const tsdp_header_U_t *)header;
  69. return (tsdp_header_t*)tsdp_header_U_create(U->value);
  70. }
  71. return tsk_null;
  72. }
  73. tsdp_header_U_t *tsdp_header_U_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_U_t *hdr_U = tsdp_header_U_create_null();
  80. const char *tag_start = tsk_null;
  81. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  82. %%write data;
  83. (void)(tsdp_machine_parser_header_U_first_final);
  84. (void)(tsdp_machine_parser_header_U_error);
  85. (void)(tsdp_machine_parser_header_U_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 \"u=\" header.");
  91. TSK_OBJECT_SAFE_FREE(hdr_U);
  92. }
  93. return hdr_U;
  94. }
  95. //========================================================
  96. // U header object definition
  97. //
  98. static tsk_object_t* tsdp_header_U_ctor(tsk_object_t *self, va_list * app)
  99. {
  100. tsdp_header_U_t *U = self;
  101. if(U)
  102. {
  103. TSDP_HEADER(U)->type = tsdp_htype_U;
  104. TSDP_HEADER(U)->tostring = tsdp_header_U_tostring;
  105. TSDP_HEADER(U)->clone = tsdp_header_U_clone;
  106. TSDP_HEADER(U)->rank = TSDP_HTYPE_U_RANK;
  107. U->value = tsk_strdup(va_arg(*app, const char*));
  108. }
  109. else{
  110. TSK_DEBUG_ERROR("Failed to create new U header.");
  111. }
  112. return self;
  113. }
  114. static tsk_object_t* tsdp_header_U_dtor(tsk_object_t *self)
  115. {
  116. tsdp_header_U_t *U = self;
  117. if(U){
  118. TSK_FREE(U->value);
  119. }
  120. else{
  121. TSK_DEBUG_ERROR("Null U header.");
  122. }
  123. return self;
  124. }
  125. static int tsdp_header_U_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  126. {
  127. if(obj1 && obj2){
  128. return tsdp_header_rank_cmp(obj1, obj2);
  129. }
  130. else{
  131. return -1;
  132. }
  133. }
  134. static const tsk_object_def_t tsdp_header_U_def_s =
  135. {
  136. sizeof(tsdp_header_U_t),
  137. tsdp_header_U_ctor,
  138. tsdp_header_U_dtor,
  139. tsdp_header_U_cmp
  140. };
  141. const tsk_object_def_t *tsdp_header_U_def_t = &tsdp_header_U_def_s;