Regexp: use streams instead of printf

This commit is contained in:
Olivier Teulière 2012-05-16 21:36:26 +02:00
parent 24a44dcd4f
commit c2290ab6cd
2 changed files with 36 additions and 41 deletions

View file

@ -21,11 +21,7 @@
#include "config.h"
#include <boost/format.hpp>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <fstream>
#include "dic.h"
#include "regexp.h"
@ -187,80 +183,78 @@ string regexpPrintLetter(char l)
#ifdef DEBUG_RE
void Node::printNode(FILE* f, int detail) const
void Node::printNode(ostream &out, int detail) const
{
switch (m_type)
{
case NODE_VAR:
fprintf(f, "%s", regexpPrintLetter(m_var).c_str());
out << regexpPrintLetter(m_var);
break;
case NODE_OR:
fprintf(f, "OR");
out << "OR";
break;
case NODE_AND:
fprintf(f, "AND");
out << "AND";
break;
case NODE_PLUS:
fprintf(f, "+");
out << "+";
break;
case NODE_STAR:
fprintf(f, "*");
out << "*";
break;
}
if (detail == 2)
{
fprintf(f, "\\n pos=%d\\n annul=%d\\n PP=0x%04x\\n DP=0x%04x",
m_position, m_annulable, m_PP, m_DP);
out << format("\\n pos=%1%\\n annul=%2%\\n PP=0x%3%\\n DP=0x%3%")
% m_position % m_annulable % m_PP % m_DP;
}
}
void Node::printNodesRec(FILE* f, int detail) const
void Node::printNodesRec(ostream &out, int detail) const
{
if (m_fg)
m_fg->printNodesRec(f, detail);
m_fg->printNodesRec(out, detail);
if (m_fd)
m_fd->printNodesRec(f, detail);
m_fd->printNodesRec(out, detail);
fprintf(f, "%d [ label=\"", m_number);
printNode(f, detail);
fprintf(f, "\"];\n");
out << m_number << " [ label=\"";
printNode(out, detail);
out << "\"];\n";
}
void Node::printEdgesRec(FILE *f) const
void Node::printEdgesRec(ostream &out) const
{
if (m_fg)
m_fg->printEdgesRec(f);
m_fg->printEdgesRec(out);
if (m_fd)
m_fd->printEdgesRec(f);
m_fd->printEdgesRec(out);
switch (m_type)
{
case NODE_OR:
fprintf(f, "%d -> %d;", m_number, m_fg->m_number);
fprintf(f, "%d -> %d;", m_number, m_fd->m_number);
out << format("%1% -> %2%;") % m_number % m_fg->m_number;
out << format("%1% -> %2%;") % m_number % m_fd->m_number;
break;
case NODE_AND:
fprintf(f, "%d -> %d;", m_number, m_fg->m_number);
fprintf(f, "%d -> %d;", m_number, m_fd->m_number);
out << format("%1% -> %2%;") % m_number % m_fg->m_number;
out << format("%1% -> %2%;") % m_number % m_fd->m_number;
break;
case NODE_PLUS:
case NODE_STAR:
fprintf(f, "%d -> %d;", m_number, m_fg->m_number);
out << format("%1% -> %2%;") % m_number % m_fg->m_number;
break;
}
}
void Node::printTreeDot(const string &iFileName, int detail) const
{
FILE *f = fopen(iFileName.c_str(), "w");
if (f == NULL)
return;
fprintf(f, "digraph %s {\n", iFileName.c_str());
printNodesRec(f, detail);
printEdgesRec(f);
fprintf(f, "fontsize=20;\n");
fprintf(f, "}\n");
fclose(f);
ofstream out(iFileName.c_str());
out << "digraph " << iFileName << " {\n";
printNodesRec(out, detail);
printEdgesRec(out);
out << "fontsize=20;\n";
out << "}\n";
out.close();
}
#endif

View file

@ -22,6 +22,8 @@
#define REGEXP_H_
#include <string>
#include <vector>
#include <iosfwd>
#define NODE_TOP 0
#define NODE_VAR 1
@ -31,6 +33,7 @@
#define NODE_PLUS 5
using std::string;
using std::vector;
class Node
{
@ -89,13 +92,13 @@ private:
#ifdef DEBUG_RE
/// Print the current node to file
void printNode(FILE* f, int detail) const;
void printNode(ostream &out, int detail) const;
/// Print recursively the current node and its subnodes to file
void printNodesRec(FILE *f, int detail) const;
void printNodesRec(ostream &out, int detail) const;
/// Print recursively the edges of the tree rooted at the current node
void printEdgesRec(FILE *f) const;
void printEdgesRec(ostream &out) const;
#endif
};
@ -142,8 +145,6 @@ struct searchRegExpLists
#define RE_LIST_USER_BEGIN 3
#define RE_LIST_USER_END 4
#include <cstdio>
string regexpPrintLetter(char l);
void regexp_print_PS(int PS[]);
void regexp_print_ptl(int ptl[]);