10 #ifndef __EMSCRIPTEN__
35 const string& fileName,
36 const string& ftpHost,
37 const string& ftpUser,
42 string fullPathAndFilename = fileDir + fileName;
45 errorMsg =
"File doesn't exist: " + fullPathAndFilename;
52 ftp.SetConnmode(ftplib::connmode::port);
54 if (ftp.Connect(ftpHost.c_str()))
56 if (ftp.Login(ftpUser.c_str(), ftpPwd.c_str()))
58 if (ftp.Chdir(ftpDir.c_str()))
65 if (!latestFile.empty())
72 stringstream versionSS;
73 versionSS <<
"(" << versionNO <<
")";
77 string newVersionFilename = fileWOExt + versionSS.str() +
".xml";
80 if (!ftp.Put(fullPathAndFilename.c_str(),
81 newVersionFilename.c_str(),
82 ftplib::transfermode::image))
84 errorMsg =
"*** ERROR: ftp.Put failed. ***\n";
90 errorMsg =
"*** ERROR: ftp.Chdir failed. ***\n";
96 errorMsg =
"*** ERROR: ftp.Login failed. ***\n";
102 errorMsg =
"*** ERROR: ftp.Connect failed. ***\n";
122 const string& fileName,
123 const string& ftpHost,
124 const string& ftpUser,
125 const string& ftpPwd,
126 const string& ftpDir,
132 ftp.SetConnmode(ftplib::connmode::port);
134 if (ftp.Connect(ftpHost.c_str()))
136 if (ftp.Login(ftpUser.c_str(), ftpPwd.c_str()))
138 if (ftp.Chdir(ftpDir.c_str()))
141 string fullPathAndFilename = fileDir + fileName;
144 ftp.Size(latestFile.c_str(),
146 ftplib::transfermode::image);
150 if (!ftp.Get(fullPathAndFilename.c_str(),
152 ftplib::transfermode::image))
154 errorMsg =
"*** ERROR: ftp.Get failed. ***\n";
160 errorMsg =
"*** No file to download ***\n";
166 errorMsg =
"*** ERROR: ftp.Chdir failed. ***\n";
172 errorMsg =
"*** ERROR: ftp.Login failed. ***\n";
178 errorMsg =
"*** ERROR: ftp.Connect failed. ***\n";
199 const string& fileName,
200 const string& ftpHost,
201 const string& ftpUser,
202 const string& ftpPwd,
203 const string& ftpDir,
206 string fullPathAndFilename = fileDir + fileName;
209 errorMsg =
"File doesn't exist: " + fullPathAndFilename;
216 ftp.SetConnmode(ftplib::connmode::port);
218 if (ftp.Connect(ftpHost.c_str()))
220 if (ftp.Login(ftpUser.c_str(), ftpPwd.c_str()))
222 if (ftp.Chdir(ftpDir.c_str()))
225 if (!ftp.Put(fullPathAndFilename.c_str(),
227 ftplib::transfermode::image))
229 errorMsg =
"*** ERROR: ftp.Put failed. ***\n";
235 errorMsg =
"*** ERROR: ftp.Chdir failed. ***\n";
241 errorMsg =
"*** ERROR: ftp.Login failed. ***\n";
247 errorMsg =
"*** ERROR: ftp.Connect failed. ***\n";
268 const string& fileName,
269 const string& ftpHost,
270 const string& ftpUser,
271 const string& ftpPwd,
272 const string& ftpDir,
278 ftp.SetConnmode(ftplib::connmode::port);
280 if (ftp.Connect(ftpHost.c_str()))
282 if (ftp.Login(ftpUser.c_str(), ftpPwd.c_str()))
284 if (ftp.Chdir(ftpDir.c_str()))
287 string fullPathAndFilename = fileDir + fileName;
289 ftp.Size(fileName.c_str(),
291 ftplib::transfermode::image);
295 if (!ftp.Get(fullPathAndFilename.c_str(),
297 ftplib::transfermode::image))
299 errorMsg =
"*** ERROR: ftp.Get failed. ***\n";
305 errorMsg =
"*** No file to download ***\n";
311 errorMsg =
"*** ERROR: ftp.Chdir failed. ***\n";
317 errorMsg =
"*** ERROR: ftp.Login failed. ***\n";
323 errorMsg =
"*** ERROR: ftp.Connect failed. ***\n";
343 const string& ftpHost,
344 const string& ftpUser,
345 const string& ftpPwd,
346 const string& ftpDir,
347 const string& searchFileTag,
353 ftp.SetConnmode(ftplib::connmode::port);
355 if (ftp.Connect(ftpHost.c_str()))
357 if (ftp.Login(ftpUser.c_str(), ftpPwd.c_str()))
359 if (ftp.Chdir(ftpDir.c_str()))
362 vector<string> retrievedFileNames;
369 for (
auto& retrievedFileName : retrievedFileNames)
372 ftp.Size(retrievedFileName.c_str(),
374 ftplib::transfermode::image);
378 string targetFilename = fileDir + retrievedFileName;
379 if (!ftp.Get(targetFilename.c_str(),
380 retrievedFileName.c_str(),
381 ftplib::transfermode::image))
383 errorMsg =
"*** ERROR: ftp.Get failed. ***\n";
392 errorMsg =
"*** ERROR: ftp.Chdir failed. ***\n";
398 errorMsg =
"*** ERROR: ftp.Login failed. ***\n";
404 errorMsg =
"*** ERROR: ftp.Connect failed. ***\n";
423 const string& localDir,
424 const string& searchFileTag,
425 vector<string>& retrievedFileNames,
429 string ftpDirResult = localDir +
"ftpDirResult.txt";
430 string searchDirAndFileType =
"*." + searchFileTag;
433 if (ftp.Dir(ftpDirResult.c_str(), searchDirAndFileType.c_str()))
436 vector<string> vecFilesInDir;
437 vector<string> strippedFiles;
441 for (
string& fileInfoLine : vecFilesInDir)
443 vector<string> splits;
446 int numOfFoundNonEmpty = 0;
450 while (pos < splits.size())
452 if (!splits[pos].empty())
453 numOfFoundNonEmpty++;
455 if (numOfFoundNonEmpty == 8)
466 if (found && pos < splits.size())
469 std::string space(
" ");
470 for (
int i = pos; i < splits.size(); ++i)
472 name.append(splits[i]);
473 if (i != splits.size() - 1)
478 retrievedFileNames.push_back(name);
483 errorMsg =
"*** ERROR: getAllFileNamesWithTag: Unexpected result: Ftp info line was not formatted as expected. ***\n";
493 errorMsg =
"*** ERROR: getAllFileNamesWithTag: directory " + localDir +
"does not exist. ***\n";
497 errorMsg =
"*** ERROR: getAllFileNamesWithTag failed. ***\n";
514 const string& fileDir,
515 const string& fileName)
518 string fullPathAndFilename = fileDir + fileName;
519 string ftpDirResult = fileDir +
"ftpDirResult.txt";
521 string filenameWOExtStar = filenameWOExt +
"*";
524 if (ftp.Dir(ftpDirResult.c_str(), filenameWOExtStar.c_str()))
526 vector<string> vecFilesInDir;
527 vector<string> strippedFiles;
531 for (
string& fileInfoLine : vecFilesInDir)
533 size_t foundAt = fileInfoLine.find(filenameWOExt);
534 if (foundAt != string::npos)
536 string fileWExt = fileInfoLine.substr(foundAt);
538 strippedFiles.push_back(fileWOExt);
543 if (!strippedFiles.empty())
547 string latest = strippedFiles.back() +
".xml";
569 if (!filenameWOExt.empty())
571 size_t len = filenameWOExt.length();
572 if (filenameWOExt.at(len - 1) ==
')')
574 size_t leftPos = filenameWOExt.rfind(
'(');
575 string verStr = filenameWOExt.substr(leftPos + 1, len - leftPos - 2);
576 versionNO = stoi(verStr);
FtpUtils provides networking functionality via the FTP protocol.
bool getAllFileNamesWithTag(ftplib &ftp, const string &localDir, const string &searchFileTag, vector< string > &retrievedFileNames, string &errorMsg)
Get a list of all filenames with given search file tag in remote directory.
string getLatestFilename(ftplib &ftp, const string &fileDir, const string &fileName)
Returns the latest fileName of the same fullPathAndFilename.
bool uploadFile(const string &fileDir, const string &fileName, const string &ftpHost, const string &ftpUser, const string &ftpPwd, const string &ftpDir, string &errorMsg)
Uploads file to the ftp server.
bool downloadAllFilesFromDir(const string &fileDir, const string &ftpHost, const string &ftpUser, const string &ftpPwd, const string &ftpDir, const string &searchFileTag, string &errorMsg)
bool downloadFile(const string &fileDir, const string &fileName, const string &ftpHost, const string &ftpUser, const string &ftpPwd, const string &ftpDir, string &errorMsg)
Download file from the ftp server.
int getVersionInFilename(const string &filename)
Returns the version number at the end of the fileName.
bool uploadFileLatestVersion(const string &fileDir, const string &fileName, const string &ftpHost, const string &ftpUser, const string &ftpPwd, const string &ftpDir, string &errorMsg)
Uploads the file to the ftp server. Checks if the filename already exists and adds a version number.
bool downloadFileLatestVersion(const string &fileDir, const string &fileName, const string &ftpHost, const string &ftpUser, const string &ftpPwd, const string &ftpDir, string &errorMsg)
Download the file from the ftp server which has the latest version and store it as fileName locally.
bool fileExists(const string &pathfilename)
Returns true if a file exists.
bool dirExists(const string &path)
Returns true if a directory exists.
bool getFileContent(const string &fileName, vector< string > &vecOfStrings)
Returns true if content of file could be put in a vector of strings.
void errorMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent error message output.
string getFileNameWOExt(const string &pathFilename)
Returns the filename without extension.
bool compareNatural(const string &a, const string &b)
Naturally compares two strings (used for filename sorting)
void splitString(const string &s, char delimiter, vector< string > &splits)
Splits an input string at a delimiter character into a string vector.