00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "htatom.h"
00022 #include "htdata.h"
00023 #include "hthist.h"
00024 #include "htstring.h"
00025 #include "tools.h"
00026
00027 #include <string.h>
00028
00029 #define ATOM_HT_HISTORY_ENTRY MAGICD("HIS\0")
00030 #define ATOM_COMPARE_KEYS_HISTORY_ENTRY MAGICD("HIS\1")
00031
00032 #define MAX_HISTORY_ENTRY_COUNT 40
00033
00034 bool insert_history_entry(ht_list *history, char *name, ht_view *view)
00035 {
00036 if (name && *name) {
00037 ht_object_stream_bin *os=0;
00038 ht_mem_file *file=0;
00039 if (view) {
00040 file=new ht_mem_file();
00041 file->init();
00042
00043 os=new ht_object_stream_bin();
00044 os->init(file);
00045
00046 view->getdata(os);
00047 }
00048
00049 ht_history_entry *e=new ht_history_entry(name, os, file);
00050 UINT li=history->find(e);
00051 int r=0;
00052 if (li==LIST_UNDEFINED) {
00053 history->prepend(e);
00054 r=1;
00055 } else {
00056 delete e;
00057 history->move(li, 0);
00058 }
00059
00060 if (history->count() > MAX_HISTORY_ENTRY_COUNT) {
00061 history->del_multiple(MAX_HISTORY_ENTRY_COUNT, history->count() - MAX_HISTORY_ENTRY_COUNT);
00062 }
00063 return 1;
00064 }
00065 return 0;
00066 }
00067
00068
00069
00070
00071
00072 ht_history_entry::ht_history_entry(char *s, ht_object_stream_bin *d, ht_mem_file *df)
00073 {
00074 if (s) desc=ht_strdup(s);
00075 data=d;
00076 datafile=df;
00077 }
00078
00079 ht_history_entry::~ht_history_entry()
00080 {
00081 if (desc) free(desc);
00082 if (data) {
00083 data->done();
00084 delete data;
00085 }
00086 if (datafile) {
00087 datafile->done();
00088 delete datafile;
00089 }
00090 }
00091
00092 int ht_history_entry::load(ht_object_stream *s)
00093 {
00094 desc=s->getString(NULL);
00095
00096 UINT size=s->getInt(4, NULL);
00097
00098 if (size) {
00099 datafile=new ht_mem_file();
00100 datafile->init();
00101
00102 data=new ht_object_stream_bin();
00103 data->init(datafile);
00104
00105 void *d=s->getBinary(size, NULL);
00106 datafile->write(d, size);
00107 free(d);
00108 } else {
00109 datafile=0;
00110 data=0;
00111 }
00112
00113 return 0;
00114 }
00115
00116 void ht_history_entry::store(ht_object_stream *s)
00117 {
00118 s->putString(desc, NULL);
00119
00120 if (datafile) {
00121 UINT size=datafile->get_size();
00122
00123 s->putInt(size, 4, NULL);
00124 s->putBinary(datafile->bufptr(), size, NULL);
00125 } else {
00126 s->putInt(0, 4, NULL);
00127 }
00128 }
00129
00130 OBJECT_ID ht_history_entry::object_id() const
00131 {
00132 return ATOM_HT_HISTORY_ENTRY;
00133 }
00134
00135 int compare_keys_history_entry(Object *key_a, Object *key_b)
00136 {
00137 return strcmp(((ht_history_entry*)key_a)->desc, ((ht_history_entry*)key_b)->desc);
00138 }
00139
00140
00141
00142
00143
00144 int hist_atoms[]={
00145 HISTATOM_GOTO,
00146 HISTATOM_FILE,
00147 HISTATOM_SEARCH_BIN,
00148 HISTATOM_SEARCH_EVALSTR,
00149 HISTATOM_SEARCH_VREGEX,
00150 HISTATOM_SEARCH_EXPR,
00151 HISTATOM_ASSEMBLER,
00152 HISTATOM_NAME_ADDR,
00153 HISTATOM_EVAL_EXPR
00154 };
00155
00156 void create_hist_atom(UINT atom)
00157 {
00158 ht_clist *c=new ht_clist();
00159 c->init(compare_keys_history_entry);
00160 register_atom(atom, c);
00161 }
00162
00163 void destroy_hist_atom(UINT atom)
00164 {
00165 ht_clist *c=(ht_clist*)find_atom(atom);
00166 if (c) {
00167 unregister_atom(atom);
00168 c->destroy();
00169 delete c;
00170 }
00171 }
00172
00173 void store_history(ht_object_stream *s)
00174 {
00175 UINT count=sizeof hist_atoms / sizeof hist_atoms[0];
00176 s->putIntDec(count, 4, NULL);
00177 for (UINT i=0; i<count; i++) {
00178 s->putIntHex(hist_atoms[i], 4, NULL);
00179 ht_clist *c=(ht_clist*)find_atom(hist_atoms[i]);
00180 s->putObject(c, NULL);
00181 }
00182 }
00183
00184 bool load_history(ht_object_stream *s)
00185 {
00186 UINT count=s->getIntDec(4, NULL);
00187 for (UINT i=0; i<count; i++) {
00188 int atom=s->getIntHex(4, NULL);
00189 destroy_hist_atom(atom);
00190 ht_clist *c=(ht_clist*)s->getObject(NULL);
00191 register_atom(atom, c);
00192 }
00193 return 1;
00194 }
00195
00196
00197
00198
00199
00200 BUILDER(ATOM_HT_HISTORY_ENTRY, ht_history_entry);
00201
00202 bool init_hist()
00203 {
00204 for (UINT i=0; i<sizeof hist_atoms / sizeof hist_atoms[0]; i++) {
00205 create_hist_atom(hist_atoms[i]);
00206 }
00207
00208 REGISTER(ATOM_HT_HISTORY_ENTRY, ht_history_entry);
00209
00210 register_atom(ATOM_COMPARE_KEYS_HISTORY_ENTRY, (void*)compare_keys_history_entry);
00211
00212 return true;
00213 }
00214
00215
00216
00217
00218
00219 void done_hist()
00220 {
00221
00222 unregister_atom(ATOM_COMPARE_KEYS_HISTORY_ENTRY);
00223
00224 UNREGISTER(ATOM_HT_HISTORY_ENTRY, ht_history_entry);
00225
00226 for (UINT i=0; i<sizeof hist_atoms / sizeof hist_atoms[0]; i++) {
00227 destroy_hist_atom(hist_atoms[i]);
00228 }
00229 }
00230