tmsrp_parser_header_Dummy.rl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2009-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 tmsrp_header_Dummy.c
  21. * @brief MSRP Dummy header.
  22. */
  23. #include "tinymsrp/headers/tmsrp_header_Dummy.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 tmsrp_machine_parser_header_Dummy;
  33. # Includes
  34. include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_hname{
  39. TSK_PARSER_SET_STRING(hdr_Dummy->name);
  40. }
  41. action parse_hval{
  42. TSK_PARSER_SET_STRING(hdr_Dummy->value);
  43. }
  44. #//hname ":" SP hval CRLF
  45. Dummy = hname>tag %parse_hname :>SP* ":" SP*<: hval>tag %parse_hval;
  46. # Entry point
  47. main := Dummy :>CRLF?;
  48. }%%
  49. tmsrp_header_Dummy_t* tmsrp_header_Dummy_create(const char* name, const char* value)
  50. {
  51. return tsk_object_new(TMSRP_HEADER_DUMMY_VA_ARGS(name, value));
  52. }
  53. tmsrp_header_Dummy_t* tmsrp_header_Dummy_create_null()
  54. {
  55. return tmsrp_header_Dummy_create(tsk_null, tsk_null);
  56. }
  57. int tmsrp_header_Dummy_tostring(const tmsrp_header_t* header, tsk_buffer_t* output)
  58. {
  59. if(header){
  60. const tmsrp_header_Dummy_t *Dummy = (const tmsrp_header_Dummy_t *)header;
  61. if(Dummy->value){
  62. return tsk_buffer_append(output, Dummy->value, tsk_strlen(Dummy->value));
  63. }
  64. return 0;
  65. }
  66. return -1;
  67. }
  68. tmsrp_header_Dummy_t *tmsrp_header_Dummy_parse(const char *data, tsk_size_t size)
  69. {
  70. int cs = 0;
  71. const char *p = data;
  72. const char *pe = p + size;
  73. const char *eof = pe;
  74. tmsrp_header_Dummy_t *hdr_Dummy = tmsrp_header_Dummy_create_null();
  75. const char *tag_start = tsk_null;
  76. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  77. %%write data;
  78. (void)(eof);
  79. (void)(tmsrp_machine_parser_header_Dummy_first_final);
  80. (void)(tmsrp_machine_parser_header_Dummy_error);
  81. (void)(tmsrp_machine_parser_header_Dummy_en_main);
  82. %%write init;
  83. %%write exec;
  84. TSK_RAGEL_DISABLE_WARNINGS_END()
  85. if( cs < %%{ write first_final; }%% ){
  86. TSK_DEBUG_ERROR("Failed to parse Dummy header.");
  87. TSK_OBJECT_SAFE_FREE(hdr_Dummy);
  88. }
  89. return hdr_Dummy;
  90. }
  91. //========================================================
  92. // Dummy header object definition
  93. //
  94. static tsk_object_t* tmsrp_header_Dummy_ctor(tsk_object_t *self, va_list * app)
  95. {
  96. tmsrp_header_Dummy_t *Dummy = self;
  97. if(Dummy){
  98. TMSRP_HEADER(Dummy)->type = tmsrp_htype_Dummy;
  99. TMSRP_HEADER(Dummy)->tostring = tmsrp_header_Dummy_tostring;
  100. Dummy->name = tsk_strdup(va_arg(*app, const char*));
  101. Dummy->value = tsk_strdup(va_arg(*app, const char*));
  102. }
  103. else{
  104. TSK_DEBUG_ERROR("Failed to create new Dummy header.");
  105. }
  106. return self;
  107. }
  108. static tsk_object_t* tmsrp_header_Dummy_dtor(tsk_object_t *self)
  109. {
  110. tmsrp_header_Dummy_t *Dummy = self;
  111. if(Dummy){
  112. TSK_FREE(Dummy->name);
  113. TSK_FREE(Dummy->value);
  114. }
  115. else{
  116. TSK_DEBUG_ERROR("Null Dummy header.");
  117. }
  118. return self;
  119. }
  120. static const tsk_object_def_t tmsrp_header_Dummy_def_s =
  121. {
  122. sizeof(tmsrp_header_Dummy_t),
  123. tmsrp_header_Dummy_ctor,
  124. tmsrp_header_Dummy_dtor,
  125. tsk_null
  126. };
  127. const tsk_object_def_t *tmsrp_header_Dummy_def_t = &tmsrp_header_Dummy_def_s;