Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nwagenbrenner committed Aug 15, 2024
1 parent 383805e commit 2ff1d41
Showing 1 changed file with 56 additions and 68 deletions.
124 changes: 56 additions & 68 deletions src/ninja/ninja_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,66 +74,61 @@ bool NinjaCheckVersions(char * mostrecentversion, char * localversion) {

}

char * NinjaQueryServerMessages(bool checkAbort) {
char * NinjaQueryServerMessages(bool checkAbort)
{
CPLSetConfigOption( "GDAL_HTTP_TIMEOUT", "5" );
const char* url = "https://ninjastorm.firelab.org/sqlitetest/messages.txt";
CPLHTTPResult *poResult = NULL;
try{
CPLHTTPResult *poResult = CPLHTTPFetch(url, NULL);
if( !poResult || poResult->nStatus != 0 || poResult->nDataLen == 0 )
{
CPLDebug( "NINJA", "Failed to reach the ninjastorm server." );
return NULL;
}
CPLSetConfigOption( "GDAL_HTTP_TIMEOUT", NULL );
CPLHTTPResult *poResult = CPLHTTPFetch(url, NULL);
if( !poResult || poResult->nStatus != 0 || poResult->nDataLen == 0 )
{
CPLDebug( "NINJA", "Failed to reach the ninjastorm server." );
return NULL;
}

if (poResult != NULL) {
const char* pszTextContent = reinterpret_cast<const char*>(poResult->pabyData);
std::vector<std::string> messages;
std::istringstream iss(pszTextContent);
std::string message;
const char* pszTextContent = reinterpret_cast<const char*>(poResult->pabyData);
std::vector<std::string> messages;
std::istringstream iss(pszTextContent);
std::string message;

// Read all lines into the vector
while (std::getline(iss, message)) {
messages.push_back(message);
}
// Read all lines into the vector
while (std::getline(iss, message)) {
messages.push_back(message);
}

// Process all lines except the last two
std::ostringstream oss;
if (checkAbort) {
for (size_t i = 0; i < messages.size(); ++i) {
if (i == messages.size()-1) { // check final line
oss << messages[i] << "\n";
}
}
// Process all lines except the last two
std::ostringstream oss;
if (checkAbort) {
for (size_t i = 0; i < messages.size(); ++i) {
if (i == messages.size()-1) { // check final line
oss << messages[i] << "\n";
}
else {
bool versionisuptodate = NinjaCheckVersions(const_cast<char*>(messages[1].c_str()), const_cast<char*>(NINJA_VERSION_STRING));
if (!versionisuptodate) {
oss << messages[0] << "\n";
oss << "You are using an outdated WindNinja version, please update to version: " << messages[1] << "\n" << "\n";
}

if (messages[4].empty() == false) {
for (size_t i = 3; i < messages.size() - 2; ++i) {
if (!messages[i].empty()) {
oss << messages[i] << "\n";
}
}
}
if (messages[4].empty() && versionisuptodate) {
return NULL;
}
}
else {
bool versionisuptodate = NinjaCheckVersions(const_cast<char*>(messages[1].c_str()), const_cast<char*>(NINJA_VERSION_STRING));
if (!versionisuptodate) {
oss << messages[0] << "\n";
oss << "You are using an outdated WindNinja version, please update to version: " << messages[1] << "\n" << "\n";
}
if (messages[4].empty() == false) {
for (size_t i = 3; i < messages.size() - 2; ++i) {
if (!messages[i].empty()) {
oss << messages[i] << "\n";
}
}

std::string resultingmessage = oss.str();
char* returnString = new char[resultingmessage.length() + 1];
std::strcpy(returnString, resultingmessage.c_str());
CPLHTTPDestroyResult(poResult);
return returnString;
}
if (messages[4].empty() && versionisuptodate) {
return NULL;
}
}
catch (std::exception& e) {
CPLDebug( "NINJA", "Failed to reach the ninjastorm server." );
}

std::string resultingmessage = oss.str();
char* returnString = new char[resultingmessage.length() + 1];
std::strcpy(returnString, resultingmessage.c_str());
CPLHTTPDestroyResult(poResult);
return returnString;

return NULL;
}

Expand Down Expand Up @@ -183,7 +178,6 @@ int NinjaInitialize(const char *pszGdalData, const char *pszWindNinjaData)
GDALAllRegister();
OGRRegisterAll();


if(!CPLCheckForFile(CPLStrdup(CPLFormFilename(CPLStrdup(pszGdalData), "gdalicon.png", NULL)), NULL))
{
CPLDebug("WINDNINJA", "Invalid path for GDAL_DATA: %s", pszGdalData);
Expand Down Expand Up @@ -246,7 +240,6 @@ int NinjaInitialize(const char* typeofrun)
CPLDebug("WINDNINJA", "Setting GDAL_DRIVER_PATH: %s", pszPlugins);

CPLSetConfigOption("GDAL_DRIVER_PATH", pszPlugins);

#endif /* defined(FIRELAB_PACKAGE) */

#if defined(NINJAFOAM) && defined(FIRELAB_PACKAGE)
Expand Down Expand Up @@ -309,27 +302,22 @@ int NinjaInitialize(const char* typeofrun)
const char *charStr = full.data();

CPLHTTPResult *poResult;
CPLSetConfigOption("GDAL_HTTP_UNSAFESSL", "YES");
char **papszOptions = NULL;

CPLSetConfigOption( "GDAL_HTTP_TIMEOUT", "5" );
// Fetch the URL with custom headers
try {
poResult = CPLHTTPFetch(charStr, papszOptions);
if( !poResult || poResult->nStatus != 0 || poResult->nDataLen == 0 )
{
CPLDebug( "NINJA", "Failed to reach the ninjastorm server." );
return 0;
}
else {
if (poResult) {
CPLHTTPDestroyResult(poResult);
}
}
}
catch (std::exception& e) {
poResult = CPLHTTPFetch(charStr, papszOptions);
CPLSetConfigOption( "GDAL_HTTP_TIMEOUT", NULL );
if( !poResult || poResult->nStatus != 0 || poResult->nDataLen == 0 )
{
CPLDebug( "NINJA", "Failed to reach the ninjastorm server." );
return 0;
}
else {
if (poResult) {
CPLHTTPDestroyResult(poResult);
}
}
}
#endif

Expand Down

0 comments on commit 2ff1d41

Please sign in to comment.