Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
6.4 KiB

  1. /*****************************************************************************
  2. * *
  3. * BF.H *
  4. * *
  5. * Copyright (C) Microsoft Corporation 1990. *
  6. * All Rights reserved. *
  7. * *
  8. ******************************************************************************
  9. * *
  10. * Module Intent *
  11. * This module implements generic buffers that can expand to any size. *
  12. * *
  13. ******************************************************************************
  14. * *
  15. * Testing Notes *
  16. * *
  17. ******************************************************************************
  18. * *
  19. * Current Owner: *
  20. * *
  21. ******************************************************************************
  22. * *
  23. * Released by Development: (date) *
  24. * *
  25. *****************************************************************************/
  26. /*****************************************************************************
  27. * *
  28. * Defines *
  29. * *
  30. *****************************************************************************/
  31. #ifndef __BFNEW_H__ // {
  32. #define __BFNEW_H__
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #define MINIMUM_ADD 0x100 // our threshold for small amounts
  37. // String buffer
  38. #define CALC_LENGTH ((WORD)0xffff)
  39. /*****************************************************************************
  40. * *
  41. * Typedefs *
  42. * *
  43. *****************************************************************************/
  44. /* Structure for a generic, expandable buffer. */
  45. typedef struct
  46. {
  47. HANDLE hnd;
  48. DWORD cIncr;
  49. HANDLE hBuf;
  50. DWORD cbSize; /* Number of bytes currently in buffer */
  51. DWORD cbMax; /* Amount allocated in qvBuffer */
  52. LPVOID qBuffer;
  53. } BF, FAR * LPBF, FAR * LPSB; // a string buffer is a special case of a buffer
  54. /*****************************************************************************
  55. * *
  56. * Static Variables *
  57. * *
  58. *****************************************************************************/
  59. /*****************************************************************************
  60. * *
  61. * Prototypes *
  62. * *
  63. *****************************************************************************/
  64. /* Generic buffer interface */
  65. #define DynBufferPtr(lpbf) ((LPBYTE)((lpbf)->qBuffer))
  66. #define DynBufferLen(lpbf) ((lpbf)->cbSize)
  67. #define DynBufferEmpty(lpbf) ((lpbf)->cbSize == 0)
  68. #define DynBufferReset(lpbf) (MEMSET((lpbf)->qBuffer, 0, (lpbf)->cbSize), ((lpbf)->cbSize = 0))
  69. #define DynBufferSetLength(lpbf, w) (((w) > (lpbf)->cbSize) ? DynBufferEnsureAdd(lpbf, (w) - (lpbf)->cbSize), ((lpbf)->cbSize = (w)) : ((lpbf)->cbSize = (w)))
  70. #define DynBufferGetHandle(lpbf) ((lpbf)->hBuf)
  71. #define DynBufferNullifyHandle(lpbf) ((lpbf)->hBuf = NULL)
  72. #define DynBufferEnsureAdd(lpbf, w) (((lpbf)->cbSize + (w) <= (lpbf)->cbMax) ? TRUE : InternalEnsureAdd(lpbf, w))
  73. LPBF DynBufferAlloc(DWORD cbIncr);
  74. LPBYTE DynBufferAppend(LPBF, LPBYTE, DWORD);
  75. LPBYTE DynBufferInsert(LPBF lpbf, DWORD lib, LPBYTE qbData, SHORT cbData);
  76. BOOL InternalEnsureAdd(LPBF, DWORD);
  77. // Use these to append small amounts quickly, but since this doesn't check for
  78. // overflow, the caller has to check this themselves if appropriate
  79. #ifdef _DEBUG
  80. #define Xassert(x) assert(x)
  81. #else
  82. #define Xassert(x) 0
  83. #endif
  84. #define DynBufferAppendByte(lpbf, b) \
  85. (\
  86. *(((QB)(lpbf)->qBuffer) + (lpbf)->cbSize) = (b), \
  87. (lpbf)->cbSize++, \
  88. Xassert((lpbf)->cbSize <= (lpbf)->cbMax), \
  89. (lpbf)->qBuffer \
  90. )
  91. #define DynBufferAppendWord(lpbf, w) \
  92. (\
  93. *(LPWORD)(((QB)(lpbf)->qBuffer) + (lpbf)->cbSize) = (w), \
  94. (lpbf)->cbSize += 2, \
  95. Xassert((lpbf)->cbSize <= (lpbf)->cbMax), \
  96. (lpbf)->qBuffer \
  97. )
  98. #define DynBufferAppendDword(lpbf, dw) \
  99. (\
  100. *(LPDWORD)(((QB)(lpbf)->qBuffer) + (lpbf)->cbSize) = (dw), \
  101. (lpbf)->cbSize += 4, \
  102. Xassert((lpbf)->cbSize <= (lpbf)->cbMax), \
  103. (lpbf)->qBuffer \
  104. )
  105. #define DynBufferPeekByte(lpbf, lich, lpb) (((lich) >= 0 && (lich) < (lpbf)->cbSize) ? \
  106. (*(lpb) = *(((QB)(lpbf)->qBuffer) + (lich))), 1 : 0)
  107. #define DynBufferUngetByte(lpbf, lpb) (((lpbf)->cbSize > 0) ? \
  108. (--(lpbf)->cbSize, *(lpb) = *(((QB)(lpbf)->qBuffer) + (lpbf)->cbSize)), 1 : 0)
  109. #define DynBufferUngetWord(lpbf, lpw) (((lpbf)->cbSize > 1) ? \
  110. ((lpbf)->cbSize -= 2, *(lpw) = *(LPWORD)(((QB)(lpbf)->qBuffer) + (lpbf)->cbSize)), 2 : 0)
  111. VOID DynBufferFree(LPBF lpbf);
  112. #define DynBufferGrow(lpbf, w) DynBufferAppend(lpbf, NULL, w)
  113. #ifdef _UNICODE
  114. #define DynBufferAppendChar(lpbf, ch) DynBufferAppendWord(lpbf, (WORD)ch)
  115. #else
  116. #define DynBufferAppendChar(lpbf, ch) DynBufferAppendByte(lpbf, (BYTE)ch)
  117. #endif
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif // __BFNEW_H__ }