Branch data Line data Source code
1 : : #include "dictionary.h" 2 : : #include <stdlib.h> 3 : : #include <stdio.h> 4 : : #include <string.h> 5 : : 6 : 0 : dictionary create_dictionary(void *value){ 7 : 0 : dictionary output = (dictionary) {.value = value}; 8 : 0 : memset(output.next_chars, 0, sizeof(dictionary *)*8); 9 : 0 : return output; 10 : : } 11 : : 12 : 0 : static void free_dictionary_recursive(dictionary *dict, void (*free_value)(void *)){ 13 : 0 : unsigned char i; 14 : : 15 [ # # ]: 0 : for(i = 0; i < 8; i++){ 16 [ # # ]: 0 : if(dict->next_chars[i]){ 17 : 0 : free_dictionary_recursive(dict->next_chars[i], free_value); 18 : 0 : free(dict->next_chars[i]); 19 : : } 20 : : } 21 : : 22 [ # # ]: 0 : if(dict->value){ 23 : 0 : free_value(dict->value); 24 : : } 25 : 0 : } 26 : : 27 : 0 : void free_dictionary(dictionary dict, void (*free_value)(void *)){ 28 : 0 : unsigned char i; 29 : : 30 [ # # ]: 0 : for(i = 0; i < 8; i++){ 31 [ # # ]: 0 : if(dict.next_chars[i]){ 32 : 0 : free_dictionary_recursive(dict.next_chars[i], free_value); 33 : 0 : free(dict.next_chars[i]); 34 : : } 35 : : } 36 : : 37 [ # # ]: 0 : if(dict.value){ 38 : 0 : free_value(dict.value); 39 : : } 40 : 0 : } 41 : : 42 : 0 : void *read_dictionary(dictionary dict, char *string, unsigned char offset){ 43 : 0 : unsigned char zeros = 0; 44 : 0 : unsigned char c; 45 : : 46 [ # # ]: 0 : while(*string){ 47 : 0 : c = (*string)>>offset | (*(string + 1))<<(8 - offset); 48 : 0 : zeros = 0; 49 : : 50 [ # # ]: 0 : if(!(c&15)){ 51 : 0 : zeros += 4; 52 : 0 : c>>=4; 53 : : } 54 [ # # ]: 0 : if(!(c&3)){ 55 : 0 : zeros += 2; 56 : 0 : c>>=2; 57 : : } 58 [ # # ]: 0 : if(!(c&1)){ 59 : 0 : zeros++; 60 : : } 61 : : 62 : 0 : offset += zeros + 1; 63 : 0 : string += (offset&0x08)>>3; 64 : 0 : offset = offset&0x07; 65 [ # # ]: 0 : if(dict.next_chars[zeros]){ 66 : 0 : dict = *(dict.next_chars[zeros]); 67 : : } else { 68 : 0 : return (void *) 0; 69 : : } 70 : : } 71 : : 72 : 0 : return dict.value; 73 : : } 74 : : 75 : 0 : void write_dictionary(dictionary *dict, char *string, void *value, unsigned char offset){ 76 : 0 : unsigned char zeros = 0; 77 : 0 : unsigned char c; 78 : : 79 [ # # ]: 0 : while(*string){ 80 : 0 : c = (*string)>>offset | (*(string + 1))<<(8 - offset); 81 : 0 : zeros = 0; 82 : : 83 [ # # ]: 0 : if(!(c&15)){ 84 : 0 : zeros += 4; 85 : 0 : c>>=4; 86 : : } 87 [ # # ]: 0 : if(!(c&3)){ 88 : 0 : zeros += 2; 89 : 0 : c>>=2; 90 : : } 91 [ # # ]: 0 : if(!(c&1)){ 92 : 0 : zeros++; 93 : : } 94 : : 95 : 0 : offset += zeros + 1; 96 : 0 : string += (offset&0x08)>>3; 97 : 0 : offset = offset&0x07; 98 [ # # ]: 0 : if(!dict->next_chars[zeros]){ 99 : 0 : dict->next_chars[zeros] = malloc(sizeof(dictionary)); 100 : 0 : *(dict->next_chars[zeros]) = create_dictionary((void *) 0); 101 : : } 102 : 0 : dict = dict->next_chars[zeros]; 103 : : } 104 : : 105 : 0 : dict->value = value; 106 : 0 : } 107 : : 108 : 0 : void iterate_dictionary(dictionary dict, void (*func)(void *)){ 109 : 0 : unsigned char i; 110 : : 111 [ # # ]: 0 : if(dict.value){ 112 : 0 : func(dict.value); 113 : : } 114 : : 115 [ # # ]: 0 : for(i = 0; i < 8; i++){ 116 [ # # ]: 0 : if(dict.next_chars[i]){ 117 : 0 : iterate_dictionary(*dict.next_chars[i], func); 118 : : } 119 : : } 120 : 0 : } 121 : :