dump_util: richer StdioFile type

This commit is contained in:
Justin Viiret 2017-06-26 14:25:44 +10:00 committed by Matthew Barr
parent 4edf1e4195
commit f762fb9af6
2 changed files with 39 additions and 35 deletions

View File

@ -1682,10 +1682,10 @@ void dumpComponentInfo(const RoseEngine *t, const string &base) {
static static
void dumpComponentInfoCsv(const RoseEngine *t, const string &base) { void dumpComponentInfoCsv(const RoseEngine *t, const string &base) {
auto f = openStdioFile(base + "/rose_components.csv", "w"); StdioFile f(base + "/rose_components.csv", "w");
fprintf(f.get(), "Index, Offset,Engine Type,States,Stream State," fprintf(f, "Index, Offset,Engine Type,States,Stream State,"
"Bytecode Size,Kind,Notes\n"); "Bytecode Size,Kind,Notes\n");
for (u32 i = 0; i < t->queueCount; i++) { for (u32 i = 0; i < t->queueCount; i++) {
const NfaInfo *nfa_info = getNfaInfoByQueue(t, i); const NfaInfo *nfa_info = getNfaInfoByQueue(t, i);
@ -1740,7 +1740,7 @@ void dumpComponentInfoCsv(const RoseEngine *t, const string &base) {
} }
} }
fprintf(f.get(), "%u,%zd,\"%s\",%u,%u,%u,%s,%s\n", i, fprintf(f, "%u,%zd,\"%s\",%u,%u,%u,%s,%s\n", i,
(const char *)n - (const char *)t, describe(*n).c_str(), (const char *)n - (const char *)t, describe(*n).c_str(),
n->nPositions, n->streamStateSize, n->length, n->nPositions, n->streamStateSize, n->length,
to_string(kind).c_str(), notes.str().c_str()); to_string(kind).c_str(), notes.str().c_str());
@ -1749,7 +1749,7 @@ void dumpComponentInfoCsv(const RoseEngine *t, const string &base) {
static static
void dumpExhaust(const RoseEngine *t, const string &base) { void dumpExhaust(const RoseEngine *t, const string &base) {
auto f = openStdioFile(base + "/rose_exhaust.csv", "w"); StdioFile f(base + "/rose_exhaust.csv", "w");
const NfaInfo *infos const NfaInfo *infos
= (const NfaInfo *)((const char *)t + t->nfaInfoOffset); = (const NfaInfo *)((const char *)t + t->nfaInfoOffset);
@ -1759,7 +1759,7 @@ void dumpExhaust(const RoseEngine *t, const string &base) {
for (u32 i = 0; i < queue_count; ++i) { for (u32 i = 0; i < queue_count; ++i) {
u32 ekey_offset = infos[i].ekeyListOffset; u32 ekey_offset = infos[i].ekeyListOffset;
fprintf(f.get(), "%u (%u):", i, ekey_offset); fprintf(f, "%u (%u):", i, ekey_offset);
if (ekey_offset) { if (ekey_offset) {
const u32 *ekeys = (const u32 *)((const char *)t + ekey_offset); const u32 *ekeys = (const u32 *)((const char *)t + ekey_offset);
@ -1769,11 +1769,11 @@ void dumpExhaust(const RoseEngine *t, const string &base) {
if (e == ~0U) { if (e == ~0U) {
break; break;
} }
fprintf(f.get(), " %u", e); fprintf(f, " %u", e);
} }
} }
fprintf(f.get(), "\n"); fprintf(f, "\n");
} }
} }
@ -1792,8 +1792,8 @@ void dumpNfas(const RoseEngine *t, bool dump_raw, const string &base) {
if (dump_raw) { if (dump_raw) {
stringstream ssraw; stringstream ssraw;
ssraw << base << "rose_nfa_" << i << ".raw"; ssraw << base << "rose_nfa_" << i << ".raw";
auto f = openStdioFile(ssraw.str(), "w"); StdioFile f(ssraw.str(), "w");
fwrite(n, 1, n->length, f.get()); fwrite(n, 1, n->length, f);
} }
} }
} }
@ -1841,8 +1841,8 @@ void dumpRevNfas(const RoseEngine *t, bool dump_raw, const string &base) {
if (dump_raw) { if (dump_raw) {
stringstream ssraw; stringstream ssraw;
ssraw << base << "som_rev_nfa_" << i << ".raw"; ssraw << base << "som_rev_nfa_" << i << ".raw";
auto f = openStdioFile(ssraw.str(), "w"); StdioFile f(ssraw.str(), "w");
fwrite(n, 1, n->length, f.get()); fwrite(n, 1, n->length, f);
} }
} }
} }
@ -2197,23 +2197,23 @@ void roseDumpPrograms(const vector<LitFragment> &fragments, const RoseEngine *t,
static static
void roseDumpLiteralMatchers(const RoseEngine *t, const string &base) { void roseDumpLiteralMatchers(const RoseEngine *t, const string &base) {
if (const HWLM *ftable = getFloatingMatcher(t)) { if (const HWLM *ftable = getFloatingMatcher(t)) {
auto f = openStdioFile(base + "/lit_table_floating.txt", "w"); StdioFile f(base + "/lit_table_floating.txt", "w");
hwlmPrintStats(ftable, f.get()); hwlmPrintStats(ftable, f);
} }
if (const HWLM *drtable = getDelayRebuildMatcher(t)) { if (const HWLM *drtable = getDelayRebuildMatcher(t)) {
auto f = openStdioFile(base + "/lit_table_delay_rebuild.txt", "w"); StdioFile f(base + "/lit_table_delay_rebuild.txt", "w");
hwlmPrintStats(drtable, f.get()); hwlmPrintStats(drtable, f);
} }
if (const HWLM *etable = getEodMatcher(t)) { if (const HWLM *etable = getEodMatcher(t)) {
auto f = openStdioFile(base + "/lit_table_eod.txt", "w"); StdioFile f(base + "/lit_table_eod.txt", "w");
hwlmPrintStats(etable, f.get()); hwlmPrintStats(etable, f);
} }
if (const HWLM *sbtable = getSmallBlockMatcher(t)) { if (const HWLM *sbtable = getSmallBlockMatcher(t)) {
auto f = openStdioFile(base + "/lit_table_small_block.txt", "w"); StdioFile f(base + "/lit_table_small_block.txt", "w");
hwlmPrintStats(sbtable, f.get()); hwlmPrintStats(sbtable, f);
} }
} }
@ -2228,15 +2228,15 @@ void dumpRose(const RoseBuildImpl &build, const vector<LitFragment> &fragments,
return; return;
} }
auto f = openStdioFile(grey.dumpPath + "/rose.txt", "w"); StdioFile f(grey.dumpPath + "/rose.txt", "w");
if (!t) { if (!t) {
fprintf(f.get(), "<< no rose >>\n"); fprintf(f, "<< no rose >>\n");
return; return;
} }
// Dump Rose table info // Dump Rose table info
roseDumpText(t, f.get()); roseDumpText(t, f);
roseDumpComponents(t, false, grey.dumpPath); roseDumpComponents(t, false, grey.dumpPath);
roseDumpPrograms(fragments, t, grey.dumpPath); roseDumpPrograms(fragments, t, grey.dumpPath);
@ -2249,8 +2249,8 @@ void dumpRose(const RoseBuildImpl &build, const vector<LitFragment> &fragments,
// Literals // Literals
dumpRoseLiterals(build, fragments, grey); dumpRoseLiterals(build, fragments, grey);
f = openStdioFile(grey.dumpPath + "/rose_struct.txt", "w"); f = StdioFile(grey.dumpPath + "/rose_struct.txt", "w");
roseDumpStructRaw(t, f.get()); roseDumpStructRaw(t, f);
} }
} // namespace ue2 } // namespace ue2

View File

@ -29,6 +29,8 @@
#ifndef DUMP_UTIL #ifndef DUMP_UTIL
#define DUMP_UTIL #define DUMP_UTIL
#include "noncopyable.h"
#include <cstdio> #include <cstdio>
#include <memory> #include <memory>
#include <string> #include <string>
@ -41,18 +43,20 @@ namespace ue2 {
FILE *fopen_or_throw(const char *path, const char *mode); FILE *fopen_or_throw(const char *path, const char *mode);
/** /**
* \brief Helper function: returns a C stdio FILE* handle wrapped in * \brief Helper class: wraps C stdio FILE* handle and takes care of closing
* a unique_ptr that takes care of closing the file on destruction. * the file on destruction.
*
* If the file cannot be opened, throws an exception.
*/ */
inline class StdioFile : noncopyable {
std::unique_ptr<FILE, decltype(&fclose)> public:
openStdioFile(const std::string &filename, const char *mode) { StdioFile(const std::string &filename, const char *mode)
return std::unique_ptr<FILE, decltype(&fclose)>( : handle(fopen_or_throw(filename.c_str(), mode), &fclose) {}
fopen_or_throw(filename.c_str(), mode), &fclose);
}
// Implicit conversion to FILE* for use by stdio calls.
operator FILE *() { return handle.get(); }
private:
std::unique_ptr<FILE, decltype(&fclose)> handle;
};
} // namespace ue2 } // namespace ue2