Back to index

im-sdk  12.3.91
EIMILJournal.c
Go to the documentation of this file.
00001 /*
00002   EIMILTextUtil.c
00003     EIMIL journaling system.
00004 */
00005 
00006 #ifdef HAVE_CONFIG_H
00007 #include <config.h>
00008 #endif
00009 
00010 #include <stdio.h>
00011 
00012 #ifdef HAVE_STDLIB_H
00013 #include <stdlib.h>
00014 #endif
00015 #ifdef HAVE_STRING_H
00016 #include <string.h>
00017 #endif
00018 #ifdef HAVE_MEMORY_H
00019 #include <memory.h>
00020 #endif
00021 
00022 #include <EIMIL.h>
00023 #include "EIMILint.h"
00024 
00025 #define EIMIL_JOURNAL_MAX_SLOT_SIZE 128
00026 #define EIMIL_JOURNAL_CHECK(eh) ((((EIMIL_data*)eh)->pjst) ? 1 : EIMIL_journal_initialize(eh))
00027 
00028 int
00029 EIMIL_journal_initialize(
00030     EIMIL_handle eh
00031 )
00032 {
00033     int i;
00034     EIMIL_data *ped = (EIMIL_data*) eh;
00035     EIMIL_journal *pj, *pjh;
00036     pj = (EIMIL_journal*) malloc(sizeof(EIMIL_journal)
00037                              * EIMIL_JOURNAL_MAX_SLOT_SIZE);
00038     if (!pj) return 0;
00039     memset(pj, 0, sizeof(EIMIL_journal) * EIMIL_JOURNAL_MAX_SLOT_SIZE);
00040     ped->pjst = pjh = pj;
00041     for (i = 0;i < EIMIL_JOURNAL_MAX_SLOT_SIZE - 1;i++) {
00042        pj->next = &pj[1];
00043        pj[1].prev = pj;
00044        pj++;
00045     }
00046     pj->next = pjh;
00047     pjh->prev = pj;
00048 
00049     return 1;
00050 }
00051 
00052 void
00053 EIMIL_journal_free(
00054     EIMIL_handle eh
00055 )
00056 {
00057     int i;
00058     EIMIL_data *ped = (EIMIL_data*) eh;
00059     EIMIL_journal *pj, *pjh;
00060 
00061     pjh = NULL;
00062     pj = ped->pjst;
00063     for (i = 0; i < EIMIL_JOURNAL_MAX_SLOT_SIZE; i++) {
00064         fprintf(stderr, "hoge\n");
00065         if (!pj) break;
00066        if (pj->pv) EIMIL_RMREF(*pj->pv);
00067        if ((pj->next - pj) < 0) pjh = pj->next;
00068        pj = pj->next;
00069     }
00070 
00071     if (pjh) free(pjh);
00072     /*
00073     ASSERT(pjh);
00074     free(pjh);
00075     */
00076 }
00077 
00078 int
00079 EIMIL_journal_set_point(
00080     EIMIL_handle eh,
00081     EIMIL_TID *ptid
00082 )
00083 {
00084     int cid;
00085     EIMIL_journal *pcj;
00086     EIMIL_data *ped = (EIMIL_data*) eh;
00087 
00088     if (!EIMIL_JOURNAL_CHECK(eh)) return 0;
00089     pcj = ped->pjst->next;
00090     cid = ped->current_journal_id;
00091     cid++;
00092     pcj->id = cid;
00093     if (pcj->pv) {
00094        EIMIL_RMREF(*pcj->pv);
00095        pcj->pv = NULL;
00096     }
00097     if (ped->current_journal_id >= EIMIL_JOURNAL_ID_MAX) {
00098        ped->current_journal_id = 1;
00099     }else{
00100        ped->current_journal_id = cid;
00101     }
00102 
00103     return 0;
00104 }
00105 
00106 int
00107 EIMIL_journal_record_change(
00108     EIMIL_handle eh,
00109     EIMIL_symbol *psym
00110 )
00111 {
00112     EIMIL_data *ped = (EIMIL_data*) eh;
00113     EIMIL_journal *pcj;
00114 
00115     ASSERT(psym->cat == EIMIL_CAT_VARIABLE);
00116     if (!EIMIL_JOURNAL_CHECK(eh)) return 0;
00117     pcj = ped->pjst;
00118 
00119     pcj->pv = EIMIL_copy_value(psym->obj.v.pv);
00120     EIMIL_ADDREF(*pcj->pv);
00121               
00122     if (EIMIL_JOURNAL_EMPTY_SLOT(*pcj)) {
00123        pcj->id = EIMIL_JOURNAL_NONRECORD_TID;
00124     }
00125     pcj = pcj->next;
00126     pcj->id = EIMIL_JOURNAL_EMPTY_TID;
00127     if (pcj->pv) {
00128        EIMIL_RMREF(*pcj->pv);
00129        pcj->pv = NULL;
00130     }
00131     ped->pjst = pcj;
00132     return 1;
00133 }
00134 
00135 int
00136 EIMIL_journal_prev_val(
00137     EIMIL_handle eh,
00138     EIMIL_TID tid,
00139     EIMIL_symbol *psym,
00140     EIMIL_value **ppv
00141 )
00142 {
00143     int i;
00144     EIMIL_data *ped = (EIMIL_data*) eh;
00145     EIMIL_journal *pcj, *pj;
00146 
00147     ASSERT(psym->cat == EIMIL_CAT_VARIABLE);
00148     if (!ped->pjst) return 0;
00149     pcj = ped->pjst;
00150     pj = NULL;
00151     for (i = 0; i < EIMIL_JOURNAL_MAX_SLOT_SIZE; i++) {
00152        if (pcj->psym == psym) pj = pcj;
00153        if (pcj->id == tid) break;
00154        pcj = pcj->prev;
00155        ASSERT(pcj->pv);
00156     }
00157     if (!pj) return 0;
00158     *ppv = pj->pv;
00159 
00160     return 1;
00161 }
00162 
00163 int
00164 EIMIL_journal_rollback(
00165     EIMIL_handle eh,
00166     EIMIL_TID tid
00167 )
00168 {
00169        /* TODO */
00170 }
00171 
00172 
00173 
00174 /* Local Variables: */
00175 /* c-file-style: "iiim-project" */
00176 /* End: */