From 73b44c94291c4d086477167a76fd7db239936c3b Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Sun, 6 Mar 2016 18:02:37 +1100 Subject: Turn core_file into a proper class that gets cleaned up safely using unique_ptr Subverted somewhat by chd_file class --- src/tools/src2html.cpp | 92 ++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 47 deletions(-) (limited to 'src/tools/src2html.cpp') diff --git a/src/tools/src2html.cpp b/src/tools/src2html.cpp index 557313f1b3e..e95cbe11764 100644 --- a/src/tools/src2html.cpp +++ b/src/tools/src2html.cpp @@ -206,12 +206,12 @@ static int recurse_dir(int srcrootlen, int dstrootlen, std::string &srcdir, std: static int output_file(file_type type, int srcrootlen, int dstrootlen, std::string &srcfile, std::string &dstfile, bool link_to_file, std::string &tempheader, std::string &tempfooter); // HTML helpers -static core_file *create_file_and_output_header(std::string &filename, std::string &templatefile, std::string &path); -static void output_footer_and_close_file(core_file *file, std::string &templatefile, std::string &path); +static util::core_file::ptr create_file_and_output_header(std::string &filename, std::string &templatefile, std::string &path); +static void output_footer_and_close_file(util::core_file::ptr &&file, std::string &templatefile, std::string &path); // path helpers static std::string &normalized_subpath(std::string &dest, std::string &path, int start); -static void output_path_as_links(core_file *file, std::string &path, bool end_is_directory, bool link_to_file); +static void output_path_as_links(util::core_file &file, std::string &path, bool end_is_directory, bool link_to_file); static bool find_include_file(std::string &srcincpath, int srcrootlen, int dstrootlen, std::string &srcfile, std::string &dstfile, std::string &filename); @@ -279,7 +279,7 @@ int main(int argc, char *argv[]) // read the template file into an std::string UINT32 bufsize; void *buffer; - if (core_fload(tempfilename.c_str(), &buffer, &bufsize) == FILERR_NONE) + if (util::core_file::load(tempfilename.c_str(), &buffer, bufsize) == FILERR_NONE) { tempheader.assign((const char *)buffer, bufsize); osd_free(buffer); @@ -333,12 +333,12 @@ static int recurse_dir(int srcrootlen, int dstrootlen, std::string &srcdir, std: // create an index file std::string indexname; indexname = string_format("%s%c%s", dstdir.c_str(), PATH_SEPARATOR[0], "index.html"); - core_file *indexfile = create_file_and_output_header(indexname, tempheader, srcdir_subpath); + util::core_file::ptr indexfile = create_file_and_output_header(indexname, tempheader, srcdir_subpath); // output the directory navigation - core_fprintf(indexfile, "

Viewing Directory: "); - output_path_as_links(indexfile, srcdir_subpath, true, false); - core_fprintf(indexfile, "

"); + indexfile->printf("

Viewing Directory: "); + output_path_as_links(*indexfile, srcdir_subpath, true, false); + indexfile->printf("

"); // iterate first over directories, then over files int result = 0; @@ -398,7 +398,7 @@ static int recurse_dir(int srcrootlen, int dstrootlen, std::string &srcdir, std: { // add a header if (curlist == list) - core_fprintf(indexfile, "\t

%s

\n\t\n"); // free all the allocated entries while (list != nullptr) @@ -451,7 +451,7 @@ static int recurse_dir(int srcrootlen, int dstrootlen, std::string &srcdir, std: } if (indexfile != nullptr) - output_footer_and_close_file(indexfile, tempfooter, srcdir_subpath); + output_footer_and_close_file(std::move(indexfile), tempfooter, srcdir_subpath); return result; } @@ -516,36 +516,35 @@ static int output_file(file_type type, int srcrootlen, int dstrootlen, std::stri is_token[(UINT8)token_chars[toknum]] = true; // open the source file - core_file *src; - if (core_fopen(srcfile.c_str(), OPEN_FLAG_READ, &src) != FILERR_NONE) + util::core_file::ptr src; + if (util::core_file::open(srcfile.c_str(), OPEN_FLAG_READ, src) != FILERR_NONE) { fprintf(stderr, "Unable to read file '%s'\n", srcfile.c_str()); return 1; } // open the output file - core_file *dst = create_file_and_output_header(dstfile, tempheader, srcfile_subpath); + util::core_file::ptr dst = create_file_and_output_header(dstfile, tempheader, srcfile_subpath); if (dst == nullptr) { fprintf(stderr, "Unable to write file '%s'\n", dstfile.c_str()); - core_fclose(src); return 1; } // output the directory navigation - core_fprintf(dst, "

Viewing File: "); - output_path_as_links(dst, srcfile_subpath, false, link_to_file); - core_fprintf(dst, "

"); + dst->printf("

Viewing File: "); + output_path_as_links(*dst, srcfile_subpath, false, link_to_file); + dst->printf("

"); // start with some tags - core_fprintf(dst, "\t
\n");
+	dst->printf("\t
\n");
 
 	// iterate over lines in the source file
 	int linenum = 1;
 	bool in_comment = false;
 	char srcline[4096];
 	std::ostringstream dstline;
-	while (core_fgets(srcline, ARRAY_LENGTH(srcline), src) != nullptr)
+	while (src->gets(srcline, ARRAY_LENGTH(srcline)) != nullptr)
 	{
 		// start with the line number
 		dstline.str("");
@@ -707,15 +706,14 @@ static int output_file(file_type type, int srcrootlen, int dstrootlen, std::stri
 
 		// append a break and move on
 		dstline.put('\n');
-		core_fputs(dst, dstline.str().c_str());
+		dst->puts(dstline.str().c_str());
 	}
 
 	// close tags
-	core_fprintf(dst, "\t
\n"); + dst->printf("\t
\n"); // close the file - output_footer_and_close_file(dst, tempfooter, srcfile_subpath); - core_fclose(src); + output_footer_and_close_file(std::move(dst), tempfooter, srcfile_subpath); return 0; } @@ -730,17 +728,17 @@ static int output_file(file_type type, int srcrootlen, int dstrootlen, std::stri HTML file with a standard header -------------------------------------------------*/ -static core_file *create_file_and_output_header(std::string &filename, std::string &templatefile, std::string &path) +static util::core_file::ptr create_file_and_output_header(std::string &filename, std::string &templatefile, std::string &path) { // create the indexfile - core_file *file; - if (core_fopen(filename.c_str(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS | OPEN_FLAG_NO_BOM, &file) != FILERR_NONE) - return nullptr; + util::core_file::ptr file; + if (util::core_file::open(filename.c_str(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS | OPEN_FLAG_NO_BOM, file) != FILERR_NONE) + return util::core_file::ptr(); // print a header std::string modified(templatefile); strreplace(modified, "", path.c_str()); - core_fwrite(file, modified.c_str(), modified.length()); + file->write(modified.c_str(), modified.length()); // return the file return file; @@ -752,12 +750,12 @@ static core_file *create_file_and_output_header(std::string &filename, std::stri standard footer to an HTML file and close it -------------------------------------------------*/ -static void output_footer_and_close_file(core_file *file, std::string &templatefile, std::string &path) +static void output_footer_and_close_file(util::core_file::ptr &&file, std::string &templatefile, std::string &path) { std::string modified(templatefile); strreplace(modified, "", path.c_str()); - core_fwrite(file, modified.c_str(), modified.length()); - core_fclose(file); + file->write(modified.c_str(), modified.length()); + file.reset(); } @@ -783,7 +781,7 @@ static std::string &normalized_subpath(std::string &dest, std::string &path, int series of links -------------------------------------------------*/ -static void output_path_as_links(core_file *file, std::string &path, bool end_is_directory, bool link_to_file) +static void output_path_as_links(util::core_file &file, std::string &path, bool end_is_directory, bool link_to_file) { // first count how deep we are int srcdepth = 0; @@ -793,10 +791,10 @@ static void output_path_as_links(core_file *file, std::string &path, bool end_is srcdepth++; // output a link to the root - core_fprintf(file, "<root>/"); + file.printf("../"); + file.printf("index.html\"><root>/"); // now output links to each path up the chain int curdepth = 0; @@ -806,10 +804,10 @@ static void output_path_as_links(core_file *file, std::string &path, bool end_is std::string substr(path, lastslash, slashindex - lastslash); curdepth++; - core_fprintf(file, "%s/", substr.c_str()); + file.printf("../"); + file.printf("index.html\">%s/", substr.c_str()); lastslash = slashindex + 1; } @@ -817,11 +815,11 @@ static void output_path_as_links(core_file *file, std::string &path, bool end_is // and a final link to the current directory std::string substr(path, lastslash, -1); if (end_is_directory) - core_fprintf(file, "%s", substr.c_str()); + file.printf("%s", substr.c_str()); else if (link_to_file) - core_fprintf(file, "%s", substr.c_str(), substr.c_str()); + file.printf("%s", substr.c_str(), substr.c_str()); else - core_fprintf(file, "%s", substr.c_str(), substr.c_str()); + file.printf("%s", substr.c_str(), substr.c_str()); } @@ -866,11 +864,11 @@ static bool find_include_file(std::string &srcincpath, int srcrootlen, int dstro srcincpath.append(PATH_SEPARATOR).append(filename.substr(lastsepindex, -1)); // see if we can open it - core_file *testfile; - if (core_fopen(srcincpath.c_str(), OPEN_FLAG_READ, &testfile) == FILERR_NONE) + util::core_file::ptr testfile; + if (util::core_file::open(srcincpath.c_str(), OPEN_FLAG_READ, testfile) == FILERR_NONE) { // close the file - core_fclose(testfile); + testfile.reset(); // find the longest matching directory substring between the include and source file lastsepindex = 0; -- cgit v1.2.3-70-g09d2