2
0

buffer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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: buffer.h /main/6 1996/08/15 14:21:05 cde-hal $
  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. #ifndef _buffer_h
  50. #define _buffer_h 1
  51. #include <ctype.h>
  52. #include <iomanip>
  53. #include "utility/funcs.h"
  54. class buffer
  55. {
  56. public:
  57. buffer(const int sz = 1024) : v_swap_order(false) { _alloc(sz); };
  58. buffer(buffer&);
  59. virtual ~buffer();
  60. // buffer management functions
  61. virtual void reset(); // clean the buffer
  62. int expand_chunk(const int); // expand buffer chunk
  63. void set_chunk(char* ptr, const int sz) {
  64. v_bufsz = sz;
  65. v_eptr = v_aptr = v_base = ptr;
  66. v_allocated = 0;
  67. }; // set buffer chunk
  68. void set_content_sz(const int sz) {v_eptr = v_base + sz;} ;
  69. // buffer status functions
  70. int buf_sz() const { return v_bufsz; }; // chunk length
  71. int content_sz() const { return v_eptr - v_aptr; }; // content length
  72. int remaining_sz() const { return v_bufsz - int(v_eptr - v_aptr); }; // remaining space
  73. char* get_base() const { return v_base; }; // get 'base'
  74. // get items
  75. buffer& get(char& y); // get current char
  76. buffer& get(unsigned short& y); // get a short
  77. buffer& get(unsigned int& y); // get a unsigned int
  78. buffer& get(int& y); // get an int
  79. buffer& get(long& y); // get a long
  80. buffer& get(float & y); // get a float
  81. buffer& get(char*, int) ; // get a string
  82. buffer& getusc(int& y); // get unsigned char to intr.
  83. // no boundary checking
  84. // put items
  85. buffer& put(const char content, Boolean exp_buf = false) ; // append char
  86. buffer& put(const unsigned char, Boolean exp_buf = false) ; //append unsigned char
  87. buffer& put(const int, Boolean exp_buf = false) ; // append an int
  88. buffer& put(const unsigned int, Boolean exp_buf = false) ; // append a unsigned int
  89. buffer& put(const long, Boolean exp_buf = false) ; // append a long
  90. buffer& put(const unsigned short, Boolean exp_buf = false) ; //append a short
  91. buffer& put(const float y, Boolean exp_buf = false); // put a float
  92. buffer& put(const char*, int, Boolean exp_buf = false) ; // append a string
  93. buffer& skip(int i) ; // skip i chars
  94. // set and get byte order
  95. void set_swap_order(Boolean x) { v_swap_order = x; };
  96. Boolean get_swap_order() { return v_swap_order; };
  97. friend Boolean operator ==(buffer&, buffer&);
  98. // show the buffer
  99. friend ostream& operator <<(ostream&, buffer&);
  100. friend class abs_storage;
  101. friend class unixf_storage;
  102. friend class page_storage;
  103. friend class mem_storage;
  104. friend class page_cache_global_part;
  105. protected:
  106. char v_allocated;
  107. unsigned int v_bufsz ; // allocated chunk size
  108. char *v_base ; // base address of the chunk
  109. char *v_eptr ; // end address of the buf content
  110. char *v_aptr ; // beginning address of the buf content
  111. int v_align_offset; // memory align offset
  112. Boolean v_swap_order;
  113. protected:
  114. void _alloc(const int sz);
  115. };
  116. typedef buffer* bufferPtr;
  117. #endif