1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information.
// You should have received a copy of these files along with MAdLib.
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------
#include "History.h"
#include <stdarg.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using std::ostream;
namespace MAd {
static int actionIntParameters[UNKNOWN_ACTION] = {1,1,1,1,1};
// -------------------------------------------------------------------
void History::initialize()
{
open = false;
refCount = -1;
}
// -------------------------------------------------------------------
void History::finalize()
{
journal.clear();
refJournal.clear();
}
// -------------------------------------------------------------------
void History::add(int ot, actionType at, ...)
{
if ( !open ) return;
va_list ap;
va_start(ap, at);
std::vector<int> intParams;
for (int iInt=0; iInt < actionIntParameters[at]; iInt++) {
intParams.push_back(va_arg(ap, int));
}
va_end(ap);
action act;
act.opType = ot;
act.actType = at;
act.intParams = intParams;
add(act);
}
// -------------------------------------------------------------------
void History::add(action act)
{
if ( !open ) return;
journal.push_back(act);
if ( !compare() ) {
std::cerr << "Error in History: operations are not the same as in the reference journal\n";
flushJournal("journal_for_diff");
exit(1);
}
}
// -------------------------------------------------------------------
bool History::compare()
{
if ( !open ) return false;
if ( refJournal.empty() || refCount >= (int) refJournal.size() ) return true;
for (; refCount < (int) journal.size(); refCount++) {
if ( journal[refCount] != refJournal[refCount] ) return false;
}
return true;
}
// -------------------------------------------------------------------
void History::loadJournal(std::string name)
{
FILE * f = fopen(name.c_str(),"r");
if ( !f ) {
std::cerr << "Error: could not open file " << name << std::endl; throw;
}
int opType;
int actType;
while ( fscanf(f,"%d %d",&opType,&actType) != EOF ) {
std::vector<int> intParams;
int par;
for (int iPar=0; iPar<actionIntParameters[actType]; iPar++) {
fscanf(f,"%d",&par);
intParams.push_back(par);
}
action act;
act.opType = opType;
act.actType = (actionType) actType;
act.intParams = intParams;
refJournal.push_back(act);
}
fclose(f);
refCount = 0;
}
// -------------------------------------------------------------------
void History::flushJournal(ostream& out) const
{
for (int iAct=0; iAct < (int) journal.size(); iAct++) {
action act = journal[iAct];
out << act.opType << "\t" << act.actType;
for (int iP=0; iP<actionIntParameters[act.actType]; iP++) {
out << "\t"<< act.intParams[iP];
}
out << "\n";
}
}
// -------------------------------------------------------------------
void History::flushJournal(std::string name) const
{
std::ofstream fileOut(name.c_str());
flushJournal(fileOut);
}
// -------------------------------------------------------------------
}
|