wmlutils.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these librararies and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * @OSF_COPYRIGHT@
  25. * COPYRIGHT NOTICE
  26. * Copyright (c) 1990, 1991, 1992, 1993 Open Software Foundation, Inc.
  27. * ALL RIGHTS RESERVED (MOTIF). See the file named COPYRIGHT.MOTIF for
  28. * the full copyright text.
  29. */
  30. /*
  31. * HISTORY
  32. */
  33. #ifdef REV_INFO
  34. #ifndef lint
  35. static char rcsid[] = "$XConsortium: wmlutils.c /main/8 1995/08/29 11:11:24 drk $"
  36. #endif
  37. #endif
  38. /*
  39. * (c) Copyright 1989, 1990, DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS. */
  40. /*
  41. * This file contains utilities used by WML.
  42. */
  43. #include "wml.h"
  44. #if defined(__STDC__)
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #endif
  48. #include <stdio.h>
  49. /*
  50. * Utility to allocate dynamic space for a string, and return the
  51. * dynamic copy. Produces a NULL on null input.
  52. */
  53. char *wmlAllocateString (stg)
  54. char *stg;
  55. {
  56. char *dynstg; /* the dynamic copy */
  57. if ( stg == NULL ) return NULL;
  58. dynstg = (char *) malloc (strlen(stg)+1);
  59. strcpy (dynstg, stg);
  60. return dynstg;
  61. }
  62. /*
  63. * Utility to convert a string to upper case. The conversion happens in
  64. * place, destroying the original string.
  65. */
  66. void wmlUpperCaseString (stg)
  67. char *stg;
  68. {
  69. int ndx; /* loop index */
  70. if ( stg == NULL ) return;
  71. for ( ndx=0 ; ndx<strlen(stg) ; ndx++ )
  72. stg[ndx] = _upper (stg[ndx]);
  73. }
  74. /*
  75. * Routines for accessing and manipulating dynamic handle lists.
  76. */
  77. /*
  78. * Initialize a dynamic handle list. Allocate a vector of the given
  79. * size, and set the count and number used (0).
  80. *
  81. * listptr the list to be inited
  82. * size # entries in handle vector
  83. * is_ordered TRUE is list is to be ordered
  84. */
  85. void wmlInitHList (listptr, size, is_ordered)
  86. DynamicHandleListDefPtr listptr;
  87. int size;
  88. int is_ordered;
  89. {
  90. listptr->cnt = 0;
  91. listptr->max = size;
  92. listptr->ordered = is_ordered;
  93. listptr->hvec = (ObjectHandleDefPtr) malloc(size*sizeof(ObjectHandleDef));
  94. return;
  95. }
  96. /*
  97. * Routine to resize a dynamic handle list. Increases the size if required,
  98. * but does nothing if the list is already big enough.
  99. *
  100. * listptr the dynamic list
  101. * new_size new list size
  102. */
  103. void wmlResizeHList (listptr, new_size)
  104. DynamicHandleListDefPtr listptr;
  105. int new_size;
  106. {
  107. ObjectHandleDefPtr new_vec; /* reallocated vector */
  108. if ( listptr->max >= new_size ) return;
  109. listptr->max = new_size;
  110. new_vec = (ObjectHandleDefPtr) realloc
  111. (listptr->hvec, new_size*sizeof(ObjectHandleDef));
  112. listptr->hvec = new_vec;
  113. return;
  114. }
  115. /*
  116. * Routine to clear a dynamic handle list. It leaves the handle vector intact,
  117. * but frees all the allocated names. The count is reset to 0.
  118. * but does nothing if the list is already big enough.
  119. *
  120. * listptr the dynamic list
  121. */
  122. void wmlClearHList (listptr)
  123. DynamicHandleListDefPtr listptr;
  124. {
  125. int ndx; /* current index in list */
  126. for ( ndx=0 ; ndx<listptr->cnt ; ndx++ )
  127. {
  128. free (listptr->hvec[ndx].objname);
  129. listptr->hvec[ndx].objname = NULL;
  130. }
  131. listptr->cnt = 0;
  132. return;
  133. }
  134. /*
  135. * Function to find a name in a dynamic list. This will function on both
  136. * ordered and unordered lists.
  137. *
  138. * listptr the dynamic list
  139. * name the name to look up in the list
  140. *
  141. * returns:
  142. * >= 0 name found, index in list
  143. * < 0 name not found
  144. */
  145. int wmlFindInHList (listptr, name)
  146. DynamicHandleListDefPtr listptr;
  147. char *name;
  148. {
  149. int ndx; /* current index in list */
  150. int londx; /* low index */
  151. int hindx; /* high index */
  152. int midndx; /* midpoint index */
  153. int cmpres; /* strcmp result */
  154. /*
  155. * Binary search if ordered, brute force otherwise
  156. */
  157. if ( listptr->ordered )
  158. {
  159. for ( londx=0,hindx=listptr->cnt-1 ; hindx>=londx ; )
  160. {
  161. midndx = (londx+hindx) / 2;
  162. cmpres = strcmp (name, listptr->hvec[midndx].objname);
  163. if ( cmpres < 0 )
  164. hindx = midndx - 1;
  165. if ( cmpres > 0 )
  166. londx = midndx + 1;
  167. if ( cmpres == 0 )
  168. return midndx;
  169. }
  170. return -1;
  171. }
  172. else
  173. {
  174. for ( ndx=0 ; ndx<listptr->cnt ; ndx++ )
  175. if ( strcmp(name,listptr->hvec[ndx].objname) == 0 )
  176. return ndx;
  177. return -1;
  178. }
  179. }
  180. /*
  181. * Routine to insert an entry into a list. The insertion is ordered or
  182. * unordered depending on the way the list is marked. Unordered lists
  183. * insert at the end. This routine assumes no duplicates will be entered
  184. * in the list.
  185. *
  186. * listptr the list
  187. * name the name under which to insert
  188. * obj the object to insert
  189. */
  190. void wmlInsertInHList (listptr, name, obj)
  191. DynamicHandleListDefPtr listptr;
  192. char *name;
  193. ObjectPtr obj;
  194. {
  195. int ndx; /* current index in list */
  196. int londx; /* low index */
  197. int hindx; /* high index */
  198. int midndx; /* midpoint index */
  199. int newndx; /* new entry index */
  200. int cmpres; /* strcmp result */
  201. /*
  202. * Guarantee enough space in the list
  203. */
  204. wmlResizeHList (listptr, listptr->cnt+1);
  205. /*
  206. * Binary search and insert if ordered, brute force otherwise
  207. */
  208. if ( listptr->ordered )
  209. {
  210. if ( listptr->cnt == 0 )
  211. {
  212. listptr->hvec[0].objname = wmlAllocateString (name);
  213. listptr->hvec[0].objptr = obj;
  214. listptr->cnt += 1;
  215. return;
  216. }
  217. for ( londx=0,hindx=listptr->cnt-1 ; hindx>=londx ; )
  218. {
  219. midndx = (londx+hindx) / 2;
  220. cmpres = strcmp (name, listptr->hvec[midndx].objname);
  221. if ( cmpres == 0 )
  222. {
  223. printf ("\nwmlInsertInHList: duplicate name '%s'found\n", name);
  224. return;
  225. }
  226. if ( londx == hindx ) break;
  227. if ( cmpres < 0 )
  228. hindx = midndx - 1;
  229. if ( cmpres > 0 )
  230. londx = midndx + 1;
  231. }
  232. /*
  233. * The new entry will go either at midndx or after midndx. Move down
  234. * the vector appropriately.
  235. */
  236. if ( cmpres < 0 )
  237. newndx = midndx;
  238. else
  239. newndx = midndx + 1;
  240. for ( ndx=listptr->cnt-1 ; ndx>=newndx ; ndx-- )
  241. {
  242. listptr->hvec[ndx+1].objname = listptr->hvec[ndx].objname;
  243. listptr->hvec[ndx+1].objptr = listptr->hvec[ndx].objptr;
  244. }
  245. listptr->hvec[newndx].objname = wmlAllocateString (name);
  246. listptr->hvec[newndx].objptr = obj;
  247. listptr->cnt += 1;
  248. return;
  249. }
  250. else
  251. {
  252. listptr->hvec[listptr->cnt].objname = wmlAllocateString (name);
  253. listptr->hvec[listptr->cnt].objptr = obj;
  254. listptr->cnt += 1;
  255. return;
  256. }
  257. }
  258. /*
  259. * Routine to insert an entry into a token list. The insertion is ordered.
  260. * This routine allows duplicates
  261. *
  262. * listptr the list
  263. * name the name under which to insert
  264. * obj the object to insert
  265. */
  266. void wmlInsertInKeyList (listptr, name, obj)
  267. DynamicHandleListDefPtr listptr;
  268. char *name;
  269. ObjectPtr obj;
  270. {
  271. int ndx; /* current index in list */
  272. int londx; /* low index */
  273. int hindx; /* high index */
  274. int midndx; /* midpoint index */
  275. int newndx; /* new entry index */
  276. int cmpres; /* strcmp result */
  277. /*
  278. * Guarantee enough space in the list
  279. */
  280. wmlResizeHList (listptr, listptr->cnt+1);
  281. /*
  282. * Binary search and insert
  283. */
  284. if ( listptr->cnt == 0 )
  285. {
  286. listptr->hvec[0].objname = wmlAllocateString (name);
  287. listptr->hvec[0].objptr = obj;
  288. listptr->cnt += 1;
  289. return;
  290. }
  291. for ( londx=0,hindx=listptr->cnt-1 ; hindx>=londx ; )
  292. {
  293. midndx = (londx+hindx) / 2;
  294. cmpres = strcmp (name, listptr->hvec[midndx].objname);
  295. if ( londx == hindx ) break;
  296. if ( cmpres < 0 )
  297. hindx = midndx - 1;
  298. if ( cmpres >= 0 )
  299. londx = midndx + 1;
  300. }
  301. /*
  302. * The new entry will go either at midndx or after midndx. Move down
  303. * the vector appropriately.
  304. */
  305. if ( cmpres < 0 )
  306. newndx = midndx;
  307. else
  308. newndx = midndx + 1;
  309. for ( ndx=listptr->cnt-1 ; ndx>=newndx ; ndx-- )
  310. {
  311. listptr->hvec[ndx+1].objname = listptr->hvec[ndx].objname;
  312. listptr->hvec[ndx+1].objptr = listptr->hvec[ndx].objptr;
  313. }
  314. listptr->hvec[newndx].objname = wmlAllocateString (name);
  315. listptr->hvec[newndx].objptr = obj;
  316. listptr->cnt += 1;
  317. return;
  318. }
  319. /*
  320. * Indicate if a resource is in a resource reference list by returning its
  321. * reference pointer.
  322. */
  323. WmlClassResDefPtr wmlResolveResIsMember (resobj, resref)
  324. WmlResourceDefPtr resobj;
  325. WmlClassResDefPtr resref;
  326. {
  327. while ( resref != NULL )
  328. {
  329. if ( resref->act_resource == resobj ) return resref;
  330. resref = resref->next;
  331. }
  332. return NULL;
  333. }
  334. /*
  335. * Indicate if a child is in a child reference list by returning its
  336. * reference pointer.
  337. */
  338. WmlClassChildDefPtr wmlResolveChildIsMember (childobj, childref)
  339. WmlChildDefPtr childobj;
  340. WmlClassChildDefPtr childref;
  341. {
  342. while ( childref != NULL )
  343. {
  344. if ( childref->act_child == childobj ) return childref;
  345. childref = childref->next;
  346. }
  347. return NULL;
  348. }