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.

296 lines
6.7 KiB

  1. /*++
  2. Copyright (c) 1991-1992 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. 05-Jun-1992 JohnRo
  32. RAID 11253: NetConfigGetAll fails when remoted to downlevel.
  33. 01-Sep-1992 JohnRo
  34. RAID 5016: NetConfigGetAll heap trash.
  35. --*/
  36. // These must be included first:
  37. #include <windef.h> // IN, VOID, LPWSTR, etc.
  38. #include <lmcons.h> // (Needed by NetLib.h)
  39. // These can be in any order:
  40. #include <netlib.h> // NetpPointerPlusSomeBytes().
  41. #include <netdebug.h> // NetpAssert(), etc.
  42. #include <strarray.h> // LPTSTR_ARRAY, my prototypes.
  43. #include <string.h> // strlen() for codepage strings.
  44. #include <tstring.h> // NetpCopyStrToTStr(), TCHAR_EOS.
  45. //
  46. //////////////////////////////// LPTSTR_ARRAY stuff //////////////////////////
  47. //
  48. VOID
  49. NetpAddTStrToTStrArray (
  50. IN OUT LPTSTR_ARRAY DestArray,
  51. IN LPTSTR Src
  52. )
  53. {
  54. DWORD AddSize; // byte count (including null char) of string being added
  55. LPTSTR DestStringStart;
  56. DWORD NewArraySize;
  57. DWORD OldArraySize;
  58. NetpAssert( DestArray != NULL );
  59. NetpAssert( Src != NULL );
  60. OldArraySize = NetpTStrArraySize( DestArray ); // May be 0.
  61. NetpAssert( STRLEN(Src) > 0 ); // We couldn't tell from end of array.
  62. AddSize = STRSIZE( Src );
  63. NewArraySize = OldArraySize + AddSize;
  64. NetpAssert( 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 = (LPTSTR)
  71. NetpPointerPlusSomeBytes( DestArray, OldArraySize-sizeof(TCHAR) );
  72. NetpAssert( DestStringStart != NULL );
  73. (void) STRCPY(
  74. DestStringStart, // dest
  75. Src); // src
  76. // Mark end of array.
  77. DestArray[NewArraySize/sizeof(TCHAR)-1] = TCHAR_EOS;
  78. NetpAssert( ! NetpIsTStrArrayEmpty( DestArray ) );
  79. } // NetpAddTStrToTStrArray
  80. VOID
  81. NetpCopyStrArrayToTStrArray(
  82. OUT LPTSTR_ARRAY Dest, // string array: TCHARs
  83. IN LPSTR Src // string array: 8-bit input in default codepage for LAN
  84. )
  85. /*++
  86. Routine Description:
  87. NetpCopyStrArrayToTStrArray copies an array of strings (in some codepage)
  88. to an array of strings (in TCHAR format).
  89. These arrays are in the following format (spaces added for clarity):
  90. one \0 two \0 three \0 \0
  91. where \0 is a null character in the appropriate format.
  92. This function is useful for the NetServerDiskEnum and NetConfigGetAll
  93. APIs, and possibly others.
  94. Arguments:
  95. Dest - is an LPTSTR_ARRAY indicating where the converted characters are to
  96. go. This area must be large enough to hold the data.
  97. Src - is an LPSTR array indicating the source strings. This must be an
  98. array of strings in the default codepage of the LAN.
  99. Return Value:
  100. None.
  101. --*/
  102. {
  103. NetpAssert( Dest != NULL );
  104. NetpAssert( Src != NULL );
  105. //
  106. // Loop for each string in the array.
  107. //
  108. while ( (*Src) != '\0' ) {
  109. DWORD SrcByteCount = strlen(Src)+1; // Bytes for this codepage str.
  110. //
  111. // Copy one string.
  112. //
  113. NetpCopyStrToTStr( Dest, Src ); // Copy and conv to TCHARs.
  114. Dest = (LPVOID) ( ((LPBYTE)Dest) + (SrcByteCount * sizeof(TCHAR)) );
  115. Src += SrcByteCount;
  116. }
  117. *Dest = '\0'; // Indicate end of array.
  118. } // NetpCopyStrArrayToTStrArray
  119. #if DBG
  120. // Caller is assumed to have displayed a prefix and/or other identifying
  121. // text.
  122. VOID
  123. NetpDisplayTStrArray (
  124. IN LPTSTR_ARRAY Array
  125. )
  126. {
  127. LPTSTR CurrentEntry = (LPVOID) Array;
  128. NetpAssert( Array != NULL );
  129. if (*CurrentEntry == TCHAR_EOS) {
  130. NetpKdPrint((" (empty)\n"));
  131. } else {
  132. while (*CurrentEntry != TCHAR_EOS) {
  133. NetpKdPrint((" " FORMAT_LPTSTR "\n", (LPTSTR) CurrentEntry));
  134. CurrentEntry += ( STRLEN( CurrentEntry ) + 1 );
  135. }
  136. }
  137. } // NetpDisplayTStrArray
  138. #endif // DBG
  139. DWORD
  140. NetpTStrArrayEntryCount (
  141. IN LPTSTR_ARRAY Array
  142. )
  143. {
  144. DWORD EntryCount = 0;
  145. LPTSTR Entry = (LPVOID) Array;
  146. NetpAssert( Array != NULL );
  147. //
  148. // Loop for each string in the array.
  149. //
  150. while ( (*Entry) != TCHAR_EOS ) {
  151. ++EntryCount;
  152. Entry += STRLEN(Entry)+1;
  153. }
  154. return (EntryCount);
  155. } // NetpTStrArrayEntryCount
  156. DWORD
  157. NetpTStrArraySize(
  158. IN LPTSTR_ARRAY Array
  159. )
  160. {
  161. DWORD ArrayByteCount = 0;
  162. LPTSTR Entry = (LPVOID) Array;
  163. NetpAssert( Array != NULL );
  164. //
  165. // Loop for each string in the array.
  166. //
  167. while ( (*Entry) != TCHAR_EOS ) {
  168. DWORD EntryByteCount = STRSIZE(Entry); // This entry and its null.
  169. ArrayByteCount += EntryByteCount;
  170. Entry = (LPTSTR)NetpPointerPlusSomeBytes(Entry, EntryByteCount);
  171. }
  172. ArrayByteCount += sizeof(TCHAR); // Indicate end of array.
  173. return (ArrayByteCount);
  174. } // NetpTStrArraySize
  175. //
  176. //////////////////////////////// LPSTR_ARRAY stuff //////////////////////////
  177. //
  178. DWORD
  179. NetpStrArraySize(
  180. IN LPSTR_ARRAY Array
  181. )
  182. {
  183. DWORD ArrayByteCount = 0;
  184. LPSTR Entry = (LPVOID) Array;
  185. NetpAssert( Array != NULL );
  186. //
  187. // Loop for each string in the array.
  188. //
  189. while ( (*Entry) != '\0' ) {
  190. DWORD EntryByteCount = strlen(Entry)+1; // This entry and its null.
  191. ArrayByteCount += EntryByteCount;
  192. Entry = (LPSTR)NetpPointerPlusSomeBytes(Entry, EntryByteCount);
  193. }
  194. ArrayByteCount += sizeof(CHAR); // Indicate end of array.
  195. return (ArrayByteCount);
  196. } // NetpStrArraySize