tsdp_parser_header_C.rl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_C.c
  21. * @brief "c=" header (Connection Data).
  22. */
  23. #include "tinysdp/headers/tsdp_header_C.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_C;
  33. # Includes
  34. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_nettype{
  39. TSK_PARSER_SET_STRING(hdr_C->nettype);
  40. }
  41. action parse_addrtype{
  42. TSK_PARSER_SET_STRING(hdr_C->addrtype);
  43. }
  44. action parse_addr{
  45. TSK_PARSER_SET_STRING(hdr_C->addr);
  46. }
  47. nettype = any* >tag %parse_nettype;
  48. addrtype = any* >tag %parse_addrtype;
  49. addr = any* >tag %parse_addr;
  50. C = 'c' SP* "=" SP*<: nettype :>SP addrtype :>SP addr;
  51. # Entry point
  52. main := C :>CRLF?;
  53. }%%
  54. tsdp_header_C_t* tsdp_header_c_create(const char* nettype, const char* addrtype, const char* addr)
  55. {
  56. return tsk_object_new(TSDP_HEADER_C_VA_ARGS(nettype, addrtype, addr));
  57. }
  58. tsdp_header_C_t* tsdp_header_c_create_null()
  59. {
  60. return tsdp_header_c_create(tsk_null, tsk_null, tsk_null);
  61. }
  62. int tsdp_header_C_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  63. {
  64. if(header){
  65. const tsdp_header_C_t *C = (const tsdp_header_C_t *)header;
  66. return tsk_buffer_append_2(output, "%s %s %s",
  67. C->nettype,
  68. C->addrtype,
  69. C->addr
  70. );
  71. return 0;
  72. }
  73. return -1;
  74. }
  75. tsdp_header_t* tsdp_header_C_clone(const tsdp_header_t* header)
  76. {
  77. if(header){
  78. const tsdp_header_C_t *C = (const tsdp_header_C_t *)header;
  79. return (tsdp_header_t*)tsdp_header_c_create(C->nettype, C->addrtype, C->addr);
  80. }
  81. return tsk_null;
  82. }
  83. tsdp_header_C_t *tsdp_header_C_parse(const char *data, tsk_size_t size)
  84. {
  85. int cs = 0;
  86. const char *p = data;
  87. const char *pe = p + size;
  88. const char *eof = pe;
  89. tsdp_header_C_t *hdr_C = tsdp_header_c_create_null();
  90. const char *tag_start = tsk_null;
  91. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  92. %%write data;
  93. (void)(tsdp_machine_parser_header_C_first_final);
  94. (void)(tsdp_machine_parser_header_C_error);
  95. (void)(tsdp_machine_parser_header_C_en_main);
  96. %%write init;
  97. %%write exec;
  98. TSK_RAGEL_DISABLE_WARNINGS_END()
  99. if( cs < %%{ write first_final; }%% ){
  100. TSK_DEBUG_ERROR("Failed to parse \"c=\" header.");
  101. TSK_OBJECT_SAFE_FREE(hdr_C);
  102. }
  103. return hdr_C;
  104. }
  105. //========================================================
  106. // E header object definition
  107. //
  108. static tsk_object_t* tsdp_header_C_ctor(tsk_object_t *self, va_list * app)
  109. {
  110. tsdp_header_C_t *C = self;
  111. if(C){
  112. TSDP_HEADER(C)->type = tsdp_htype_C;
  113. TSDP_HEADER(C)->tostring = tsdp_header_C_tostring;
  114. TSDP_HEADER(C)->clone = tsdp_header_C_clone;
  115. TSDP_HEADER(C)->rank = TSDP_HTYPE_C_RANK;
  116. C->nettype = tsk_strdup(va_arg(*app, const char*));
  117. C->addrtype = tsk_strdup(va_arg(*app, const char*));
  118. C->addr = tsk_strdup(va_arg(*app, const char*));
  119. }
  120. else{
  121. TSK_DEBUG_ERROR("Failed to create new C header.");
  122. }
  123. return self;
  124. }
  125. static tsk_object_t* tsdp_header_C_dtor(tsk_object_t *self)
  126. {
  127. tsdp_header_C_t *C = self;
  128. if(C){
  129. TSK_FREE(C->nettype);
  130. TSK_FREE(C->addrtype);
  131. TSK_FREE(C->addr);
  132. }
  133. else{
  134. TSK_DEBUG_ERROR("Null PC header.");
  135. }
  136. return self;
  137. }
  138. static int tsdp_header_C_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  139. {
  140. if(obj1 && obj2){
  141. return tsdp_header_rank_cmp(obj1, obj2);
  142. }
  143. else{
  144. return -1;
  145. }
  146. }
  147. static const tsk_object_def_t tsdp_header_C_def_s =
  148. {
  149. sizeof(tsdp_header_C_t),
  150. tsdp_header_C_ctor,
  151. tsdp_header_C_dtor,
  152. tsdp_header_C_cmp
  153. };
  154. const tsk_object_def_t *tsdp_header_C_def_t = &tsdp_header_C_def_s;