mirror of
https://github.com/holub/mame
synced 2025-04-20 07:22:04 +03:00
regrep.cpp: Code cleanup
- Eliminate use of sprintf to construct temporary strings - Pass string views instead of string references wherever convenient - Use util::path_concat for building paths
This commit is contained in:
parent
9dbf099b65
commit
3d532402e5
@ -8,7 +8,9 @@
|
||||
|
||||
#include "corefile.h"
|
||||
#include "corestr.h"
|
||||
#include "path.h"
|
||||
#include "png.h"
|
||||
#include "strformat.h"
|
||||
|
||||
#include "osdcomm.h"
|
||||
|
||||
@ -20,9 +22,6 @@
|
||||
#include <new>
|
||||
|
||||
|
||||
using util::string_format;
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
CONSTANTS & DEFINES
|
||||
***************************************************************************/
|
||||
@ -155,15 +154,15 @@ static int CLIB_DECL compare_file(const void *file0ptr, const void *file1ptr);
|
||||
static summary_file *sort_file_list(void);
|
||||
|
||||
/* HTML helpers */
|
||||
static util::core_file::ptr create_file_and_output_header(std::string &filename, std::string &templatefile, std::string &title);
|
||||
static void output_footer_and_close_file(util::write_stream::ptr &&file, std::string &templatefile, std::string &title);
|
||||
static util::core_file::ptr create_file_and_output_header(std::string_view filename, std::string_view templatefile, const std::string &title);
|
||||
static void output_footer_and_close_file(util::write_stream::ptr &&file, std::string_view templatefile, const std::string &title);
|
||||
|
||||
/* report generators */
|
||||
static void output_report(std::string &dirname, std::string &tempheader, std::string &tempfooter, summary_file *filelist);
|
||||
static void output_report(std::string_view dirname, std::string_view tempheader, std::string_view tempfooter, summary_file *filelist);
|
||||
static int compare_screenshots(summary_file *curfile);
|
||||
static int generate_png_diff(const summary_file *curfile, std::string &destdir, const char *destname);
|
||||
static void create_linked_file(std::string &dirname, const summary_file *curfile, const summary_file *prevfile, const summary_file *nextfile, const char *pngfile, std::string &tempheader, std::string &tempfooter);
|
||||
static void append_driver_list_table(const char *header, std::string &dirname, util::core_file &indexfile, const summary_file *listhead, std::string &tempheader, std::string &tempfooter);
|
||||
static int generate_png_diff(const summary_file *curfile, std::string_view destdir, std::string_view destname);
|
||||
static void create_linked_file(std::string_view dirname, const summary_file *curfile, const summary_file *prevfile, const summary_file *nextfile, std::string_view pngfile, std::string_view tempheader, std::string_view tempfooter);
|
||||
static void append_driver_list_table(const char *header, std::string_view dirname, util::core_file &indexfile, const summary_file *listhead, std::string_view tempheader, std::string_view tempfooter);
|
||||
|
||||
|
||||
|
||||
@ -242,7 +241,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* read the template file into an astring */
|
||||
std::string tempheader;
|
||||
if (!util::core_file::load(tempfilename.c_str(), &buffer, bufsize))
|
||||
if (!util::core_file::load(tempfilename, &buffer, bufsize))
|
||||
{
|
||||
tempheader.assign((const char *)buffer, bufsize);
|
||||
free(buffer);
|
||||
@ -567,7 +566,7 @@ static summary_file *sort_file_list(void)
|
||||
HTML file with a standard header
|
||||
-------------------------------------------------*/
|
||||
|
||||
static util::core_file::ptr create_file_and_output_header(std::string &filename, std::string &templatefile, std::string &title)
|
||||
static util::core_file::ptr create_file_and_output_header(std::string_view filename, std::string_view templatefile, const std::string &title)
|
||||
{
|
||||
util::core_file::ptr file;
|
||||
|
||||
@ -577,7 +576,7 @@ static util::core_file::ptr create_file_and_output_header(std::string &filename,
|
||||
|
||||
/* print a header */
|
||||
std::string modified(templatefile);
|
||||
strreplace(modified, "<!--TITLE-->", title.c_str());
|
||||
strreplace(modified, "<!--TITLE-->", title);
|
||||
std::size_t written;
|
||||
file->write(modified.c_str(), modified.length(), written); // FIXME: check for errors
|
||||
|
||||
@ -591,10 +590,10 @@ static util::core_file::ptr create_file_and_output_header(std::string &filename,
|
||||
standard footer to an HTML file and close it
|
||||
-------------------------------------------------*/
|
||||
|
||||
static void output_footer_and_close_file(util::write_stream::ptr &&file, std::string &templatefile, std::string &title)
|
||||
static void output_footer_and_close_file(util::write_stream::ptr &&file, std::string_view templatefile, const std::string &title)
|
||||
{
|
||||
std::string modified(templatefile);
|
||||
strreplace(modified, "<!--TITLE-->", title.c_str());
|
||||
strreplace(modified, "<!--TITLE-->", title);
|
||||
std::size_t written;
|
||||
file->write(modified.c_str(), modified.length(), written); // FIXME: check for errors
|
||||
file.reset();
|
||||
@ -611,7 +610,7 @@ static void output_footer_and_close_file(util::write_stream::ptr &&file, std::st
|
||||
report HTML files
|
||||
-------------------------------------------------*/
|
||||
|
||||
static void output_report(std::string &dirname, std::string &tempheader, std::string &tempfooter, summary_file *filelist)
|
||||
static void output_report(std::string_view dirname, std::string_view tempheader, std::string_view tempfooter, summary_file *filelist)
|
||||
{
|
||||
summary_file *buckethead[BUCKET_COUNT], **buckettailptr[BUCKET_COUNT];
|
||||
summary_file *curfile;
|
||||
@ -694,7 +693,7 @@ static void output_report(std::string &dirname, std::string &tempheader, std::st
|
||||
*buckettailptr[bucknum] = nullptr;
|
||||
|
||||
/* output header */
|
||||
std::string tempname = string_format("%s" PATH_SEPARATOR "%s", dirname.c_str(), "index.html");
|
||||
std::string tempname = util::path_concat(dirname, "index.html");
|
||||
indexfile = create_file_and_output_header(tempname, tempheader, title);
|
||||
if (!indexfile)
|
||||
{
|
||||
@ -739,7 +738,7 @@ static int compare_screenshots(summary_file *curfile)
|
||||
util::core_file::ptr file;
|
||||
|
||||
/* get the filename for the image */
|
||||
fullname = string_format("%s" PATH_SEPARATOR "snap" PATH_SEPARATOR "%s" PATH_SEPARATOR "final.png", lists[listnum].dir, curfile->name);
|
||||
fullname = util::path_concat(lists[listnum].dir, "snap", curfile->name, "final.png");
|
||||
|
||||
/* open the file */
|
||||
filerr = util::core_file::open(fullname, OPEN_FLAG_READ, file);
|
||||
@ -748,7 +747,7 @@ static int compare_screenshots(summary_file *curfile)
|
||||
if (filerr)
|
||||
{
|
||||
/* get the filename for the image */
|
||||
fullname = string_format("%s" PATH_SEPARATOR "snap" PATH_SEPARATOR "_%s.png", lists[listnum].dir, curfile->name);
|
||||
fullname = util::path_concat(lists[listnum].dir, "snap", util::string_format("_%s.png", curfile->name));
|
||||
|
||||
/* open the file */
|
||||
filerr = util::core_file::open(fullname, OPEN_FLAG_READ, file);
|
||||
@ -834,7 +833,7 @@ static int compare_screenshots(summary_file *curfile)
|
||||
side with a third set of differences
|
||||
-------------------------------------------------*/
|
||||
|
||||
static int generate_png_diff(const summary_file *curfile, std::string &destdir, const char *destname)
|
||||
static int generate_png_diff(const summary_file *curfile, std::string_view destdir, std::string_view destname)
|
||||
{
|
||||
bitmap_argb32 bitmaps[MAX_COMPARES];
|
||||
std::string srcimgname;
|
||||
@ -848,14 +847,14 @@ static int generate_png_diff(const summary_file *curfile, std::string &destdir,
|
||||
int starty;
|
||||
|
||||
/* generate the common source filename */
|
||||
dstfilename = string_format("%s" PATH_SEPARATOR "%s", destdir.c_str(), destname);
|
||||
srcimgname = string_format("snap" PATH_SEPARATOR "%s" PATH_SEPARATOR "final.png", curfile->name);
|
||||
dstfilename = util::path_concat(destdir, destname);
|
||||
srcimgname = util::path_concat("snap", curfile->name, "final.png");
|
||||
|
||||
/* open and load all unique bitmaps */
|
||||
for (int listnum = 0; listnum < list_count; listnum++)
|
||||
if (curfile->matchbitmap[listnum] == listnum)
|
||||
{
|
||||
std::string tempname = string_format("%s" PATH_SEPARATOR "%s", lists[listnum].dir, srcimgname.c_str());
|
||||
std::string tempname = util::path_concat(lists[listnum].dir, srcimgname);
|
||||
|
||||
/* open the source image */
|
||||
filerr = util::core_file::open(tempname, OPEN_FLAG_READ, file);
|
||||
@ -952,7 +951,7 @@ error:
|
||||
file between differing versions
|
||||
-------------------------------------------------*/
|
||||
|
||||
static void create_linked_file(std::string &dirname, const summary_file *curfile, const summary_file *prevfile, const summary_file *nextfile, const char *pngfile, std::string &tempheader, std::string &tempfooter)
|
||||
static void create_linked_file(std::string_view dirname, const summary_file *curfile, const summary_file *prevfile, const summary_file *nextfile, std::string_view pngfile, std::string_view tempheader, std::string_view tempfooter)
|
||||
{
|
||||
std::string linkname;
|
||||
std::string filename;
|
||||
@ -961,11 +960,11 @@ static void create_linked_file(std::string &dirname, const summary_file *curfile
|
||||
int listnum;
|
||||
|
||||
/* create the filename */
|
||||
filename = string_format("%s.html", curfile->name);
|
||||
filename = util::string_format("%s.html", curfile->name);
|
||||
|
||||
/* output header */
|
||||
title = string_format("%s Regressions (%s)", curfile->name, curfile->source);
|
||||
linkname = string_format("%s" PATH_SEPARATOR "%s", dirname.c_str(), filename.c_str());
|
||||
title = util::string_format("%s Regressions (%s)", curfile->name, curfile->source);
|
||||
linkname = util::path_concat(dirname, filename);
|
||||
linkfile = create_file_and_output_header(linkname, tempheader, title);
|
||||
if (linkfile == nullptr)
|
||||
{
|
||||
@ -997,7 +996,7 @@ static void create_linked_file(std::string &dirname, const summary_file *curfile
|
||||
linkfile->printf("\n\t<h2>%s</h2>\n", lists[listnum].version);
|
||||
linkfile->printf("\t<p>\n");
|
||||
linkfile->printf("\t<b>Status:</b> %s\n", status_text[curfile->status[listnum]]);
|
||||
if (pngfile != nullptr)
|
||||
if (!pngfile.empty())
|
||||
imageindex = get_unique_index(curfile, listnum);
|
||||
if (imageindex != -1)
|
||||
linkfile->printf(" [%d]", imageindex);
|
||||
@ -1006,13 +1005,13 @@ static void create_linked_file(std::string &dirname, const summary_file *curfile
|
||||
{
|
||||
linkfile->printf("\t<p>\n");
|
||||
linkfile->printf("\t<b>Errors:</b>\n");
|
||||
linkfile->printf("\t<pre>%s</pre>\n", curfile->text[listnum].c_str());
|
||||
linkfile->printf("\t<pre>%s</pre>\n", curfile->text[listnum]);
|
||||
linkfile->printf("\t</p>\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* output link to the image */
|
||||
if (pngfile != nullptr)
|
||||
if (!pngfile.empty())
|
||||
{
|
||||
linkfile->printf("\n\t<h2>Screenshot Comparisons</h2>\n");
|
||||
linkfile->printf("\t<p>\n");
|
||||
@ -1030,7 +1029,7 @@ static void create_linked_file(std::string &dirname, const summary_file *curfile
|
||||
of drivers from a list to an HTML file
|
||||
-------------------------------------------------*/
|
||||
|
||||
static void append_driver_list_table(const char *header, std::string &dirname, util::core_file &indexfile, const summary_file *listhead, std::string &tempheader, std::string &tempfooter)
|
||||
static void append_driver_list_table(const char *header, std::string_view dirname, util::core_file &indexfile, const summary_file *listhead, std::string_view tempheader, std::string_view tempfooter)
|
||||
{
|
||||
const summary_file *curfile, *prevfile;
|
||||
int width = 100 / (2 + list_count);
|
||||
@ -1058,7 +1057,6 @@ static void append_driver_list_table(const char *header, std::string &dirname, u
|
||||
for (prevfile = nullptr, curfile = listhead; curfile != nullptr; prevfile = curfile, curfile = curfile->next)
|
||||
{
|
||||
int rowspan = 0, uniqueshots = 0;
|
||||
char pngdiffname[40];
|
||||
|
||||
/* if this is the first entry in this source file, count how many rows we need to span */
|
||||
if (prevfile == nullptr || strcmp(prevfile->source, curfile->source) != 0)
|
||||
@ -1072,19 +1070,19 @@ static void append_driver_list_table(const char *header, std::string &dirname, u
|
||||
}
|
||||
|
||||
/* create screenshots if necessary */
|
||||
pngdiffname[0] = 0;
|
||||
std::string pngdiffname;
|
||||
for (listnum = 0; listnum < list_count; listnum++)
|
||||
if (curfile->matchbitmap[listnum] == listnum)
|
||||
uniqueshots++;
|
||||
if (uniqueshots > 1)
|
||||
{
|
||||
sprintf(pngdiffname, "compare_%s.png", curfile->name);
|
||||
pngdiffname = util::string_format("compare_%s.png", curfile->name);
|
||||
if (generate_png_diff(curfile, dirname, pngdiffname) != 0)
|
||||
pngdiffname[0] = 0;
|
||||
pngdiffname.clear();
|
||||
}
|
||||
|
||||
/* create a linked file */
|
||||
create_linked_file(dirname, curfile, prevfile, curfile->next, (pngdiffname[0] == 0) ? nullptr : pngdiffname, tempheader, tempfooter);
|
||||
create_linked_file(dirname, curfile, prevfile, curfile->next, pngdiffname, tempheader, tempfooter);
|
||||
|
||||
/* create a row */
|
||||
indexfile.printf("\t\t<tr>\n\t\t\t");
|
||||
@ -1095,7 +1093,7 @@ static void append_driver_list_table(const char *header, std::string &dirname, u
|
||||
{
|
||||
int unique_index = -1;
|
||||
|
||||
if (pngdiffname[0] != 0)
|
||||
if (!pngdiffname.empty())
|
||||
unique_index = get_unique_index(curfile, listnum);
|
||||
if (unique_index != -1)
|
||||
indexfile.printf("<td><span style=\"%s\"> </span> %s [<a href=\"%s\" target=\"blank\">%d</a>]</td>", status_color[curfile->status[listnum]], status_text[curfile->status[listnum]], pngdiffname, unique_index);
|
||||
|
Loading…
Reference in New Issue
Block a user