Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
- http://pymbed.appspot.com/
- https://code.google.com/p/python-on-a-chip/
- http://www.youtube.com/watch?v=Oyqc2bFRW9I
- https://bitbucket.org/va009039/pymbed/
more info: python-on-a-chip
vm/dict.c@0:65f1469d6bfb, 2013-03-02 (annotated)
- Committer:
- va009039
- Date:
- Sat Mar 02 11:54:20 2013 +0000
- Revision:
- 0:65f1469d6bfb
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:65f1469d6bfb | 1 | /* |
va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2002 Dean Hall. |
va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
va009039 | 0:65f1469d6bfb | 6 | */ |
va009039 | 0:65f1469d6bfb | 7 | |
va009039 | 0:65f1469d6bfb | 8 | |
va009039 | 0:65f1469d6bfb | 9 | #undef __FILE_ID__ |
va009039 | 0:65f1469d6bfb | 10 | #define __FILE_ID__ 0x02 |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief Dict Object Type |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * Dict object type operations. |
va009039 | 0:65f1469d6bfb | 18 | */ |
va009039 | 0:65f1469d6bfb | 19 | |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | #include "pm.h" |
va009039 | 0:65f1469d6bfb | 22 | |
va009039 | 0:65f1469d6bfb | 23 | |
va009039 | 0:65f1469d6bfb | 24 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 25 | dict_new(pPmObj_t *r_pdict) |
va009039 | 0:65f1469d6bfb | 26 | { |
va009039 | 0:65f1469d6bfb | 27 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 28 | pPmDict_t pdict = C_NULL; |
va009039 | 0:65f1469d6bfb | 29 | uint8_t *pchunk; |
va009039 | 0:65f1469d6bfb | 30 | |
va009039 | 0:65f1469d6bfb | 31 | /* Allocate a dict */ |
va009039 | 0:65f1469d6bfb | 32 | retval = heap_getChunk(sizeof(PmDict_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 33 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 34 | |
va009039 | 0:65f1469d6bfb | 35 | /* Init dict fields */ |
va009039 | 0:65f1469d6bfb | 36 | pdict = (pPmDict_t)pchunk; |
va009039 | 0:65f1469d6bfb | 37 | OBJ_SET_TYPE(pdict, OBJ_TYPE_DIC); |
va009039 | 0:65f1469d6bfb | 38 | pdict->length = 0; |
va009039 | 0:65f1469d6bfb | 39 | pdict->d_keys = C_NULL; |
va009039 | 0:65f1469d6bfb | 40 | pdict->d_vals = C_NULL; |
va009039 | 0:65f1469d6bfb | 41 | |
va009039 | 0:65f1469d6bfb | 42 | *r_pdict = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 43 | return retval; |
va009039 | 0:65f1469d6bfb | 44 | } |
va009039 | 0:65f1469d6bfb | 45 | |
va009039 | 0:65f1469d6bfb | 46 | |
va009039 | 0:65f1469d6bfb | 47 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 48 | dict_clear(pPmObj_t pdict) |
va009039 | 0:65f1469d6bfb | 49 | { |
va009039 | 0:65f1469d6bfb | 50 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 51 | |
va009039 | 0:65f1469d6bfb | 52 | C_ASSERT(pdict != C_NULL); |
va009039 | 0:65f1469d6bfb | 53 | |
va009039 | 0:65f1469d6bfb | 54 | /* Raise TypeError if arg is not a dict */ |
va009039 | 0:65f1469d6bfb | 55 | if (OBJ_GET_TYPE(pdict) != OBJ_TYPE_DIC) |
va009039 | 0:65f1469d6bfb | 56 | { |
va009039 | 0:65f1469d6bfb | 57 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 58 | return retval; |
va009039 | 0:65f1469d6bfb | 59 | } |
va009039 | 0:65f1469d6bfb | 60 | |
va009039 | 0:65f1469d6bfb | 61 | /* clear length */ |
va009039 | 0:65f1469d6bfb | 62 | ((pPmDict_t)pdict)->length = 0; |
va009039 | 0:65f1469d6bfb | 63 | |
va009039 | 0:65f1469d6bfb | 64 | /* Free the keys and values seglists if needed */ |
va009039 | 0:65f1469d6bfb | 65 | if (((pPmDict_t)pdict)->d_keys != C_NULL) |
va009039 | 0:65f1469d6bfb | 66 | { |
va009039 | 0:65f1469d6bfb | 67 | PM_RETURN_IF_ERROR(seglist_clear(((pPmDict_t)pdict)->d_keys)); |
va009039 | 0:65f1469d6bfb | 68 | PM_RETURN_IF_ERROR(heap_freeChunk((pPmObj_t) |
va009039 | 0:65f1469d6bfb | 69 | ((pPmDict_t)pdict)->d_keys)); |
va009039 | 0:65f1469d6bfb | 70 | ((pPmDict_t)pdict)->d_keys = C_NULL; |
va009039 | 0:65f1469d6bfb | 71 | } |
va009039 | 0:65f1469d6bfb | 72 | if (((pPmDict_t)pdict)->d_vals != C_NULL) |
va009039 | 0:65f1469d6bfb | 73 | { |
va009039 | 0:65f1469d6bfb | 74 | PM_RETURN_IF_ERROR(seglist_clear(((pPmDict_t)pdict)->d_vals)); |
va009039 | 0:65f1469d6bfb | 75 | retval = heap_freeChunk((pPmObj_t)((pPmDict_t)pdict)->d_vals); |
va009039 | 0:65f1469d6bfb | 76 | ((pPmDict_t)pdict)->d_vals = C_NULL; |
va009039 | 0:65f1469d6bfb | 77 | } |
va009039 | 0:65f1469d6bfb | 78 | return retval; |
va009039 | 0:65f1469d6bfb | 79 | } |
va009039 | 0:65f1469d6bfb | 80 | |
va009039 | 0:65f1469d6bfb | 81 | |
va009039 | 0:65f1469d6bfb | 82 | /* |
va009039 | 0:65f1469d6bfb | 83 | * Sets a value in the dict using the given key. |
va009039 | 0:65f1469d6bfb | 84 | * |
va009039 | 0:65f1469d6bfb | 85 | * Scans dict for the key. If key val found, replace old |
va009039 | 0:65f1469d6bfb | 86 | * with new val. If no key found, add key/val pair to dict. |
va009039 | 0:65f1469d6bfb | 87 | */ |
va009039 | 0:65f1469d6bfb | 88 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 89 | dict_setItem(pPmObj_t pdict, pPmObj_t pkey, pPmObj_t pval) |
va009039 | 0:65f1469d6bfb | 90 | { |
va009039 | 0:65f1469d6bfb | 91 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 92 | int16_t indx; |
va009039 | 0:65f1469d6bfb | 93 | |
va009039 | 0:65f1469d6bfb | 94 | C_ASSERT(pdict != C_NULL); |
va009039 | 0:65f1469d6bfb | 95 | C_ASSERT(pkey != C_NULL); |
va009039 | 0:65f1469d6bfb | 96 | C_ASSERT(pval != C_NULL); |
va009039 | 0:65f1469d6bfb | 97 | |
va009039 | 0:65f1469d6bfb | 98 | /* If it's not a dict, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 99 | if (OBJ_GET_TYPE(pdict) != OBJ_TYPE_DIC) |
va009039 | 0:65f1469d6bfb | 100 | { |
va009039 | 0:65f1469d6bfb | 101 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 102 | return retval; |
va009039 | 0:65f1469d6bfb | 103 | } |
va009039 | 0:65f1469d6bfb | 104 | |
va009039 | 0:65f1469d6bfb | 105 | /* #112: Force Dict keys to be of hashable type */ |
va009039 | 0:65f1469d6bfb | 106 | /* If key is not hashable, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 107 | if (OBJ_GET_TYPE(pkey) > OBJ_TYPE_HASHABLE_MAX) |
va009039 | 0:65f1469d6bfb | 108 | { |
va009039 | 0:65f1469d6bfb | 109 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 110 | return retval; |
va009039 | 0:65f1469d6bfb | 111 | } |
va009039 | 0:65f1469d6bfb | 112 | |
va009039 | 0:65f1469d6bfb | 113 | /* #147: Change boolean keys to integers */ |
va009039 | 0:65f1469d6bfb | 114 | if (pkey == PM_TRUE) |
va009039 | 0:65f1469d6bfb | 115 | { |
va009039 | 0:65f1469d6bfb | 116 | pkey = PM_ONE; |
va009039 | 0:65f1469d6bfb | 117 | } |
va009039 | 0:65f1469d6bfb | 118 | else if (pkey == PM_FALSE) |
va009039 | 0:65f1469d6bfb | 119 | { |
va009039 | 0:65f1469d6bfb | 120 | pkey = PM_ZERO; |
va009039 | 0:65f1469d6bfb | 121 | } |
va009039 | 0:65f1469d6bfb | 122 | |
va009039 | 0:65f1469d6bfb | 123 | /* |
va009039 | 0:65f1469d6bfb | 124 | * #115: If this is the first key/value pair to be added to the Dict, |
va009039 | 0:65f1469d6bfb | 125 | * allocate the key and value seglists that hold those items |
va009039 | 0:65f1469d6bfb | 126 | */ |
va009039 | 0:65f1469d6bfb | 127 | if (((pPmDict_t)pdict)->length == 0) |
va009039 | 0:65f1469d6bfb | 128 | { |
va009039 | 0:65f1469d6bfb | 129 | retval = seglist_new(&((pPmDict_t)pdict)->d_keys); |
va009039 | 0:65f1469d6bfb | 130 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 131 | retval = seglist_new(&((pPmDict_t)pdict)->d_vals); |
va009039 | 0:65f1469d6bfb | 132 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 133 | } |
va009039 | 0:65f1469d6bfb | 134 | else |
va009039 | 0:65f1469d6bfb | 135 | { |
va009039 | 0:65f1469d6bfb | 136 | /* Check for matching key */ |
va009039 | 0:65f1469d6bfb | 137 | indx = 0; |
va009039 | 0:65f1469d6bfb | 138 | retval = seglist_findEqual(((pPmDict_t)pdict)->d_keys, pkey, &indx); |
va009039 | 0:65f1469d6bfb | 139 | |
va009039 | 0:65f1469d6bfb | 140 | /* If found a matching key, replace val obj */ |
va009039 | 0:65f1469d6bfb | 141 | if (retval == PM_RET_OK) |
va009039 | 0:65f1469d6bfb | 142 | { |
va009039 | 0:65f1469d6bfb | 143 | retval = seglist_setItem(((pPmDict_t)pdict)->d_vals, pval, indx); |
va009039 | 0:65f1469d6bfb | 144 | return retval; |
va009039 | 0:65f1469d6bfb | 145 | } |
va009039 | 0:65f1469d6bfb | 146 | } |
va009039 | 0:65f1469d6bfb | 147 | |
va009039 | 0:65f1469d6bfb | 148 | /* Otherwise, insert the key,val pair */ |
va009039 | 0:65f1469d6bfb | 149 | retval = seglist_insertItem(((pPmDict_t)pdict)->d_keys, pkey, 0); |
va009039 | 0:65f1469d6bfb | 150 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 151 | retval = seglist_insertItem(((pPmDict_t)pdict)->d_vals, pval, 0); |
va009039 | 0:65f1469d6bfb | 152 | ((pPmDict_t)pdict)->length++; |
va009039 | 0:65f1469d6bfb | 153 | |
va009039 | 0:65f1469d6bfb | 154 | return retval; |
va009039 | 0:65f1469d6bfb | 155 | } |
va009039 | 0:65f1469d6bfb | 156 | |
va009039 | 0:65f1469d6bfb | 157 | |
va009039 | 0:65f1469d6bfb | 158 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 159 | dict_getItem(pPmObj_t pdict, pPmObj_t pkey, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 160 | { |
va009039 | 0:65f1469d6bfb | 161 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 162 | int16_t indx = 0; |
va009039 | 0:65f1469d6bfb | 163 | |
va009039 | 0:65f1469d6bfb | 164 | /* C_ASSERT(pdict != C_NULL);*/ |
va009039 | 0:65f1469d6bfb | 165 | |
va009039 | 0:65f1469d6bfb | 166 | /* if it's not a dict, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 167 | if (OBJ_GET_TYPE(pdict) != OBJ_TYPE_DIC) |
va009039 | 0:65f1469d6bfb | 168 | { |
va009039 | 0:65f1469d6bfb | 169 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 170 | return retval; |
va009039 | 0:65f1469d6bfb | 171 | } |
va009039 | 0:65f1469d6bfb | 172 | |
va009039 | 0:65f1469d6bfb | 173 | /* if dict is empty, raise KeyError */ |
va009039 | 0:65f1469d6bfb | 174 | if (((pPmDict_t)pdict)->length <= 0) |
va009039 | 0:65f1469d6bfb | 175 | { |
va009039 | 0:65f1469d6bfb | 176 | PM_RAISE(retval, PM_RET_EX_KEY); |
va009039 | 0:65f1469d6bfb | 177 | return retval; |
va009039 | 0:65f1469d6bfb | 178 | } |
va009039 | 0:65f1469d6bfb | 179 | |
va009039 | 0:65f1469d6bfb | 180 | /* #147: Change boolean keys to integers */ |
va009039 | 0:65f1469d6bfb | 181 | if (pkey == PM_TRUE) |
va009039 | 0:65f1469d6bfb | 182 | { |
va009039 | 0:65f1469d6bfb | 183 | pkey = PM_ONE; |
va009039 | 0:65f1469d6bfb | 184 | } |
va009039 | 0:65f1469d6bfb | 185 | else if (pkey == PM_FALSE) |
va009039 | 0:65f1469d6bfb | 186 | { |
va009039 | 0:65f1469d6bfb | 187 | pkey = PM_ZERO; |
va009039 | 0:65f1469d6bfb | 188 | } |
va009039 | 0:65f1469d6bfb | 189 | |
va009039 | 0:65f1469d6bfb | 190 | /* check for matching key */ |
va009039 | 0:65f1469d6bfb | 191 | retval = seglist_findEqual(((pPmDict_t)pdict)->d_keys, pkey, &indx); |
va009039 | 0:65f1469d6bfb | 192 | /* if key not found, raise KeyError */ |
va009039 | 0:65f1469d6bfb | 193 | if (retval == PM_RET_NO) |
va009039 | 0:65f1469d6bfb | 194 | { |
va009039 | 0:65f1469d6bfb | 195 | PM_RAISE(retval, PM_RET_EX_KEY); |
va009039 | 0:65f1469d6bfb | 196 | } |
va009039 | 0:65f1469d6bfb | 197 | /* return any other error */ |
va009039 | 0:65f1469d6bfb | 198 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 199 | |
va009039 | 0:65f1469d6bfb | 200 | /* key was found, get obj from vals */ |
va009039 | 0:65f1469d6bfb | 201 | retval = seglist_getItem(((pPmDict_t)pdict)->d_vals, indx, r_pobj); |
va009039 | 0:65f1469d6bfb | 202 | return retval; |
va009039 | 0:65f1469d6bfb | 203 | } |
va009039 | 0:65f1469d6bfb | 204 | |
va009039 | 0:65f1469d6bfb | 205 | |
va009039 | 0:65f1469d6bfb | 206 | #ifdef HAVE_DEL |
va009039 | 0:65f1469d6bfb | 207 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 208 | dict_delItem(pPmObj_t pdict, pPmObj_t pkey) |
va009039 | 0:65f1469d6bfb | 209 | { |
va009039 | 0:65f1469d6bfb | 210 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 211 | int16_t indx = 0; |
va009039 | 0:65f1469d6bfb | 212 | |
va009039 | 0:65f1469d6bfb | 213 | C_ASSERT(pdict != C_NULL); |
va009039 | 0:65f1469d6bfb | 214 | |
va009039 | 0:65f1469d6bfb | 215 | /* Check for matching key */ |
va009039 | 0:65f1469d6bfb | 216 | retval = seglist_findEqual(((pPmDict_t)pdict)->d_keys, pkey, &indx); |
va009039 | 0:65f1469d6bfb | 217 | |
va009039 | 0:65f1469d6bfb | 218 | /* Raise KeyError if key is not found */ |
va009039 | 0:65f1469d6bfb | 219 | if (retval == PM_RET_NO) |
va009039 | 0:65f1469d6bfb | 220 | { |
va009039 | 0:65f1469d6bfb | 221 | PM_RAISE(retval, PM_RET_EX_KEY); |
va009039 | 0:65f1469d6bfb | 222 | } |
va009039 | 0:65f1469d6bfb | 223 | |
va009039 | 0:65f1469d6bfb | 224 | /* Return any other error */ |
va009039 | 0:65f1469d6bfb | 225 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 226 | |
va009039 | 0:65f1469d6bfb | 227 | /* Remove the key and value */ |
va009039 | 0:65f1469d6bfb | 228 | retval = seglist_removeItem(((pPmDict_t)pdict)->d_keys, indx); |
va009039 | 0:65f1469d6bfb | 229 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 230 | retval = seglist_removeItem(((pPmDict_t)pdict)->d_vals, indx); |
va009039 | 0:65f1469d6bfb | 231 | |
va009039 | 0:65f1469d6bfb | 232 | /* Reduce the item count */ |
va009039 | 0:65f1469d6bfb | 233 | ((pPmDict_t)pdict)->length--; |
va009039 | 0:65f1469d6bfb | 234 | |
va009039 | 0:65f1469d6bfb | 235 | return retval; |
va009039 | 0:65f1469d6bfb | 236 | } |
va009039 | 0:65f1469d6bfb | 237 | #endif /* HAVE_DEL */ |
va009039 | 0:65f1469d6bfb | 238 | |
va009039 | 0:65f1469d6bfb | 239 | |
va009039 | 0:65f1469d6bfb | 240 | #ifdef HAVE_PRINT |
va009039 | 0:65f1469d6bfb | 241 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 242 | dict_print(pPmObj_t pdict) |
va009039 | 0:65f1469d6bfb | 243 | { |
va009039 | 0:65f1469d6bfb | 244 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 245 | int16_t index; |
va009039 | 0:65f1469d6bfb | 246 | pSeglist_t keys, |
va009039 | 0:65f1469d6bfb | 247 | vals; |
va009039 | 0:65f1469d6bfb | 248 | pPmObj_t pobj1; |
va009039 | 0:65f1469d6bfb | 249 | |
va009039 | 0:65f1469d6bfb | 250 | C_ASSERT(pdict != C_NULL); |
va009039 | 0:65f1469d6bfb | 251 | |
va009039 | 0:65f1469d6bfb | 252 | /* if it's not a dict, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 253 | if (OBJ_GET_TYPE(pdict) != OBJ_TYPE_DIC) |
va009039 | 0:65f1469d6bfb | 254 | { |
va009039 | 0:65f1469d6bfb | 255 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 256 | return retval; |
va009039 | 0:65f1469d6bfb | 257 | } |
va009039 | 0:65f1469d6bfb | 258 | |
va009039 | 0:65f1469d6bfb | 259 | plat_putByte('{'); |
va009039 | 0:65f1469d6bfb | 260 | |
va009039 | 0:65f1469d6bfb | 261 | keys = ((pPmDict_t)pdict)->d_keys; |
va009039 | 0:65f1469d6bfb | 262 | vals = ((pPmDict_t)pdict)->d_vals; |
va009039 | 0:65f1469d6bfb | 263 | |
va009039 | 0:65f1469d6bfb | 264 | /* if dict is empty, raise KeyError */ |
va009039 | 0:65f1469d6bfb | 265 | for (index = 0; index < ((pPmDict_t)pdict)->length; index++) |
va009039 | 0:65f1469d6bfb | 266 | { |
va009039 | 0:65f1469d6bfb | 267 | if (index != 0) |
va009039 | 0:65f1469d6bfb | 268 | { |
va009039 | 0:65f1469d6bfb | 269 | plat_putByte(','); |
va009039 | 0:65f1469d6bfb | 270 | plat_putByte(' '); |
va009039 | 0:65f1469d6bfb | 271 | } |
va009039 | 0:65f1469d6bfb | 272 | retval = seglist_getItem(keys, index, &pobj1); |
va009039 | 0:65f1469d6bfb | 273 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 274 | retval = obj_print(pobj1, C_FALSE, C_TRUE); |
va009039 | 0:65f1469d6bfb | 275 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 276 | |
va009039 | 0:65f1469d6bfb | 277 | plat_putByte(':'); |
va009039 | 0:65f1469d6bfb | 278 | retval = seglist_getItem(vals, index, &pobj1); |
va009039 | 0:65f1469d6bfb | 279 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 280 | retval = obj_print(pobj1, C_FALSE, C_TRUE); |
va009039 | 0:65f1469d6bfb | 281 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 282 | } |
va009039 | 0:65f1469d6bfb | 283 | |
va009039 | 0:65f1469d6bfb | 284 | return plat_putByte('}'); |
va009039 | 0:65f1469d6bfb | 285 | } |
va009039 | 0:65f1469d6bfb | 286 | #endif /* HAVE_PRINT */ |
va009039 | 0:65f1469d6bfb | 287 | |
va009039 | 0:65f1469d6bfb | 288 | |
va009039 | 0:65f1469d6bfb | 289 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 290 | dict_update(pPmObj_t pdestdict, pPmObj_t psourcedict, uint8_t omit_underscored) |
va009039 | 0:65f1469d6bfb | 291 | { |
va009039 | 0:65f1469d6bfb | 292 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 293 | int16_t i; |
va009039 | 0:65f1469d6bfb | 294 | pPmObj_t pkey; |
va009039 | 0:65f1469d6bfb | 295 | pPmObj_t pval; |
va009039 | 0:65f1469d6bfb | 296 | |
va009039 | 0:65f1469d6bfb | 297 | C_ASSERT(pdestdict != C_NULL); |
va009039 | 0:65f1469d6bfb | 298 | C_ASSERT(psourcedict != C_NULL); |
va009039 | 0:65f1469d6bfb | 299 | |
va009039 | 0:65f1469d6bfb | 300 | /* If it's not a dict, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 301 | if (OBJ_GET_TYPE(pdestdict) != OBJ_TYPE_DIC) |
va009039 | 0:65f1469d6bfb | 302 | { |
va009039 | 0:65f1469d6bfb | 303 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 304 | return retval; |
va009039 | 0:65f1469d6bfb | 305 | } |
va009039 | 0:65f1469d6bfb | 306 | |
va009039 | 0:65f1469d6bfb | 307 | /* If it's not a dict, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 308 | if (OBJ_GET_TYPE(psourcedict) != OBJ_TYPE_DIC) |
va009039 | 0:65f1469d6bfb | 309 | { |
va009039 | 0:65f1469d6bfb | 310 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 311 | return retval; |
va009039 | 0:65f1469d6bfb | 312 | } |
va009039 | 0:65f1469d6bfb | 313 | |
va009039 | 0:65f1469d6bfb | 314 | /* Iterate over the add-on dict */ |
va009039 | 0:65f1469d6bfb | 315 | for (i = 0; i < ((pPmDict_t)psourcedict)->length; i++) |
va009039 | 0:65f1469d6bfb | 316 | { |
va009039 | 0:65f1469d6bfb | 317 | /* Get the key,val from the add-on dict */ |
va009039 | 0:65f1469d6bfb | 318 | retval = seglist_getItem(((pPmDict_t)psourcedict)->d_keys, i, &pkey); |
va009039 | 0:65f1469d6bfb | 319 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 320 | retval = seglist_getItem(((pPmDict_t)psourcedict)->d_vals, i, &pval); |
va009039 | 0:65f1469d6bfb | 321 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 322 | |
va009039 | 0:65f1469d6bfb | 323 | if (!(omit_underscored && (OBJ_GET_TYPE(pkey) == OBJ_TYPE_STR) |
va009039 | 0:65f1469d6bfb | 324 | && ((pPmString_t)pkey)->val[0] == '_')) |
va009039 | 0:65f1469d6bfb | 325 | { |
va009039 | 0:65f1469d6bfb | 326 | /* Set the key,val to the destination dict */ |
va009039 | 0:65f1469d6bfb | 327 | retval = dict_setItem(pdestdict, pkey, pval); |
va009039 | 0:65f1469d6bfb | 328 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 329 | } |
va009039 | 0:65f1469d6bfb | 330 | } |
va009039 | 0:65f1469d6bfb | 331 | |
va009039 | 0:65f1469d6bfb | 332 | return retval; |
va009039 | 0:65f1469d6bfb | 333 | } |
va009039 | 0:65f1469d6bfb | 334 | |
va009039 | 0:65f1469d6bfb | 335 | |
va009039 | 0:65f1469d6bfb | 336 | int8_t |
va009039 | 0:65f1469d6bfb | 337 | dict_compare(pPmObj_t d1, pPmObj_t d2) |
va009039 | 0:65f1469d6bfb | 338 | { |
va009039 | 0:65f1469d6bfb | 339 | pPmDict_t pd1 = (pPmDict_t)d1; |
va009039 | 0:65f1469d6bfb | 340 | pPmDict_t pd2 = (pPmDict_t)d2; |
va009039 | 0:65f1469d6bfb | 341 | pPmObj_t pkey1; |
va009039 | 0:65f1469d6bfb | 342 | pPmObj_t pval1; |
va009039 | 0:65f1469d6bfb | 343 | pPmObj_t pval2; |
va009039 | 0:65f1469d6bfb | 344 | uint16_t i; |
va009039 | 0:65f1469d6bfb | 345 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 346 | |
va009039 | 0:65f1469d6bfb | 347 | /* Return if lengths are not equal */ |
va009039 | 0:65f1469d6bfb | 348 | if (pd1->length != pd2->length) |
va009039 | 0:65f1469d6bfb | 349 | { |
va009039 | 0:65f1469d6bfb | 350 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 351 | } |
va009039 | 0:65f1469d6bfb | 352 | |
va009039 | 0:65f1469d6bfb | 353 | for (i = 0; i < pd1->length; i++) |
va009039 | 0:65f1469d6bfb | 354 | { |
va009039 | 0:65f1469d6bfb | 355 | /* Get the key,val from one dict */ |
va009039 | 0:65f1469d6bfb | 356 | retval = seglist_getItem(pd1->d_keys, i, &pkey1); |
va009039 | 0:65f1469d6bfb | 357 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 358 | retval = seglist_getItem(pd1->d_vals, i, &pval1); |
va009039 | 0:65f1469d6bfb | 359 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 360 | |
va009039 | 0:65f1469d6bfb | 361 | /* Return if the key,val pair is not in the other dict */ |
va009039 | 0:65f1469d6bfb | 362 | retval = dict_getItem(d2, pkey1, &pval2); |
va009039 | 0:65f1469d6bfb | 363 | if (retval != PM_RET_OK) |
va009039 | 0:65f1469d6bfb | 364 | { |
va009039 | 0:65f1469d6bfb | 365 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 366 | } |
va009039 | 0:65f1469d6bfb | 367 | if (obj_compare(pval1, pval2) != C_SAME) |
va009039 | 0:65f1469d6bfb | 368 | { |
va009039 | 0:65f1469d6bfb | 369 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 370 | } |
va009039 | 0:65f1469d6bfb | 371 | } |
va009039 | 0:65f1469d6bfb | 372 | |
va009039 | 0:65f1469d6bfb | 373 | /* All key,values match */ |
va009039 | 0:65f1469d6bfb | 374 | return C_SAME; |
va009039 | 0:65f1469d6bfb | 375 | } |