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.

213 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1991-92 Microsoft Corporation
  3. Module Name:
  4. StrArray.c
  5. Abstract:
  6. This is the a header file of handy functions and macros for TCHAR
  7. string arrays.
  8. These arrays are in the following format (spaces added for clarity):
  9. one \0 two \0 three \0 \0
  10. where \0 is a null character in the appropriate format.
  11. These functions are useful for the NetServerDiskEnum and NetConfigGetAll
  12. APIs, and possibly others.
  13. Author:
  14. John Rogers (JohnRo) 24-Oct-1991
  15. Environment:
  16. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  17. Requires ANSI C extensions: slash-slash comments, long external names.
  18. Note:
  19. This function assumes that the machine's default codepage is the same
  20. as the LAN's default codepage.
  21. Revision History:
  22. 24-Oct-1991 JohnRo
  23. Created.
  24. 02-Jan-1992 JohnRo
  25. Moved my RxpCopyStrArrayToTStrArray() from RxCommon to NetLib,
  26. and renamed it. Added some other random functions.
  27. 30-Jan-1992 JohnRo
  28. Fixed off-by-one bug in NetpAddTStrToTStrArray().
  29. Enhanced handling of "empty" TCHAR string arrays.
  30. Use TCHAR_EOS equate.
  31. 29-Apr-1992 RitaW
  32. Changed for service controller use.
  33. --*/
  34. #include <scpragma.h>
  35. #include <stdlib.h>
  36. #include <string.h> // strlen() for codepage strings.
  37. #include <nt.h>
  38. #include <ntrtl.h> // Needed by <scdebug.h>
  39. // These must be included first:
  40. #include <windef.h> // IN, VOID, LPWSTR, etc.
  41. // These can be in any order:
  42. #include <strarray.h> // Exported function prototypes
  43. #include <tstr.h> // WCSSIZE
  44. #include <scdebug.h> // SC_ASSERT
  45. VOID
  46. ScAddWStrToWStrArray (
  47. IN OUT LPWSTR DestArray,
  48. IN LPWSTR Src
  49. )
  50. {
  51. DWORD AddSize; // byte count (including null char) of string being added
  52. LPWSTR DestStringStart;
  53. DWORD NewArraySize;
  54. DWORD OldArraySize;
  55. SC_ASSERT( DestArray != NULL );
  56. SC_ASSERT( Src != NULL );
  57. if ((DestArray == NULL) || (Src == NULL)) {
  58. return;
  59. }
  60. OldArraySize = ScWStrArraySize( DestArray ); // May be 2 bytes.
  61. SC_ASSERT( wcslen(Src) > 0 ); // We couldn't tell from end of array.
  62. AddSize = (DWORD) WCSSIZE( Src );
  63. NewArraySize = OldArraySize + AddSize;
  64. SC_ASSERT( NewArraySize > 0 ); // We couldn't tell from end of array.
  65. //
  66. // Figure-out where new string would start. Note that OldArraySize
  67. // includes the null char which was the end of the array, so we have
  68. // to subtract a character to start the new one where that null char is.
  69. //
  70. DestStringStart = (LPWSTR) ((DWORD_PTR) DestArray + OldArraySize-sizeof(WCHAR));
  71. SC_ASSERT( DestStringStart != NULL );
  72. (void) wcscpy(
  73. DestStringStart, // dest
  74. Src); // src
  75. // Mark end of array.
  76. DestArray[NewArraySize / sizeof(WCHAR) - 1] = 0;
  77. SC_ASSERT( ! ScIsWStrArrayEmpty( DestArray ) );
  78. } // ScAddWStrToWStrArray
  79. #if DBG
  80. VOID
  81. ScDisplayWStrArray (
  82. IN LPWSTR Array
  83. )
  84. {
  85. LPWSTR CurrentEntry = Array;
  86. if (Array == NULL) {
  87. return;
  88. }
  89. if (*CurrentEntry == 0) {
  90. KdPrintEx((DPFLTR_SCSERVER_ID,
  91. DEBUG_DEPEND_DUMP | DEBUG_WARNING | DEBUG_CONFIG,
  92. " (empty)\n"));
  93. } else {
  94. while (*CurrentEntry != 0) {
  95. KdPrintEx((DPFLTR_SCSERVER_ID,
  96. DEBUG_DEPEND_DUMP | DEBUG_WARNING | DEBUG_CONFIG,
  97. " " FORMAT_LPWSTR "\n",
  98. (LPWSTR)CurrentEntry));
  99. CurrentEntry += ( wcslen( CurrentEntry ) + 1 );
  100. }
  101. }
  102. } // ScDisplayWStrArray
  103. #endif // DBG
  104. DWORD
  105. ScWStrArraySize(
  106. IN LPWSTR Array
  107. )
  108. {
  109. DWORD ArrayByteCount = 0;
  110. LPWSTR Entry = Array;
  111. if (Array == NULL) {
  112. return(0);
  113. }
  114. //
  115. // Loop for each string in the array.
  116. //
  117. while ( (*Entry) != 0 ) {
  118. DWORD EntryByteCount = (DWORD) WCSSIZE(Entry); // This entry and its null.
  119. ArrayByteCount += EntryByteCount;
  120. Entry = (LPWSTR) ((LPBYTE) Entry + EntryByteCount);
  121. }
  122. ArrayByteCount += sizeof(WCHAR); // Indicate end of array.
  123. return (ArrayByteCount);
  124. } // ScWStrArraySize
  125. DWORD
  126. ScAStrArraySize(
  127. IN LPSTR Array
  128. )
  129. {
  130. DWORD ArrayByteCount = 0;
  131. LPSTR Entry = Array;
  132. if (Array == NULL) {
  133. return(0);
  134. }
  135. //
  136. // Loop for each string in the array.
  137. //
  138. while ( (*Entry) != 0 ) {
  139. DWORD EntryByteCount = (DWORD) strlen(Entry) + sizeof(CHAR);
  140. ArrayByteCount += EntryByteCount;
  141. Entry = (LPSTR) ((LPBYTE) Entry + EntryByteCount);
  142. }
  143. ArrayByteCount += sizeof(CHAR); // Indicate end of array.
  144. return (ArrayByteCount);
  145. } // ScAStrArraySize