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.

305 lines
7.6 KiB

  1. /*
  2. * Windows Calendar
  3. * Copyright (c) 1985 by Microsoft Corporation, all rights reserved.
  4. * Written by Mark L. Chamberlin, consultant to Microsoft.
  5. *
  6. */
  7. /*
  8. *****
  9. ***** caltdd.c
  10. *****
  11. */
  12. #include "cal.h"
  13. #define CDDMAX 512 /* Max number of DD in the tdd. */
  14. #define CDDEXTRA 8 /* When grabbing more memory for the tdd,
  15. allocate CDDEXTRA extra DDs. This avoids
  16. doing a ReAlloc for each DD needed, so
  17. things happen faster.
  18. */
  19. /**** InitTdd - Initialize the tdd. */
  20. VOID APIENTRY InitTdd ()
  21. {
  22. //- InitTdd: Fixed to take care of bug with LocalReAlloc of returning
  23. //- NULL when the original block is already of size zero.
  24. //- Now just free and allocate later.
  25. if (vcddAllocated != 0 && vhlmTdd)
  26. {
  27. LocalFree (vhlmTdd);
  28. }
  29. vcddUsed = vcddAllocated = 0;
  30. vhlmTdd = (LOCALHANDLE)NULL;
  31. }
  32. /**** FSearchTdd - Search the tdd for the specified DT.
  33. If found, return TRUE and the index of the matching entry.
  34. If not found, return FALSE and the index of where to insert (the
  35. first dd having a DT greater than the one searched for).
  36. */
  37. BOOL APIENTRY FSearchTdd (
  38. DT dt, /* Input - Date to search for. */
  39. INT *pitdd) /* Output - index of match or insertion point if no
  40. match.
  41. */
  42. {
  43. /* Note - it's important that the indices be declared as signed
  44. ints since it's possible for itddHigh to go to -1 (if the
  45. item being searched for is less than the first entry in the
  46. table). If it were necessary for the indices to be unsigned
  47. (to allow a larger table size), some coding changes would be
  48. necessary, but for this application ints will work fine since
  49. the table will not be allowed to exceed 32767 entries (in fact,
  50. the limit will be much lower).
  51. */
  52. register INT itddLow;
  53. register INT itddHigh;
  54. BOOL fFound;
  55. DD *pddFirst;
  56. DT dtTemp;
  57. BOOL fGreater;
  58. /* Lock down the tdd and get the address of the first dd in it. */
  59. pddFirst = TddLock ();
  60. /* Note - in case the tdd is empty, initialize the insertion point
  61. to 0 for the caller. Also set fGreater to FALSE so if the tdd is
  62. empty, the 0 in itdd will get returned without being incremented.
  63. */
  64. *pitdd = itddLow = 0;
  65. itddHigh = vcddUsed - 1;
  66. fFound = fGreater = FALSE;
  67. while (itddLow <= itddHigh && !fFound)
  68. {
  69. fGreater = FALSE;
  70. *pitdd = (itddLow + itddHigh) / 2;
  71. if (dt == (dtTemp = (pddFirst + *pitdd) -> dt))
  72. fFound = TRUE;
  73. else if (dt > dtTemp)
  74. {
  75. fGreater = TRUE;
  76. itddLow = *pitdd + 1;
  77. }
  78. else
  79. itddHigh = *pitdd - 1;
  80. }
  81. TddUnlock ();
  82. /* The search item was greater than the table item on the last
  83. comparison made. Return the index of the next higher table
  84. entry, since this is the insertion point. Note that if
  85. dt == dtTemp, the index is already that of the matching item,
  86. and if dt < dtTemp, the index is already that of the insertion
  87. point.
  88. */
  89. if (fGreater)
  90. (*pitdd)++;
  91. return (fFound);
  92. }
  93. /**** FGrowTdd - Grow the tdd by the specified number of DD at the specified
  94. place. If can't grow that much, put up an error message then return
  95. FALSE. If successful, return TRUE.
  96. */
  97. BOOL APIENTRY FGrowTdd (
  98. INT itdd, /* INPUT - Where the insertion occurs. */
  99. INT cdd) /* INPUT - How many DD to insert. */
  100. {
  101. DD *pdd;
  102. register INT cddUsedNew;
  103. register INT cddAllocatedNew;
  104. if ((cddUsedNew = vcddUsed + cdd) > CDDMAX)
  105. {
  106. /* Can't make it that big. */
  107. AlertBox (vszTooManyDates, (CHAR *)NULL,
  108. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  109. return (FALSE);
  110. }
  111. if (cddUsedNew > vcddAllocated)
  112. {
  113. /* We must allocate some more memory to the tdd. Allocate
  114. more than we need right now to avoid always having to allocate
  115. for each new DD.
  116. */
  117. cddAllocatedNew = cddUsedNew + CDDEXTRA;
  118. //- GrowTdd: Fixed to call LocalAlloc instead of LocalReAlloc because
  119. //- of bug in LocalReAlloc with zero size allocation.
  120. if (vcddAllocated == 0 || vhlmTdd == 0)
  121. {
  122. if ((vhlmTdd = LocalAlloc (LMEM_MOVEABLE,
  123. cddAllocatedNew * sizeof (DD))) == (LOCALHANDLE)NULL)
  124. {
  125. /* Could not get the requested memory. */
  126. AlertBox (vszOutOfMemory, (CHAR *)NULL,
  127. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  128. return (FALSE);
  129. }
  130. }
  131. else
  132. {
  133. if ((vhlmTdd = LocalReAlloc (vhlmTdd, cddAllocatedNew * sizeof (DD),
  134. LMEM_MOVEABLE)) == (LOCALHANDLE)NULL)
  135. {
  136. /* Could not get the requested memory. */
  137. AlertBox (vszOutOfMemory, (CHAR *)NULL,
  138. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  139. return (FALSE);
  140. }
  141. }
  142. vcddAllocated = cddAllocatedNew;
  143. }
  144. /* Block transfer up all DD at or beyond the insertion point. */
  145. pdd = TddLock () + itdd;
  146. BltByte ((BYTE *)pdd, (BYTE *)(pdd + cdd),
  147. (WORD)(sizeof (DD) * (vcddUsed - itdd)));
  148. TddUnlock ();
  149. vcddUsed = cddUsedNew;
  150. return (TRUE);
  151. }
  152. /**** ShrinkTdd - Shrink the tdd by the specified number of DD. */
  153. VOID APIENTRY ShrinkTdd (
  154. INT itdd, /* The index of the first DD to be deleted. */
  155. INT cdd) /* The number of DD to be deleted. */
  156. {
  157. register DD *pdd;
  158. register INT cddAllocatedNew;
  159. /* Lock the tdd, and get a pointer to the deletion point. */
  160. pdd = TddLock () + itdd;
  161. /* Block transfer down all dd beyond the deletion point. */
  162. BltByte ((BYTE *)(pdd + cdd), (BYTE *)pdd,
  163. (WORD)(sizeof (DD) * (vcddUsed - (itdd + cdd))));
  164. /* Adjust the count of dd. */
  165. vcddUsed -= cdd;
  166. TddUnlock ();
  167. if (vcddAllocated > (cddAllocatedNew = vcddUsed + CDDEXTRA))
  168. {
  169. /* There's more than CDDEXTRA free DDs now, so free up the
  170. extra ones.
  171. */
  172. //- ShrinkTdd: Fixed to handle bug in LocalReAlloc when trying to do
  173. //- realloc of size zero.
  174. if ((vcddAllocated = cddAllocatedNew) == 0)
  175. {
  176. if (vhlmTdd)
  177. vhlmTdd = LocalFree (vhlmTdd);
  178. }
  179. else
  180. {
  181. if (vhlmTdd)
  182. vhlmTdd = LocalReAlloc (vhlmTdd, cddAllocatedNew * sizeof (DD),
  183. LMEM_MOVEABLE);
  184. }
  185. }
  186. }
  187. /**** BltByte - Block transfer a range of bytes either up or down. */
  188. BYTE * APIENTRY BltByte (
  189. BYTE *pbSrc,
  190. BYTE *pbDst,
  191. UINT cb)
  192. {
  193. register BYTE *pbMax;
  194. pbMax = pbDst + cb;
  195. if (pbSrc >= pbDst)
  196. {
  197. /* Transferring down (from high to low addresses).
  198. Start at the beginning of the block and work
  199. towards higher addresses to avoid overwrite.
  200. */
  201. while (cb-- != 0)
  202. *pbDst++ = *pbSrc++;
  203. }
  204. else
  205. {
  206. /* Transferring up (from low to high addresses).
  207. Start at the end of the block and work towards lower
  208. addresses to avoid overwrite.
  209. */
  210. pbSrc += cb;
  211. pbDst = pbMax;
  212. while (cb-- != 0)
  213. *--pbDst = *--pbSrc;
  214. }
  215. /* Return a pointer to the first byte following those moved to the
  216. destination.
  217. */
  218. return (pbMax);
  219. }
  220. /**** DeleteEmptyDd - delete DD from tdd if the DD is "empty".
  221. The DD is "empty" if the date is not marked and it has
  222. no longer has any data (on disk or in memory). Note that
  223. it cannot have any alarms if there are no Qrs for it, so
  224. there is no need to check cAlarms. If it is empty, get rid of the DD.
  225. */
  226. VOID APIENTRY DeleteEmptyDd (INT itdd)
  227. {
  228. register DD *pdd;
  229. register BOOL fEmpty;
  230. pdd = TddLock () + itdd;
  231. fEmpty = !pdd -> fMarked && pdd -> dl == DLNIL && pdd -> idr == IDRNIL;
  232. TddUnlock ();
  233. if (fEmpty)
  234. ShrinkTdd (itdd, 1);
  235. }
  236. /**** TddLock */
  237. DD * APIENTRY TddLock ()
  238. {
  239. return ((DD *)LocalLock (vhlmTdd));
  240. }
  241. /**** TddUnlock */
  242. VOID APIENTRY TddUnlock ()
  243. {
  244. LocalUnlock (vhlmTdd);
  245. }