token_stack.C 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $XConsortium: token_stack.C /main/5 1996/08/21 15:51:52 drk $
  25. *
  26. * Copyright (c) 1993 HAL Computer Systems International, Ltd.
  27. * All rights reserved. Unpublished -- rights reserved under
  28. * the Copyright Laws of the United States. USE OF A COPYRIGHT
  29. * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
  30. * OR DISCLOSURE.
  31. *
  32. * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
  33. * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
  34. * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
  35. * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
  36. * INTERNATIONAL, LTD.
  37. *
  38. * RESTRICTED RIGHTS LEGEND
  39. * Use, duplication, or disclosure by the Government is subject
  40. * to the restrictions as set forth in subparagraph (c)(l)(ii)
  41. * of the Rights in Technical Data and Computer Software clause
  42. * at DFARS 252.227-7013.
  43. *
  44. * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
  45. * 1315 Dell Avenue
  46. * Campbell, CA 95008
  47. *
  48. */
  49. #include "dstr/token_stack.h"
  50. #ifdef C_API
  51. #include "utility/c_stream.h"
  52. #else
  53. #include <sstream>
  54. using namespace std;
  55. #endif
  56. token_stack::token_stack() : curr_token_start(0)
  57. {
  58. v_curr_token_buf = new buffer(LBUFSIZ);
  59. v_curr_list_cell = new slist_void_ptr_cell(v_curr_token_buf);
  60. chunk_list.insert_as_tail(v_curr_list_cell);
  61. }
  62. token_stack::~token_stack()
  63. {
  64. long ind = chunk_list.first();
  65. while (ind) {
  66. slist_void_ptr_cell *p = (slist_void_ptr_cell*)ind;
  67. buffer *w = (buffer*)(p -> void_ptr());
  68. delete w;
  69. chunk_list.next(ind);
  70. }
  71. }
  72. void token_stack::clear()
  73. {
  74. long ind = chunk_list.first();
  75. while (ind) {
  76. slist_void_ptr_cell *p = (slist_void_ptr_cell*)ind;
  77. buffer *w = (buffer*)(p -> void_ptr());
  78. w -> reset();
  79. chunk_list.next(ind);
  80. }
  81. v_curr_list_cell = (slist_void_ptr_cell*)(chunk_list.get_head());
  82. v_curr_token_buf = (buffer*)(v_curr_list_cell -> void_ptr());
  83. }
  84. void token_stack::new_token()
  85. {
  86. if ( curr_token_start )
  87. v_curr_token_buf -> put(char(0));
  88. curr_token_start = 0;
  89. }
  90. void token_stack::add_partial_token(char* x)
  91. {
  92. if ( v_curr_token_buf -> remaining_sz() < (int)(strlen(x) + 1) ) {
  93. int partial_str_len = curr_token_start ? strlen(curr_token_start) : 0;
  94. buffer* new_buf = 0;
  95. slist_void_ptr_cell* next_cell =
  96. (slist_void_ptr_cell*)(v_curr_list_cell -> v_succ);
  97. if ( next_cell ) {
  98. new_buf = (buffer*)(next_cell -> void_ptr());
  99. } else {
  100. new_buf = new buffer(MAX(partial_str_len + 1, LBUFSIZ));
  101. next_cell = new slist_void_ptr_cell(new_buf);
  102. chunk_list.insert_as_tail(next_cell);
  103. }
  104. if ( curr_token_start ) {
  105. v_curr_token_buf -> put(char(0));
  106. new_buf -> put(curr_token_start, strlen(curr_token_start));
  107. v_curr_token_buf -> set_content_sz(
  108. int(curr_token_start - v_curr_token_buf->get_base())
  109. );
  110. }
  111. v_curr_list_cell = next_cell;
  112. v_curr_token_buf = new_buf;
  113. }
  114. if ( curr_token_start == 0 ) {
  115. curr_token_start =
  116. v_curr_token_buf -> get_base() + v_curr_token_buf -> content_sz();
  117. }
  118. v_curr_token_buf -> put(x, strlen(x));
  119. /*
  120. MESSAGE(cerr, "add_partial_token():");
  121. debug(cerr, last_buf -> content_sz());
  122. */
  123. }
  124. ostream& operator <<(ostream& out, token_stack& ts)
  125. {
  126. long ind = ts.chunk_list.first();
  127. while (ind) {
  128. slist_void_ptr_cell *p = (slist_void_ptr_cell*)ind;
  129. buffer *w = (buffer*)(p -> void_ptr());
  130. int offset = 0;
  131. while ( offset < w -> content_sz() ) {
  132. char* str = (char*)(w -> get_base() + offset);
  133. out << str << "\n";
  134. offset += strlen(str) + 1;
  135. }
  136. ts.chunk_list.next(ind);
  137. }
  138. return out;
  139. }