Skip to content

Commit 70a1a38

Browse files
committed
initial casefile -noinputs
1 parent adb465f commit 70a1a38

6 files changed

Lines changed: 308 additions & 79 deletions

File tree

src/gui/mainWindow.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include "mainWindow.h"
3131
#include <vector>
3232
#include <string>
33-
#include <iostream>
3433
#include <fstream>
34+
#include <iostream>
3535
#include "cpl_vsi.h"
3636
#include "cpl_error.h"
3737
#include "cpl_string.h"
@@ -1669,10 +1669,13 @@ void mainWindow::openOutputPath()
16691669

16701670
int mainWindow::solve()
16711671
{
1672+
CaseFile casefile;
1673+
std::string getdir = tree->solve->outputDirectory().toStdString();
1674+
std::string inputpath = getdir + "/config.cfg";
1675+
1676+
std::ofstream outFile(inputpath);
16721677

1673-
std::ofstream outFile("config.cfg");
16741678

1675-
// Check if the file was opened successfully
16761679
if (!outFile) {
16771680
std::cerr << "Error: Could not open the file for writing!" << std::endl;
16781681
return 1;
@@ -1688,6 +1691,8 @@ int mainWindow::solve()
16881691
//dem file
16891692
std::string demFile = inputFileName.toStdString();
16901693
outFile << "--elevation_file " + demFile;
1694+
1695+
16911696

16921697

16931698
#ifdef NINJAFOAM
@@ -1707,7 +1712,6 @@ int mainWindow::solve()
17071712
vegetation = WindNinjaInputs::trees;
17081713
outFile << "--vegetation " + vegetation;
17091714
}
1710-
17111715
//mesh
17121716
int meshIndex = tree->surface->meshResComboBox->currentIndex();
17131717
Mesh::eMeshChoice meshChoice;
@@ -1728,17 +1732,21 @@ int mainWindow::solve()
17281732
else
17291733
meshUnits = lengthUnits::meters;
17301734
}
1731-
outFile << "--meshchoice " + meshChoice;
1732-
outFile << "--mesh_resolution " + std::to_string(meshRes);
1733-
1734-
outFile << "--units_mesh_resolution " + meshUnits;
1735-
1736-
std::string zipFilePath = "example.zip";
17371735

1738-
outFile.close();
1736+
///outFile << "--meshchoice " + meshChoice;f
1737+
// outFile << "--mesh_resolution " + std::to_string(meshRes);
17391738

1740-
addFileToZip(zipFilePath, "config.cfg", "config.cfg");
1741-
1739+
//outFile << "--units_mesh_resolution " + meshUnits;
1740+
1741+
std::string getfileName = casefile.parse("file", inputFileName.toStdString());
1742+
std::string zipFilePath = getdir + "/" + getfileName + "-" + casefile.getTime() + ".ninja";
1743+
casefile.setdir(getdir);
1744+
casefile.setzip(zipFilePath);
1745+
outFile.close();
1746+
1747+
casefile.addFileToZip(zipFilePath, getdir, getfileName, inputFileName.toStdString());
1748+
casefile.addFileToZip(zipFilePath, getdir, "config.cfg", inputpath);
1749+
casefile.deleteFileFromPath(getdir, "config.cfg");
17421750

17431751
#ifdef NINJAFOAM
17441752
WindNinjaInputs::eNinjafoamMeshChoice ninjafoamMeshChoice;
@@ -1782,17 +1790,17 @@ int mainWindow::solve()
17821790
std::string timeZone = temp.toString().toStdString();
17831791

17841792

1785-
outFile << "--time_zone " + timeZone;
1793+
//outFile << "--time_zone " + timeZone;
17861794

17871795
//diurnal
17881796
bool useDiurnal = tree->diurnal->diurnalGroupBox->isChecked();
17891797

1790-
outFile << "--diurnal_winds " + useDiurnal;
1798+
// outFile << "--diurnal_winds " + useDiurnal;
17911799

17921800
//stability
17931801
bool useStability = tree->stability->stabilityGroupBox->isChecked();
17941802

1795-
outFile << "--non_neutral_stability " + useStability;
1803+
// outFile << "--non_neutral_stability " + useStability;
17961804
//initialization method
17971805
WindNinjaInputs::eInitializationMethod initMethod;
17981806
if( tree->wind->windGroupBox->isChecked() )

src/gui/mainWindow.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ class mainWindow : public QMainWindow
185185
bool getLatLon();
186186

187187
void test();
188-
void addFileToZip(const std::string& zipFilePath, const std::string& fileToAdd, const std::string& zipEntryName);
189188
int solve();
190189
void cancelSolve();
191190
int countRuns();

src/ninja/casefile.cpp

Lines changed: 199 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,219 @@
11
#include "casefile.h"
22

3-
class CaseFile {
4-
public:
5-
void addFileToZip(const std::string& zipFilePath, const std::string& fileToAdd, const std::string& zipEntryName) {
6-
VSILFILE *fin;
7-
fin = VSIFOpenL(fileToAdd.c_str(), "r");
8-
if (fin == NULL) {
9-
std::cerr << "Failed to open file: " << fileToAdd << std::endl;
10-
return;
11-
}
3+
std::string CaseFile::zipfilename = "";
4+
std::string CaseFile::directory = "";
125

13-
vsi_l_offset offset;
14-
VSIFSeekL(fin, 0, SEEK_END);
15-
offset = VSIFTellL(fin);
16-
VSIRewindL(fin);
6+
std::mutex zipMutex;
177

18-
char *data = (char*)CPLMalloc(offset * sizeof(char));
19-
if (data == NULL) {
20-
std::cerr << "Failed to allocate memory for file data." << std::endl;
21-
VSIFCloseL(fin);
22-
return;
23-
}
24-
if (VSIFReadL(data, 1, offset, fin) != offset) {
25-
std::cerr << "Failed to read file contents: " << fileToAdd << std::endl;
26-
CPLFree(data);
27-
VSIFCloseL(fin);
28-
return;
8+
CaseFile::CaseFile() {
9+
}
10+
11+
bool CaseFile::isCfgFile(const std::string& filePath) {
12+
const std::string extension = ".cfg";
13+
if (filePath.length() >= extension.length()) {
14+
std::string fileExtension = filePath.substr(filePath.length() - extension.length());
15+
return fileExtension == extension;
16+
} else {
17+
return false;
18+
}
19+
}
20+
21+
bool CaseFile::isVTKFile(const std::string& filePath) {
22+
const std::string extension = ".vtk";
23+
if (filePath.length() >= extension.length()) {
24+
std::string fileExtension = filePath.substr(filePath.length() - extension.length());
25+
return fileExtension == extension;
26+
} else {
27+
return false;
28+
}
29+
}
30+
31+
bool CaseFile::lookforzip(const std::string& zipFilePath, const std::string& directory) {
32+
char** papszDir = VSIReadDir(directory.c_str());
33+
if (papszDir != nullptr) {
34+
35+
for (int i = 0; papszDir[i] != nullptr; i++) {
36+
std::string entry = papszDir[i];
37+
38+
if (entry == "." || entry == "..") {
39+
continue;
2940
}
30-
VSIFCloseL(fin);
3141

32-
void* zipHandle = CPLCreateZip(zipFilePath.c_str(), NULL);
33-
if (zipHandle == NULL) {
34-
std::cerr << "Failed to create or open zip file: " << zipFilePath << std::endl;
35-
CPLFree(data);
36-
return;
42+
if (entry == parse("file", getzip())) {
43+
return true;
3744
}
45+
}
46+
47+
CSLDestroy(papszDir);
48+
}
49+
return false;
50+
}
3851

39-
if (CPLCreateFileInZip(zipHandle, zipEntryName.c_str(), NULL) != CE_None) {
40-
std::cerr << "Failed to create file in zip: " << zipEntryName << std::endl;
41-
CPLFree(data);
42-
CPLCloseZip(zipHandle);
52+
bool CaseFile::lookfordate(const std::string& date) {
53+
return false;
54+
}
55+
56+
57+
std::string CaseFile::parse(const std::string& type, const std::string& path) {
58+
size_t found = path.find_last_of("/");
59+
if (found != std::string::npos) {
60+
if (strcmp(type.c_str(), "directory") == 0) {
61+
return path.substr(0, found);
62+
63+
}
64+
else
65+
if (strcmp(type.c_str(), "file") == 0) {
66+
return path.substr(found + 1); // Extract substring after the last '/'
67+
}
68+
}
69+
}
70+
71+
72+
void CaseFile::addFileToZip(const std::string& zipFilePath, const std::string& dirPath, const std::string& fileToAdd, const std::string& usrlocalpath) {
73+
// Enable CPL logging
74+
std::lock_guard<std::mutex> lock(zipMutex); // for multithreading issue?? not sure I think i busted it
75+
CPLSetConfigOption("CPL_DEBUG", "ON");
76+
77+
std::cout << "ZIP File Path: " << zipFilePath << std::endl;
78+
std::cout << "Directory Path: " << dirPath << std::endl;
79+
std::cout << "File to Add Path: " << usrlocalpath << std::endl;
80+
std::cout << "File to Add: " << fileToAdd << std::endl;
81+
82+
bool foundzip = lookforzip(zipFilePath, dirPath);
83+
std::cout << "Found ZIP: " << foundzip << std::endl;
84+
85+
if (foundzip) {
86+
std::ifstream infile(zipFilePath);
87+
if (!infile.good()) {
88+
std::cerr << "ZIP file does not exist: " << zipFilePath << std::endl;
4389
return;
4490
}
91+
}
4592

46-
if (CPLWriteFileInZip(zipHandle, data, static_cast<int>(offset)) != CE_None) {
47-
std::cerr << "Failed to write data to file in zip: " << zipEntryName << std::endl;
48-
}
93+
zipFile zip;
94+
if (!foundzip) {
95+
zip = cpl_zipOpen(zipFilePath.c_str(), APPEND_STATUS_CREATE);
96+
std::cout << "Creating new ZIP file: " << zipFilePath << std::endl;
97+
} else {
98+
zip = cpl_zipOpen(zipFilePath.c_str(), APPEND_STATUS_ADDINZIP);
99+
std::cout << "Appending to existing ZIP file: " << zipFilePath << std::endl;
100+
}
49101

50-
if (CPLCloseFileInZip(zipHandle) != CE_None) {
51-
std::cerr << "Failed to close file in zip: " << zipEntryName << std::endl;
52-
}
102+
if (zip == NULL) {
103+
std::cerr << "Failed to open ZIP file: " << zipFilePath << std::endl;
104+
CPLError(CE_Failure, CPLE_OpenFailed, "Failed to open ZIP file %s", zipFilePath.c_str());
105+
const char* errMsg = CPLGetLastErrorMsg();
106+
std::cerr << "GDAL Error: " << errMsg << std::endl;
107+
return;
108+
}
53109

54-
CPLCloseZip(zipHandle);
110+
zip_fileinfo zi = {0};
111+
if (cpl_zipOpenNewFileInZip(zip, fileToAdd.c_str(), &zi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK) {
112+
std::cerr << "Could not open new file in ZIP: " << fileToAdd << std::endl;
113+
CPLError(CE_Failure, CPLE_FileIO, "Could not open new file in ZIP %s", fileToAdd.c_str());
114+
cpl_zipClose(zip, nullptr);
115+
return;
116+
}
117+
118+
VSILFILE *file = VSIFOpenL(usrlocalpath.c_str(), "rb");
119+
if (file == nullptr) {
120+
std::cerr << "Could not open file for reading with VSIL: " << usrlocalpath << std::endl;
121+
CPLError(CE_Failure, CPLE_FileIO, "Could not open file for reading with VSIL %s", usrlocalpath.c_str());
122+
cpl_zipCloseFileInZip(zip);
123+
cpl_zipClose(zip, nullptr);
124+
return;
125+
}
126+
127+
VSIFSeekL(file, 0, SEEK_END);
128+
vsi_l_offset fileSize = VSIFTellL(file);
129+
VSIFSeekL(file, 0, SEEK_SET);
55130

131+
char *data = (char*)CPLMalloc(fileSize);
132+
if (data == nullptr) {
133+
std::cerr << "Failed to allocate memory for file data." << std::endl;
134+
CPLError(CE_Failure, CPLE_OutOfMemory, "Failed to allocate memory for file data.");
135+
VSIFCloseL(file);
136+
cpl_zipCloseFileInZip(zip);
137+
cpl_zipClose(zip, nullptr);
138+
return;
139+
}
140+
141+
if (VSIFReadL(data, 1, fileSize, file) != fileSize) {
142+
std::cerr << "Failed to read file contents: " << fileToAdd << std::endl;
143+
CPLError(CE_Failure, CPLE_FileIO, "Failed to read file contents %s", fileToAdd.c_str());
56144
CPLFree(data);
145+
VSIFCloseL(file);
146+
cpl_zipCloseFileInZip(zip);
147+
cpl_zipClose(zip, nullptr);
148+
return;
149+
}
57150

58-
std::cout << "File added to ZIP: " << zipEntryName << std::endl;
151+
if (cpl_zipWriteInFileInZip(zip, data, static_cast<unsigned int>(fileSize)) != ZIP_OK) {
152+
std::cerr << "Error writing data to ZIP file: " << fileToAdd << std::endl;
153+
CPLError(CE_Failure, CPLE_FileIO, "Error writing data to ZIP file %s", fileToAdd.c_str());
59154
}
60155

61-
void deleteFileFromPath(std::string directoryPath, std::string Filename) {
62-
for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) {
63-
if (entry.is_regular_file() && entry.path().filename() == Filename) {
64-
std::filesystem::remove(entry.path()); // Use std::filesystem explicitly
65-
std::cout << "Deleted file: " << entry.path() << std::endl;
66-
}
156+
CPLFree(data);
157+
VSIFCloseL(file);
158+
cpl_zipCloseFileInZip(zip);
159+
160+
if (cpl_zipClose(zip, nullptr) != ZIP_OK) {
161+
std::cerr << "Error closing ZIP file" << std::endl;
162+
CPLError(CE_Failure, CPLE_FileIO, "Error closing ZIP file %s", zipFilePath.c_str());
163+
}
164+
std::cout << "File added to ZIP: " << fileToAdd << std::endl;
165+
}
166+
167+
168+
169+
std::string CaseFile::getTime() {
170+
auto now = std::chrono::system_clock::now();
171+
172+
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
173+
174+
std::tm* local_tm = std::localtime(&now_time_t);
175+
176+
std::ostringstream oss;
177+
oss << std::put_time(local_tm, "%Y-%m-%d %H:%M:%S");
178+
return oss.str();
179+
}
180+
181+
182+
void CaseFile::deleteFileFromPath(std::string directoryPath, std::string filename) {
183+
184+
char** papszDir = VSIReadDir(directoryPath.c_str());
185+
if (papszDir != nullptr) {
186+
187+
for (int i = 0; papszDir[i] != nullptr; i++) {
188+
std::string entry = papszDir[i];
189+
190+
if (entry == "." || entry == "..") {
191+
continue;
192+
}
193+
194+
std::string fullPath = directoryPath + "/" + entry;
195+
196+
if (entry == filename) {
197+
VSIUnlink(fullPath.c_str());
67198
}
199+
200+
}
201+
202+
CSLDestroy(papszDir);
203+
}
204+
}
205+
void CaseFile::setdir(std::string dir) {
206+
directory = dir;
207+
}
208+
209+
std::string CaseFile::getzip() {
210+
return zipfilename;
211+
}
212+
213+
void CaseFile::setzip(std::string zip) {
214+
zipfilename = zip;
215+
}
216+
217+
std::string CaseFile::getdir() {
218+
return directory;
68219
}
69-
};

0 commit comments

Comments
 (0)