Clean up file handling in main()s, and remove a leak
This commit is contained in:
24
pycdas.cpp
24
pycdas.cpp
@@ -3,10 +3,10 @@
|
|||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include "pyc_module.h"
|
#include "pyc_module.h"
|
||||||
#include "pyc_numeric.h"
|
#include "pyc_numeric.h"
|
||||||
#include "bytecode.h"
|
#include "bytecode.h"
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
# define PATHSEP '\\'
|
# define PATHSEP '\\'
|
||||||
@@ -265,20 +265,20 @@ int main(int argc, char* argv[])
|
|||||||
bool marshalled = false;
|
bool marshalled = false;
|
||||||
const char* version = nullptr;
|
const char* version = nullptr;
|
||||||
unsigned disasm_flags = 0;
|
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) {
|
for (int arg = 1; arg < argc; ++arg) {
|
||||||
if (strcmp(argv[arg], "-o") == 0) {
|
if (strcmp(argv[arg], "-o") == 0) {
|
||||||
if (arg + 1 < argc) {
|
if (arg + 1 < argc) {
|
||||||
const char* filename = argv[++arg];
|
const char* filename = argv[++arg];
|
||||||
|
out_file.open(filename, std::ios_base::out);
|
||||||
auto* outfile = new std::filebuf;
|
if (out_file.fail()) {
|
||||||
if(! outfile->open(filename, std::ios::out)) {
|
|
||||||
fprintf(stderr, "Error opening file '%s' for writing\n",
|
fprintf(stderr, "Error opening file '%s' for writing\n",
|
||||||
argv[arg]);
|
filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
pyc_output.rdbuf(outfile);
|
pyc_output = &out_file;
|
||||||
} else {
|
} else {
|
||||||
fputs("Option '-o' requires a filename\n", stderr);
|
fputs("Option '-o' requires a filename\n", stderr);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -327,7 +327,7 @@ int main(int argc, char* argv[])
|
|||||||
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
|
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!version) {
|
if (!version) {
|
||||||
fputs("Opening raw code objects requires a version to be specified\n", stderr);
|
fputs("Opening raw code objects requires a version to be specified\n", stderr);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -344,10 +344,12 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
const char* dispname = strrchr(infile, PATHSEP);
|
const char* dispname = strrchr(infile, PATHSEP);
|
||||||
dispname = (dispname == NULL) ? infile : dispname + 1;
|
dispname = (dispname == NULL) ? infile : dispname + 1;
|
||||||
formatted_print(pyc_output, "%s (Python %d.%d%s)\n", dispname, mod.majorVer(), mod.minorVer(),
|
formatted_print(*pyc_output, "%s (Python %d.%d%s)\n", dispname,
|
||||||
(mod.majorVer() < 3 && mod.isUnicode()) ? " -U" : "");
|
mod.majorVer(), mod.minorVer(),
|
||||||
|
(mod.majorVer() < 3 && mod.isUnicode()) ? " -U" : "");
|
||||||
try {
|
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) {
|
} catch (std::exception& ex) {
|
||||||
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
|
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
|
||||||
return 1;
|
return 1;
|
||||||
|
20
pycdc.cpp
20
pycdc.cpp
@@ -14,19 +14,20 @@ int main(int argc, char* argv[])
|
|||||||
const char* infile = nullptr;
|
const char* infile = nullptr;
|
||||||
bool marshalled = false;
|
bool marshalled = false;
|
||||||
const char* version = nullptr;
|
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) {
|
for (int arg = 1; arg < argc; ++arg) {
|
||||||
if (strcmp(argv[arg], "-o") == 0) {
|
if (strcmp(argv[arg], "-o") == 0) {
|
||||||
if (arg + 1 < argc) {
|
if (arg + 1 < argc) {
|
||||||
const char* filename = argv[++arg];
|
const char* filename = argv[++arg];
|
||||||
auto* outfile = new std::filebuf;
|
out_file.open(filename, std::ios_base::out);
|
||||||
if(! outfile->open(filename, std::ios::out)) {
|
if (out_file.fail()) {
|
||||||
fprintf(stderr, "Error opening file '%s' for writing\n",
|
fprintf(stderr, "Error opening file '%s' for writing\n",
|
||||||
argv[arg]);
|
filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
pyc_output.rdbuf(outfile);
|
pyc_output = &out_file;
|
||||||
} else {
|
} else {
|
||||||
fputs("Option '-o' requires a filename\n", stderr);
|
fputs("Option '-o' requires a filename\n", stderr);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -88,11 +89,12 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
const char* dispname = strrchr(infile, PATHSEP);
|
const char* dispname = strrchr(infile, PATHSEP);
|
||||||
dispname = (dispname == NULL) ? infile : dispname + 1;
|
dispname = (dispname == NULL) ? infile : dispname + 1;
|
||||||
std::cout << "# Source Generated with Decompyle++\n";
|
*pyc_output << "# Source Generated with Decompyle++\n";
|
||||||
formatted_print(pyc_output, "# File: %s (Python %d.%d%s)\n\n", dispname, mod.majorVer(), mod.minorVer(),
|
formatted_print(*pyc_output, "# File: %s (Python %d.%d%s)\n\n", dispname,
|
||||||
(mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : "");
|
mod.majorVer(), mod.minorVer(),
|
||||||
|
(mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : "");
|
||||||
try {
|
try {
|
||||||
decompyle(mod.code(), &mod, pyc_output);
|
decompyle(mod.code(), &mod, *pyc_output);
|
||||||
} catch (std::exception& ex) {
|
} catch (std::exception& ex) {
|
||||||
fprintf(stderr, "Error decompyling %s: %s\n", infile, ex.what());
|
fprintf(stderr, "Error decompyling %s: %s\n", infile, ex.what());
|
||||||
return 1;
|
return 1;
|
||||||
|
Reference in New Issue
Block a user