plist.cpp 36 KB

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