Skip to content

Commit

Permalink
Clean up file handling in main()s, and remove a leak
Browse files Browse the repository at this point in the history
  • Loading branch information
zrax committed Jun 5, 2023
1 parent 8bb8386 commit 46ea76c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
24 changes: 13 additions & 11 deletions pycdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <cstdarg>
#include <string>
#include <iostream>
#include <fstream>
#include "pyc_module.h"
#include "pyc_numeric.h"
#include "bytecode.h"
#include <fstream>

#ifdef WIN32
# define PATHSEP '\\'
Expand Down Expand Up @@ -265,20 +265,20 @@ int main(int argc, char* argv[])
bool marshalled = false;
const char* version = nullptr;
unsigned disasm_flags = 0;
std::ostream &pyc_output = std::cout;
std::ostream* pyc_output = &std::cout;
std::ofstream out_file;

for (int arg = 1; arg < argc; ++arg) {
if (strcmp(argv[arg], "-o") == 0) {
if (arg + 1 < argc) {
const char* filename = argv[++arg];

auto* outfile = new std::filebuf;
if(! outfile->open(filename, std::ios::out)) {
out_file.open(filename, std::ios_base::out);
if (out_file.fail()) {
fprintf(stderr, "Error opening file '%s' for writing\n",
argv[arg]);
filename);
return 1;
}
pyc_output.rdbuf(outfile);
pyc_output = &out_file;
} else {
fputs("Option '-o' requires a filename\n", stderr);
return 1;
Expand Down Expand Up @@ -327,7 +327,7 @@ int main(int argc, char* argv[])
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
return 1;
}
} else {
} else {
if (!version) {
fputs("Opening raw code objects requires a version to be specified\n", stderr);
return 1;
Expand All @@ -344,10 +344,12 @@ int main(int argc, char* argv[])
}
const char* dispname = strrchr(infile, PATHSEP);
dispname = (dispname == NULL) ? infile : dispname + 1;
formatted_print(pyc_output, "%s (Python %d.%d%s)\n", dispname, mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " -U" : "");
formatted_print(*pyc_output, "%s (Python %d.%d%s)\n", dispname,
mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " -U" : "");
try {
output_object(mod.code().try_cast<PycObject>(), &mod, 0, disasm_flags, pyc_output);
output_object(mod.code().try_cast<PycObject>(), &mod, 0, disasm_flags,
*pyc_output);
} catch (std::exception& ex) {
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
return 1;
Expand Down
20 changes: 11 additions & 9 deletions pycdc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ int main(int argc, char* argv[])
const char* infile = nullptr;
bool marshalled = false;
const char* version = nullptr;
std::ostream &pyc_output = std::cout;
std::ostream* pyc_output = &std::cout;
std::ofstream out_file;

for (int arg = 1; arg < argc; ++arg) {
if (strcmp(argv[arg], "-o") == 0) {
if (arg + 1 < argc) {
const char* filename = argv[++arg];
auto* outfile = new std::filebuf;
if(! outfile->open(filename, std::ios::out)) {
out_file.open(filename, std::ios_base::out);
if (out_file.fail()) {
fprintf(stderr, "Error opening file '%s' for writing\n",
argv[arg]);
filename);
return 1;
}
pyc_output.rdbuf(outfile);
pyc_output = &out_file;
} else {
fputs("Option '-o' requires a filename\n", stderr);
return 1;
Expand Down Expand Up @@ -88,11 +89,12 @@ int main(int argc, char* argv[])
}
const char* dispname = strrchr(infile, PATHSEP);
dispname = (dispname == NULL) ? infile : dispname + 1;
std::cout << "# Source Generated with Decompyle++\n";
formatted_print(pyc_output, "# File: %s (Python %d.%d%s)\n\n", dispname, mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : "");
*pyc_output << "# Source Generated with Decompyle++\n";
formatted_print(*pyc_output, "# File: %s (Python %d.%d%s)\n\n", dispname,
mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : "");
try {
decompyle(mod.code(), &mod, pyc_output);
decompyle(mod.code(), &mod, *pyc_output);
} catch (std::exception& ex) {
fprintf(stderr, "Error decompyling %s: %s\n", infile, ex.what());
return 1;
Expand Down

0 comments on commit 46ea76c

Please sign in to comment.