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

  1. /***
  2. *stream.c - find a stream not in use
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _getstream() - find a stream not in use
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #ifdef _WIN32
  13. #include <windows.h>
  14. #endif  /* _WIN32 */
  15. #include <malloc.h>
  16. #include <stdio.h>
  17. #include <file2.h>
  18. #include <internal.h>
  19. #include <mtdll.h>
  20. #include <dbgint.h>
  21.  
  22. /***
  23. *FILE *_getstream() - find a stream not in use
  24. *
  25. *Purpose:
  26. *       Find a stream not in use and make it available to caller. Intended
  27. *       for use inside library only
  28. *
  29. *Entry:
  30. *       None. Scans __piob[]
  31. *
  32. *Exit:
  33. *       Returns a pointer to a free stream, or NULL if all are in use.  A
  34. *       stream becomes allocated if the caller decided to use it by setting
  35. *       any r, w, r/w mode.
  36. *
  37. *       [Multi-thread note: If a free stream is found, it is returned in a
  38. *       LOCKED state.  It is the caller's responsibility to unlock the stream.]
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43.  
  44. FILE * __cdecl _getstream (
  45.         void
  46.         )
  47. {
  48.         REG2 FILE *retval = NULL;
  49.  
  50. #ifdef _WIN32
  51.  
  52.         REG1 int i;
  53.  
  54.         /* Get the iob[] scan lock */
  55.         _mlock(_IOB_SCAN_LOCK);
  56.  
  57.         /*
  58.          * Loop through the __piob table looking for a free stream, or the
  59.          * first NULL entry.
  60.          */
  61.         for ( i = 0 ; i < _nstream ; i++ ) {
  62.  
  63.             if ( __piob[i] != NULL ) {
  64.                 /*
  65.                  * if the stream is not inuse, return it.
  66.                  */
  67.                 if ( !inuse( (FILE *)__piob[i] ) ) {
  68. #ifdef _MT
  69.                     _lock_str2(i, __piob[i]);
  70.  
  71.                     if ( inuse( (FILE *)__piob[i] ) ) {
  72.                         _unlock_str2(i, __piob[i]);
  73.                         continue;
  74.                     }
  75. #endif  /* _MT */
  76.                     retval = (FILE *)__piob[i];
  77.                     break;
  78.                 }
  79.             }
  80.             else {
  81.                 /*
  82.                  * allocate a new _FILEX, set _piob[i] to it and return a
  83.                  * pointer to it.
  84.                  */
  85.                 if ( (__piob[i] = _malloc_crt( sizeof(_FILEX) )) != NULL ) {
  86.  
  87. #if defined (_MT)
  88.                     InitializeCriticalSection( &(((_FILEX *)__piob[i])->lock) );
  89.                     EnterCriticalSection( &(((_FILEX *)__piob[i])->lock) );
  90. #endif  /* defined (_MT) */
  91.                     retval = (FILE *)__piob[i];
  92.                 }
  93.  
  94.                 break;
  95.             }
  96.         }
  97.  
  98.         /*
  99.          * Initialize the return stream.
  100.          */
  101.         if ( retval != NULL ) {
  102.             retval->_flag = retval->_cnt = 0;
  103.             retval->_tmpfname = retval->_ptr = retval->_base = NULL;
  104.             retval->_file = -1;
  105.         }
  106.  
  107.         _munlock(_IOB_SCAN_LOCK);
  108.  
  109. #else  /* _WIN32 */
  110. #if defined (_M_MPPC) || defined (_M_M68K)
  111.  
  112.         REG1 FILE *stream = _iob;
  113.  
  114.         /* Loop through the _iob table looking for a free stream.*/
  115.         for (; stream <= _lastiob; stream++) {
  116.  
  117.                 if ( !inuse(stream) ) {
  118.                         stream->_flag = stream->_cnt = 0;
  119.                         stream->_tmpfname = stream->_ptr = stream->_base = NULL;
  120.                         stream->_file = -1;
  121.                         retval = stream;
  122.                         break;
  123.                 }
  124.         }
  125.  
  126. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  127. #endif  /* _WIN32 */
  128.  
  129.         return(retval);
  130. }
  131.