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_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_LGE_BUFSIZ 8192 // Size of SQL buffer
#define ZM_NETWORK_BUFSIZ 32768 // Size of network buffer

View File

@ -20,6 +20,7 @@
#include "zm_group.h"
#include "zm_logger.h"
#include "zm_utils.h"
#include <cstring>
Group::Group() {
@ -41,23 +42,23 @@ Group::Group(const MYSQL_ROW &dbrow) {
Group::Group(unsigned int p_id) {
id = 0;
if ( p_id ) {
char sql[ZM_SQL_SML_BUFSIZ];
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);
if (p_id) {
std::string sql = stringtf("SELECT `Id`, `ParentId`, `Name` FROM `Group` WHERE `Id`=%u", p_id);
Debug(2, "Loading Group for %u using %s", p_id, sql.c_str());
zmDbRow dbrow;
if ( !dbrow.fetch(sql) ) {
if (!dbrow.fetch(sql.c_str())) {
Error("Unable to load group for id %u: %s", p_id, mysql_error(&dbconn));
} else {
unsigned int index = 0;
id = atoi(dbrow[index++]);
parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++;
strncpy(name, dbrow[index++], sizeof(name)-1);
parent_id = dbrow[index] ? atoi(dbrow[index]) : 0;
index++;
strncpy(name, dbrow[index++], sizeof(name) - 1);
Debug(1, "Loaded Group area %d '%s'", id, this->Name());
}
}
if ( ! id ) {
Debug(1,"No id passed to Group constructor.");
if (!id) {
Debug(1, "No id passed to Group constructor.");
strcpy(name, "Default");
}
}

View File

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

View File

@ -86,11 +86,10 @@ bool RtspThread::recvResponse(std::string &response) {
int RtspThread::requestPorts() {
if ( !smMinDataPort ) {
char sql[ZM_SQL_SML_BUFSIZ];
//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 position = 0;

View File

@ -21,17 +21,18 @@
#include "zm_db.h"
#include "zm_logger.h"
#include "zm_utils.h"
#include <cstring>
Storage::Storage() : id(0) {
Warning("Instantiating default Storage Object. Should not happen.");
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
snprintf(path, sizeof(path), "%s/%s",
staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str());
} else {
strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path)-1);
strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path) - 1);
}
scheme = MEDIUM;
scheme_str = "Medium";
@ -40,13 +41,13 @@ Storage::Storage() : id(0) {
Storage::Storage(MYSQL_ROW &dbrow) {
unsigned int index = 0;
id = atoi(dbrow[index++]);
strncpy(name, dbrow[index++], sizeof(name)-1);
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++]);
scheme_str = std::string(dbrow[index++]);
if ( scheme_str == "Deep" ) {
if (scheme_str == "Deep") {
scheme = DEEP;
} else if ( scheme_str == "Medium" ) {
} else if (scheme_str == "Medium") {
scheme = MEDIUM;
} else {
scheme = SHALLOW;
@ -55,24 +56,22 @@ Storage::Storage(MYSQL_ROW &dbrow) {
/* 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) {
if ( id ) {
char sql[ZM_SQL_SML_BUFSIZ];
snprintf(sql, sizeof(sql), "SELECT `Id`, `Name`, `Path`, `Type`, `Scheme` FROM `Storage` WHERE `Id`=%u", id);
Debug(2, "Loading Storage for %u using %s", id, sql);
if (id) {
std::string sql = stringtf("SELECT `Id`, `Name`, `Path`, `Type`, `Scheme` FROM `Storage` WHERE `Id`=%u", id);
Debug(2, "Loading Storage for %u using %s", id, sql.c_str());
zmDbRow dbrow;
if ( !dbrow.fetch(sql) ) {
if (!dbrow.fetch(sql.c_str())) {
Error("Unable to load storage area for id %d: %s", id, mysql_error(&dbconn));
} else {
unsigned int index = 0;
id = atoi(dbrow[index++]);
strncpy(name, dbrow[index++], sizeof(name)-1);
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++]);
scheme_str = std::string(dbrow[index++]);
if ( scheme_str == "Deep" ) {
if (scheme_str == "Deep") {
scheme = DEEP;
} else if ( scheme_str == "Medium" ) {
} else if (scheme_str == "Medium") {
scheme = MEDIUM;
} else {
scheme = SHALLOW;
@ -80,13 +79,13 @@ Storage::Storage(unsigned int p_id) : id(p_id) {
Debug(1, "Loaded Storage area %d '%s'", id, name);
}
}
if ( !id ) {
if ( staticConfig.DIR_EVENTS[0] != '/' ) {
if (!id) {
if (staticConfig.DIR_EVENTS[0] != '/') {
// not using an absolute path. Make it one by appending ZM_PATH_WEB
snprintf(path, sizeof(path), "%s/%s",
staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str());
} 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);
strcpy(name, "Default");

View File

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

View File

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