tsdp_parser_header_Dummy.rl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_Dummy.c
  21. * @brief SDP Dummy header.
  22. */
  23. #include "tinysdp/headers/tsdp_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 tsdp_machine_parser_header_Dummy;
  33. # Includes
  34. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_name{
  39. hdr_Dummy->name = *tag_start;
  40. }
  41. action parse_value{
  42. TSK_PARSER_SET_STRING(hdr_Dummy->value);
  43. }
  44. Dummy = alpha>tag %parse_name SP* "=" SP*<: any*>tag %parse_value;
  45. # Entry point
  46. main := Dummy :>CRLF?;
  47. }%%
  48. tsdp_header_Dummy_t* tsdp_header_dummy_create(char name, const char* value)
  49. {
  50. return tsk_object_new(TSDP_HEADER_DUMMY_VA_ARGS(name, value));
  51. }
  52. tsdp_header_Dummy_t* tsdp_header_dummy_create_null()
  53. {
  54. return tsdp_header_dummy_create(0, tsk_null);
  55. }
  56. int tsdp_header_Dummy_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  57. {
  58. if(header)
  59. {
  60. const tsdp_header_Dummy_t *Dummy = (const tsdp_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. tsdp_header_t* tsdp_header_Dummy_clone(const tsdp_header_t* header)
  69. {
  70. if(header){
  71. const tsdp_header_Dummy_t *Dummy = (const tsdp_header_Dummy_t *)header;
  72. return (tsdp_header_t*)tsdp_header_dummy_create(Dummy->name, Dummy->value);
  73. }
  74. return tsk_null;
  75. }
  76. tsdp_header_Dummy_t *tsdp_header_Dummy_parse(const char *data, tsk_size_t size)
  77. {
  78. int cs = 0;
  79. const char *p = data;
  80. const char *pe = p + size;
  81. const char *eof = pe;
  82. tsdp_header_Dummy_t *hdr_Dummy = tsdp_header_dummy_create_null();
  83. const char *tag_start = tsk_null;
  84. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  85. %%write data;
  86. (void)(tsdp_machine_parser_header_Dummy_first_final);
  87. (void)(tsdp_machine_parser_header_Dummy_error);
  88. (void)(tsdp_machine_parser_header_Dummy_en_main);
  89. %%write init;
  90. %%write exec;
  91. TSK_RAGEL_DISABLE_WARNINGS_END()
  92. if( cs < %%{ write first_final; }%% ){
  93. TSK_DEBUG_ERROR("Failed to parse dummy header.");
  94. TSK_OBJECT_SAFE_FREE(hdr_Dummy);
  95. }
  96. return hdr_Dummy;
  97. }
  98. //========================================================
  99. // Dummy header object definition
  100. //
  101. static tsk_object_t* tsdp_header_Dummy_ctor(tsk_object_t *self, va_list * app)
  102. {
  103. tsdp_header_Dummy_t *Dummy = self;
  104. if(Dummy){
  105. TSDP_HEADER(Dummy)->type = tsdp_htype_Dummy;
  106. TSDP_HEADER(Dummy)->tostring = tsdp_header_Dummy_tostring;
  107. TSDP_HEADER(Dummy)->clone = tsdp_header_Dummy_clone;
  108. TSDP_HEADER(Dummy)->rank = TSDP_HTYPE_DUMMY_RANK;
  109. #if defined(__GNUC__)
  110. Dummy->name = va_arg(*app, const int);
  111. #else
  112. Dummy->name = va_arg(*app, const char);
  113. #endif
  114. Dummy->value = tsk_strdup(va_arg(*app, const char*));
  115. }
  116. else{
  117. TSK_DEBUG_ERROR("Failed to create new Dummy header.");
  118. }
  119. return self;
  120. }
  121. static tsk_object_t* tsdp_header_Dummy_dtor(tsk_object_t *self)
  122. {
  123. tsdp_header_Dummy_t *Dummy = self;
  124. if(Dummy){
  125. TSK_FREE(Dummy->value);
  126. }
  127. else{
  128. TSK_DEBUG_ERROR("Null Dummy header.");
  129. }
  130. return self;
  131. }
  132. static int tsdp_header_Dummy_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  133. {
  134. if(obj1 && obj2){
  135. return tsdp_header_rank_cmp(obj1, obj2);
  136. }
  137. else{
  138. return -1;
  139. }
  140. }
  141. static const tsk_object_def_t tsdp_header_Dummy_def_s =
  142. {
  143. sizeof(tsdp_header_Dummy_t),
  144. tsdp_header_Dummy_ctor,
  145. tsdp_header_Dummy_dtor,
  146. tsdp_header_Dummy_cmp
  147. };
  148. const tsk_object_def_t *tsdp_header_Dummy_def_t = &tsdp_header_Dummy_def_s;