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.

179 lines
4.4 KiB

  1. // SVUTIL.CPP
  2. #include <windows.h>
  3. #include <objidl.h>
  4. #include <mvopsys.h>
  5. // ***************************************************************************
  6. BOOL WINAPI StreamGetLine
  7. (IStream *pStream, LPWSTR lpstrDest, int *cch, int iReceptacleSize)
  8. {
  9. LPWSTR pDest;
  10. int fRet = FALSE;
  11. ULONG ulRead;
  12. if (NULL == pStream)
  13. return FALSE;
  14. pDest = lpstrDest;
  15. /********************
  16. STRING RETRIEVAL LOOP
  17. *********************/
  18. iReceptacleSize--; // save room for 0
  19. for (;;iReceptacleSize--)
  20. {
  21. if (FAILED (pStream->Read (pDest, sizeof (WCHAR), &ulRead)) || !ulRead)
  22. break;
  23. if (*pDest == L'\r')
  24. continue;
  25. if (*pDest == L'\n')
  26. {
  27. fRet = TRUE;
  28. break;
  29. }
  30. pDest++;
  31. }
  32. *pDest++ = L'\0';
  33. if(cch)
  34. *cch = (int)(pDest - lpstrDest);
  35. return fRet;
  36. } /* StreamGetLine */
  37. // ***************************************************************************
  38. BOOL StreamGetLineASCII
  39. (IStream *pStream, LPWSTR lpstrDest, int *cch, int iReceptacleSize)
  40. {
  41. LPSTR pDest;
  42. int fRet = FALSE;
  43. ULONG ulRead;
  44. char rgchLocal[8192]={0};
  45. if (NULL == pStream)
  46. return FALSE;
  47. pDest = rgchLocal;
  48. /********************
  49. STRING RETRIEVAL LOOP
  50. *********************/
  51. iReceptacleSize--; // save room for 0
  52. for (;;iReceptacleSize--)
  53. {
  54. if (FAILED (pStream->Read (pDest, sizeof (CHAR), &ulRead)) || !ulRead)
  55. break;
  56. if (*pDest == '\r')
  57. continue;
  58. if (*pDest == '\n')
  59. {
  60. fRet = TRUE;
  61. break;
  62. }
  63. pDest++;
  64. }
  65. *pDest = '\0';
  66. int iSize = MultiByteToWideChar (CP_ACP, 0, rgchLocal, -1, lpstrDest, 8192);
  67. if(cch)
  68. *cch = iSize;
  69. return fRet;
  70. } /* StreamGetLine */
  71. // ***************************************************************************
  72. DWORD BinFromHex(LPSTR lpszHex, LPSTR lpbData, DWORD dwSize)
  73. {
  74. char *pchend, chBuf[3];
  75. LPSTR lpbStart = lpbData;
  76. chBuf[2] = 0;
  77. if (dwSize &(DWORD)0x01)
  78. {
  79. chBuf[0] = '0';
  80. chBuf[1] = *lpszHex++;
  81. *lpbData++ =(char)strtol(chBuf, &pchend, 16);
  82. --dwSize;
  83. }
  84. for (dwSize /= 2;dwSize; dwSize--)
  85. {
  86. chBuf[0] = *lpszHex++;
  87. chBuf[1] = *lpszHex++;
  88. *lpbData++ =(BYTE)strtol(chBuf, &pchend, 16);
  89. }
  90. return (DWORD)(lpbData - lpbStart);
  91. } /* BinFromHex */
  92. /********************************************************************
  93. * @method DWORD | HexFromBin
  94. * Takes a pointer to binary data and writes it to a buuffer in ASCII
  95. * hex notation.
  96. *
  97. * @comm
  98. ********************************************************************/
  99. DWORD HexFromBin(LPSTR lpszHex, LPBYTE lpbData, DWORD dwSize)
  100. {
  101. LPBYTE lpb, lpbMax;
  102. BYTE ch;
  103. // for each byte, write the hex equivalent
  104. for (lpb = lpbData, lpbMax = lpbData + dwSize; lpb < lpbMax;lpb++)
  105. {
  106. const char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  107. ch = *lpb;
  108. *lpszHex++ = hex[ch >> 4];
  109. *lpszHex++ = hex[ch & 0xf];
  110. }
  111. return dwSize;
  112. } /* Hex FromBin */
  113. /*************************************************************************
  114. * @doc INTERNAL
  115. *
  116. * @func LPSTR PASCAL NEAR | StringToLong |
  117. * The function reads in a string of digits and convert them into
  118. * a DWORD. The function will move the input pointer correspondingly
  119. *
  120. * @parm LPCSTR | lszBuf |
  121. * Input buffer containing the string of digit with no sign
  122. *
  123. * @parm LPDW | lpValue |
  124. * Pointer to a DWORD that receives the result
  125. *
  126. * @rdesc NULL, if there is no digit, else the new position of the input
  127. * buffer pointer
  128. *************************************************************************/
  129. LPSTR WINAPI StringToLong(LPCSTR lszBuf, LPDWORD lpValue)
  130. {
  131. register DWORD Result; // Returned result
  132. register int i; // Scratch variable
  133. char fGetDigit; // Flag to mark we do get a digit
  134. /* Skip all blanks, tabs */
  135. while (*lszBuf == ' ' || *lszBuf == '\t')
  136. lszBuf++;
  137. Result = fGetDigit = 0;
  138. if (*lszBuf >= '0' && *lszBuf <= '9')
  139. {
  140. fGetDigit = TRUE;
  141. /* The credit of this piece of code goes to Leon */
  142. while (i = *lszBuf - '0', i >= 0 && i <= 9)
  143. {
  144. Result = Result * 10 + i;
  145. lszBuf++;
  146. }
  147. }
  148. *lpValue = Result;
  149. return(fGetDigit ? (LPSTR)lszBuf : NULL);
  150. } /* StringToLong */