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.

221 lines
6.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. //BeginExport(typedef)
  13. typedef struct _M_OPTDEF {
  14. DWORD OptId;
  15. DWORD Type;
  16. LPWSTR OptName;
  17. LPWSTR OptComment;
  18. LPBYTE OptVal;
  19. DWORD OptValLen;
  20. } M_OPTDEF, *PM_OPTDEF, *LPM_OPTDEF;
  21. typedef struct _M_OPTDEFLIST {
  22. ARRAY OptDefArray;
  23. } M_OPTDEFLIST, *PM_OPTDEFLIST, *LPM_OPTDEFLIST;
  24. //EndExport(typedef)
  25. //BeginExport(inline)
  26. DWORD _inline
  27. MemOptDefListInit(
  28. IN OUT PM_OPTDEFLIST OptDefList
  29. ) {
  30. AssertRet(OptDefList, ERROR_INVALID_PARAMETER);
  31. return MemArrayInit(&OptDefList->OptDefArray);
  32. }
  33. //EndExport(inline)
  34. //BeginExport(inline)
  35. DWORD _inline
  36. MemOptDefListCleanup(
  37. IN OUT PM_OPTDEFLIST OptDefList
  38. ) {
  39. return MemArrayCleanup(&OptDefList->OptDefArray);
  40. }
  41. //EndExport(inline)
  42. //BeginExport(function)
  43. DWORD
  44. MemOptDefListFindOptDefInternal( // Dont use this function out of optdefl.c
  45. IN PM_OPTDEFLIST OptDefList,
  46. IN DWORD OptId,
  47. IN LPWSTR OptName, // either OptId or OptName need only be specified..
  48. OUT PARRAY_LOCATION Location
  49. ) //EndExport(function)
  50. {
  51. DWORD Error;
  52. PM_OPTDEF RetOptDef;
  53. Error = MemArrayInitLoc(&OptDefList->OptDefArray, Location);
  54. while(ERROR_FILE_NOT_FOUND != Error) {
  55. Require(ERROR_SUCCESS == Error);
  56. Error = MemArrayGetElement(&OptDefList->OptDefArray, Location, (LPVOID*)&RetOptDef);
  57. Require(ERROR_SUCCESS == Error);
  58. if( RetOptDef->OptId == OptId ) return ERROR_SUCCESS;
  59. if(OptName)
  60. if( 0 == wcscmp(RetOptDef->OptName, OptName) ) return ERROR_SUCCESS;
  61. Error = MemArrayNextLoc(&OptDefList->OptDefArray, Location);
  62. }
  63. return ERROR_FILE_NOT_FOUND;
  64. }
  65. //BeginExport(inline)
  66. DWORD _inline
  67. MemOptDefListFindOptDef(
  68. IN PM_OPTDEFLIST OptDefList,
  69. IN DWORD OptId,
  70. IN LPWSTR OptName, // only either the name or the option id need be given..
  71. OUT PM_OPTDEF *OptDef
  72. ) {
  73. ARRAY_LOCATION Location;
  74. DWORD Error;
  75. Error = MemOptDefListFindOptDefInternal(
  76. OptDefList,
  77. OptId,
  78. OptName,
  79. &Location
  80. );
  81. if( ERROR_SUCCESS != Error ) return Error;
  82. return MemArrayGetElement(
  83. &OptDefList->OptDefArray,
  84. &Location,
  85. (LPVOID *)OptDef
  86. );
  87. }
  88. //EndExport(inline)
  89. //BeginExport(function)
  90. DWORD
  91. MemOptDefListAddOptDef( // Add or replace an option defintion for given Option Id
  92. IN OUT PM_OPTDEFLIST OptDefList,
  93. IN DWORD OptId,
  94. IN DWORD Type,
  95. IN LPWSTR OptName,
  96. IN LPWSTR OptComment,
  97. IN LPBYTE OptVal,
  98. IN DWORD OptValLen
  99. ) //EndExport(function)
  100. {
  101. ARRAY_LOCATION Location;
  102. PM_OPTDEF OptDef;
  103. PM_OPTDEF ThisOptDef;
  104. DWORD Size;
  105. DWORD Error;
  106. AssertRet(OptDefList, ERROR_INVALID_PARAMETER);
  107. Error = MemOptDefListFindOptDefInternal(
  108. OptDefList,
  109. OptId,
  110. OptName,
  111. &Location
  112. );
  113. Require(ERROR_FILE_NOT_FOUND == Error || ERROR_SUCCESS == Error);
  114. Size = sizeof(M_OPTDEF) + OptValLen ;
  115. Size = ROUND_UP_COUNT(Size, ALIGN_WORST);
  116. if( OptName ) Size += (1+wcslen(OptName))*sizeof(WCHAR);
  117. if( OptComment ) Size += (1+wcslen(OptComment))*sizeof(WCHAR);
  118. OptDef = MemAlloc(Size);
  119. if( NULL == OptDef ) return ERROR_NOT_ENOUGH_MEMORY;
  120. memcpy(sizeof(M_OPTDEF) +(LPBYTE)OptDef, OptVal, OptValLen);
  121. Size = sizeof(M_OPTDEF) + OptValLen ;
  122. Size = ROUND_UP_COUNT(Size, ALIGN_WORST);
  123. OptDef->OptVal = sizeof(M_OPTDEF) + (LPBYTE)OptDef;
  124. OptDef->OptValLen = OptValLen;
  125. OptDef->OptId = OptId;
  126. OptDef->Type = Type;
  127. if( OptName ) {
  128. OptDef->OptName = (LPWSTR)(Size + (LPBYTE)OptDef);
  129. wcscpy(OptDef->OptName, OptName);
  130. Size += sizeof(WCHAR)*(1 + wcslen(OptName));
  131. } else {
  132. OptDef->OptName = NULL;
  133. }
  134. if( OptComment) {
  135. OptDef->OptComment = (LPWSTR)(Size + (LPBYTE)OptDef);
  136. wcscpy(OptDef->OptComment, OptComment);
  137. } else {
  138. OptDef->OptComment = NULL;
  139. }
  140. if( ERROR_SUCCESS == Error ) {
  141. DebugPrint2("Overwriting option definition for 0x%lx\n", OptId);
  142. Error = MemArrayGetElement(
  143. &OptDefList->OptDefArray,
  144. &Location,
  145. (LPVOID*)&ThisOptDef
  146. );
  147. Require(ERROR_SUCCESS == Error && ThisOptDef);
  148. MemFree(ThisOptDef);
  149. Error = MemArraySetElement(
  150. &OptDefList->OptDefArray,
  151. &Location,
  152. (LPVOID)OptDef
  153. );
  154. Require(ERROR_SUCCESS==Error);
  155. return Error;
  156. }
  157. Error = MemArrayAddElement(
  158. &OptDefList->OptDefArray,
  159. (LPVOID)OptDef
  160. );
  161. if( ERROR_SUCCESS != Error ) MemFree(OptDef);
  162. return Error;
  163. }
  164. //BeginExport(inline)
  165. DWORD _inline
  166. MemOptDefListDelOptDef(
  167. IN OUT PM_OPTDEFLIST OptDefList,
  168. IN DWORD OptId
  169. ) {
  170. ARRAY_LOCATION Location;
  171. DWORD Error;
  172. PM_OPTDEF OptDef;
  173. Error = MemOptDefListFindOptDefInternal(
  174. OptDefList,
  175. OptId,
  176. NULL,
  177. &Location
  178. );
  179. if( ERROR_SUCCESS != Error ) return Error;
  180. Error = MemArrayDelElement(
  181. &OptDefList->OptDefArray,
  182. &Location,
  183. &OptDef
  184. );
  185. Require(ERROR_SUCCESS == Error && OptDef);
  186. MemFree(OptDef);
  187. return ERROR_SUCCESS;
  188. }
  189. //EndExport(inline)
  190. //================================================================================
  191. // end of file
  192. //================================================================================