Convert more char array buffers to std::string

Remove now unused ZM_SQL_*SIZE defines
This commit is contained in:
Peter Keresztes Schmidt 2021-06-30 20:49:37 +02:00
parent fc15afefcf
commit c60b577aec
7 changed files with 71 additions and 80 deletions

View File

@ -38,10 +38,7 @@
#define ZM_SCALE_BASE 100 // The factor by which we bump up 'scale' to simulate FP #define ZM_SCALE_BASE 100 // The factor by which we bump up 'scale' to simulate FP
#define ZM_RATE_BASE 100 // The factor by which we bump up 'rate' to simulate FP #define ZM_RATE_BASE 100 // The factor by which we bump up 'rate' to simulate FP
#define ZM_SQL_BATCH_SIZE 50 // Limit the size of multi-row SQL statements
#define ZM_SQL_SML_BUFSIZ 256 // Size of SQL buffer
#define ZM_SQL_MED_BUFSIZ 1024 // Size of SQL buffer #define ZM_SQL_MED_BUFSIZ 1024 // Size of SQL buffer
#define ZM_SQL_LGE_BUFSIZ 8192 // Size of SQL buffer
#define ZM_NETWORK_BUFSIZ 32768 // Size of network buffer #define ZM_NETWORK_BUFSIZ 32768 // Size of network buffer

View File

@ -20,6 +20,7 @@
#include "zm_group.h" #include "zm_group.h"
#include "zm_logger.h" #include "zm_logger.h"
#include "zm_utils.h"
#include <cstring> #include <cstring>
Group::Group() { Group::Group() {
@ -39,27 +40,27 @@ Group::Group(const MYSQL_ROW &dbrow) {
/* If a zero or invalid p_id is passed, then the old default path will be assumed. */ /* If a zero or invalid p_id is passed, then the old default path will be assumed. */
Group::Group(unsigned int p_id) { Group::Group(unsigned int p_id) {
id = 0; id = 0;
if ( p_id ) { if (p_id) {
char sql[ZM_SQL_SML_BUFSIZ]; std::string sql = stringtf("SELECT `Id`, `ParentId`, `Name` FROM `Group` WHERE `Id`=%u", p_id);
snprintf(sql, sizeof(sql), "SELECT `Id`, `ParentId`, `Name` FROM `Group` WHERE `Id`=%u", p_id); Debug(2, "Loading Group for %u using %s", p_id, sql.c_str());
Debug(2,"Loading Group for %u using %s", p_id, sql); zmDbRow dbrow;
zmDbRow dbrow; if (!dbrow.fetch(sql.c_str())) {
if ( !dbrow.fetch(sql) ) { Error("Unable to load group for id %u: %s", p_id, mysql_error(&dbconn));
Error("Unable to load group for id %u: %s", p_id, mysql_error(&dbconn)); } else {
} else { unsigned int index = 0;
unsigned int index = 0; id = atoi(dbrow[index++]);
id = atoi(dbrow[index++]); parent_id = dbrow[index] ? atoi(dbrow[index]) : 0;
parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++; index++;
strncpy(name, dbrow[index++], sizeof(name)-1); strncpy(name, dbrow[index++], sizeof(name) - 1);
Debug(1, "Loaded Group area %d '%s'", id, this->Name()); Debug(1, "Loaded Group area %d '%s'", id, this->Name());
} }
} }
if ( ! id ) { if (!id) {
Debug(1,"No id passed to Group constructor."); Debug(1, "No id passed to Group constructor.");
strcpy(name, "Default"); strcpy(name, "Default");
} }
} }
Group::~Group() { Group::~Group() {

View File

@ -1287,17 +1287,15 @@ void Monitor::actionReload() {
void Monitor::actionEnable() { void Monitor::actionEnable() {
shared_data->action |= RELOAD; shared_data->action |= RELOAD;
char sql[ZM_SQL_SML_BUFSIZ]; std::string sql = stringtf("UPDATE `Monitors` SET `Enabled` = 1 WHERE `Id` = %u", id);
snprintf(sql, sizeof(sql), "UPDATE `Monitors` SET `Enabled` = 1 WHERE `Id` = %u", id); zmDbDo(sql.c_str());
zmDbDo(sql);
} }
void Monitor::actionDisable() { void Monitor::actionDisable() {
shared_data->action |= RELOAD; shared_data->action |= RELOAD;
char sql[ZM_SQL_SML_BUFSIZ]; std::string sql = stringtf("UPDATE `Monitors` SET `Enabled` = 0 WHERE `Id` = %u", id);
snprintf(sql, sizeof(sql), "UPDATE `Monitors` SET `Enabled` = 0 WHERE `Id` = %u", id); zmDbDo(sql.c_str());
zmDbDo(sql);
} }
void Monitor::actionSuspend() { void Monitor::actionSuspend() {

View File

@ -86,11 +86,10 @@ bool RtspThread::recvResponse(std::string &response) {
int RtspThread::requestPorts() { int RtspThread::requestPorts() {
if ( !smMinDataPort ) { if ( !smMinDataPort ) {
char sql[ZM_SQL_SML_BUFSIZ];
//FIXME Why not load specifically by Id? This will get ineffeicient with a lot of monitors //FIXME Why not load specifically by Id? This will get ineffeicient with a lot of monitors
strncpy(sql, "SELECT `Id` FROM `Monitors` WHERE `Function` != 'None' AND `Type` = 'Remote' AND `Protocol` = 'rtsp' AND `Method` = 'rtpUni' ORDER BY `Id` ASC", sizeof(sql)); std::string sql = "SELECT `Id` FROM `Monitors` WHERE `Function` != 'None' AND `Type` = 'Remote' AND `Protocol` = 'rtsp' AND `Method` = 'rtpUni' ORDER BY `Id` ASC";
MYSQL_RES *result = zmDbFetch(sql); MYSQL_RES *result = zmDbFetch(sql.c_str());
int nMonitors = mysql_num_rows(result); int nMonitors = mysql_num_rows(result);
int position = 0; int position = 0;

View File

@ -21,32 +21,33 @@
#include "zm_db.h" #include "zm_db.h"
#include "zm_logger.h" #include "zm_logger.h"
#include "zm_utils.h"
#include <cstring> #include <cstring>
Storage::Storage() : id(0) { Storage::Storage() : id(0) {
Warning("Instantiating default Storage Object. Should not happen."); Warning("Instantiating default Storage Object. Should not happen.");
strcpy(name, "Default"); strcpy(name, "Default");
if ( staticConfig.DIR_EVENTS[0] != '/' ) { if (staticConfig.DIR_EVENTS[0] != '/') {
// not using an absolute path. Make it one by appending ZM_PATH_WEB // not using an absolute path. Make it one by appending ZM_PATH_WEB
snprintf(path, sizeof(path), "%s/%s", snprintf(path, sizeof(path), "%s/%s",
staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str()); staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str());
} else { } else {
strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path)-1); strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path) - 1);
} }
scheme = MEDIUM; scheme = MEDIUM;
scheme_str = "Medium"; scheme_str = "Medium";
} }
Storage::Storage(MYSQL_ROW &dbrow) { Storage::Storage(MYSQL_ROW &dbrow) {
unsigned int index = 0; unsigned int index = 0;
id = atoi(dbrow[index++]); id = atoi(dbrow[index++]);
strncpy(name, dbrow[index++], sizeof(name)-1); strncpy(name, dbrow[index++], sizeof(name) - 1);
strncpy(path, dbrow[index++], sizeof(path)-1); strncpy(path, dbrow[index++], sizeof(path) - 1);
type_str = std::string(dbrow[index++]); type_str = std::string(dbrow[index++]);
scheme_str = std::string(dbrow[index++]); scheme_str = std::string(dbrow[index++]);
if ( scheme_str == "Deep" ) { if (scheme_str == "Deep") {
scheme = DEEP; scheme = DEEP;
} else if ( scheme_str == "Medium" ) { } else if (scheme_str == "Medium") {
scheme = MEDIUM; scheme = MEDIUM;
} else { } else {
scheme = SHALLOW; scheme = SHALLOW;
@ -55,44 +56,42 @@ Storage::Storage(MYSQL_ROW &dbrow) {
/* If a zero or invalid p_id is passed, then the old default path will be assumed. */ /* If a zero or invalid p_id is passed, then the old default path will be assumed. */
Storage::Storage(unsigned int p_id) : id(p_id) { Storage::Storage(unsigned int p_id) : id(p_id) {
if (id) {
if ( id ) { std::string sql = stringtf("SELECT `Id`, `Name`, `Path`, `Type`, `Scheme` FROM `Storage` WHERE `Id`=%u", id);
char sql[ZM_SQL_SML_BUFSIZ]; Debug(2, "Loading Storage for %u using %s", id, sql.c_str());
snprintf(sql, sizeof(sql), "SELECT `Id`, `Name`, `Path`, `Type`, `Scheme` FROM `Storage` WHERE `Id`=%u", id); zmDbRow dbrow;
Debug(2, "Loading Storage for %u using %s", id, sql); if (!dbrow.fetch(sql.c_str())) {
zmDbRow dbrow; Error("Unable to load storage area for id %d: %s", id, mysql_error(&dbconn));
if ( !dbrow.fetch(sql) ) { } else {
Error("Unable to load storage area for id %d: %s", id, mysql_error(&dbconn)); unsigned int index = 0;
} else { id = atoi(dbrow[index++]);
unsigned int index = 0; strncpy(name, dbrow[index++], sizeof(name) - 1);
id = atoi(dbrow[index++]); strncpy(path, dbrow[index++], sizeof(path) - 1);
strncpy(name, dbrow[index++], sizeof(name)-1);
strncpy(path, dbrow[index++], sizeof(path)-1);
type_str = std::string(dbrow[index++]); type_str = std::string(dbrow[index++]);
scheme_str = std::string(dbrow[index++]); scheme_str = std::string(dbrow[index++]);
if ( scheme_str == "Deep" ) { if (scheme_str == "Deep") {
scheme = DEEP; scheme = DEEP;
} else if ( scheme_str == "Medium" ) { } else if (scheme_str == "Medium") {
scheme = MEDIUM; scheme = MEDIUM;
} else { } else {
scheme = SHALLOW; scheme = SHALLOW;
} }
Debug(1, "Loaded Storage area %d '%s'", id, name); Debug(1, "Loaded Storage area %d '%s'", id, name);
} }
} }
if ( !id ) { if (!id) {
if ( staticConfig.DIR_EVENTS[0] != '/' ) { if (staticConfig.DIR_EVENTS[0] != '/') {
// not using an absolute path. Make it one by appending ZM_PATH_WEB // not using an absolute path. Make it one by appending ZM_PATH_WEB
snprintf(path, sizeof(path), "%s/%s", snprintf(path, sizeof(path), "%s/%s",
staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str()); staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str());
} else { } else {
strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path)-1); strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path) - 1);
} }
Debug(1, "No id passed to Storage constructor. Using default path %s instead", path); Debug(1, "No id passed to Storage constructor. Using default path %s instead", path);
strcpy(name, "Default"); strcpy(name, "Default");
scheme = MEDIUM; scheme = MEDIUM;
scheme_str = "Medium"; scheme_str = "Medium";
} }
} }
Storage::~Storage() { Storage::~Storage() {

View File

@ -116,8 +116,7 @@ Zone::~Zone() {
} }
void Zone::RecordStats(const Event *event) { void Zone::RecordStats(const Event *event) {
static char sql[ZM_SQL_MED_BUFSIZ]; std::string sql = stringtf(
snprintf(sql, sizeof(sql),
"INSERT INTO Stats SET MonitorId=%d, ZoneId=%d, EventId=%" PRIu64 ", FrameId=%d, " "INSERT INTO Stats SET MonitorId=%d, ZoneId=%d, EventId=%" PRIu64 ", FrameId=%d, "
"PixelDiff=%d, AlarmPixels=%d, FilterPixels=%d, BlobPixels=%d, " "PixelDiff=%d, AlarmPixels=%d, FilterPixels=%d, BlobPixels=%d, "
"Blobs=%d, MinBlobSize=%d, MaxBlobSize=%d, " "Blobs=%d, MinBlobSize=%d, MaxBlobSize=%d, "
@ -136,7 +135,7 @@ void Zone::RecordStats(const Event *event) {
stats.alarm_box_.Hi().y_, stats.alarm_box_.Hi().y_,
stats.score_ stats.score_
); );
zmDbDo(sql); zmDbDo(sql.c_str());
} // end void Zone::RecordStats( const Event *event ) } // end void Zone::RecordStats( const Event *event )
bool Zone::CheckOverloadCount() { bool Zone::CheckOverloadCount() {

View File

@ -232,7 +232,6 @@ int main(int argc, char *argv[]) {
while (!zm_terminate) { while (!zm_terminate) {
result = 0; result = 0;
static char sql[ZM_SQL_SML_BUFSIZ];
for (const std::shared_ptr<Monitor> &monitor : monitors) { for (const std::shared_ptr<Monitor> &monitor : monitors) {
monitor->LoadCamera(); monitor->LoadCamera();
@ -244,11 +243,11 @@ int main(int argc, char *argv[]) {
monitor->SetStartupTime(now); monitor->SetStartupTime(now);
monitor->SetHeartbeatTime(now); monitor->SetHeartbeatTime(now);
snprintf(sql, sizeof(sql), std::string sql = stringtf(
"INSERT INTO Monitor_Status (MonitorId,Status,CaptureFPS,AnalysisFPS)" "INSERT INTO Monitor_Status (MonitorId,Status,CaptureFPS,AnalysisFPS)"
" VALUES (%u, 'Running',0,0) ON DUPLICATE KEY UPDATE Status='Running',CaptureFPS=0,AnalysisFPS=0", " VALUES (%u, 'Running',0,0) ON DUPLICATE KEY UPDATE Status='Running',CaptureFPS=0,AnalysisFPS=0",
monitor->Id()); monitor->Id());
zmDbDo(sql); zmDbDo(sql.c_str());
Seconds sleep_time = Seconds(0); Seconds sleep_time = Seconds(0);
while (monitor->PrimeCapture() <= 0) { while (monitor->PrimeCapture() <= 0) {
@ -273,10 +272,10 @@ int main(int argc, char *argv[]) {
break; break;
} }
snprintf(sql, sizeof(sql), sql = stringtf(
"INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'", "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'",
monitor->Id()); monitor->Id());
zmDbDo(sql); zmDbDo(sql.c_str());
} // end foreach monitor } // end foreach monitor
if (zm_terminate){ if (zm_terminate){
@ -360,11 +359,10 @@ int main(int argc, char *argv[]) {
} // end while ! zm_terminate outer connection loop } // end while ! zm_terminate outer connection loop
for (std::shared_ptr<Monitor> &monitor : monitors) { for (std::shared_ptr<Monitor> &monitor : monitors) {
static char sql[ZM_SQL_SML_BUFSIZ]; std::string sql = stringtf(
snprintf(sql, sizeof(sql),
"INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'NotRunning') ON DUPLICATE KEY UPDATE Status='NotRunning'", "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'NotRunning') ON DUPLICATE KEY UPDATE Status='NotRunning'",
monitor->Id()); monitor->Id());
zmDbDo(sql); zmDbDo(sql.c_str());
} }
Image::Deinitialise(); Image::Deinitialise();