| 1 | /* -------------------------------------------------------------- FREQLIST.C */ |
|---|
| 2 | |
|---|
| 3 | /* ///////////////////////////////////////////////////////////////////////// |
|---|
| 4 | |
|---|
| 5 | [ Version 1.0 ] |
|---|
| 6 | 17.Nov.1998 - Frequency list. |
|---|
| 7 | |
|---|
| 8 | ///////////////////////////////////////////////////////////////////////// */ |
|---|
| 9 | |
|---|
| 10 | /* ---------------------------------------------------------- C HEADER FILES */ |
|---|
| 11 | #include <string.h> |
|---|
| 12 | /* ------------------------------------------------------------ HEADER FILES */ |
|---|
| 13 | /* include "cisis.h" */ |
|---|
| 14 | #include "easyfc.h" |
|---|
| 15 | #include "freqlist.h" |
|---|
| 16 | |
|---|
| 17 | /* --------------------------------------------------------------- freq_free */ |
|---|
| 18 | void freq_free(STRUCT_FREQLIST *freq_pos) |
|---|
| 19 | { |
|---|
| 20 | STRUCT_FREQLIST *freq_next; |
|---|
| 21 | |
|---|
| 22 | for ( ; freq_pos; freq_pos = freq_next) { |
|---|
| 23 | freq_next = freq_pos->next; |
|---|
| 24 | efc_free(freq_pos->item); |
|---|
| 25 | efc_free(freq_pos->sort); |
|---|
| 26 | efc_free(freq_pos); |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | /* ---------------------------------------------------------------- freq_new */ |
|---|
| 30 | STRUCT_FREQLIST *freq_new(STRUCT_FREQLIST *prev, |
|---|
| 31 | STRUCT_FREQLIST *next, |
|---|
| 32 | char *item) |
|---|
| 33 | { |
|---|
| 34 | STRUCT_FREQLIST *new_item; |
|---|
| 35 | |
|---|
| 36 | new_item = (STRUCT_FREQLIST *)efc_new(sizeof(STRUCT_FREQLIST)); |
|---|
| 37 | if (!new_item) return NULL; |
|---|
| 38 | new_item->item = strdup(item); |
|---|
| 39 | if (!new_item->item) return NULL; |
|---|
| 40 | new_item->qtt = 0L; |
|---|
| 41 | new_item->sort = NULL; |
|---|
| 42 | new_item->prev = prev; |
|---|
| 43 | new_item->next = next; |
|---|
| 44 | if (prev) prev->next = new_item; |
|---|
| 45 | if (next) next->prev = new_item; |
|---|
| 46 | return new_item; |
|---|
| 47 | } |
|---|
| 48 | /* --------------------------------------------------------------- freq_find */ |
|---|
| 49 | STRUCT_FREQLIST *freq_find(FREQLIST_VAR *freq_var, |
|---|
| 50 | STRUCT_FREQLIST *freq_pos, |
|---|
| 51 | char *item, |
|---|
| 52 | BOOLEAN *is_equal) |
|---|
| 53 | { |
|---|
| 54 | *is_equal = FALSE; |
|---|
| 55 | for ( ; freq_pos; freq_pos = freq_pos->next) { |
|---|
| 56 | if (strcmp(item,freq_pos->item) > 0) continue; |
|---|
| 57 | if (strcmp(item,freq_pos->item) == 0) { |
|---|
| 58 | *is_equal = TRUE; |
|---|
| 59 | break; |
|---|
| 60 | } |
|---|
| 61 | if (!freq_var->check_all) break; |
|---|
| 62 | } |
|---|
| 63 | return freq_pos; |
|---|
| 64 | } |
|---|
| 65 | /* ---------------------------------------------------------------- freq_add */ |
|---|
| 66 | BOOLEAN freq_add(FREQLIST_VAR *freq_var, |
|---|
| 67 | char *item, |
|---|
| 68 | long freq_count, |
|---|
| 69 | BOOLEAN list_style, |
|---|
| 70 | BOOLEAN sort_style) |
|---|
| 71 | { |
|---|
| 72 | STRUCT_FREQLIST *freq_pos; |
|---|
| 73 | STRUCT_FREQLIST *prev; |
|---|
| 74 | STRUCT_FREQLIST *next; |
|---|
| 75 | BOOLEAN is_equal = FALSE; |
|---|
| 76 | |
|---|
| 77 | if (!*item) return TRUE; |
|---|
| 78 | freq_pos = list_style ? NULL : freq_find(freq_var,freq_var->head,item,&is_equal); |
|---|
| 79 | if (!is_equal || list_style || sort_style) { |
|---|
| 80 | prev = freq_var->tail; |
|---|
| 81 | next = NULL; |
|---|
| 82 | if (freq_pos) { |
|---|
| 83 | prev = freq_pos->prev; |
|---|
| 84 | next = freq_pos; |
|---|
| 85 | } |
|---|
| 86 | freq_pos = freq_new(prev,next,item); |
|---|
| 87 | if (!freq_pos) return FALSE; |
|---|
| 88 | if (!prev) freq_var->head = freq_pos; |
|---|
| 89 | if (!next) freq_var->tail = freq_pos; |
|---|
| 90 | freq_var->item_tot++; |
|---|
| 91 | } |
|---|
| 92 | freq_pos->qtt += freq_count; |
|---|
| 93 | freq_var->freq_tot += freq_count; |
|---|
| 94 | return TRUE; |
|---|
| 95 | } |
|---|
| 96 | /* --------------------------------------------------------------- freq_load */ |
|---|
| 97 | BOOLEAN freq_load(FREQLIST_VAR *freq_var, |
|---|
| 98 | char *itens, |
|---|
| 99 | long value, |
|---|
| 100 | BOOLEAN list_style, |
|---|
| 101 | BOOLEAN sort_style) |
|---|
| 102 | { |
|---|
| 103 | char *p; |
|---|
| 104 | char *pbeg; |
|---|
| 105 | |
|---|
| 106 | for (p = pbeg = itens; ; p++) { |
|---|
| 107 | if (*p && *p != '\r' && *p != '\n') continue; |
|---|
| 108 | while (*p == '\r' || *p == '\n') *p++ = '\0'; |
|---|
| 109 | if (!*pbeg) break; |
|---|
| 110 | if (!freq_add(freq_var,pbeg,value,list_style,sort_style)) return FALSE; |
|---|
| 111 | pbeg = p--; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | return TRUE; |
|---|
| 115 | } |
|---|
| 116 | /* ---------------------------------------------------------- freq_sort_find */ |
|---|
| 117 | STRUCT_FREQLIST *freq_sort_find(STRUCT_FREQLIST *freq_pos, |
|---|
| 118 | char *item) |
|---|
| 119 | { |
|---|
| 120 | for ( ; freq_pos; freq_pos = freq_pos->next) |
|---|
| 121 | if (strcmp(item,freq_pos->sort) < 0) break; |
|---|
| 122 | return freq_pos; |
|---|
| 123 | } |
|---|
| 124 | /* --------------------------------------------------------------- freq_sort */ |
|---|
| 125 | void freq_sort(FREQLIST_VAR *freq_var) |
|---|
| 126 | { |
|---|
| 127 | STRUCT_FREQLIST *freq_pos; |
|---|
| 128 | STRUCT_FREQLIST *freqnext; |
|---|
| 129 | STRUCT_FREQLIST *sort_head = NULL; |
|---|
| 130 | STRUCT_FREQLIST *sort_tail = NULL; |
|---|
| 131 | STRUCT_FREQLIST *sort_pos; |
|---|
| 132 | STRUCT_FREQLIST *prev; |
|---|
| 133 | STRUCT_FREQLIST *next; |
|---|
| 134 | |
|---|
| 135 | for (freq_pos = freq_var->head; freq_pos; freq_pos = freqnext) { |
|---|
| 136 | sort_pos = freq_sort_find(sort_head,freq_pos->sort); |
|---|
| 137 | freqnext = freq_pos->next; |
|---|
| 138 | |
|---|
| 139 | prev = sort_tail; |
|---|
| 140 | next = NULL; |
|---|
| 141 | if (sort_pos) { |
|---|
| 142 | prev = sort_pos->prev; |
|---|
| 143 | next = sort_pos; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | freq_pos->prev = prev; |
|---|
| 147 | freq_pos->next = next; |
|---|
| 148 | if (prev) prev->next = freq_pos; |
|---|
| 149 | if (next) next->prev = freq_pos; |
|---|
| 150 | |
|---|
| 151 | if (!prev) sort_head = freq_pos; |
|---|
| 152 | if (!next) sort_tail = freq_pos; |
|---|
| 153 | } |
|---|
| 154 | freq_var->head = sort_head; |
|---|
| 155 | freq_var->tail = sort_tail; |
|---|
| 156 | freq_var->check_all = TRUE; |
|---|
| 157 | } |
|---|