Leaked source code of windows server 2003
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.

124 lines
2.2 KiB

  1. /*
  2. * UTIL.C
  3. *
  4. * Point-of-Sale Control Panel Applet
  5. *
  6. * Author: Ervin Peretz
  7. *
  8. * (c) 2001 Microsoft Corporation
  9. */
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <commctrl.h>
  13. #include <cpl.h>
  14. #include <setupapi.h>
  15. #include <hidsdi.h>
  16. #include "internal.h"
  17. #include "res.h"
  18. #include "debug.h"
  19. /*
  20. * WStrNCpy
  21. *
  22. * Like wcsncpy, but terminates the string if truncated.
  23. * Also, tolerates NULL src string.
  24. */
  25. ULONG WStrNCpy(WCHAR *dest, const WCHAR *src, ULONG maxWChars)
  26. {
  27. ULONG wCharsWritten = 0;
  28. ASSERT(dest);
  29. if (src){
  30. while ((maxWChars-- > 1) && (*dest++ = *src++)){
  31. wCharsWritten++;
  32. }
  33. if (maxWChars == 1){
  34. *dest = L'\0';
  35. wCharsWritten++;
  36. }
  37. }
  38. else {
  39. *dest = L'\0';
  40. wCharsWritten++;
  41. }
  42. return wCharsWritten;
  43. }
  44. /*
  45. * AsciiToWChar
  46. *
  47. * Like mbstowcs, but terminates the string if truncated.
  48. * Also, tolerates NULL ascii string.
  49. */
  50. ULONG AsciiToWChar(WCHAR *dest, const char *src, ULONG maxChars)
  51. {
  52. ULONG charsWritten = 0;
  53. if (src){
  54. while ((maxChars-- > 1) && (*dest++ = (WCHAR)*src++)){
  55. charsWritten++;
  56. }
  57. if (maxChars == 1){
  58. *dest = (WCHAR)NULL;
  59. charsWritten++;
  60. }
  61. }
  62. else {
  63. *dest = (WCHAR)NULL;
  64. charsWritten++;
  65. }
  66. return charsWritten;
  67. }
  68. VOID IntToWChar(WCHAR *buf, DWORD x)
  69. {
  70. int i;
  71. WCHAR tmpbuf[11] = {0};
  72. if (x){
  73. for (i = 10; x && (i >= 0); i--){
  74. tmpbuf[i] = (WCHAR)(L'0' + (x % 10));
  75. x /= 10;
  76. }
  77. WStrNCpy(buf, &tmpbuf[i+1], 11);
  78. }
  79. else {
  80. WStrNCpy(buf, L"0", 2);
  81. }
  82. }
  83. VOID HexToWChar(WCHAR *buf, DWORD x)
  84. {
  85. int i;
  86. WCHAR tmpbuf[9] = {0};
  87. if (x){
  88. for (i = 7; x && (i >= 0); i--){
  89. ULONG nibble = (x % 16);
  90. if (nibble < 10){
  91. tmpbuf[i] = (WCHAR)(L'0' + nibble);
  92. }
  93. else {
  94. tmpbuf[i] = (WCHAR)(L'A' + nibble - 10);
  95. }
  96. x /= 16;
  97. }
  98. WStrNCpy(buf, &tmpbuf[i+1], 9);
  99. }
  100. else {
  101. WStrNCpy(buf, L"0", 2);
  102. }
  103. }