Automaton: use streams instead of printf

This commit is contained in:
Olivier Teulière 2012-05-16 21:13:22 +02:00
parent 50998079c2
commit 24a44dcd4f

View file

@ -23,9 +23,10 @@
#include <set> #include <set>
#include <list> #include <list>
#include <algorithm> #include <algorithm>
#include <fstream>
#include <sstream> #include <sstream>
#include <cstring> #include <cstring>
#include <cstdio> #include <boost/format.hpp>
#include "dic.h" #include "dic.h"
#include "regexp.h" #include "regexp.h"
@ -33,6 +34,7 @@
#include "debug.h" #include "debug.h"
using namespace std; using namespace std;
using boost::format;
INIT_LOGGER(dic, Automaton); INIT_LOGGER(dic, Automaton);
@ -119,8 +121,8 @@ private:
void addState(State * s); void addState(State * s);
State * getState(const set<uint64_t> &iId) const; State * getState(const set<uint64_t> &iId) const;
void printNodes(FILE* f) const; void printNodes(ostream &out) const;
void printEdges(FILE* f) const; void printEdges(ostream &out) const;
void setAccept(State * s) const; void setAccept(State * s) const;
set<uint64_t> getSuccessor(const set<uint64_t> &S, int letter, set<uint64_t> getSuccessor(const set<uint64_t> &S, int letter,
const searchRegExpLists &iList) const; const searchRegExpLists &iList) const;
@ -213,33 +215,32 @@ void Automaton::finalize(const AutomatonHelper &iHelper)
void Automaton::dump(const string &iFileName) const void Automaton::dump(const string &iFileName) const
{ {
FILE *f = fopen(iFileName.c_str(), "w"); ofstream out(iFileName.c_str());
fprintf(f, "digraph automaton {\n"); out << "digraph automaton {\n";
for (unsigned int i = 1; i <= m_nbStates; i++) for (unsigned int i = 1; i <= m_nbStates; i++)
{ {
fprintf(f, "\t%d [label = \"%d\"", i, i); out << format("\t%1% [label = \"%2%\"") % i % i;
if (i == m_init) if (i == m_init)
fprintf(f, ", style = filled, color=lightgrey"); out << ", style = filled, color=lightgrey";
if (accept(i)) if (accept(i))
fprintf(f, ", shape = doublecircle"); out << ", shape = doublecircle";
fprintf(f, "];\n"); out << "];\n";
} }
fprintf(f, "\n"); out << "\n";
for (unsigned int i = 1; i <= m_nbStates; i++) for (unsigned int i = 1; i <= m_nbStates; i++)
{ {
for (int l = 0; l < MAX_TRANSITION_LETTERS; l++) for (int l = 0; l < MAX_TRANSITION_LETTERS; l++)
{ {
if (m_transitions[i][l]) if (m_transitions[i][l])
{ {
fprintf(f, "\t%d -> %d [label = \"", i, m_transitions[i][l]); out << format("\t%1% -> %2%") % i % m_transitions[i][l];
fprintf(f, "%s", regexpPrintLetter(l).c_str()); out << format(" [label = \"%1%\"];\n") % regexpPrintLetter(l);
fprintf(f, "\"];\n");
} }
} }
} }
fprintf(f, "fontsize=20;\n"); out << "fontsize=20;\n";
fprintf(f, "}\n"); out << "}\n";
fclose(f); out.close();
} }
@ -490,29 +491,29 @@ static string idToString(const set<uint64_t> &iId)
} }
void AutomatonHelper::printNodes(FILE* f) const void AutomatonHelper::printNodes(ostream &out) const
{ {
list<State *>::const_iterator it; list<State *>::const_iterator it;
for (it = m_states.begin(); it != m_states.end(); it++) for (it = m_states.begin(); it != m_states.end(); it++)
{ {
State * s = *it; State * s = *it;
string sid = idToString(s->getId()); const string &sid = idToString(s->getId());
fprintf(f, "\t\"%s\" [label = \"%s\"", sid.c_str(), sid.c_str()); out << format("\t\"%1%\" [label = \"%2%\"") % sid % sid;
if (s == m_initState) if (s == m_initState)
{ {
fprintf(f, ", style = filled, color=lightgrey"); out << ", style = filled, color=lightgrey";
} }
if (s->m_accept) if (s->m_accept)
{ {
fprintf(f, ", shape = doublecircle"); out << ", shape = doublecircle";
} }
fprintf(f, "];\n"); out << "];\n";
} }
fprintf(f, "\n"); out << "\n";
} }
void AutomatonHelper::printEdges(FILE* f) const void AutomatonHelper::printEdges(ostream &out) const
{ {
list<State *>::const_iterator it; list<State *>::const_iterator it;
for (it = m_states.begin(); it != m_states.end(); it++) for (it = m_states.begin(); it != m_states.end(); it++)
@ -522,10 +523,10 @@ void AutomatonHelper::printEdges(FILE* f) const
{ {
if (s->m_next[letter]) if (s->m_next[letter])
{ {
fprintf(f, "\t\"%s\" -> ", idToString(s->getId()).c_str()); out << format("\t\"%1%\" -> ") % idToString(s->getId());
fprintf(f, "\"%s\" [label = \"", idToString(s->m_next[letter]->getId()).c_str()); out << format("\"%1%\" [label = \"") % idToString(s->m_next[letter]->getId());
fprintf(f, "%s", regexpPrintLetter(letter).c_str()); out << regexpPrintLetter(letter);
fprintf(f, "\"];\n"); out << "\"];\n";
} }
} }
} }
@ -535,13 +536,13 @@ void AutomatonHelper::printEdges(FILE* f) const
#ifdef DEBUG_AUTOMATON #ifdef DEBUG_AUTOMATON
void AutomatonHelper::dump(const string &iFileName) const void AutomatonHelper::dump(const string &iFileName) const
{ {
FILE *f = fopen(iFileName.c_str(), "w"); ofstream out(iFileName.c_str());
fprintf(f, "digraph automaton {\n"); out << "digraph automaton {\n";
printNodes(f); printNodes(out);
printEdges(f); printEdges(out);
fprintf(f, "fontsize=20;\n"); out << "fontsize=20;\n";
fprintf(f, "}\n"); out << "}\n";
fclose(f); out.close();
} }
#endif #endif