|
1 | 1 | #include "casefile.h" |
2 | 2 |
|
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 = ""; |
12 | 5 |
|
13 | | - vsi_l_offset offset; |
14 | | - VSIFSeekL(fin, 0, SEEK_END); |
15 | | - offset = VSIFTellL(fin); |
16 | | - VSIRewindL(fin); |
| 6 | +std::mutex zipMutex; |
17 | 7 |
|
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; |
29 | 40 | } |
30 | | - VSIFCloseL(fin); |
31 | 41 |
|
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; |
37 | 44 | } |
| 45 | + } |
| 46 | + |
| 47 | + CSLDestroy(papszDir); |
| 48 | + } |
| 49 | + return false; |
| 50 | +} |
38 | 51 |
|
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; |
43 | 89 | return; |
44 | 90 | } |
| 91 | + } |
45 | 92 |
|
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 | + } |
49 | 101 |
|
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 | + } |
53 | 109 |
|
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); |
55 | 130 |
|
| 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()); |
56 | 144 | CPLFree(data); |
| 145 | + VSIFCloseL(file); |
| 146 | + cpl_zipCloseFileInZip(zip); |
| 147 | + cpl_zipClose(zip, nullptr); |
| 148 | + return; |
| 149 | + } |
57 | 150 |
|
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()); |
59 | 154 | } |
60 | 155 |
|
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()); |
67 | 198 | } |
| 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; |
68 | 219 | } |
69 | | -}; |
|
0 commit comments