plist.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * MODULE: plist.c
  14. *
  15. * DESCRIPTION:
  16. *
  17. * This module implements property lists. A property list is an
  18. * ordered array of property values. Each property value has an
  19. * handle for some data item, and may have a reference to
  20. * another property list which describes the type of the data
  21. * item. Each property value has a property index which specifies
  22. * its position in the property list. A property value may also
  23. * have a name. Since the data item associated with a property
  24. * value may reference another property list, it is possible to
  25. * construct arbitrary linked structures of property lists.
  26. *
  27. * IMPLEMENTATION NOTES:
  28. */
  29. #include "netsite.h"
  30. #include "base/plist.h"
  31. #include "plist_pvt.h"
  32. int plistHashSizes[] = PLSTSIZES;
  33. /*
  34. * FUNCTION: PListAssignValue
  35. *
  36. * DESCRIPTION:
  37. *
  38. * This function sets the value and/or type of a defined property
  39. * in given property list. If the property type is specified as
  40. * NULL, it is unchanged. However, the property value is always
  41. * set to the specified value.
  42. *
  43. * ARGUMENTS:
  44. *
  45. * plist - handle for the property list
  46. * pname - the property name
  47. * pvalue - the new property value
  48. * ptype - the new property type, or NULL
  49. *
  50. * RETURNS:
  51. *
  52. * If successful, the property index of the referenced property is
  53. * returned as the function value. Errors are indicated by a
  54. * negative return code as defined in plist.h.
  55. */
  56. NSAPI_PUBLIC int
  57. PListAssignValue(PList_t plist, const char *pname,
  58. const void *pvalue, PList_t ptype)
  59. {
  60. PListStruct_t *pl = (PListStruct_t *)plist;
  61. PLValueStruct_t *pv;
  62. int pindex;
  63. int i;
  64. if (!plist) return ERRPLUNDEF;
  65. /* Got a symbol table for this property list? */
  66. if (pl->pl_symtab) {
  67. /* Yes, compute hash of specified name */
  68. i = PListHashName(pl->pl_symtab, pname);
  69. /* Search hash collision list for matching name */
  70. for (pv = pl->pl_symtab->pt_hash[i]; pv; pv = pv->pv_next) {
  71. if (!strcmp(pname, pv->pv_name)) {
  72. /* Name match, get property index */
  73. pindex = pv->pv_pi;
  74. /* Set the new value */
  75. pv->pv_value = (char *)pvalue;
  76. /* Set type if type is given */
  77. if (ptype) pv->pv_type = (PListStruct_t *)ptype;
  78. /* Return the property index */
  79. return pindex;
  80. }
  81. }
  82. }
  83. /* Error - specified property name is undefined */
  84. return ERRPLUNDEF;
  85. }
  86. /*
  87. * FUNCTION: PListCreate
  88. *
  89. * DESCRIPTION:
  90. *
  91. * This function creates a new property list and returns a handle for
  92. * it. It allows the caller to reserve a specified number of
  93. * property indices at the beginning of the list, and also to limit
  94. * the total number of property values that may be added to the list.
  95. *
  96. * ARGUMENTS:
  97. *
  98. * mempool - handle for a memory pool to be associated
  99. * with the new property list
  100. * resvprop - number of reserved property indices
  101. * maxprop - maximum number of properties in list
  102. * (zero or negative imposes no limit)
  103. * flags - unused, reserved, must be zero
  104. *
  105. * RETURNS:
  106. *
  107. * If successful, the function return value is a handle for the new
  108. * property list. Otherwise NULL is returned.
  109. */
  110. NSAPI_PUBLIC PList_t
  111. PListCreate(pool_handle_t *mempool, int resvprop, int maxprop, int flags)
  112. {
  113. PListStruct_t *plist; /* pointer to property list structure */
  114. int i;
  115. plist = (PListStruct_t *)pool_malloc(mempool, sizeof(PListStruct_t));
  116. if (plist) {
  117. /* Negative maxprop is the same as zero, i.e. no limit */
  118. if (maxprop < 0) maxprop = 0;
  119. /* If resvprop and maxprop are both specified, limit resvprop */
  120. if (resvprop > 0) {
  121. if (maxprop && (resvprop > maxprop)) resvprop = maxprop;
  122. }
  123. else resvprop = 0;
  124. /* Initialize property list structure */
  125. plist->pl_mempool = mempool;
  126. plist->pl_symtab = NULL;
  127. plist->pl_maxprop = maxprop;
  128. plist->pl_resvpi = resvprop;
  129. plist->pl_initpi = resvprop;
  130. plist->pl_lastpi = resvprop;
  131. /* Set initialize size for array of property value pointers */
  132. plist->pl_cursize = (resvprop) ? resvprop : PLIST_DEFSIZE;
  133. /* Allocate the initial array of property value pointers */
  134. plist->pl_ppval = (pb_entry **)pool_malloc(mempool,
  135. (plist->pl_cursize *
  136. sizeof(PLValueStruct_t *)));
  137. if (!plist->pl_ppval) {
  138. /* Failed - insufficient memory */
  139. pool_free(mempool, (void *)plist);
  140. plist = NULL;
  141. }
  142. else {
  143. /* NULL out pointers in the reserved index range, if any */
  144. for (i = 0; i < plist->pl_lastpi; ++i) {
  145. plist->pl_ppval[i] = 0;
  146. }
  147. }
  148. }
  149. return (PList_t)plist;
  150. }
  151. /*
  152. * FUNCTION: PListDefProp
  153. *
  154. * DESCRIPTION:
  155. *
  156. * This function creates a new property in a specified property list.
  157. * The 'pindex' argument may be used to request a particular property
  158. * index for the new property. If 'pindex' is greater than zero,
  159. * the specified value is used as the new property's index, provided
  160. * there is no property at that index already. If 'pindex' is zero,
  161. * then the next available property index is assigned to the new
  162. * property. A name may optionally be specified for the new property.
  163. *
  164. * ARGUMENTS:
  165. *
  166. * plist - handle for the property list
  167. * pindex - new property index (or zero)
  168. * pname - new property name (or NULL)
  169. *
  170. * RETURNS:
  171. *
  172. * If successful, the index of the new property is returned as the
  173. * function value. Errors are indicated by a negative return code
  174. * as defined in plist.h.
  175. */
  176. NSAPI_PUBLIC int
  177. PListDefProp(PList_t plist, int pindex, const char *pname, const int flags)
  178. {
  179. PListStruct_t *pl = (PListStruct_t *)plist;
  180. PLValueStruct_t **ppval;
  181. PLValueStruct_t *pv;
  182. int cursize;
  183. int i;
  184. int wrapped;
  185. if (!plist) return ERRPLUNDEF;
  186. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  187. /* Is pindex specified? */
  188. if (pindex > 0) {
  189. /* Yes, is it in the reserved range? */
  190. if (flags != PLFLG_IGN_RES && pindex > pl->pl_resvpi) {
  191. /* No, error */
  192. return ERRPLINVPI;
  193. }
  194. i = pindex - 1;
  195. if (ppval[i]) {
  196. /* Error - property already exists at specified index */
  197. return ERRPLEXIST;
  198. }
  199. }
  200. else {
  201. /*
  202. * Look for a free property index, starting at pl_lastpi + 1.
  203. * (Note that i is the property index - 1)
  204. */
  205. for (wrapped = 0, i = pl->pl_lastpi; ;) {
  206. /* Are we in an initialized part of the array? */
  207. if (i < pl->pl_initpi) {
  208. /* Yes, use this index if it's free */
  209. if (ppval[i] == 0) break;
  210. /* Otherwise step to the next one */
  211. ++i;
  212. }
  213. else {
  214. /* Have we reached the end yet? */
  215. if (i < pl->pl_cursize) {
  216. /*
  217. * We are above the highest initialized index, but
  218. * still within the allocated size. An index in
  219. * this range can be used with no further checks.
  220. */
  221. ppval[i] = 0;
  222. }
  223. else {
  224. /*
  225. * It's looking like time to grow the array, but
  226. * first go back and look for an unused, unreserved
  227. * index that might have been freed.
  228. */
  229. if (!wrapped) {
  230. i = pl->pl_resvpi;
  231. wrapped = 1;
  232. continue;
  233. }
  234. /*
  235. * Grow the array unless there is a specified maximum
  236. * size and we've reached it.
  237. */
  238. i = pl->pl_cursize;
  239. if (pl->pl_maxprop && (i > pl->pl_maxprop)) {
  240. /* Error - property list is full */
  241. return ERRPLFULL;
  242. }
  243. /* Increase planned size of list */
  244. cursize = i + PLIST_DEFGROW;
  245. /* Reallocate the array of property value pointers */
  246. ppval = (PLValueStruct_t **)pool_realloc(pl->pl_mempool,
  247. (void *)ppval,
  248. (cursize * sizeof(PLValueStruct_t *)));
  249. if (!ppval) {
  250. /* Error - insufficient memory */
  251. return ERRPLNOMEM;
  252. }
  253. /* Initialize the first new entry and select it */
  254. ppval[i] = NULL;
  255. pl->pl_ppval = (pb_entry **)ppval;
  256. pl->pl_cursize = cursize;
  257. }
  258. /* Update the highest initialized index value */
  259. pl->pl_initpi = i + 1;
  260. break;
  261. }
  262. }
  263. /* Set the starting point for the next allocation */
  264. pl->pl_lastpi = i + 1;
  265. }
  266. /* We have a property index (i + 1). Create a new property value */
  267. pv = (PLValueStruct_t *)pool_calloc(pl->pl_mempool,
  268. 1, sizeof(PLValueStruct_t));
  269. if (!pv) {
  270. /* Error - insufficient memory */
  271. return ERRPLNOMEM;
  272. }
  273. pv->pv_pbentry.param = &pv->pv_pbparam;
  274. pv->pv_pi = i + 1;
  275. ppval[i] = pv;
  276. /* Name the property if the name was specified */
  277. if (pname) {
  278. /* XXX Maybe should delete property if naming fails */
  279. return PListNameProp(plist, i + 1, pname);
  280. }
  281. /* Return the property index of the new property */
  282. return i + 1;
  283. }
  284. /*
  285. * FUNCTION: PListDeleteProp
  286. *
  287. * DESCRIPTION:
  288. *
  289. * This function deletes a property from a specified property list.
  290. * The property can be specified by its property index, using a
  291. * pindex value greater than zero, or by its name, by specifying
  292. * pindex as zero and pname as the property name. This does not
  293. * have any effect on the data referenced by the property value,
  294. * if any, nor does it have any effect on the property list that
  295. * describes the property value's type, if any.
  296. *
  297. * ARGUMENTS:
  298. *
  299. * plist - handle for the property list
  300. * pindex - the property index, or zero
  301. * pname - the property name, or NULL
  302. */
  303. NSAPI_PUBLIC const void *
  304. PListDeleteProp(PList_t plist, int pindex, const char *pname_in)
  305. {
  306. PListStruct_t *pl = (PListStruct_t *)plist;
  307. PLValueStruct_t **ppval;
  308. PLValueStruct_t **pvp;
  309. PLValueStruct_t *pv = NULL;
  310. int i;
  311. const void *pvalue = NULL;
  312. char *pname = (char *)pname_in;
  313. if (!plist) return NULL;
  314. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  315. /* Check for valid property index */
  316. if ((pindex > 0) && (pindex <= pl->pl_initpi)) {
  317. /* Get the pointer to the property structure */
  318. pv = ppval[pindex - 1];
  319. pname = 0;
  320. if (pv) {
  321. pname = pv->pv_name;
  322. }
  323. }
  324. if (pname && pl->pl_symtab) {
  325. /* Compute hash of specified property name */
  326. i = PListHashName(pl->pl_symtab, pname);
  327. /* Search hash collision list for matching name */
  328. for (pvp = &pl->pl_symtab->pt_hash[i]; *pvp; pvp = &(*pvp)->pv_next) {
  329. pv = *pvp;
  330. if (!strcmp(pname, pv->pv_name)) {
  331. /* Found it. Get its index and remove it. */
  332. pindex = pv->pv_pi;
  333. *pvp = pv->pv_next;
  334. break;
  335. }
  336. }
  337. }
  338. /* Found the indicated property by index or name? */
  339. if (pv) {
  340. /* Yes, remove it from the property list */
  341. ppval[pindex - 1] = NULL;
  342. /* Free the property name, if any */
  343. if (pv->pv_name) {
  344. pool_free(pl->pl_mempool, (void *)(pv->pv_name));
  345. }
  346. pvalue = pv->pv_value;
  347. /* Free the property */
  348. pool_free(pl->pl_mempool, (void *)pv);
  349. }
  350. return(pvalue);
  351. }
  352. /*
  353. * FUNCTION: PListFindValue
  354. *
  355. * DESCRIPTION:
  356. *
  357. * This function retrieves the value and type of a property with a
  358. * specified property name. If the pvalue argument is non-NULL,
  359. * it specifies a location in which to return the property value.
  360. * Similarly, if ptype is non-NULL, it specifies where the property
  361. * list describing the property type is to be returned. If a
  362. * property has no value, the value returned for pvalue is NULL.
  363. * If a property has no type, the value returned for ptype is NULL.
  364. * A property can have a value, a type, both, or neither.
  365. *
  366. * ARGUMENTS:
  367. *
  368. * plist - handle for the property list
  369. * pname - pointer to property name string
  370. * pvalue - property value return pointer
  371. * ptype - property type return pointer
  372. *
  373. * RETURNS:
  374. *
  375. * If successful, the index of the referenced property is returned
  376. * as the function value. Errors are indicated by a negative
  377. * return code as defined in plist.h.
  378. */
  379. NSAPI_PUBLIC int
  380. PListFindValue(PList_t plist, const char *pname, void **pvalue, PList_t *ptype)
  381. {
  382. PListStruct_t *pl = (PListStruct_t *)plist;
  383. PLValueStruct_t *pv;
  384. int pindex;
  385. int i;
  386. if (!plist) return ERRPLUNDEF;
  387. /* Got a symbol table for this property list? */
  388. if (pl->pl_symtab) {
  389. /* Yes, compute hash of specified name */
  390. i = PListHashName(pl->pl_symtab, pname);
  391. /* Search hash collision list for matching name */
  392. for (pv = pl->pl_symtab->pt_hash[i]; pv; pv = pv->pv_next) {
  393. if (!strcmp(pname, pv->pv_name)) {
  394. /* Name match, get property index */
  395. pindex = pv->pv_pi;
  396. /* Return the value if requested */
  397. if (pvalue) *pvalue = (void *)(pv->pv_value);
  398. /* Return the type if requested */
  399. if (ptype) *ptype = (PList_t)(pv->pv_type);
  400. /* Return the property index */
  401. return pindex;
  402. }
  403. }
  404. }
  405. /* Error - specified property name is undefined */
  406. return ERRPLUNDEF;
  407. }
  408. /*
  409. * FUNCTION: PListInitProp
  410. *
  411. * DESCRIPTION:
  412. *
  413. * This function combines the functions of PListDefProp() and
  414. * PListSetValue(), defining a new property and assigning it an
  415. * initial value and optionally a type and/or a name.
  416. *
  417. * ARGUMENTS:
  418. *
  419. * plist - handle for the property list
  420. * pindex - a reserved property index, or zero
  421. * pname - the new property name, or NULL
  422. * pvalue - the new property value
  423. * ptype - the new property type, or NULL
  424. *
  425. * RETURNS:
  426. *
  427. * If successful, the property index (pindex) is returned as the
  428. * function value. Errors are indicated by a negative return code
  429. * as defined in plist.h.
  430. */
  431. NSAPI_PUBLIC int
  432. PListInitProp(PList_t plist, int pindex, const char *pname,
  433. const void *pvalue, PList_t ptype)
  434. {
  435. int rv;
  436. if (!plist) return ERRPLUNDEF;
  437. /* Create the property */
  438. rv = PListDefProp(plist, pindex, pname, PLFLG_USE_RES);
  439. if (rv > 0) {
  440. /* If that worked, set the value and type */
  441. rv = PListSetValue(plist, rv, pvalue, ptype);
  442. }
  443. return rv;
  444. }
  445. /*
  446. * FUNCTION: PListNew
  447. *
  448. * DESCRIPTION:
  449. *
  450. * This function creates a new property list, using the specified
  451. * memory pool for allocating the internal data structures used to
  452. * represent it. If the mempool argument is NULL, the default
  453. * memory pool is used.
  454. *
  455. * ARGUMENTS:
  456. *
  457. * mempool - handle for a memory pool to be associated
  458. * with the new property list
  459. *
  460. * RETURNS:
  461. *
  462. * If successful, the function return value is a handle for the new
  463. * property list. Otherwise NULL is returned.
  464. */
  465. NSAPI_PUBLIC PList_t
  466. PListNew(pool_handle_t *mempool)
  467. {
  468. /* Just call PListCreate with default parameters */
  469. return PListCreate(mempool, 0, 0, 0);
  470. }
  471. /*
  472. * FUNCTION: PListDestroy
  473. *
  474. * DESCRIPTION:
  475. *
  476. * This function destroys a specified property list. This means
  477. * that any dynamic memory which was allocated as a result of calls
  478. * to the property list API is freed to the memory pool from which
  479. * it was allocated. Property value data is not freed, nor are
  480. * any property lists associated with property types.
  481. *
  482. * ARGUMENTS:
  483. *
  484. * plist - handle for the property list
  485. */
  486. void
  487. PListDestroy(PList_t plist)
  488. {
  489. PListStruct_t *pl = (PListStruct_t *)plist;
  490. PLValueStruct_t **ppval;
  491. PLValueStruct_t *pv;
  492. int i;
  493. if (!plist) return;
  494. /* Free the property name symbol table if any */
  495. if (pl->pl_symtab) {
  496. pool_free(pl->pl_mempool, (void *)(pl->pl_symtab));
  497. }
  498. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  499. /* Loop over the initialized property indices */
  500. for (i = 0; i < pl->pl_initpi; ++i) {
  501. /* Got a property here? */
  502. pv = ppval[i];
  503. if (pv) {
  504. /* Free the property name string if any */
  505. if (pv->pv_name) {
  506. pool_free(pl->pl_mempool, (void *)(pv->pv_name));
  507. }
  508. /* Free the property value structure */
  509. pool_free(pl->pl_mempool, (void *)pv);
  510. }
  511. }
  512. /* Free the array of pointers to property values */
  513. pool_free(pl->pl_mempool, (void *)ppval);
  514. /* Free the property list head */
  515. pool_free(pl->pl_mempool, (void *)pl);
  516. }
  517. /*
  518. * FUNCTION: PListGetValue
  519. *
  520. * DESCRIPTION:
  521. *
  522. * This function retrieves the value and type of the property with
  523. * the property index given by pindex in the specified property
  524. * list. The pindex argument must specify the index of a defined
  525. * property. If the pvalue argument is non-NULL, it specifies a
  526. * location in which to return the property value. Similarly, if
  527. * ptype is non-NULL, it specifies where the property list
  528. * describing the property type is to be returned. If a property
  529. * has no value, the value returned for pvalue is NULL. If a
  530. * property has no type, the value returned for ptype is NULL. A
  531. * property can have a value, a type, both, or neither.
  532. *
  533. * ARGUMENTS:
  534. *
  535. * plist - handle for the property list
  536. * pindex - the property index
  537. * pvalue - property value return pointer
  538. * ptype - property type return pointer
  539. *
  540. * RETURNS:
  541. *
  542. * If successful, the property index (pindex) is returned as the
  543. * function value. Errors are indicated by a negative return code
  544. * as defined in plist.h.
  545. */
  546. NSAPI_PUBLIC int
  547. PListGetValue(PList_t plist, int pindex, void **pvalue, PList_t *ptype)
  548. {
  549. PListStruct_t *pl = (PListStruct_t *)plist;
  550. PLValueStruct_t **ppval;
  551. PLValueStruct_t *pv;
  552. if (!plist) return ERRPLUNDEF;
  553. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  554. /* Check for valid property index */
  555. if ((pindex > 0) && (pindex <= pl->pl_initpi)) {
  556. /* Does the property exist? */
  557. pv = ppval[pindex - 1];
  558. if (pv) {
  559. /* Yes, return the value if requested */
  560. if (pvalue) *pvalue = (void *)(pv->pv_value);
  561. /* Return the type if requested */
  562. if (ptype) *ptype = (PList_t)(pv->pv_type);
  563. /* Successful return */
  564. return pindex;
  565. }
  566. }
  567. /* Error - invalid property index or non-existent property */
  568. return ERRPLINVPI;
  569. }
  570. /*
  571. * FUNCTION: PListHashName
  572. *
  573. * DESCRIPTION:
  574. *
  575. * This function hashes a given property name for a specified
  576. * symbol table. It produces a value that can be used as an
  577. * index in the pt_hash array associated with the symbol table.
  578. *
  579. * ARGUMENTS:
  580. *
  581. * symtab - pointer to the symbol table
  582. * pname - pointer to the property name string
  583. *
  584. * RETURNS:
  585. *
  586. * The hash index is returned as the function value.
  587. */
  588. int
  589. PListHashName(PLSymbolTable_t *symtab, const char *pname)
  590. {
  591. unsigned int hashval = 0; /* hash value */
  592. while (*pname) {
  593. hashval = (hashval<<5) ^ (*pname++ & 0x7f);
  594. }
  595. return hashval % PLSIZENDX(symtab->pt_sizendx);
  596. }
  597. /*
  598. * FUNCTION: PListNameProp
  599. *
  600. * DESCRIPTION:
  601. *
  602. * This function assigns a name to a defined property with the
  603. * property index, pindex. If the property has an existing name,
  604. * it will be replaced with the name specified by pname.
  605. *
  606. * ARGUMENTS:
  607. *
  608. * plist - handle for the property list
  609. * pindex - the property index
  610. * pname - the new property name
  611. *
  612. * RETURNS:
  613. *
  614. * If successful, the property index (pindex) is returned as the
  615. * function value. Errors are indicated by a negative return code
  616. * as defined in plist.h.
  617. */
  618. NSAPI_PUBLIC int
  619. PListNameProp(PList_t plist, int pindex, const char *pname)
  620. {
  621. PListStruct_t *pl = (PListStruct_t *)plist;
  622. PLValueStruct_t *pv;
  623. PLSymbolTable_t *pt;
  624. int i;
  625. if (!plist) return ERRPLUNDEF;
  626. pt = pl->pl_symtab;
  627. /* Check for valid property index */
  628. if ((pindex > 0) && (pindex <= pl->pl_initpi)) {
  629. /* Does the property exist? */
  630. pv = ((PLValueStruct_t **)(pl->pl_ppval))[pindex - 1];
  631. if (pv) {
  632. /* If it has a name already, unname it */
  633. if (pv->pv_name) {
  634. PLValueStruct_t **pvp;
  635. /* Get hash bucket index */
  636. i = PListHashName(pt, pv->pv_name);
  637. /* Seach hash collision list for this property */
  638. for (pvp = &pt->pt_hash[i];
  639. *pvp; pvp = &(*pvp)->pv_next) {
  640. if (*pvp == pv) {
  641. /* Remove it from the list */
  642. *pvp = pv->pv_next;
  643. break;
  644. }
  645. }
  646. /* Free the current name string */
  647. pool_free(pl->pl_mempool, (void *)(pv->pv_name));
  648. }
  649. /* Got a new name? */
  650. if (pname) {
  651. /* Yes, is there a hash table? */
  652. if (!pt) {
  653. /* No, create one */
  654. pt = (PLSymbolTable_t *)pool_calloc(pl->pl_mempool, 1,
  655. PLHASHSIZE(0));
  656. if (!pt) {
  657. return ERRPLNOMEM;
  658. }
  659. pl->pl_symtab = pt;
  660. }
  661. else {
  662. /* Is it time to grow the hash table? */
  663. i = PLSIZENDX(pt->pt_sizendx);
  664. /* cannot allow pt->pt_sizendx == PLMAXSIZENDX */
  665. if (((size_t)(pt->pt_sizendx + 1) < PLMAXSIZENDX) &&
  666. pt->pt_nsyms >= (i + i)) {
  667. PLSymbolTable_t *npt;
  668. /* Yes, allocate the new table */
  669. npt = (PLSymbolTable_t *)pool_calloc(pl->pl_mempool, 1,
  670. PLHASHSIZE(pt->pt_sizendx+1));
  671. if (npt) {
  672. PLValueStruct_t *opv;
  673. PLValueStruct_t *npv;
  674. int j;
  675. npt->pt_sizendx = pt->pt_sizendx + 1;
  676. npt->pt_nsyms = pt->pt_nsyms;
  677. /* Rehash all the names into the new table */
  678. for (i = 0; i < PLSIZENDX(pt->pt_sizendx); ++i) {
  679. for (opv = pt->pt_hash[i]; opv; opv = npv) {
  680. npv = opv->pv_next;
  681. j = PListHashName(npt, opv->pv_name);
  682. opv->pv_next = npt->pt_hash[j];
  683. npt->pt_hash[j] = opv;
  684. }
  685. }
  686. pl->pl_symtab = npt;
  687. /* Free the old symbol table */
  688. pool_free(pl->pl_mempool, (void *)pt);
  689. pt = npt;
  690. }
  691. }
  692. }
  693. /* Duplicate the name string */
  694. pv->pv_name = pool_strdup(pl->pl_mempool, (char *)pname);
  695. /* Add name to symbol table */
  696. i = PListHashName(pt, pname);
  697. pv->pv_next = pt->pt_hash[i];
  698. pt->pt_hash[i] = pv;
  699. }
  700. /* Successful return */
  701. return pindex;
  702. }
  703. }
  704. /* Error - invalid property index or non-existent property */
  705. return ERRPLINVPI;
  706. }
  707. /*
  708. * FUNCTION: PListSetType
  709. *
  710. * DESCRIPTION:
  711. *
  712. * This function sets the property type of the defined property
  713. * with the property index, pindex. The property list describing
  714. * the property type is specified by ptype. If ptype is NULL,
  715. * the property type will be set to be undefined (NULL).
  716. *
  717. *
  718. * ARGUMENTS:
  719. *
  720. * plist - handle for the property list
  721. * pindex - the property index
  722. * ptype - the new property type, or NULL
  723. *
  724. * RETURNS:
  725. *
  726. * If successful, the property index (pindex) is returned as the
  727. * function value. Errors are indicated by a negative return code
  728. * as defined in plist.h.
  729. */
  730. NSAPI_PUBLIC int
  731. PListSetType(PList_t plist, int pindex, PList_t ptype)
  732. {
  733. PListStruct_t *pl = (PListStruct_t *)plist;
  734. PLValueStruct_t **ppval;
  735. PLValueStruct_t *pv;
  736. if (!plist) return ERRPLUNDEF;
  737. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  738. /* Check for valid property index */
  739. if ((pindex > 0) && (pindex <= pl->pl_initpi)) {
  740. /* Does the property exist? */
  741. pv = ppval[pindex - 1];
  742. if (pv) {
  743. /* Yes, set the new type */
  744. pv->pv_type = ptype;
  745. /* Successful return */
  746. return pindex;
  747. }
  748. }
  749. /* Error - invalid property index or non-existent property */
  750. return ERRPLINVPI;
  751. }
  752. /*
  753. * FUNCTION: PListSetValue
  754. *
  755. * DESCRIPTION:
  756. *
  757. * This function sets the value and optionally the type of a
  758. * defined property in a given property list. The pindex argument
  759. * specifies the property index, which must be greater than zero.
  760. * The ptype argument specifies a property list that describes the
  761. * property type. If ptype is NULL, the property type, if any, is
  762. * unchanged by this function. However, the property value is
  763. * always set to the value given by pvalue.
  764. *
  765. * ARGUMENTS:
  766. *
  767. * plist - handle for the property list
  768. * pindex - the property index
  769. * pvalue - the new property value
  770. * ptype - the new property type, or NULL
  771. *
  772. * RETURNS:
  773. *
  774. * If successful, the property index (pindex) is returned as the
  775. * function value. Errors are indicated by a negative return code
  776. * as defined in plist.h.
  777. */
  778. NSAPI_PUBLIC int
  779. PListSetValue(PList_t plist, int pindex, const void *pvalue, PList_t ptype)
  780. {
  781. PListStruct_t *pl = (PListStruct_t *)plist;
  782. PLValueStruct_t **ppval;
  783. PLValueStruct_t *pv;
  784. if (!plist) return ERRPLUNDEF;
  785. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  786. /* Check for valid property index */
  787. if ((pindex > 0) && (pindex <= pl->pl_initpi)) {
  788. /* Does the property exist? */
  789. pv = ppval[pindex - 1];
  790. if (pv) {
  791. /* Yes, set the new value */
  792. pv->pv_value = (char *)pvalue;
  793. /* Set type if type is given */
  794. if (ptype) pv->pv_type = (PListStruct_t *)ptype;
  795. /* Successful return */
  796. return pindex;
  797. }
  798. }
  799. /* Error - invalid property index or non-existent property */
  800. return ERRPLINVPI;
  801. }
  802. /*
  803. * FUNCTION: PListEnumerate
  804. *
  805. * DESCRIPTION:
  806. *
  807. * This function walks through a specified property list
  808. * calling a user supplied function with the property
  809. * name and value as parameters.
  810. *
  811. * ARGUMENTS:
  812. *
  813. * plist - handle for the property list
  814. * user_func - handle for the user function
  815. */
  816. NSAPI_PUBLIC void
  817. PListEnumerate(PList_t plist, PListFunc_t *user_func, void *user_data)
  818. {
  819. PListStruct_t *pl = (PListStruct_t *)plist;
  820. PLValueStruct_t **ppval;
  821. PLValueStruct_t *pv;
  822. int i;
  823. if (!plist) return;
  824. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  825. /* Loop over the initialized property indices */
  826. for (i = 0; i < pl->pl_initpi; ++i) {
  827. /* Got a property here? */
  828. pv = ppval[i];
  829. if (pv) {
  830. (*user_func)(pv->pv_name, pv->pv_value, user_data);
  831. }
  832. }
  833. }
  834. /*
  835. * FUNCTION: PListCreateDuplicate
  836. *
  837. * DESCRIPTION:
  838. *
  839. * This function creates a new property list and returns a handle for
  840. * it. The source plist provides the new plists parameters.
  841. *
  842. * ARGUMENTS:
  843. *
  844. * src_plist - source plist to duplicate
  845. * mempool - handle for a memory pool to be associated
  846. * with the new property list, only
  847. * used if flags is set to PLFLG_NEW_MPOOL
  848. * flags - if PLFLG_NEW_MPOOL uses new_mempool
  849. * parameter
  850. *
  851. * RETURNS:
  852. *
  853. * If successful, the function return value is a handle for the new
  854. * property list. Otherwise NULL is returned.
  855. */
  856. static PList_t
  857. PListCreateDuplicate(PList_t src_plist, pool_handle_t *new_mempool, int flags)
  858. {
  859. PListStruct_t *plist; /* pointer to property list structure */
  860. int i;
  861. pool_handle_t *mempool;
  862. mempool = (flags == PLFLG_NEW_MPOOL) ? new_mempool : src_plist->pl_mempool;
  863. plist = (PListStruct_t *)pool_malloc(mempool, sizeof(PListStruct_t));
  864. if (plist) {
  865. /* Initialize property list structure */
  866. plist->pl_mempool = mempool;
  867. plist->pl_symtab = NULL;
  868. plist->pl_maxprop = src_plist->pl_maxprop;
  869. plist->pl_resvpi = src_plist->pl_resvpi;
  870. plist->pl_initpi = src_plist->pl_initpi;
  871. plist->pl_lastpi = src_plist->pl_lastpi;
  872. /* Set initialize size for array of property value pointers */
  873. plist->pl_cursize = src_plist->pl_cursize;
  874. /* Allocate the initial array of property value pointers */
  875. plist->pl_ppval = (pb_entry **)pool_malloc(mempool,
  876. (plist->pl_cursize *
  877. sizeof(PLValueStruct_t *)));
  878. if (!plist->pl_ppval) {
  879. /* Failed - insufficient memory */
  880. pool_free(mempool, (void *)plist);
  881. plist = NULL;
  882. }
  883. else {
  884. /* NULL out pointers in the reserved index range, if any */
  885. for (i = 0; i < plist->pl_lastpi; ++i) {
  886. plist->pl_ppval[i] = 0;
  887. }
  888. }
  889. }
  890. return (PList_t)plist;
  891. }
  892. /*
  893. * FUNCTION: PListDuplicate
  894. *
  895. * DESCRIPTION:
  896. *
  897. * This function duplicates a specified PList_t.
  898. *
  899. * ARGUMENTS:
  900. *
  901. * plist - handle for the property list
  902. * mempool - handle for a memory pool to be associated
  903. * with the new property list
  904. * resvprop - number of reserved property indices
  905. * maxprop - maximum number of properties in list
  906. * (zero or negative imposes no limit)
  907. * flags - unused, reserved, must be zero
  908. *
  909. * RETURNS:
  910. *
  911. * If successful, the function return value is a handle for the new
  912. * property list. Otherwise NULL is returned.
  913. */
  914. NSAPI_PUBLIC PList_t
  915. PListDuplicate(PList_t plist, pool_handle_t *new_mempool, int flags)
  916. {
  917. PListStruct_t *pl = (PListStruct_t *)plist;
  918. PLValueStruct_t **ppval;
  919. PLValueStruct_t *pv;
  920. int i;
  921. int rv = 0;
  922. PList_t new_plist;
  923. if (!plist) return NULL;
  924. new_plist = PListCreateDuplicate(plist, new_mempool, flags);
  925. if (new_plist == NULL) {
  926. return(NULL);
  927. }
  928. ppval = (PLValueStruct_t **)(pl->pl_ppval);
  929. /* Loop over the initialized property indices */
  930. for (i = 0; i < pl->pl_initpi; ++i) {
  931. /* Got a property here? */
  932. pv = ppval[i];
  933. if (pv) {
  934. /* Create the property */
  935. rv = PListDefProp(new_plist, i + 1, pv->pv_name, PLFLG_IGN_RES);
  936. if (rv > 0) {
  937. /* If that worked, set the value and type */
  938. rv = PListSetValue(new_plist, rv, pv->pv_value, pv->pv_type);
  939. }
  940. if ( rv <= 0 ) {
  941. PListDestroy(new_plist);
  942. return(NULL);
  943. }
  944. }
  945. }
  946. return(new_plist);
  947. }
  948. /*
  949. * FUNCTION: PListGetPool
  950. *
  951. * DESCRIPTION:
  952. *
  953. * This function returns the memory pool the PList is allocated from.
  954. *
  955. * ARGUMENTS:
  956. *
  957. * plist - handle for the property list
  958. *
  959. * RETURNS:
  960. *
  961. * The memory pool address, which can be NULL.
  962. */
  963. NSAPI_PUBLIC pool_handle_t *
  964. PListGetPool(PList_t plist)
  965. {
  966. if (!plist) return NULL;
  967. return(plist->pl_mempool);
  968. }