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.

76 lines
1.5 KiB

  1. #include "pch.h"
  2. //Read From Stdin
  3. //Return Value:
  4. // Number of WCHAR read if successful
  5. // -1 in case of Failure. Call GetLastError to get the error.
  6. LONG ReadFromIn(OUT LPWSTR *ppBuffer)
  7. {
  8. LPWSTR pBuffer = NULL;
  9. DWORD dwErr = ERROR_SUCCESS;
  10. pBuffer = (LPWSTR)LocalAlloc(LPTR,INIT_SIZE*sizeof(WCHAR));
  11. if(!pBuffer)
  12. {
  13. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  14. return -1;
  15. }
  16. LONG Pos = 0;
  17. LONG MaxSize = INIT_SIZE;
  18. wint_t ch;
  19. while((ch = getwchar()) != WEOF)
  20. {
  21. if(Pos == MaxSize -1 )
  22. {
  23. if(ERROR_SUCCESS != ResizeByTwo(&pBuffer,&MaxSize))
  24. {
  25. LocalFree(pBuffer);
  26. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  27. return -1;
  28. }
  29. }
  30. pBuffer[Pos++] = (WCHAR)ch;
  31. }
  32. pBuffer[Pos] = L'\0';
  33. *ppBuffer = pBuffer;
  34. return Pos;
  35. }
  36. //General Utility Functions
  37. DWORD ResizeByTwo( LPTSTR *ppBuffer,
  38. LONG *pSize )
  39. {
  40. LPWSTR pTempBuffer = (LPWSTR)LocalAlloc(LPTR,(*pSize)*2*sizeof(WCHAR));
  41. if(!pTempBuffer)
  42. return ERROR_NOT_ENOUGH_MEMORY;
  43. memcpy(pTempBuffer,*ppBuffer,*pSize*sizeof(WCHAR));
  44. LocalFree(*ppBuffer);
  45. *ppBuffer = pTempBuffer;
  46. *pSize *=2;
  47. return ERROR_SUCCESS;
  48. }
  49. BOOL StringCopy( LPWSTR *ppDest, LPWSTR pSrc)
  50. {
  51. *ppDest = NULL;
  52. if(!pSrc)
  53. return TRUE;
  54. *ppDest = (LPWSTR)LocalAlloc(LPTR, (wcslen(pSrc) + 1)*sizeof(WCHAR));
  55. if(!*ppDest)
  56. return FALSE;
  57. wcscpy(*ppDest,pSrc);
  58. return TRUE;
  59. }