tmsrp_parser_header_Message-ID.rl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_Message_Id.c
  21. * @brief MSRP 'Message-Id' header.
  22. *
  23. */
  24. #include "tinymsrp/headers/tmsrp_header_Message-ID.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 tmsrp_machine_parser_header_Message_Id;
  34. # Includes
  35. include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
  36. action tag{
  37. tag_start = p;
  38. }
  39. action parse_id{
  40. TSK_PARSER_SET_STRING(hdr_Message_Id->value);
  41. }
  42. #// "Message-ID:" SP ident
  43. Message_Id = "Message-ID:"i SP ident>tag %parse_id;
  44. # Entry point
  45. main := Message_Id :>CRLF?;
  46. }%%
  47. tmsrp_header_Message_ID_t* tmsrp_header_Message_ID_create(const char* value)
  48. {
  49. return tsk_object_new(TMSRP_HEADER_MESSAGE_ID_VA_ARGS(value));
  50. }
  51. tmsrp_header_Message_ID_t* tmsrp_header_Message_ID_create_null()
  52. {
  53. return tmsrp_header_Message_ID_create(tsk_null);
  54. }
  55. int tmsrp_header_Message_ID_tostring(const tmsrp_header_t* header, tsk_buffer_t* output)
  56. {
  57. if(header)
  58. {
  59. const tmsrp_header_Message_ID_t *Message_Id = (const tmsrp_header_Message_ID_t *)header;
  60. if(Message_Id->value){
  61. return tsk_buffer_append(output, Message_Id->value, tsk_strlen(Message_Id->value));
  62. }
  63. return 0;
  64. }
  65. return -1;
  66. }
  67. tmsrp_header_Message_ID_t *tmsrp_header_Message_ID_parse(const char *data, tsk_size_t size)
  68. {
  69. int cs = 0;
  70. const char *p = data;
  71. const char *pe = p + size;
  72. const char *eof = pe;
  73. tmsrp_header_Message_ID_t *hdr_Message_Id = tmsrp_header_Message_ID_create_null();
  74. const char *tag_start = tsk_null;
  75. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  76. %%write data;
  77. (void)(eof);
  78. (void)(tmsrp_machine_parser_header_Message_Id_first_final);
  79. (void)(tmsrp_machine_parser_header_Message_Id_error);
  80. (void)(tmsrp_machine_parser_header_Message_Id_en_main);
  81. %%write init;
  82. %%write exec;
  83. TSK_RAGEL_DISABLE_WARNINGS_END()
  84. if( cs < %%{ write first_final; }%% ){
  85. TSK_DEBUG_ERROR("Failed to parse 'Message-Id' header.");
  86. TSK_OBJECT_SAFE_FREE(hdr_Message_Id);
  87. }
  88. return hdr_Message_Id;
  89. }
  90. //========================================================
  91. // Message_Id header object definition
  92. //
  93. static tsk_object_t* tmsrp_header_Message_ID_ctor(tsk_object_t *self, va_list * app)
  94. {
  95. tmsrp_header_Message_ID_t *Message_Id = self;
  96. if(Message_Id){
  97. TMSRP_HEADER(Message_Id)->type = tmsrp_htype_Message_ID;
  98. TMSRP_HEADER(Message_Id)->tostring = tmsrp_header_Message_ID_tostring;
  99. Message_Id->value = tsk_strdup(va_arg(*app, const char*));
  100. }
  101. else{
  102. TSK_DEBUG_ERROR("Failed to create new Message-Id header.");
  103. }
  104. return self;
  105. }
  106. static tsk_object_t* tmsrp_header_Message_ID_dtor(tsk_object_t *self)
  107. {
  108. tmsrp_header_Message_ID_t *Message_Id = self;
  109. if(Message_Id){
  110. TSK_FREE(Message_Id->value);
  111. }
  112. else{
  113. TSK_DEBUG_ERROR("Null Message-Id header.");
  114. }
  115. return self;
  116. }
  117. static const tsk_object_def_t tmsrp_header_Message_ID_def_s =
  118. {
  119. sizeof(tmsrp_header_Message_ID_t),
  120. tmsrp_header_Message_ID_ctor,
  121. tmsrp_header_Message_ID_dtor,
  122. tsk_null
  123. };
  124. const tsk_object_def_t *tmsrp_header_Message_ID_def_t = &tmsrp_header_Message_ID_def_s;