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.

632 lines
11 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. SUB_STRING
  5. Abstract:
  6. This module contains the definition of the SUB_STRING class.
  7. Author:
  8. Steve Rowe (stever)
  9. Environment:
  10. ULIB, User Mode
  11. Notes:
  12. A substring is a subsequence of characters taken from a "base"
  13. string. Substrings are useful in those cases in which we want
  14. to operate on "chunks" of a given string (e.g. parsing), without
  15. having to create separate strings for each one.
  16. One must be careful when using substrings, though. Since
  17. substrings depend on a base string, the base string must exist
  18. during the life span of all its substrings. In other words,
  19. the base string must not be deleted before having deleted all
  20. the substrings obtained from it.
  21. Note that GENERIC_STRINGs have no substring support. It is up to
  22. the derived string classes to provide it.
  23. Since substrings are just "windows" into a base string, any
  24. modification of a substring will be reflected in the base
  25. string (hence in all other substrings). This is why we have
  26. two classes of substrings:
  27. SUB_STRING allows only read operations. Note however that
  28. one can modify the base string directly.
  29. DYNAMIC_SUB_STRING is derived from the above, and it allows
  30. write operations. Needless to say, you should
  31. not use this class unless you're sure of what
  32. you are doing.
  33. Substring objects cannot be created and initialized directly by the
  34. user, they must be obtained thru a base string class with substring
  35. support.
  36. Substrings pertaining to the same base string are chained together
  37. in a doubly-linked queue. Since substrings have no knowledge of the
  38. internals of their base string, the chain has a dummy element as its
  39. head. In order to provide substring support, the base string must
  40. follow these steps:
  41. 1.- Before spawning any substring, the substring chain head must
  42. be created and initialized using the InitializeChainHead() method.
  43. Note that this substring is a dummy one, beknown only to the base
  44. string.
  45. 2.- Whenever spawning a new substring, it must be linked to the chain
  46. using the InitializeChainNode() method. Any substring already in
  47. the chain may be use as argument, but the obvious choice is the
  48. chain head.
  49. 3.- Each time that the size of the string changes, the Update() of the
  50. chain head must be invoked.
  51. 4.- Before destroying a base string, you have to make sure that all its
  52. substrings have gone away. This means that the chain head must be
  53. the only element of the chain. Use the HasLinks() method to
  54. verify this.
  55. 5.- Don't forget to destroy the chain head before destorying the base
  56. string.
  57. --*/
  58. //
  59. // This class is no longer supported
  60. //
  61. #include "wstring.hxx"
  62. #define _SUB_STRING_
  63. #if !defined (_SUB_STRING_)
  64. #define _SUB_STRING_
  65. #include "string.hxx"
  66. DECLARE_CLASS( SUB_STRING );
  67. class SUB_STRING : public GENERIC_STRING {
  68. friend SUB_STRING;
  69. public:
  70. DECLARE_CONSTRUCTOR( SUB_STRING );
  71. DECLARE_CAST_MEMBER_FUNCTION( SUB_STRING );
  72. VIRTUAL
  73. ~SUB_STRING (
  74. );
  75. NONVIRTUAL
  76. BOOLEAN
  77. HasLinks (
  78. );
  79. NONVIRTUAL
  80. BOOLEAN
  81. InitializeChainHead (
  82. IN PGENERIC_STRING BaseString
  83. );
  84. NONVIRTUAL
  85. BOOLEAN
  86. InitializeChainNode (
  87. IN OUT PSUB_STRING SubString,
  88. IN CHNUM Position,
  89. IN CHNUM Length
  90. );
  91. VIRTUAL
  92. PBYTE
  93. GetInternalBuffer (
  94. IN CHNUM Position DEFAULT 0
  95. ) CONST;
  96. VIRTUAL
  97. BOOLEAN
  98. IsChAt (
  99. IN WCHAR Char,
  100. IN CHNUM Position DEFAULT 0
  101. ) CONST;
  102. VIRTUAL
  103. BOOLEAN
  104. MakeNumber (
  105. OUT PLONG Number,
  106. IN CHNUM Position DEFAULT 0,
  107. IN CHNUM Length DEFAULT TO_END
  108. ) CONST;
  109. VIRTUAL
  110. ULONG
  111. QueryByteCount (
  112. IN CHNUM Position DEFAULT 0,
  113. IN CHNUM Length DEFAULT TO_END
  114. ) CONST;
  115. VIRTUAL
  116. WCHAR
  117. QueryChAt(
  118. IN CHNUM Position DEFAULT 0
  119. ) CONST;
  120. VIRTUAL
  121. CHNUM
  122. QueryChCount (
  123. ) CONST;
  124. VIRTUAL
  125. PGENERIC_STRING
  126. QueryGenericString (
  127. IN CHNUM Position DEFAULT 0,
  128. IN CHNUM Length DEFAULT TO_END
  129. ) CONST;
  130. VIRTUAL
  131. PSTR
  132. QuerySTR(
  133. IN CHNUM Position DEFAULT 0,
  134. IN CHNUM Length DEFAULT TO_END,
  135. IN OUT PSTR Buffer DEFAULT NULL,
  136. IN ULONG BufferSize DEFAULT 0
  137. ) CONST;
  138. VIRTUAL
  139. PSUB_STRING
  140. QuerySubString (
  141. IN CHNUM Position DEFAULT 0,
  142. IN CHNUM Length DEFAULT TO_END,
  143. OUT PSUB_STRING SubString DEFAULT NULL
  144. );
  145. VIRTUAL
  146. PWSTR
  147. QueryWSTR (
  148. IN CHNUM Position DEFAULT 0,
  149. IN CHNUM Length DEFAULT TO_END,
  150. IN OUT PWSTR Buffer DEFAULT NULL,
  151. IN ULONG BufferSize DEFAULT 0,
  152. IN BOOLEAN ForceNull DEFAULT TRUE
  153. ) CONST;
  154. VIRTUAL
  155. BOOLEAN
  156. Replace (
  157. IN PCGENERIC_STRING String2,
  158. IN CHNUM Position DEFAULT 0,
  159. IN CHNUM Length DEFAULT TO_END,
  160. IN CHNUM Position2 DEFAULT 0,
  161. IN CHNUM Length2 DEFAULT TO_END
  162. );
  163. VIRTUAL
  164. BOOLEAN
  165. SetChAt (
  166. IN WCHAR Char,
  167. IN CHNUM Position DEFAULT 0,
  168. IN CHNUM Length DEFAULT TO_END
  169. );
  170. VIRTUAL
  171. CHNUM
  172. Strchr (
  173. IN WCHAR Char,
  174. IN CHNUM Position DEFAULT 0,
  175. IN CHNUM Length DEFAULT TO_END
  176. ) CONST;
  177. VIRTUAL
  178. LONG
  179. Strcmp (
  180. IN PCGENERIC_STRING GenericString
  181. ) CONST;
  182. VIRTUAL
  183. CHNUM
  184. Strcspn (
  185. IN PCGENERIC_STRING GenericString,
  186. IN CHNUM Position DEFAULT 0,
  187. IN CHNUM Length DEFAULT TO_END
  188. ) CONST;
  189. VIRTUAL
  190. LONG
  191. Stricmp (
  192. IN PCGENERIC_STRING GenericString
  193. ) CONST;
  194. VIRTUAL
  195. LONG
  196. StringCompare (
  197. IN CHNUM Position1,
  198. IN CHNUM Length1,
  199. IN PCGENERIC_STRING GenericString2,
  200. IN CHNUM Position2,
  201. IN CHNUM Length2,
  202. IN USHORT CompareFlags DEFAULT COMPARE_IGNORECASE
  203. ) CONST;
  204. VIRTUAL
  205. CHNUM
  206. StrLen (
  207. ) CONST;
  208. VIRTUAL
  209. CHNUM
  210. Strrchr (
  211. IN WCHAR Char,
  212. IN CHNUM Position DEFAULT 0,
  213. IN CHNUM Length DEFAULT TO_END
  214. ) CONST;
  215. VIRTUAL
  216. CHNUM
  217. Strspn (
  218. IN PCGENERIC_STRING GenericString,
  219. IN CHNUM Position DEFAULT 0,
  220. IN CHNUM Length DEFAULT TO_END
  221. ) CONST;
  222. VIRTUAL
  223. CHNUM
  224. Strstr (
  225. IN PCGENERIC_STRING GenericString,
  226. IN CHNUM Position DEFAULT 0,
  227. IN CHNUM Length DEFAULT TO_END
  228. ) CONST;
  229. NONVIRTUAL
  230. BOOLEAN
  231. Update (
  232. IN CHNUM Length
  233. );
  234. protected:
  235. NONVIRTUAL
  236. PGENERIC_STRING
  237. GetBaseString (
  238. );
  239. NONVIRTUAL
  240. PSUB_STRING
  241. GetNextInChain (
  242. );
  243. NONVIRTUAL
  244. PSUB_STRING
  245. GetPreviousInChain (
  246. );
  247. NONVIRTUAL
  248. VOID
  249. GetValidLength(
  250. IN CHNUM Position,
  251. IN OUT PCHNUM Length
  252. ) CONST;
  253. NONVIRTUAL
  254. CHNUM
  255. QueryStartingPosition (
  256. ) CONST;
  257. NONVIRTUAL
  258. VOID
  259. SetChCount(
  260. IN CHNUM NewCount
  261. );
  262. private:
  263. NONVIRTUAL
  264. VOID
  265. Construct (
  266. );
  267. NONVIRTUAL
  268. VOID
  269. Destroy (
  270. );
  271. PGENERIC_STRING _BaseString; // base string to take substring
  272. CHNUM _StartingPosition; // starting index within base
  273. CHNUM _CountOfChars; // # of chars in substring
  274. PSUB_STRING _Previous; // Previous in chain
  275. PSUB_STRING _Next; // Next in chain
  276. #if DBG==1
  277. ULONG _Signature;
  278. BOOLEAN _Initialized;
  279. #endif
  280. };
  281. INLINE
  282. PGENERIC_STRING
  283. SUB_STRING::GetBaseString (
  284. )
  285. /*++
  286. Routine Description:
  287. Gets a pointer to the base string
  288. Arguments:
  289. none
  290. Return Value:
  291. Pointer to the base string
  292. --*/
  293. {
  294. DebugAssert( _Initialized );
  295. return _BaseString;
  296. }
  297. INLINE
  298. PSUB_STRING
  299. SUB_STRING::GetNextInChain (
  300. )
  301. /*++
  302. Routine Description:
  303. Gets a pointer to next substring in the chain
  304. Arguments:
  305. none
  306. Return Value:
  307. Pointer to substring
  308. --*/
  309. {
  310. DebugAssert( _Initialized );
  311. return _Next;
  312. }
  313. INLINE
  314. PSUB_STRING
  315. SUB_STRING::GetPreviousInChain (
  316. )
  317. /*++
  318. Routine Description:
  319. Gets a pointer to previous substring in the chain
  320. Arguments:
  321. none
  322. Return Value:
  323. Pointer to the previous substring
  324. --*/
  325. {
  326. DebugAssert( _Initialized );
  327. return _Previous;
  328. }
  329. INLINE
  330. VOID
  331. SUB_STRING::GetValidLength (
  332. IN CHNUM Position,
  333. IN OUT PCHNUM Length
  334. ) CONST
  335. /*++
  336. Routine Description:
  337. If the length passed has the magic value TO_END, then it is
  338. updated to be the from the passed position up to the end of the
  339. substring.
  340. Arguments:
  341. Position - Supplies a starting position within the substring
  342. Length - Supplies a length
  343. Return Value:
  344. none
  345. --*/
  346. {
  347. DebugAssert(_Initialized );
  348. DebugAssert( Position <= _CountOfChars );
  349. if ( *Length == TO_END ) {
  350. *Length = _CountOfChars - Position;
  351. }
  352. DebugAssert( Position + *Length <= _CountOfChars );
  353. }
  354. INLINE
  355. BOOLEAN
  356. SUB_STRING::HasLinks (
  357. )
  358. /*++
  359. Routine Description:
  360. Determines if the substring has forward and backward links
  361. Arguments:
  362. none
  363. Return Value:
  364. TRUE if the stubstring has forward or backward links
  365. FALSE otherwise
  366. --*/
  367. {
  368. DebugAssert( _Initialized );
  369. return ( (GetNextInChain() != NULL ) || (GetPreviousInChain() != NULL ) );
  370. }
  371. INLINE
  372. CHNUM
  373. SUB_STRING::QueryStartingPosition (
  374. ) CONST
  375. /*++
  376. Routine Description:
  377. Returns the starting character position within the base string.
  378. Arguments:
  379. None.
  380. Return Value:
  381. The starting character position within the base string.
  382. --*/
  383. {
  384. DebugAssert( _Initialized );
  385. return _StartingPosition;
  386. }
  387. INLINE
  388. VOID
  389. SUB_STRING::SetChCount (
  390. IN CHNUM NewCount
  391. )
  392. /*++
  393. Routine Description:
  394. Sets the number of characters in the substring
  395. Arguments:
  396. NewCount - Supplies the new count of characters
  397. Return Value:
  398. none
  399. --*/
  400. {
  401. DebugAssert( _Initialized );
  402. _CountOfChars = NewCount;
  403. }
  404. #endif // _SUB_STRING_
  405. /*++
  406. Copyright (c) 1990 Microsoft Corporation
  407. Module Name:
  408. DYNAMIC_SUB_STRING
  409. Abstract:
  410. This module contains the definition of the DYNAMIC_SUB_STRING class.
  411. Author:
  412. steve rowe stever 2/1/91
  413. Notes:
  414. DYNAMIC_SUB_STRINGs can modify the base string (see notes for
  415. SUB_STRING class).
  416. Revision History:
  417. --*/
  418. #define _DYNAMIC_SUB_STRING_
  419. #if !defined (_DYNAMIC_SUB_STRING_)
  420. #define _DYNAMIC_SUB_STRING_
  421. DECLARE_CLASS( DYNAMIC_SUB_STRING );
  422. class DYNAMIC_SUB_STRING : public SUB_STRING {
  423. public:
  424. DECLARE_CONSTRUCTOR( DYNAMIC_SUB_STRING );
  425. DECLARE_CAST_MEMBER_FUNCTION( DYNAMIC_SUB_STRING );
  426. NONVIRTUAL
  427. BOOLEAN
  428. Copy (
  429. IN PCGENERIC_STRING GenericString
  430. );
  431. VIRTUAL
  432. BOOLEAN
  433. SetChAt (
  434. IN WCHAR Char,
  435. IN CHNUM Position DEFAULT 0,
  436. IN CHNUM Length DEFAULT TO_END
  437. );
  438. NONVIRTUAL
  439. CHNUM
  440. Truncate (
  441. IN CHNUM Position DEFAULT 0
  442. );
  443. private:
  444. VOID
  445. Construct (
  446. );
  447. };
  448. #endif // _DYNAMIC_SUB_STRING_