NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
filecheck.cpp
Go to the documentation of this file.
1#include <wx/filename.h>
2#include "../../kernel.hpp"
3
4
14static bool checkInvalidChars(std::string sPathname)
15{
16 // Define the invalid chars
17 const std::string sINVALID_CHARS = "\"#%&|*?";
18
19 // Go through all chars and check for invalid ones
20 for (size_t i = 0; i < sPathname.length(); i++)
21 {
22 if (sPathname[i] == '~' || sINVALID_CHARS.find(sPathname[i]) != std::string::npos)
23 return false;
24 }
25 return true;
26}
27
28
37bool is_dir(std::string sPathname)
38{
39 // Check for invalid chars
40 if (!checkInvalidChars(sPathname))
41 return false;
42
43 // Get the FileSystem instance to replace symbols in the file name using an existing method
44 FileSystem& fileSystemInstance = NumeReKernel::getInstance()->getFileSystem();
45 sPathname = fileSystemInstance.ValidFolderName(sPathname, false);
46
47 // Check for double ':' at the beginning of the path or left over '<' '>' symbols
48 if (sPathname.find(':', 2) != std::string::npos || sPathname.find_first_of("<>") != std::string::npos)
49 return false;
50
51 // Check for a valid volume identifier (be of pattern "C:/" if not a network path)
52 if (sPathname[2] != '/' && !(sPathname.substr(0,2) == "//" && isalpha(sPathname[2])))
53 return false;
54
55 return true;
56}
57
58
67bool is_file(std::string sPathname)
68{
69 // Check for invalid chars
70 if (!checkInvalidChars(sPathname))
71 return false;
72
73 // Get the FileSystem instance to replace symbols in the file name using an existing method
74 FileSystem& fileSystemInstance = NumeReKernel::getInstance()->getFileSystem();
75 sPathname = fileSystemInstance.ValidFileName(sPathname, "", false, false);
76
77 // Check for double ':' at the beginning of the path or left over '<' '>' symbols
78 if (sPathname.find(':', 2) != std::string::npos || sPathname.find_first_of("<>") != std::string::npos)
79 return false;
80
81 // Check for a valid volume identifier (be of pattern "C:/" if not a network path)
82 if (sPathname[2] != '/' && !(sPathname.substr(0,2) == "//" && isalpha(sPathname[2])))
83 return false;
84
85 // Manually check if there could be file extension. Check for a dot and if there are chars after it
86 if (sPathname.find_last_of(".") == std::string::npos || sPathname.back() == '.')
87 return false;
88
89 // The file extension can not contain anything but alphabetical chars
90 size_t extStart = sPathname.find_last_of(".");
91 std::string ext = sPathname.substr(extStart + 1, sPathname.length() - extStart);
92 if (!std::all_of(ext.begin(), ext.end(), [](char const &c) { return std::isalnum(c);}))
93 return false;
94
95 // Check if there is valid file name
96 if (!isalnum(sPathname[sPathname.find_last_of(".") - 1]))
97 return false;
98
99 return true;
100}
101
This class implements the basic input/ output file system and provides functionalities to work with f...
Definition: filesystem.hpp:92
std::string ValidFolderName(std::string _sFileName, bool doCleanPath=true, bool appendTrailingSeparator=true) const
This member function evaluates, whether the passed foldername is a valid foldername.
Definition: filesystem.cpp:369
std::string ValidFileName(std::string _sFileName, const std::string sExtension=".dat", bool checkExtension=true, bool doCleanPath=true) const
This member function evaluates, whether the passed filename is a valid filename. One may supply a pre...
Definition: filesystem.cpp:280
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
Definition: kernel.hpp:221
FileSystem & getFileSystem()
Definition: kernel.hpp:258
static bool checkInvalidChars(std::string sPathname)
This function checks for the invalid chars that can not appear in a valid directory name.
Definition: filecheck.cpp:14
bool is_file(std::string sPathname)
This function checks whether a given string is a valid file path.
Definition: filecheck.cpp:67
bool is_dir(std::string sPathname)
This function checks whether a given string is a valid directory path.
Definition: filecheck.cpp:37