From bb719537889be85b9ab32a431a273f7b1f67e750 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 11 Jan 2019 01:10:37 -0800 Subject: [PATCH] Convert windows EOL to unix EOL in Python uploads (#48) --- .../tools/rpiConfigServer_src/Application.cpp | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/deps/tools/rpiConfigServer_src/Application.cpp b/deps/tools/rpiConfigServer_src/Application.cpp index fd300d8..88b584e 100644 --- a/deps/tools/rpiConfigServer_src/Application.cpp +++ b/deps/tools/rpiConfigServer_src/Application.cpp @@ -87,12 +87,14 @@ void Application::Set(wpi::StringRef appType, void Application::Upload(wpi::ArrayRef contents, std::function onFail) { wpi::StringRef filename; + bool text = false; if (m_appType == "upload-java") { filename = "/uploaded.jar"; } else if (m_appType == "upload-cpp") { filename = "/uploaded"; } else if (m_appType == "upload-python") { filename = "/uploaded.py"; + text = true; } else { wpi::SmallString<64> msg; msg = "cannot upload application type '"; @@ -137,7 +139,23 @@ void Application::Upload(wpi::ArrayRef contents, } // write contents and close file - wpi::raw_fd_ostream(fd, true) << contents; + wpi::raw_fd_ostream out(fd, true); + if (text) { + wpi::StringRef str(reinterpret_cast(contents.data()), + contents.size()); + // convert any Windows EOL to Unix + for (;;) { + size_t idx = str.find("\r\n"); + if (idx == wpi::StringRef::npos) break; + out << str.slice(0, idx) << '\n'; + str = str.slice(idx + 2, wpi::StringRef::npos); + } + out << str; + // ensure file ends with EOL + if (!str.empty() && str.back() != '\n') out << '\n'; + } else { + out << contents; + } } // terminate vision process so it reloads