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.

202 lines
6.0 KiB

  1. /*************************************************************************
  2. * *
  3. * ITUTILS.CPP *
  4. * *
  5. * Copyright (C) Microsoft Corporation 1997 *
  6. * All Rights reserved. *
  7. * *
  8. **************************************************************************
  9. * *
  10. * Module Intent *
  11. * Provide a place to put miscellaneous utility routines. *
  12. * *
  13. **************************************************************************
  14. * *
  15. * Current Owner: InfoTech Team *
  16. * *
  17. *************************************************************************/
  18. #include <mvopsys.h>
  19. #ifdef _DEBUG
  20. static char s_aszModule[] = __FILE__; /* For error report */
  21. #endif
  22. #include <mem.h>
  23. #include <orkin.h>
  24. #include <iterror.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /*************************************************************************
  29. * @doc INTERNAL
  30. *
  31. * @func HRESULT FAR PASCAL | ReallocBufferHmem |
  32. * This function will reallocate or allocate anew a buffer of
  33. * requested size.
  34. *
  35. * @parm HGLOBAL | *phmemBuf |
  36. * Pointer to buffer handle; buffer handle can be NULL if
  37. * a new buffer needs to be allocated. New buffer handle
  38. * is returned through this param.
  39. *
  40. * @parm DWORD | *pcbBufCur |
  41. * Current size of existing buffer, if any. Should be
  42. * 0 if *phmemBuf == 0. New size is returned through
  43. * this param.
  44. *
  45. * @parm DWORD | cbBufNew |
  46. * Current size of existing buffer, if any. Should be
  47. * 0 if *phmemBuf == 0.
  48. *
  49. * @rvalue E_POINTER | phmemBuf or pcbBufCur was NULL
  50. * @rvalue E_OUTOFMEMORY | Ran out of memory (re)allocating the buffer.
  51. *************************************************************************/
  52. HRESULT FAR PASCAL ReallocBufferHmem(HGLOBAL *phmemBuf, DWORD *pcbBufCur,
  53. DWORD cbBufNew)
  54. {
  55. HRESULT hr = S_OK;
  56. if (phmemBuf == NULL || pcbBufCur == NULL)
  57. return (E_POINTER);
  58. // Need to make sure we have a buffer big enough to hold what the caller
  59. // needs to store.
  60. if (cbBufNew > *pcbBufCur)
  61. {
  62. HGLOBAL hmemNew;
  63. if (*phmemBuf == NULL)
  64. hmemNew = _GLOBALALLOC(GMEM_MOVEABLE, cbBufNew);
  65. else
  66. hmemNew = _GLOBALREALLOC(*phmemBuf, cbBufNew, GMEM_MOVEABLE);
  67. if (hmemNew != NULL)
  68. {
  69. // Do reassignment just in case the new hmem is different
  70. // than the old or if we just allocated a buffer for the
  71. // first time.
  72. *phmemBuf = hmemNew;
  73. *pcbBufCur = cbBufNew;
  74. }
  75. else
  76. // A pre-existing *phmemBuf is still valid;
  77. // we'll free it in Close().
  78. hr = E_OUTOFMEMORY;
  79. }
  80. return (hr);
  81. }
  82. /*************************************************************************
  83. * @doc INTERNAL
  84. *
  85. * @func void FAR PASCAL | SetGrfFlag |
  86. * Sets or clears a bit flag in a group of flags.
  87. *
  88. * @parm DWORD | *pgrf |
  89. * Pointer to the group of flags.
  90. *
  91. * @parm DWORD | fGrfFlag |
  92. * Flag to set or clear.
  93. *
  94. * @parm BOOL | fSet |
  95. * TRUE to set fGrfFlag; FALSE to clear fGrfFlag.
  96. *************************************************************************/
  97. void FAR PASCAL SetGrfFlag(DWORD *pgrf, DWORD fGrfFlag, BOOL fSet)
  98. {
  99. if (pgrf == NULL)
  100. return;
  101. *pgrf &= (~fGrfFlag);
  102. if (fSet)
  103. *pgrf |= fGrfFlag;
  104. }
  105. /*
  106. Memory Maps a give file for Read-Only, sequential access
  107. Return a pointer to the memory mapped address space and sets
  108. pdwFileSize to the size of the file if it is not NULL.
  109. */
  110. LPSTR MapSequentialReadFile(LPCSTR szFilename, LPDWORD pdwFileSize)
  111. {
  112. LPSTR pMemory;
  113. HANDLE hInput, hMemMap;
  114. if (NULL == szFilename)
  115. return NULL;
  116. // Open input file
  117. hInput = CreateFile(szFilename, GENERIC_READ, FILE_SHARE_READ,
  118. NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  119. if (INVALID_HANDLE_VALUE == hInput)
  120. return NULL;
  121. // Get the file size for the user
  122. if (pdwFileSize)
  123. *pdwFileSize = GetFileSize(hInput, NULL);
  124. // Create file mapping object
  125. hMemMap = CreateFileMapping(hInput, NULL, PAGE_READONLY, 0, 0, NULL);
  126. CloseHandle(hInput); // Done with this handle
  127. if (NULL == hMemMap)
  128. return NULL;
  129. // Link the object to memory space
  130. pMemory =(LPSTR)MapViewOfFile(hMemMap, FILE_MAP_READ, 0, 0, 0);
  131. CloseHandle(hMemMap); // Done with this handle
  132. return pMemory;
  133. } /* MapSequentialReadFile */
  134. // This routine was extracted from the C runtime, simplified, and renamed.
  135. /***
  136. *int _wcsicmp(dst, src) - compare wide-character strings, ignore case
  137. *
  138. *Purpose:
  139. * _wcsicmp perform a case-insensitive wchar_t string comparision.
  140. *
  141. *Entry:
  142. * wchar_t *dst, *src - strings to compare
  143. *
  144. *Return:
  145. * <0 if dst < src
  146. * 0 if dst = src
  147. * >0 if dst > src
  148. * This range of return values may differ from other *cmp/*coll functions.
  149. *
  150. *Exceptions:
  151. *
  152. *******************************************************************************/
  153. int __cdecl _it_wcsicmp (
  154. const wchar_t * dst,
  155. const wchar_t * src
  156. )
  157. {
  158. wchar_t f,l;
  159. do {
  160. f = ((*dst <= L'Z') && (*dst >= L'A'))
  161. ? *dst + L'a' - L'A'
  162. : *dst;
  163. l = ((*src <= L'Z') && (*src >= L'A'))
  164. ? *src + L'a' - L'A'
  165. : *src;
  166. dst++;
  167. src++;
  168. } while ( (f) && (f == l) );
  169. return (int)(f - l);
  170. }
  171. #ifdef __cplusplus
  172. }
  173. #endif