From 5b80200417bf7422dc93131e677defa2383194a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tr=C3=A1vn=C3=AD=C4=8Dek?= <jan.travnicek@fit.cvut.cz> Date: Fri, 26 Nov 2021 19:00:42 +0100 Subject: [PATCH] tidy: replace C arrays with C++ constructs --- alib2cli/src/builtin/Dot.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/alib2cli/src/builtin/Dot.cpp b/alib2cli/src/builtin/Dot.cpp index 24c2b8c59c..20baf1d861 100644 --- a/alib2cli/src/builtin/Dot.cpp +++ b/alib2cli/src/builtin/Dot.cpp @@ -16,8 +16,8 @@ namespace cli::builtin { std::vector < std::string> Dot::allowedOutputTypes { "dot", "xdot", "ps", "pdf", "svg", "svgz", "fig", "png", "gif", "jpg", "jpeg", "json", "imap", "cmapx" }; void Dot::run ( const std::string & data, const std::string & outputType, const std::optional < std::string > & outputFile, bool runInBackground ) { - int fd [ 2 ]; - if ( pipe ( fd ) != 0 ) + std::array < int, 2 > fd; + if ( pipe ( fd.data ( ) ) != 0 ) throw exception::CommonException ( "Dot: Failed to initialize communication pipe." ); pid_t pid = fork ( ); @@ -31,14 +31,17 @@ void Dot::run ( const std::string & data, const std::string & outputType, const close ( fd [ 0 ] ); close ( fd [ 1 ] ); - const char* cmd [ 6 ] = { "dot", "-T", outputType.c_str ( ), nullptr }; + std::vector < const char * > cmd; + cmd.push_back ( "dot" ); + cmd.push_back ( "-T" ); + cmd.push_back ( outputType.c_str ( ) ); if ( outputFile.has_value ( ) ) { - cmd [ 3 ] = "-o"; - cmd [ 4 ] = outputFile->c_str ( ); - cmd [ 5 ] = nullptr; + cmd.push_back ( "-o" ); + cmd.push_back ( outputFile->c_str ( ) ); } + cmd.push_back ( nullptr ); - if ( execvp ( "dot", const_cast < char* const* > ( cmd ) ) == -1 ) // I do not like the const_cast but this is a real issue when mixing C with C++ + if ( execvp ( "dot", const_cast < char* const* > ( cmd.data ( ) ) ) == -1 ) // I do not like the const_cast but this is a real issue when mixing C with C++ throw exception::CommonException ( "Dot: Failed to spawn child process." ); } else { close ( fd [ 0 ] ); -- GitLab