2
0

nametbl.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 libraries 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. /* $XConsortium: nametbl.c /main/1 1996/04/21 19:24:03 drk $ */
  24. /*
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company
  26. * (c) Copyright 1993, 1994 International Business Machines Corp.
  27. * (c) Copyright 1993, 1994 Novell, Inc.
  28. * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  29. */
  30. #include <EUSCompat.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "nametbl.h"
  35. #include "hash.h"
  36. #define MAX_CAL_TBL_SIZE 23
  37. #define MAX_ENTRY_TBL_SIZE 37
  38. extern _DtCmNameTable *
  39. _DtCm_make_name_table(int size, char **names)
  40. {
  41. _DtCmNameTable *tbl;
  42. int i;
  43. if ((tbl = (_DtCmNameTable *)calloc(1, sizeof(_DtCmNameTable))) == NULL)
  44. return (NULL);
  45. if ((tbl->tbl = _DtCmMakeHash(MAX_CAL_TBL_SIZE)) == NULL) {
  46. free(tbl);
  47. return (NULL);
  48. }
  49. if ((tbl->names = (char **)malloc(sizeof(char *) * (size+1))) == NULL) {
  50. _DtCm_free_name_table(tbl);
  51. return (NULL);
  52. }
  53. for (i = 1; i <= size; i++) {
  54. if ((tbl->names[i] = strdup(names[i])) == NULL) {
  55. tbl->size = i;
  56. _DtCm_free_name_table(tbl);
  57. return (NULL);
  58. }
  59. *(int *)_DtCmGetHash(tbl->tbl, (unsigned char *)names[i]) = i;
  60. }
  61. tbl->size = i - 1;
  62. return (tbl);
  63. }
  64. extern void
  65. _DtCm_free_name_table(_DtCmNameTable *tbl)
  66. {
  67. int i;
  68. if (tbl == NULL) return;
  69. if (tbl->tbl)
  70. _DtCmDestroyHash(tbl->tbl, NULL, NULL);
  71. for (i = 1; i <= tbl->size; i++)
  72. free(tbl->names[i]);
  73. if (tbl->names)
  74. free(tbl->names);
  75. free(tbl);
  76. }
  77. /*
  78. * if index == 0, then add one more element to the table
  79. * otherwise use the index to add the new name and extend the
  80. * table by (index - size)
  81. */
  82. extern CSA_return_code
  83. _DtCm_add_name_to_table(_DtCmNameTable *tbl, int index, char *newname)
  84. {
  85. int *ptr;
  86. char **newptr;
  87. if (index > 0 && index <= tbl->size && tbl->names[index])
  88. return (CSA_E_INVALID_PARAMETER);
  89. if (index == 0)
  90. index = tbl->size + 1;
  91. /* add new name to table */
  92. ptr = (int *)_DtCmGetHash(tbl->tbl, (unsigned char *)newname);
  93. if (ptr)
  94. *ptr = index;
  95. if (index > tbl->size) {
  96. if ((newptr = (char **)realloc(tbl->names,
  97. sizeof(char *)*(index + 1))) == NULL) {
  98. *ptr = -1;
  99. return (CSA_E_INSUFFICIENT_MEMORY);
  100. } else {
  101. tbl->names = newptr;
  102. memset((void *)&tbl->names[tbl->size+1], 0,
  103. sizeof(char *)*(index - tbl->size));
  104. }
  105. }
  106. if ((tbl->names[index] = strdup(newname)) == NULL) {
  107. *ptr = -1;
  108. return (CSA_E_INSUFFICIENT_MEMORY);
  109. } else {
  110. if (index > tbl->size)
  111. tbl->size = index;
  112. }
  113. return (CSA_SUCCESS);
  114. }
  115. extern int
  116. _DtCm_get_index_from_table(_DtCmNameTable *tbl, char *name)
  117. {
  118. int *ptr;
  119. ptr = (int *)_DtCmFindHash(tbl->tbl, (unsigned char *)name);
  120. if (ptr)
  121. return (*ptr);
  122. else
  123. return (-1);
  124. }
  125. /*
  126. * if index == 0, then add one more element to the table
  127. * other use the index to add the new name and extend the
  128. * table by (index - size)
  129. */
  130. extern CSA_return_code
  131. _DtCmExtendNameTable(
  132. char *name,
  133. int index,
  134. int type,
  135. _DtCmNameTable *base,
  136. int basesize,
  137. char **basenames,
  138. _DtCmNameTable **tbl,
  139. int **types)
  140. {
  141. _DtCmNameTable *ntbl;
  142. int *newarray;
  143. int newindex;
  144. if (index > 0 && index <= (*tbl)->size)
  145. return (CSA_E_INVALID_PARAMETER);
  146. if (*tbl == base) {
  147. if ((ntbl = _DtCm_make_name_table(basesize, basenames)) == NULL)
  148. {
  149. return (CSA_E_INSUFFICIENT_MEMORY);
  150. } else
  151. *tbl = ntbl;
  152. }
  153. if (types) {
  154. newindex = (index == 0) ? (*tbl)->size+1 : index;
  155. if ((newarray = (int *)realloc(*types,
  156. sizeof(int) * (newindex+1))) == NULL) {
  157. return (CSA_E_INSUFFICIENT_MEMORY);
  158. } else {
  159. *types = newarray;
  160. (*types)[newindex] = type;
  161. }
  162. }
  163. return (_DtCm_add_name_to_table(*tbl, index, name));
  164. }