home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / _getbuf.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  98 lines

  1. /***
  2. *_getbuf.c - Get a stream buffer
  3. *
  4. *       Copyright (c) 1987-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Allocate a buffer and init stream data bases.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <file2.h>
  14. #include <malloc.h>
  15. #include <internal.h>
  16. #include <dbgint.h>
  17.  
  18. /***
  19. *_getbuf() - Allocate a buffer and init stream data bases
  20. *
  21. *Purpose:
  22. *       Allocates a buffer for a stream and inits the stream data bases.
  23. *
  24. *       [NOTE  1: This routine assumes the caller has already checked to make
  25. *       sure the stream needs a buffer.
  26. *
  27. *       [NOTE 2: Multi-thread - Assumes caller has aquired stream lock, if
  28. *       needed.]
  29. *
  30. *Entry:
  31. *       FILE *stream = stream to allocate a buffer for
  32. *
  33. *Exit:
  34. *       void
  35. *
  36. *Exceptions:
  37. *
  38. *******************************************************************************/
  39.  
  40. void __cdecl _getbuf (
  41.         FILE *str
  42.         )
  43. {
  44.         REG1 FILE *stream;
  45.  
  46.         _ASSERTE(str != NULL);
  47.  
  48. #if !defined (CRTDLL)
  49.         /* force library pre-termination procedure */
  50.         _cflush++;
  51. #endif  /* !defined (CRTDLL) */
  52.  
  53.         /* Init pointers */
  54.         stream = str;
  55.  
  56.  
  57.         /* Try to get a big buffer */
  58. #ifdef _WIN32
  59.         if (stream->_base = _malloc_crt(_INTERNAL_BUFSIZ))
  60. #else  /* _WIN32 */
  61. #if defined (_M_MPPC) || defined (_M_M68K)
  62.         if (stream->_base = _malloc_crt(BUFSIZ))
  63. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  64. #endif  /* _WIN32 */
  65.         {
  66.                 /* Got a big buffer */
  67.                 stream->_flag |= _IOMYBUF;
  68. #ifdef _WIN32
  69.                 stream->_bufsiz = _INTERNAL_BUFSIZ;
  70. #else  /* _WIN32 */
  71. #if defined (_M_MPPC) || defined (_M_M68K)
  72.                 stream->_bufsiz = BUFSIZ;
  73. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  74. #endif  /* _WIN32 */
  75.         }
  76.  
  77.         else {
  78.  
  79.  
  80.                 /* Did NOT get a buffer - use single char buffering. */
  81.                 stream->_flag |= _IONBF;
  82.                 stream->_base = (char *)&(stream->_charbuf);
  83. #ifdef _WIN32
  84.                 stream->_bufsiz = 2;
  85. #else  /* _WIN32 */
  86. #if defined (_M_MPPC) || defined (_M_M68K)
  87.                 stream->_bufsiz = 1;
  88. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  89. #endif  /* _WIN32 */
  90.  
  91.         }
  92.  
  93.         stream->_ptr = stream->_base;
  94.         stream->_cnt = 0;
  95.  
  96.         return;
  97. }
  98.