Skip to content

Commit 9aa0e0d

Browse files
committed
Moved GetDriveLetter() and GetDrivePath() functions from Context.cpp to Validator class. These functions will be required for Implementing the Validator's 'class' attribute.
Moved validation code in Validator::Validate() of each attributes into their own functions. This is to allow reusability and to reduce the complexity of the Validator::Validate() function.
1 parent 681e123 commit 9aa0e0d

File tree

4 files changed

+153
-88
lines changed

4 files changed

+153
-88
lines changed

include/shellanything/Validator.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ namespace shellanything
125125
/// <returns>Returns true if the given context is valid against the set of constraints. Returns false otherwise.</returns>
126126
bool Validate(const Context & iContext) const;
127127

128+
public:
129+
130+
/// <summary>
131+
/// Returns the drive letter of the given path.
132+
/// Returns an empty string if the element is not mapped to a drive.
133+
/// </summary>
134+
/// <param name="element">The path to an element.</param>
135+
/// <returns>Returns the drive letter of the given path. Returns an empty string if the element is not mapped to a drive.</returns>
136+
static std::string GetDriveLetter(const std::string & element);
137+
138+
/// <summary>
139+
/// Returns the drive path of the given path. The drive path is returned as 3 characters path. For example "C:\".
140+
/// Returns an empty string if the element is not mapped to a drive.
141+
/// </summary>
142+
/// <param name="element">The path to an element.</param>
143+
/// <returns>Returns the drive path of the given path. Returns an empty string if the element is not mapped to a drive.</returns>
144+
static std::string GetDrivePath(const std::string & element);
145+
146+
private:
147+
bool ValidateProperties(const Context & context, const std::string & properties, bool inversed) const;
148+
bool ValidateFileExtensions(const Context & context, const std::string & file_extensions, bool inversed) const;
149+
bool ValidateExists(const Context & context, const std::string & file_exists, bool inversed) const;
150+
128151
private:
129152
int mMaxFiles;
130153
int mMaxDirectories;

src/Context.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*********************************************************************************/
2424

2525
#include "shellanything/Context.h"
26+
#include "shellanything/Validator.h"
2627
#include "PropertyManager.h"
2728

2829
#include "rapidassist/filesystem_utf8.h"
@@ -59,30 +60,6 @@ namespace shellanything
5960
return (*this);
6061
}
6162

62-
inline bool IsDriveLetter(char c)
63-
{
64-
if ( (c >= 'a' && c <= 'z') ||
65-
(c >= 'A' && c <= 'Z') )
66-
return true;
67-
return false;
68-
}
69-
70-
inline std::string GetDriveLetter(const std::string & element)
71-
{
72-
std::string letter;
73-
if (element.size() >= 2 && element[1] == ':' && IsDriveLetter(element[0]))
74-
letter.append(1, element[0]);
75-
return letter;
76-
}
77-
78-
inline std::string GetDrivePath(const std::string & element)
79-
{
80-
std::string letter;
81-
if (element.size() >= 3 && element[1] == ':' && IsDriveLetter(element[0]) && element[2] == '\\')
82-
letter.append(element.c_str(), 3);
83-
return letter;
84-
}
85-
8663
void Context::RegisterProperties() const
8764
{
8865
PropertyManager & pmgr = PropertyManager::GetInstance();
@@ -123,8 +100,8 @@ namespace shellanything
123100
std::string element_selection_filename = ra::filesystem::GetFilename(element_selection_path.c_str());
124101
std::string element_selection_filename_noext = ra::filesystem::GetFilenameWithoutExtension(element_selection_path.c_str());
125102
std::string element_selection_filename_ext = ra::filesystem::GetFileExtention(element_selection_filename);
126-
std::string element_selection_drive_letter = GetDriveLetter(element);
127-
std::string element_selection_drive_path = GetDrivePath(element);
103+
std::string element_selection_drive_letter = Validator::GetDriveLetter(element);
104+
std::string element_selection_drive_path = Validator::GetDrivePath(element);
128105

129106
// Add a separator between values
130107
if (!selection_path .empty()) selection_path .append( selection_multi_separator );

src/Validator.cpp

Lines changed: 126 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -219,87 +219,152 @@ namespace shellanything
219219
//validate properties
220220
PropertyManager & pmgr = PropertyManager::GetInstance();
221221
const std::string properties = pmgr.Expand(mProperties);
222-
bool properties_inversed = IsInversed("properties");
223222
if (!properties.empty())
224223
{
225-
//split
226-
ra::strings::StringVector property_list = ra::strings::Split(properties, ";");
227-
228-
//each property specified must exists and be non-empty
229-
for(size_t i=0; i<property_list.size(); i++)
230-
{
231-
const std::string & p = property_list[i];
232-
233-
if (!properties_inversed)
234-
{
235-
if (!pmgr.HasProperty(p))
236-
return false; //missing property
237-
const std::string & p_value = pmgr.GetProperty(p);
238-
if (p_value.empty())
239-
return false; //empty
240-
}
241-
else
242-
{
243-
// properties_inversed=true
244-
if (pmgr.HasProperty(p))
245-
{
246-
const std::string & p_value = pmgr.GetProperty(p);
247-
if (!p_value.empty())
248-
return false; //not empty
249-
}
250-
}
251-
252-
}
224+
bool inversed = IsInversed("properties");
225+
bool valid = ValidateProperties(iContext, properties, inversed);
226+
if (!valid)
227+
return false;
253228
}
254229

255230
//validate file extentions
256231
const std::string file_extensions = pmgr.Expand(mFileExtensions);
257-
bool fileextensions_inversed = IsInversed("fileextensions");
258232
if (!file_extensions.empty())
259233
{
260-
//split
261-
ra::strings::StringVector accepted_file_extensions = ra::strings::Split(file_extensions, ";");
262-
Uppercase(accepted_file_extensions);
263-
264-
//for each file selected
265-
const Context::ElementList & elements = iContext.GetElements();
266-
for(size_t i=0; i<elements.size(); i++)
267-
{
268-
const std::string & element = elements[i];
269-
std::string current_file_extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(element));
270-
271-
//each file extension must be part of accepted_file_extensions
272-
bool found = HasValue(accepted_file_extensions, current_file_extension);
273-
if (!fileextensions_inversed && !found)
274-
return false; //current file extension is not accepted
275-
if (fileextensions_inversed && found)
276-
return false; //current file extension is not accepted
277-
}
234+
bool inversed = IsInversed("fileextensions");
235+
bool valid = ValidateFileExtensions(iContext, file_extensions, inversed);
236+
if (!valid)
237+
return false;
278238
}
279239

280240
//validate file/directory exists
281241
const std::string file_exists = pmgr.Expand(mFileExists);
282-
bool exists_inversed = IsInversed("exists");
283242
if (!file_exists.empty())
284243
{
285-
//split
286-
ra::strings::StringVector mandatory_files = ra::strings::Split(file_exists, ";");
244+
bool inversed = IsInversed("exists");
245+
bool valid = ValidateExists(iContext, file_exists, inversed);
246+
if (!valid)
247+
return false;
248+
}
249+
250+
return true;
251+
}
252+
253+
inline bool IsDriveLetter(char c)
254+
{
255+
if ( (c >= 'a' && c <= 'z') ||
256+
(c >= 'A' && c <= 'Z') )
257+
return true;
258+
return false;
259+
}
260+
261+
std::string Validator::GetDriveLetter(const std::string & element)
262+
{
263+
std::string letter;
264+
if (element.size() >= 2 && element[1] == ':' && IsDriveLetter(element[0]))
265+
letter.append(1, element[0]);
266+
return letter;
267+
}
268+
269+
std::string Validator::GetDrivePath(const std::string & element)
270+
{
271+
std::string letter;
272+
if (element.size() >= 3 && element[1] == ':' && IsDriveLetter(element[0]) && element[2] == '\\')
273+
letter.append(element.c_str(), 3);
274+
return letter;
275+
}
276+
277+
bool Validator::ValidateProperties(const Context & context, const std::string & properties, bool inversed) const
278+
{
279+
if (properties.empty())
280+
return true;
281+
282+
PropertyManager & pmgr = PropertyManager::GetInstance();
287283

288-
//for each file
289-
for(size_t i=0; i<mandatory_files.size(); i++)
284+
//split
285+
ra::strings::StringVector property_list = ra::strings::Split(properties, ";");
286+
287+
//each property specified must exists and be non-empty
288+
for(size_t i=0; i<property_list.size(); i++)
289+
{
290+
const std::string & p = property_list[i];
291+
292+
if (!inversed)
293+
{
294+
if (!pmgr.HasProperty(p))
295+
return false; //missing property
296+
const std::string & p_value = pmgr.GetProperty(p);
297+
if (p_value.empty())
298+
return false; //empty
299+
}
300+
else
290301
{
291-
const std::string & element = mandatory_files[i];
292-
bool element_exists = false;
293-
element_exists |= ra::filesystem::FileExistsUtf8(element.c_str());
294-
element_exists |= ra::filesystem::DirectoryExistsUtf8(element.c_str());
295-
if (!exists_inversed && !element_exists)
296-
return false; //mandatory file/directory not found
297-
if (exists_inversed && element_exists)
298-
return false; //mandatory file/directory not found
302+
// inversed
303+
if (pmgr.HasProperty(p))
304+
{
305+
const std::string & p_value = pmgr.GetProperty(p);
306+
if (!p_value.empty())
307+
return false; //not empty
308+
}
299309
}
300310
}
301311

302312
return true;
303313
}
304314

315+
bool Validator::ValidateFileExtensions(const Context & context, const std::string & file_extensions, bool inversed) const
316+
{
317+
if (file_extensions.empty())
318+
return true;
319+
320+
PropertyManager & pmgr = PropertyManager::GetInstance();
321+
322+
//split
323+
ra::strings::StringVector accepted_file_extensions = ra::strings::Split(file_extensions, ";");
324+
Uppercase(accepted_file_extensions);
325+
326+
//for each file selected
327+
const Context::ElementList & elements = context.GetElements();
328+
for(size_t i=0; i<elements.size(); i++)
329+
{
330+
const std::string & element = elements[i];
331+
std::string current_file_extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(element));
332+
333+
//each file extension must be part of accepted_file_extensions
334+
bool found = HasValue(accepted_file_extensions, current_file_extension);
335+
if (!inversed && !found)
336+
return false; //current file extension is not accepted
337+
if (inversed && found)
338+
return false; //current file extension is not accepted
339+
}
340+
341+
return true;
342+
}
343+
344+
bool Validator::ValidateExists(const Context & context, const std::string & file_exists, bool inversed) const
345+
{
346+
if (file_exists.empty())
347+
return true;
348+
349+
PropertyManager & pmgr = PropertyManager::GetInstance();
350+
351+
//split
352+
ra::strings::StringVector mandatory_files = ra::strings::Split(file_exists, ";");
353+
354+
//for each file
355+
for(size_t i=0; i<mandatory_files.size(); i++)
356+
{
357+
const std::string & element = mandatory_files[i];
358+
bool element_exists = false;
359+
element_exists |= ra::filesystem::FileExistsUtf8(element.c_str());
360+
element_exists |= ra::filesystem::DirectoryExistsUtf8(element.c_str());
361+
if (!inversed && !element_exists)
362+
return false; //mandatory file/directory not found
363+
if (inversed && element_exists)
364+
return false; //mandatory file/directory not found
365+
}
366+
367+
return true;
368+
}
369+
305370
} //namespace shellanything

src/Wildcard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ namespace shellanything
104104

105105
} //namespace shellanything
106106

107-
#endif //SA_ACTION_PROPERTY_H
107+
#endif //SA_WILDCARD_H

0 commit comments

Comments
 (0)