Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

hthist.cc

Go to the documentation of this file.
00001 /* 
00002  *      HT Editor
00003  *      hthist.cc
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 /* limit number of history entries to MAX_HISTORY_ENTRY_COUNT */
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  *      CLASS ht_history_entry
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  *      ATOMS
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  *      INIT
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  *      DONE
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 

Generated on Fri May 7 21:15:34 2004 by doxygen 1.3.5