Use C++ streams over C style IO

This commit is contained in:
Perceval Wajsbürt
2023-06-02 00:36:58 +02:00
committed by Perceval Wajsbürt
parent 409f175827
commit c4c35fc531
10 changed files with 488 additions and 460 deletions

View File

@@ -1,4 +1,6 @@
#include <cstring>
#include <fstream>
#include <iostream>
#include "ASTree.h"
#ifdef WIN32
@@ -12,17 +14,19 @@ int main(int argc, char* argv[])
const char* infile = nullptr;
bool marshalled = false;
const char* version = nullptr;
std::ostream &pyc_output = std::cout;
for (int arg = 1; arg < argc; ++arg) {
if (strcmp(argv[arg], "-o") == 0) {
if (arg + 1 < argc) {
const char* filename = argv[++arg];
FILE* outfile = fopen(filename, "w");
if (!outfile) {
auto* outfile = new std::filebuf;
if(! outfile->open(filename, std::ios::out)) {
fprintf(stderr, "Error opening file '%s' for writing\n",
argv[arg]);
return 1;
}
pyc_output = outfile;
pyc_output.rdbuf(outfile);
} else {
fputs("Option '-o' requires a filename\n", stderr);
return 1;
@@ -84,11 +88,11 @@ int main(int argc, char* argv[])
}
const char* dispname = strrchr(infile, PATHSEP);
dispname = (dispname == NULL) ? infile : dispname + 1;
fputs("# Source Generated with Decompyle++\n", pyc_output);
fprintf(pyc_output, "# File: %s (Python %d.%d%s)\n\n", dispname, mod.majorVer(), mod.minorVer(),
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" : "");
try {
decompyle(mod.code(), &mod);
decompyle(mod.code(), &mod, pyc_output);
} catch (std::exception& ex) {
fprintf(stderr, "Error decompyling %s: %s\n", infile, ex.what());
return 1;