From 548991ea3c4d6045dba7e73ff7d45bf215f8733e Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 26 Jun 2025 14:53:38 -0400 Subject: [PATCH] Move escape_json_string and remove_newlines to zm_utils --- src/zm_monitor_janus.cpp | 12 ------------ src/zm_monitor_rtsp2web.cpp | 24 +----------------------- src/zm_utils.cpp | 19 +++++++++++++++++++ src/zm_utils.h | 2 ++ 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/zm_monitor_janus.cpp b/src/zm_monitor_janus.cpp index 1ab63042d..329cc1077 100644 --- a/src/zm_monitor_janus.cpp +++ b/src/zm_monitor_janus.cpp @@ -23,7 +23,6 @@ #include "zm_time.h" #include -std::string escape_json_string(std::string input); Monitor::JanusManager::JanusManager(Monitor *parent_) : parent(parent_), @@ -348,14 +347,3 @@ int Monitor::JanusManager::get_janus_handle() { return 1; } //get_janus_handle -std::string escape_json_string( std::string input ) { - std::string tmp; - tmp = regex_replace(input, std::regex("\n"), "\\n"); - tmp = regex_replace(tmp, std::regex("\b"), "\\b"); - tmp = regex_replace(tmp, std::regex("\f"), "\\f"); - tmp = regex_replace(tmp, std::regex("\r"), "\\r"); - tmp = regex_replace(tmp, std::regex("\t"), "\\t"); - tmp = regex_replace(tmp, std::regex("\""), "\\\""); - tmp = regex_replace(tmp, std::regex("[\\\\]"), "\\\\"); - return tmp; -} diff --git a/src/zm_monitor_rtsp2web.cpp b/src/zm_monitor_rtsp2web.cpp index 8b1e8d8ef..b48eccb2b 100644 --- a/src/zm_monitor_rtsp2web.cpp +++ b/src/zm_monitor_rtsp2web.cpp @@ -21,13 +21,11 @@ #include "zm_monitor.h" #include "zm_server.h" #include "zm_time.h" +#include "zm_utils.h" #include #include -std::string remove_newlines(std::string input); -std::string escape_json_string(std::string input); - Monitor::RTSP2WebManager::RTSP2WebManager(Monitor *parent_) : parent(parent_), RTSP2Web_Healthy(false) { @@ -187,23 +185,3 @@ size_t Monitor::RTSP2WebManager::WriteCallback(void *contents, size_t size, size ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; } - -std::string remove_newlines( std::string str ) { - while (!str.empty() && str.find("\n") != std::string::npos) - str.erase(std::remove(str.begin(), str.end(), '\n'), str.cend()); - return str; -} - -/* -std::string escape_json_string( std::string input ) { - std::string tmp; - tmp = regex_replace(input, std::regex("\n"), "\\n"); - tmp = regex_replace(tmp, std::regex("\b"), "\\b"); - tmp = regex_replace(tmp, std::regex("\f"), "\\f"); - tmp = regex_replace(tmp, std::regex("\r"), "\\r"); - tmp = regex_replace(tmp, std::regex("\t"), "\\t"); - tmp = regex_replace(tmp, std::regex("\""), "\\\""); - tmp = regex_replace(tmp, std::regex("[\\\\]"), "\\\\"); - return tmp; -} -*/ diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index 4b6cfb7eb..661fec566 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -26,6 +26,7 @@ #include #include #include /* Definition of AT_* constants */ +#include #include #include @@ -520,3 +521,21 @@ std::string remove_authentication(const std::string &url) { } return result; } + +std::string remove_newlines( std::string str ) { + while (!str.empty() && str.find("\n") != std::string::npos) + str.erase(std::remove(str.begin(), str.end(), '\n'), str.cend()); + return str; +} + +std::string escape_json_string( std::string input ) { + std::string tmp; + tmp = regex_replace(input, std::regex("\n"), "\\n"); + tmp = regex_replace(tmp, std::regex("\b"), "\\b"); + tmp = regex_replace(tmp, std::regex("\f"), "\\f"); + tmp = regex_replace(tmp, std::regex("\r"), "\\r"); + tmp = regex_replace(tmp, std::regex("\t"), "\\t"); + tmp = regex_replace(tmp, std::regex("\""), "\\\""); + tmp = regex_replace(tmp, std::regex("[\\\\]"), "\\\\"); + return tmp; +} diff --git a/src/zm_utils.h b/src/zm_utils.h index d90f0e87a..0b8568d62 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -43,6 +43,8 @@ typedef std::vector StringVector; +std::string escape_json_string(std::string input); +std::string remove_newlines(std::string input); std::string Trim(const std::string &str, const std::string &char_set); inline std::string TrimSpaces(const std::string &str) { return Trim(str, " \t"); } std::string ReplaceAll(std::string str, const std::string &old_value, const std::string &new_value);