From bf5623224c80a5ad9b2a29c7f3af3155b9cbe006 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sun, 25 May 2014 00:34:11 -0700 Subject: [PATCH] Add plPrintf - a cstdio-directed version of plFormat --- Sources/Plasma/Apps/plFileEncrypt/main.cpp | 6 +-- Sources/Plasma/Apps/plFileSecure/main.cpp | 2 +- Sources/Plasma/Apps/plPageOptimizer/main.cpp | 2 +- Sources/Plasma/Apps/plPythonPack/main.cpp | 42 ++++++++++---------- Sources/Plasma/CoreLib/plFormat.h | 13 ++++++ 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Sources/Plasma/Apps/plFileEncrypt/main.cpp b/Sources/Plasma/Apps/plFileEncrypt/main.cpp index 502119f2..3e0885be 100644 --- a/Sources/Plasma/Apps/plFileEncrypt/main.cpp +++ b/Sources/Plasma/Apps/plFileEncrypt/main.cpp @@ -108,12 +108,12 @@ void EncryptFiles(const plFileName& dir, const char* ext, bool encrypt) { if (encrypt) { - printf("encrypting: %s\n", iter->GetFileName().c_str()); + plPrintf("encrypting: {}\n", iter->GetFileName()); plEncryptedStream::FileEncrypt(*iter); } else - { - printf("decrypting: %s\n", iter->GetFileName().c_str()); + { + plPrintf("decrypting: {}\n", iter->GetFileName()); plEncryptedStream::FileDecrypt(*iter); } } diff --git a/Sources/Plasma/Apps/plFileSecure/main.cpp b/Sources/Plasma/Apps/plFileSecure/main.cpp index 5210bae4..ee69b5aa 100644 --- a/Sources/Plasma/Apps/plFileSecure/main.cpp +++ b/Sources/Plasma/Apps/plFileSecure/main.cpp @@ -110,7 +110,7 @@ void SecureFiles(const plFileName& dir, const plString& ext, uint32_t* key) std::vector files = plFileSystem::ListDir(dir, ext.c_str()); for (auto iter = files.begin(); iter != files.end(); ++iter) { - printf("securing: %s\n", iter->GetFileName().c_str()); + plPrintf("securing: {}\n", iter->GetFileName()); plSecureStream::FileEncrypt(*iter, key); } } diff --git a/Sources/Plasma/Apps/plPageOptimizer/main.cpp b/Sources/Plasma/Apps/plPageOptimizer/main.cpp index 4aa284b4..11d794d2 100644 --- a/Sources/Plasma/Apps/plPageOptimizer/main.cpp +++ b/Sources/Plasma/Apps/plPageOptimizer/main.cpp @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) } plFileName filename = argv[1]; - printf("Optimizing %s...", filename.GetFileName().c_str()); + plPrintf("Optimizing {}...", filename); #ifndef _DEBUG try { diff --git a/Sources/Plasma/Apps/plPythonPack/main.cpp b/Sources/Plasma/Apps/plPythonPack/main.cpp index eede46de..9a836ab2 100644 --- a/Sources/Plasma/Apps/plPythonPack/main.cpp +++ b/Sources/Plasma/Apps/plPythonPack/main.cpp @@ -84,11 +84,11 @@ void WritePythonFile(const plFileName &fileName, const plFileName &path, hsStrea if (!pyStream.Open(filePath) || !glueStream.Open(glueFile)) { - printf("Unable to open path %s, ", filePath.AsString().c_str()); + plPrintf("Unable to open path {}, ", filePath); return; } - printf("==Packing %s, ", fileName.AsString().c_str()); + plPrintf("==Packing {}, ", fileName); pyStream.FastFwd(); uint32_t pyFileSize = pyStream.GetPosition(); @@ -168,20 +168,20 @@ void WritePythonFile(const plFileName &fileName, const plFileName &path, hsStrea } pythonCode = PythonInterface::CompileString(code, fileName); hsAssert(pythonCode,"Not sure why this didn't compile the second time???"); - printf("an import file "); + fputs("an import file ", stdout); } else - printf("a PythonFile modifier(tm) "); + fputs("a PythonFile modifier(tm) ", stdout); } else { - printf("......blast! Error during run-code!\n"); + fputs("......blast! Error during run-code!\n", stdout); char* errmsg; int chars_read = PythonInterface::getOutputAndReset(&errmsg); if (chars_read > 0) { - printf("%s\n", errmsg); + plPrintf("{}\n", errmsg); } } } @@ -193,20 +193,20 @@ void WritePythonFile(const plFileName &fileName, const plFileName &path, hsStrea char* pycode; PythonInterface::DumpObject(pythonCode,&pycode,&size); - printf("\n"); + fputc('\n', stdout); // print any message after each module char* errmsg; int chars_read = PythonInterface::getOutputAndReset(&errmsg); if (chars_read > 0) { - printf("%s\n", errmsg); + plPrintf("{}\n", errmsg); } s->WriteLE32(size); s->Write(size, pycode); } else { - printf("......blast! Compile error!\n"); + fputs("......blast! Compile error!\n", stdout); s->WriteLE32(0); PyErr_Print(); @@ -216,7 +216,7 @@ void WritePythonFile(const plFileName &fileName, const plFileName &path, hsStrea int chars_read = PythonInterface::getOutputAndReset(&errmsg); if (chars_read > 0) { - printf("%s\n", errmsg); + plPrintf("{}\n", errmsg); } } @@ -353,16 +353,16 @@ void FindPackages(std::vector& fileNames, std::vector& p void PackDirectory(const plFileName& dir, const plFileName& rootPath, const plFileName& pakName, std::vector& extraDirs, bool packSysAndPlasma = false) { - printf("\nCreating %s using the contents of %s\n", pakName.AsString().c_str(), dir.AsString().c_str()); - printf("Changing working directory to %s\n", rootPath.AsString().c_str()); + plPrintf("\nCreating {} using the contents of {}\n", pakName, dir); + plPrintf("Changing working directory to {}\n", rootPath); if (!plFileSystem::SetCWD(rootPath)) { - printf("ERROR: Directory change to %s failed for some reason\n", rootPath.AsString().c_str()); - printf("Unable to continue with the packing of this directory, aborting...\n"); + plPrintf("ERROR: Directory change to {} failed for some reason\n", rootPath); + fputs("Unable to continue with the packing of this directory, aborting...\n", stdout); return; } else - printf("Directory changed to %s\n", rootPath.AsString().c_str()); + plPrintf("Directory changed to {}\n", rootPath); std::vector fileNames; std::vector pathNames; @@ -371,7 +371,7 @@ void PackDirectory(const plFileName& dir, const plFileName& rootPath, const plFi FindPackages(fileNames, pathNames, dir); if (packSysAndPlasma) { - printf("Adding the system and plasma directories to this pack file\n"); + fputs("Adding the system and plasma directories to this pack file\n", stdout); plFileName tempPath; tempPath = plFileName::Join(dir, "system"); FindFiles(fileNames, pathNames, tempPath); @@ -431,15 +431,15 @@ void PackDirectory(const plFileName& dir, const plFileName& rootPath, const plFi void PrintUsage() { - printf("Usage:\n"); - printf("plPythonPack [directory to pack...]\n"); - printf("NOTE: the directory to pack must have full system and plasma dirs and\n"); - printf(" must be a relative path to the current working directory\n"); + fputs("Usage:\n", stdout); + fputs("plPythonPack [directory to pack...]\n", stdout); + fputs("NOTE: the directory to pack must have full system and plasma dirs and\n", stdout); + fputs(" must be a relative path to the current working directory\n", stdout); } int main(int argc, char *argv[]) { - printf("The Python Pack Utility\n"); + fputs("The Python Pack Utility\n", stdout); plFileName baseWorkingDir = plFileSystem::GetCWD(); diff --git a/Sources/Plasma/CoreLib/plFormat.h b/Sources/Plasma/CoreLib/plFormat.h index a3901b01..a7c52aba 100644 --- a/Sources/Plasma/CoreLib/plFormat.h +++ b/Sources/Plasma/CoreLib/plFormat.h @@ -251,4 +251,17 @@ plString plFormat(const char *fmt_str, _Type value, _Args... args) return plFormat_Private::_IFormat(data, args...); } +template +void plPrintf(FILE *fd, const char *fmt_str, _Type value, _Args... args) +{ + plString output = plFormat(fmt_str, value, args...); + fwrite(output.c_str(), sizeof(char), output.GetSize(), fd); +} + +template +void plPrintf(const char *fmt_str, _Type value, _Args... args) +{ + plPrintf(stdout, fmt_str, value, args...); +} + #endif // plFormat_Defined