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.

181 lines
5.5 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: implements the basic structures for a list of option definitions
  5. // ThreadSafe: no
  6. // Locks: none
  7. // Please read stdinfo.txt for programming style.
  8. //================================================================================
  9. #include <mm.h>
  10. #include <array.h>
  11. #include <wchar.h>
  12. #include "optdefl.h"
  13. #include "server\uniqid.h"
  14. //BeginExport(function)
  15. DWORD
  16. MemOptDefListFindOptDefInternal( // Dont use this function out of optdefl.c
  17. IN PM_OPTDEFLIST OptDefList,
  18. IN DWORD OptId,
  19. IN LPWSTR OptName, // either OptId or OptName need only be specified..
  20. OUT PARRAY_LOCATION Location
  21. ) //EndExport(function)
  22. {
  23. DWORD Error;
  24. PM_OPTDEF RetOptDef;
  25. Error = MemArrayInitLoc(&OptDefList->OptDefArray, Location);
  26. while(ERROR_FILE_NOT_FOUND != Error) {
  27. Require(ERROR_SUCCESS == Error);
  28. Error = MemArrayGetElement(&OptDefList->OptDefArray, Location, (LPVOID*)&RetOptDef);
  29. Require(ERROR_SUCCESS == Error);
  30. if( RetOptDef->OptId == OptId ) return ERROR_SUCCESS;
  31. if(OptName)
  32. if( 0 == wcscmp(RetOptDef->OptName, OptName) ) return ERROR_SUCCESS;
  33. Error = MemArrayNextLoc(&OptDefList->OptDefArray, Location);
  34. }
  35. return ERROR_FILE_NOT_FOUND;
  36. }
  37. DWORD
  38. MemOptDefListDelOptDef(
  39. IN OUT PM_OPTDEFLIST OptDefList,
  40. IN DWORD OptId
  41. ) {
  42. ARRAY_LOCATION Location;
  43. DWORD Error;
  44. PM_OPTDEF OptDef;
  45. Error = MemOptDefListFindOptDefInternal(
  46. OptDefList,
  47. OptId,
  48. NULL,
  49. &Location
  50. );
  51. if( ERROR_SUCCESS != Error ) return Error;
  52. // Delete it from the database first
  53. Error = MemArrayGetElement( &OptDefList->OptDefArray,
  54. &Location,
  55. &OptDef );
  56. Require( Error == ERROR_SUCCESS );
  57. Error = DeleteRecord( OptDef->UniqId );
  58. if ( ERROR_SUCCESS != Error ) {
  59. return Error;
  60. }
  61. Error = MemArrayDelElement(
  62. &OptDefList->OptDefArray,
  63. &Location,
  64. &OptDef
  65. );
  66. Require(ERROR_SUCCESS == Error && OptDef);
  67. MemFree(OptDef);
  68. return ERROR_SUCCESS;
  69. }
  70. //BeginExport(function)
  71. DWORD
  72. MemOptDefListAddOptDef( // Add or replace an option defintion for given Option Id
  73. IN OUT PM_OPTDEFLIST OptDefList,
  74. IN DWORD OptId,
  75. IN DWORD Type,
  76. IN LPWSTR OptName,
  77. IN LPWSTR OptComment,
  78. IN LPBYTE OptVal,
  79. IN DWORD OptValLen,
  80. IN ULONG UniqId
  81. ) //EndExport(function)
  82. {
  83. ARRAY_LOCATION Location;
  84. PM_OPTDEF OptDef;
  85. PM_OPTDEF ThisOptDef;
  86. DWORD Size;
  87. DWORD Error;
  88. AssertRet(OptDefList, ERROR_INVALID_PARAMETER);
  89. Error = MemOptDefListFindOptDefInternal(
  90. OptDefList,
  91. OptId,
  92. OptName,
  93. &Location
  94. );
  95. Require(ERROR_FILE_NOT_FOUND == Error || ERROR_SUCCESS == Error);
  96. Size = sizeof(M_OPTDEF) + OptValLen ;
  97. Size = ROUND_UP_COUNT(Size, ALIGN_WORST);
  98. if( OptName ) Size += (1+wcslen(OptName))*sizeof(WCHAR);
  99. if( OptComment ) Size += (1+wcslen(OptComment))*sizeof(WCHAR);
  100. // This contains optdef struct + all the values in one buffer
  101. OptDef = MemAlloc(Size);
  102. if( NULL == OptDef ) return ERROR_NOT_ENOUGH_MEMORY;
  103. memcpy(sizeof(M_OPTDEF) +(LPBYTE)OptDef, OptVal, OptValLen);
  104. Size = sizeof(M_OPTDEF) + OptValLen ;
  105. Size = ROUND_UP_COUNT(Size, ALIGN_WORST);
  106. OptDef->OptVal = sizeof(M_OPTDEF) + (LPBYTE)OptDef;
  107. OptDef->OptValLen = OptValLen;
  108. OptDef->OptId = OptId;
  109. OptDef->Type = Type;
  110. if( OptName ) {
  111. OptDef->OptName = (LPWSTR)(Size + (LPBYTE)OptDef);
  112. wcscpy(OptDef->OptName, OptName);
  113. Size += sizeof(WCHAR)*(1 + wcslen(OptName));
  114. } else {
  115. OptDef->OptName = NULL;
  116. }
  117. if( OptComment) {
  118. OptDef->OptComment = (LPWSTR)(Size + (LPBYTE)OptDef);
  119. wcscpy(OptDef->OptComment, OptComment);
  120. } else {
  121. OptDef->OptComment = NULL;
  122. }
  123. OptDef->UniqId = UniqId;
  124. if( ERROR_SUCCESS == Error ) {
  125. Error = MemArrayGetElement(
  126. &OptDefList->OptDefArray,
  127. &Location,
  128. (LPVOID*)&ThisOptDef
  129. );
  130. Require(ERROR_SUCCESS == Error && ThisOptDef);
  131. Error = DeleteRecord( ThisOptDef->UniqId );
  132. MemFree(ThisOptDef);
  133. if ( ERROR_SUCCESS != Error ) {
  134. return Error;
  135. }
  136. Error = MemArraySetElement(
  137. &OptDefList->OptDefArray,
  138. &Location,
  139. (LPVOID)OptDef
  140. );
  141. Require(ERROR_SUCCESS==Error);
  142. return Error;
  143. } // if
  144. Error = MemArrayAddElement(
  145. &OptDefList->OptDefArray,
  146. (LPVOID)OptDef
  147. );
  148. if( ERROR_SUCCESS != Error ) MemFree(OptDef);
  149. return Error;
  150. } // MemOptDefListAddOptDef()
  151. //================================================================================
  152. // end of file
  153. //================================================================================