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.

352 lines
7.3 KiB

  1. //
  2. // Copyright (c) 1997-1999 Microsoft Corporation.
  3. //
  4. #include "stdafx.h"
  5. #define LISTDATAMAX 4
  6. #define NIL (void *)0
  7. struct vecdata {
  8. short x, y, atr;
  9. };
  10. struct VDATA {
  11. struct VDATA *next, *prev;
  12. struct vecdata vd;
  13. };
  14. struct VHEAD {
  15. struct VHEAD *next, *prev;
  16. struct VDATA *headp;
  17. int nPoints;
  18. };
  19. struct VCNTL {
  20. struct VHEAD *rootHead;
  21. struct VHEAD *currentHead;
  22. int nCont;
  23. struct VDATA *cvp;
  24. int mendp;
  25. void *memroot;
  26. void *cmem;
  27. };
  28. int VDInit(void);
  29. void VDTerm(void);
  30. void VDNew(int lsthdl);
  31. int VDClose(int lsthdl);
  32. int VDSetData(int lsthdl,struct vecdata *pnt);
  33. int VDGetData(int lsthdl,int contN,int pn,struct vecdata *pnt);
  34. int VDGetHead(int lsthdl,struct VHEAD * *vhd);
  35. static void *getmem(int lsthdl,int siz);
  36. int VDGetNCont(int lstHdl);
  37. int VDReverseList(int lstHdl);
  38. int VDCopy(int srcH,int dstH);
  39. struct VCNTL VCntlTbl[LISTDATAMAX];
  40. #define ALLOCMEMUNIT 2048
  41. static int init=0;
  42. /***********************************************************************
  43. * initialize data
  44. */
  45. /* */ int
  46. /* */ VDInit()
  47. /*
  48. * returns : 0, -1( out of memory)
  49. ***********************************************************************/
  50. {
  51. int lsthdl;
  52. void *mem;
  53. if ( init)
  54. return( 0);
  55. for( lsthdl = 0; lsthdl < LISTDATAMAX; lsthdl++) {
  56. /* Allocate First memory */
  57. mem = (void *)malloc( ALLOCMEMUNIT);
  58. if ( mem==NIL)
  59. return( -1);
  60. *((void **)mem) = NIL;
  61. VCntlTbl[lsthdl].memroot = mem;
  62. VDNew(lsthdl);
  63. }
  64. init = 1;
  65. return( 0);
  66. }
  67. /***********************************************************************
  68. * Terminate
  69. */
  70. /* */ void
  71. /* */ VDTerm()
  72. /*
  73. * returns : none
  74. ***********************************************************************/
  75. {
  76. void *mem, *nextmem;
  77. int lsthdl;
  78. if ( init) {
  79. for( lsthdl = 0; lsthdl < LISTDATAMAX; lsthdl++) {
  80. mem = VCntlTbl[lsthdl].memroot;
  81. do {
  82. nextmem = *((void * *)mem);
  83. free( mem);
  84. mem = nextmem;
  85. } while ( mem!=NIL);
  86. }
  87. init = 0;
  88. }
  89. return;
  90. }
  91. /***********************************************************************
  92. * New Data
  93. */
  94. /* */ void
  95. /* */ VDNew(int lsthdl)
  96. /*
  97. * returns : none
  98. ***********************************************************************/
  99. {
  100. struct VCNTL *vc;
  101. vc = VCntlTbl+lsthdl;
  102. vc->cmem = vc->memroot;
  103. vc->mendp = sizeof( void *);
  104. vc->currentHead = vc->rootHead = (struct VHEAD *)((char *)(vc->cmem)+vc->mendp);
  105. vc->mendp += sizeof( struct VHEAD);
  106. vc->currentHead->prev = (struct VHEAD *)NIL;
  107. vc->currentHead->next = (struct VHEAD *)NIL;
  108. vc->currentHead->headp = (struct VDATA *)NIL;
  109. vc->currentHead->nPoints = 0;
  110. vc->cvp = (struct VDATA *)NIL;
  111. vc->nCont = 0;
  112. }
  113. /***********************************************************************
  114. * Close Contour
  115. */
  116. /* */ int
  117. /* */ VDClose(int lsthdl)
  118. /*
  119. * returns : none
  120. ***********************************************************************/
  121. {
  122. struct VHEAD *vh;
  123. struct VCNTL *vc;
  124. vc = VCntlTbl+lsthdl;
  125. vc->cvp->next = vc->currentHead->headp;
  126. vc->currentHead->headp->prev = vc->cvp;
  127. vh = (struct VHEAD *)getmem( lsthdl, sizeof(struct VHEAD));
  128. if ( vh == NIL) return( -1);
  129. vc->currentHead->next = vh;
  130. vh->prev = vc->currentHead;
  131. vh->next = (struct VHEAD *)NIL;
  132. vh->headp = (struct VDATA *)NIL;
  133. vh->nPoints = 0;
  134. vc->currentHead = vh;
  135. vc->cvp = (struct VDATA *)NIL;
  136. vc->nCont++;
  137. return (0);
  138. }
  139. /***********************************************************************
  140. * Set Data
  141. */
  142. /* */ int
  143. /* */ VDSetData (
  144. /* */ int lsthdl,
  145. /* */ struct vecdata *pnt)
  146. /*
  147. * return : 0, -1 ( no memory)
  148. ***********************************************************************/
  149. {
  150. void *mem;
  151. struct VCNTL *vc;
  152. vc = VCntlTbl+lsthdl;
  153. mem = getmem( lsthdl,sizeof( struct VDATA));
  154. if ( mem == NIL) {
  155. return -1;
  156. }
  157. if ( vc->cvp== NIL) { /* Contour First Point*/
  158. vc->cvp = vc->currentHead->headp = (struct VDATA *)mem;
  159. vc->cvp->vd = *pnt;
  160. vc->cvp->next = vc->cvp->prev= (struct VDATA *)NIL;
  161. }
  162. else {
  163. vc->cvp->next = (struct VDATA *)mem;
  164. vc->cvp->next->prev = vc->cvp;
  165. vc->cvp = vc->cvp->next;
  166. vc->cvp->vd = *pnt;
  167. vc->cvp->next =(struct VDATA *) NIL;
  168. }
  169. vc->currentHead->nPoints++;
  170. return 0;
  171. }
  172. /***********************************************************************
  173. * Get Data
  174. */
  175. /* */ int
  176. /* */ VDGetData(
  177. /* */ int lsthdl,
  178. /* */ int contN,
  179. /* */ int pn,
  180. /* */ struct vecdata *pnt)
  181. /*
  182. * returns : 0, -1 ( Illeagal Coontour Number)
  183. ***********************************************************************/
  184. {
  185. struct VHEAD *vhd;
  186. struct VDATA *cvd;
  187. if ( lsthdl <0 ||lsthdl >= LISTDATAMAX)
  188. return -1;
  189. if ((vhd = VCntlTbl[lsthdl].rootHead)==NIL)
  190. return -1;
  191. while ( contN-->0)
  192. vhd = vhd->next;
  193. cvd = vhd->headp;
  194. while ( pn-->0)
  195. cvd = cvd->next;
  196. *pnt = cvd->vd;
  197. return 0;
  198. }
  199. /***********************************************************************
  200. * Get Data Head
  201. */
  202. /* */ int
  203. /* */ VDGetHead(
  204. /* */ int lsthdl,
  205. /* */ struct VHEAD **vhd)
  206. /*
  207. * returns : 0, -1
  208. ***********************************************************************/
  209. {
  210. if ( lsthdl >= 0 && lsthdl < LISTDATAMAX) {
  211. *vhd = VCntlTbl[lsthdl].rootHead;
  212. return( 0);
  213. }
  214. else
  215. return( -1);
  216. }
  217. /***********************************************************************
  218. * Get Memory
  219. */
  220. /* */ static void *
  221. /* */ getmem( int lsthdl, int siz)
  222. /*
  223. * returns : 0, -1
  224. ***********************************************************************/
  225. {
  226. void *mem;
  227. struct VCNTL *vc;
  228. vc = VCntlTbl+lsthdl;
  229. if ( vc->mendp + siz >= ALLOCMEMUNIT) {
  230. mem = *((void **)vc->cmem);
  231. if ( mem == NIL ) {
  232. mem = (void *)malloc(ALLOCMEMUNIT);
  233. if ( mem == NIL)
  234. return( NIL);
  235. *((void * *)mem) = NIL;
  236. *((void * *)vc->cmem) = mem; /* */
  237. vc->cmem = mem;
  238. }
  239. else
  240. vc->cmem = mem;
  241. vc->mendp = sizeof(void *);
  242. }
  243. mem = (void *)((char *)(vc->cmem) + vc->mendp);
  244. vc->mendp += siz;
  245. return(mem );
  246. }
  247. /***********************************************************************
  248. * Get Number of COntours
  249. */
  250. /* */ int
  251. /* */ VDGetNCont( int lstHdl)
  252. /*
  253. * returns : Number of Contour
  254. ***********************************************************************/
  255. {
  256. if ( lstHdl >= 0 && lstHdl < LISTDATAMAX)
  257. return VCntlTbl[lstHdl].nCont;
  258. else
  259. return( -1);
  260. }
  261. /***********************************************************************
  262. * Reverse List
  263. */
  264. /* */ int
  265. /* */ VDReverseList( int lstHdl)
  266. /*
  267. * returns : 0, -1 ( handle No )
  268. ***********************************************************************/
  269. {
  270. int cont;
  271. int np;
  272. struct VHEAD *vh;
  273. struct VDATA *vp, *nvp;
  274. if ( lstHdl < 0 || lstHdl >= LISTDATAMAX)
  275. return -1;
  276. vh = VCntlTbl[lstHdl].rootHead;
  277. for ( cont = 0; cont < VCntlTbl[lstHdl].nCont; cont++ ) {
  278. vp = vh ->headp;
  279. np = vh->nPoints;
  280. while ( np-->0) {
  281. nvp = vp->next;
  282. vp->next = vp->prev;
  283. vp->prev = nvp;
  284. vp = nvp;
  285. }
  286. vh = vh->next;
  287. }
  288. return 0;
  289. }
  290. /***********************************************************************
  291. * Copy Data
  292. */
  293. /* */ int
  294. /* */ VDCopy( int srcH, int dstH)
  295. /*
  296. * returns : 0, -1(Invalid Handle)
  297. ***********************************************************************/
  298. {
  299. int cont;
  300. int np;
  301. struct VHEAD *vh;
  302. struct VDATA *vp;
  303. if ( srcH < 0 || srcH >= LISTDATAMAX
  304. || dstH < 0 || dstH >= LISTDATAMAX)
  305. return -1;
  306. VDNew( dstH);
  307. vh = VCntlTbl[srcH].rootHead;
  308. for ( cont = 0; cont < VCntlTbl[srcH].nCont; cont++ ) {
  309. vp = vh ->headp;
  310. np = vh->nPoints;
  311. while ( np-->0) {
  312. if ( VDSetData( dstH, &vp->vd))
  313. return -1;
  314. vp = vp->next;
  315. }
  316. vh = vh->next;
  317. VDClose( dstH);
  318. }
  319. return 0;
  320. }
  321. /* EOF */