diff --git a/labs/08 Recursion/Recursion.pdf b/labs/08 Recursion/Recursion.pdf new file mode 100644 index 0000000000000000000000000000000000000000..7537db957834a8542cfade1f592d7e2da1664480 Binary files /dev/null and b/labs/08 Recursion/Recursion.pdf differ diff --git a/labs/08 Recursion/Recursion_Lab_Starter.zip b/labs/08 Recursion/Recursion_Lab_Starter.zip new file mode 100644 index 0000000000000000000000000000000000000000..d6a52eee157f8d5ea2c42fcc6426fc61192e3d7b Binary files /dev/null and b/labs/08 Recursion/Recursion_Lab_Starter.zip differ diff --git a/labs/08 Recursion/Recursion_Lab_Starter/File.cpp b/labs/08 Recursion/Recursion_Lab_Starter/File.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ec0b6107cd6fec77abe2c4bfe9ab56f7412e035 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/File.cpp @@ -0,0 +1,28 @@ +#include "File.hpp" + +File::File() {} + +File::File( string name, string contents ) + : FileSystemThing( name, "File" ) +{ + Setup( contents ); +} + +void File::Setup( string contents ) +{ + m_contents = contents; + m_bytes = sizeof( m_contents ); +} + +void File::Display() +{ + FileSystemThing::Display(); +} + +void File::Details() +{ + FileSystemThing::Details(); + cout << "Size: " << m_bytes << " bytes" << endl; + cout << "Contents: " << m_contents << endl; + cout << endl; +} diff --git a/labs/08 Recursion/Recursion_Lab_Starter/File.hpp b/labs/08 Recursion/Recursion_Lab_Starter/File.hpp new file mode 100644 index 0000000000000000000000000000000000000000..47b45fbc07482277eba6823aa51da19c11e0a513 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/File.hpp @@ -0,0 +1,23 @@ +#ifndef _FILE +#define _FILE + +#include "FileSystemThing.hpp" + +class File : public FileSystemThing +{ + public: + File(); + File( string name, string contents ); + + void Setup( string contents ); + void Display(); + void Details(); + + protected: + int m_bytes; + string m_contents; + + friend class FileSystemManager; +}; + +#endif diff --git a/labs/08 Recursion/Recursion_Lab_Starter/FileSystemManager.cpp b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemManager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93f2823c8c28ea7aa3f2e5a0bc97e76333c7452f --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemManager.cpp @@ -0,0 +1,146 @@ +#include "FileSystemManager.hpp" + +#include "utilities/Logger.hpp" +#include "utilities/StringUtil.hpp" + +File* FileSystemManager::FindFile( string name ) +{ + Logger::Out( "Find file: " + name, "FileSystemManager::FindFile" ); + // Begin searching at root + return Recursive_FindFile( m_allFolders[0], name ); +} + +Folder* FileSystemManager::FindFolder( string name ) +{ + Logger::Out( "Find folder: " + name, "FileSystemManager::FindFolder" ); + // Begin searching at root + return Recursive_FindFolder( m_allFolders[0], name ); +} + +File* FileSystemManager::Recursive_FindFile( Folder* ptrFolder, string name ) +{ + Logger::Out( "Searching folder: " + ptrFolder->GetName(), "FileSystemManager::Recursive_FindFile" ); + return nullptr; +} + +Folder* FileSystemManager::Recursive_FindFolder( Folder* ptrFolder, string name ) +{ + Logger::Out( "Searching folder: " + ptrFolder->GetName(), "FileSystemManager::Recursive_FindFolder" ); + return nullptr; +} + + +void FileSystemManager::Display() +{ + Logger::Out( "Display filesystem", "FileSystemManager::Display" ); + // Start at root + Recursive_DisplayFolder( m_allFolders[0], 0 ); +} + +void FileSystemManager::Recursive_DisplayFile( File* ptrFile, int level ) +{ + Logger::Out( "Display file: " + ptrFile->GetName(), "FileSystemManager::Recursive_DisplayFile" ); + Indent( level ); + ptrFile->Display(); + cout << endl; +} + +void FileSystemManager::Recursive_DisplayFolder( Folder* ptrFolder, int level ) +{ + Logger::Out( "Display folder: " + ptrFolder->GetName(), "FileSystemManager::Recursive_DisplayFolder" ); + // Display self + Indent( level ); + ptrFolder->Display(); + cout << endl; + + // Display children + for ( auto& ptr : ptrFolder->GetFolders() ) + { + Recursive_DisplayFolder( ptr, level+1 ); + } + for ( auto& ptr : ptrFolder->GetFiles() ) + { + Recursive_DisplayFile( ptr, level+1 ); + } +} + +FileSystemManager::FileSystemManager() +{ + // Create files and folders and set up + // which files go in what folders and so on. + Folder* root = new Folder( "home" ); m_allFolders.push_back( root ); + + Folder* folder1 = new Folder( "school" ); m_allFolders.push_back( folder1 ); root->AddFolder( folder1 ); + Folder* folder1a = new Folder( "cs235" ); m_allFolders.push_back( folder1a ); folder1->AddFolder( folder1a ); + Folder* folder1b = new Folder( "cs210" ); m_allFolders.push_back( folder1b ); folder1->AddFolder( folder1b ); + + Folder* folder2 = new Folder( "work" ); m_allFolders.push_back( folder2 ); root->AddFolder( folder2 ); + Folder* folder2a = new Folder( "dayjob" ); m_allFolders.push_back( folder2a ); folder2->AddFolder( folder2a ); + Folder* folder2b = new Folder( "sidegig" ); m_allFolders.push_back( folder2b ); folder2->AddFolder( folder2b ); + Folder* folder2ba = new Folder( "billing" ); m_allFolders.push_back( folder2ba ); folder2b->AddFolder( folder2ba ); + + Folder* folder3 = new Folder( "hobbies" ); m_allFolders.push_back( folder3 ); root->AddFolder( folder3 ); + Folder* folder3a = new Folder( "basketweaving" ); m_allFolders.push_back( folder3a ); folder3->AddFolder( folder3a ); + Folder* folder3b = new Folder( "constructed-languages" ); m_allFolders.push_back( folder3b ); folder3->AddFolder( folder3b ); + + Folder* folder4 = new Folder( "files" ); m_allFolders.push_back( folder4 ); root->AddFolder( folder4 ); + + File* file; + file = new File( "homework1.odt", "1. Learn polymorphism, 2. Get A" ); m_allFiles.push_back( file ); folder1a->AddFile ( file ); + file = new File( "homework2.odt", "!(p && q) = !p || !q" ); m_allFiles.push_back( file ); folder1b->AddFile ( file ); + file = new File( "transcript.odt", "Got all the good grades @ jccc" ); m_allFiles.push_back( file ); folder1->AddFile ( file ); + file = new File( "references.odt", "Person who isn't family: 111-2222" ); m_allFiles.push_back( file ); folder1->AddFile ( file ); + file = new File( "resume.odt", "Worked job at place, 2019" ); m_allFiles.push_back( file ); folder2->AddFile ( file ); + file = new File( "logfile.txt", "Everything crashed" ); m_allFiles.push_back( file ); folder2a->AddFile ( file ); + file = new File( "complaints.csv", "Bob microwaves fish" ); m_allFiles.push_back( file ); folder2a->AddFile ( file ); + file = new File( "posting.txt", "Software Engineer, $75,000/yr" ); m_allFiles.push_back( file ); folder2b->AddFile ( file ); + file = new File( "january.csv", "Co1 owes me $100" ); m_allFiles.push_back( file ); folder2ba->AddFile( file ); + file = new File( "february.csv", "Co1 still hasn't paid" ); m_allFiles.push_back( file ); folder2ba->AddFile( file ); + file = new File( "march.csv", "Still waiting on Co1's payment" ); m_allFiles.push_back( file ); folder2ba->AddFile( file ); + file = new File( "todo.txt", "Learn to draw, learn piano" ); m_allFiles.push_back( file ); folder3->AddFile( file ); + file = new File( "done.txt", "Mastered basketweaving!" ); m_allFiles.push_back( file ); folder3->AddFile( file ); + file = new File( "coolbaskets.jpg", "u U v V" ); m_allFiles.push_back( file ); folder3a->AddFile( file ); + file = new File( "esperanto.txt", "Chu vi parolas?" ); m_allFiles.push_back( file ); folder3b->AddFile( file ); + file = new File( "ido.txt", "Ka vu parolas?" ); m_allFiles.push_back( file ); folder3b->AddFile( file ); + file = new File( "tokipona.txt", "sina pilin seme?" ); m_allFiles.push_back( file ); folder3b->AddFile( file ); + file = new File( "laadan.txt", "BAa thal ne?" ); m_allFiles.push_back( file ); folder3b->AddFile( file ); + file = new File( "notpirated.mp4", "The Lost Skeleton of Cadavra" ); m_allFiles.push_back( file ); folder4->AddFile( file ); + file = new File( "notavirus.exe", "DeleteHarddrive();" ); m_allFiles.push_back( file ); folder4->AddFile( file ); + file = new File( "screenshot.png", "(>o.o)>----| (o_o)" ); m_allFiles.push_back( file ); folder4->AddFile( file ); + + Logger::Out( StringUtil::ToString( m_allFiles.size() ) + " files created", "FileSystemManager::FileSystemManager" ); + for ( unsigned int i = 0; i < m_allFiles.size(); i++ ) + { + Logger::Out( "File " + StringUtil::ToString( i ) + ": " + m_allFiles[i]->GetName(), "FileSystemManager::FileSystemManager" ); + } + + Logger::Out( StringUtil::ToString( m_allFolders.size() ) + " folders created", "FileSystemManager::FileSystemManager" ); + for ( unsigned int i = 0; i < m_allFolders.size(); i++ ) + { + Logger::Out( "Folder " + StringUtil::ToString( i ) + ": " + m_allFolders[i]->GetName(), "FileSystemManager::FileSystemManager" ); + } +} + +FileSystemManager::~FileSystemManager() +{ + for ( unsigned int i = 0; i < m_allFolders.size(); i++ ) + { + Logger::Out( "Delete folder " + m_allFolders[i]->GetName(), "FileSystemManager::~FileSystemManager" ); + delete m_allFolders[i]; + } + + for ( unsigned int i = 0; i < m_allFiles.size(); i++ ) + { + Logger::Out( "Delete file " + m_allFiles[i]->GetName(), "FileSystemManager::~FileSystemManager" ); + delete m_allFiles[i]; + } +} + +void FileSystemManager::Indent( int level ) +{ + for ( int i = 0; i < level; i++ ) + { + cout << "--"; + } + cout << " "; +} diff --git a/labs/08 Recursion/Recursion_Lab_Starter/FileSystemManager.hpp b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemManager.hpp new file mode 100644 index 0000000000000000000000000000000000000000..436ab3a8423bb3af0c930c63d354d2ee7c166eff --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemManager.hpp @@ -0,0 +1,32 @@ +#ifndef _FILE_SYSTEM_MANAGER +#define _FILE_SYSTEM_MANAGER + +#include "File.hpp" +#include "Folder.hpp" + +#include +using namespace std; + +class FileSystemManager +{ + public: + FileSystemManager(); + ~FileSystemManager(); + + void Display(); + File* FindFile( string name ); + Folder* FindFolder( string name ); + + private: + void Recursive_DisplayFile( File* ptrFile, int level ); + void Recursive_DisplayFolder( Folder* ptrFolder, int level ); + + File* Recursive_FindFile( Folder* ptrFolder, string name ); + Folder* Recursive_FindFolder( Folder* ptrFolder, string name ); + + void Indent( int level ); + vector m_allFiles; + vector m_allFolders; +}; + +#endif diff --git a/labs/08 Recursion/Recursion_Lab_Starter/FileSystemThing.cpp b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemThing.cpp new file mode 100644 index 0000000000000000000000000000000000000000..717a1782087a3277a2fa7db0b1ac80ece97a6522 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemThing.cpp @@ -0,0 +1,35 @@ +#include "FileSystemThing.hpp" + +FileSystemThing::FileSystemThing() {} + +FileSystemThing::FileSystemThing( string name, string type ) +{ + SetName( name ); + m_type = type; +} + +void FileSystemThing::SetName( string name ) +{ + m_name = name; +} + +string FileSystemThing::GetName() +{ + return m_name; +} + +string FileSystemThing::GetType() +{ + return m_type; +} + +void FileSystemThing::Display() +{ + cout << m_name; +} + +void FileSystemThing::Details() +{ + cout << "Name: " << m_name << endl; + cout << "Type: " << m_type << endl; +} diff --git a/labs/08 Recursion/Recursion_Lab_Starter/FileSystemThing.hpp b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemThing.hpp new file mode 100644 index 0000000000000000000000000000000000000000..aa320b1794eee5ad04788f8f3d57754046182d92 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/FileSystemThing.hpp @@ -0,0 +1,27 @@ +#ifndef _FILE_SYSTEM_THING +#define _FILE_SYSTEM_THING + +#include +#include +using namespace std; + +class FileSystemThing +{ + public: + FileSystemThing(); + FileSystemThing( string name, string type ); + + void SetName( string name ); + string GetName(); + + string GetType(); + + void Display(); + void Details(); + + protected: + string m_name; + string m_type; +}; + +#endif diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Folder.cpp b/labs/08 Recursion/Recursion_Lab_Starter/Folder.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f364a908be452bd565e118df8463a777abdefc1 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Folder.cpp @@ -0,0 +1,41 @@ +#include "Folder.hpp" + +Folder::Folder() {} +Folder::Folder( string name ) + : FileSystemThing( name, "Folder" ) +{ +} + +void Folder::Display() +{ + FileSystemThing::Display(); + cout << "/"; +} + +void Folder::Details() +{ + FileSystemThing::Details(); + cout << "Total subfolders: " << m_childFolders.size() << endl; + cout << "Total files: " << m_childFiles.size() << endl; + cout << endl; +} + +void Folder::AddFile( File* ptrFile ) +{ + m_childFiles.push_back( ptrFile ); +} + +void Folder::AddFolder( Folder* ptrFolder ) +{ + m_childFolders.push_back( ptrFolder ); +} + +vector& Folder::GetFolders() +{ + return m_childFolders; +} + +vector& Folder::GetFiles() +{ + return m_childFiles; +} diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Folder.hpp b/labs/08 Recursion/Recursion_Lab_Starter/Folder.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d3cd64eece1a147f609a0ae06287819aae9920cd --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Folder.hpp @@ -0,0 +1,32 @@ +#ifndef _FOLDER +#define _FOLDER + +#include +using namespace std; + +#include "FileSystemThing.hpp" +#include "File.hpp" + +class Folder : public FileSystemThing +{ + public: + Folder(); + Folder( string name ); + + void Display(); + void Details(); + + void AddFile( File* ptrFile ); + void AddFolder( Folder* ptrFolder ); + + vector& GetFolders(); + vector& GetFiles(); + + protected: + vector m_childFolders; + vector m_childFiles; + + friend class FileSystemManager; +}; + +#endif diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.cbp b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.cbp new file mode 100644 index 0000000000000000000000000000000000000000..73bb7f41bed6bdb320298f077c3983c13f24c994 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.cbp @@ -0,0 +1,51 @@ + + + + + + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.cbp.save b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.cbp.save new file mode 100644 index 0000000000000000000000000000000000000000..07964db87cc010ba43e3455c05479364d4c3bca5 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.cbp.save @@ -0,0 +1,51 @@ + + + + + + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.depend b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.depend new file mode 100644 index 0000000000000000000000000000000000000000..e7957302586a4b3797448edf7f5a6814a3ffc976 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.depend @@ -0,0 +1,78 @@ +# depslib dependency file v1.0 +1600824253 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/Menu.cpp + "Menu.hpp" + +1600927610 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/Menu.hpp + + + + + + + + +1601400108 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/main.cpp + + "FileSystemManager.hpp" + "utilities/Menu.hpp" + "utilities/Logger.hpp" + +1601401424 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/FileSystemManager.hpp + "File.hpp" + "Folder.hpp" + + +1601399885 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/File.hpp + "FileSystemThing.hpp" + +1601399902 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/FileSystemThing.hpp + + + +1601399934 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/Folder.hpp + + "FileSystemThing.hpp" + "File.hpp" + +1601397528 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/StringGenerator.hpp + + +1601400123 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/StringGenerator.cpp + "StringGenerator.hpp" + + + + +1601401495 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/FileSystemManager.cpp + "FileSystemManager.hpp" + "utilities/Logger.hpp" + "utilities/StringUtil.hpp" + +1601400027 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/FileSystemThing.cpp + "FileSystemThing.hpp" + +1601400022 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/Folder.cpp + "Folder.hpp" + +1601400034 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/File.cpp + "File.hpp" + +1600665288 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/Logger.hpp + + + + + + +1600665380 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/Logger.cpp + "Logger.hpp" + +1600814300 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/StringUtil.cpp + "StringUtil.hpp" + + + +1600814276 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Recursion/Recursion_Lab/utilities/StringUtil.hpp + + + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.layout b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.layout new file mode 100644 index 0000000000000000000000000000000000000000..e030cf8ab113d3742ade5925dd3a98014d22a835 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.layout @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.layout.save b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.layout.save new file mode 100644 index 0000000000000000000000000000000000000000..bdcdc7b0a6bcc58a096691473f60df2369786402 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/Recursion.layout.save @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/log.html b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/log.html new file mode 100644 index 0000000000000000000000000000000000000000..5dfbcf9d1eb67deaa0bb86896d19b93e83c6f8a1 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_CodeBlocks/log.html @@ -0,0 +1,95 @@ +LOG Sep 29 2020 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab.sln b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab.sln new file mode 100644 index 0000000000000000000000000000000000000000..d29537a9ac06a18825b0ad5fcff2b42ce15bdaad --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RecursionLab", "RecursionLab\RecursionLab.vcxproj", "{3B3CF74A-8E12-429A-806E-50836DDC0CF0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Debug|x64.ActiveCfg = Debug|x64 + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Debug|x64.Build.0 = Debug|x64 + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Debug|x86.ActiveCfg = Debug|Win32 + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Debug|x86.Build.0 = Debug|Win32 + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Release|x64.ActiveCfg = Release|x64 + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Release|x64.Build.0 = Release|x64 + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Release|x86.ActiveCfg = Release|Win32 + {3B3CF74A-8E12-429A-806E-50836DDC0CF0}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B0F8CC1D-9F37-4AA7-BC71-7C38ACB6E4E9} + EndGlobalSection +EndGlobal diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/RecursionLab.vcxproj b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/RecursionLab.vcxproj new file mode 100644 index 0000000000000000000000000000000000000000..c46d6374d11894e560c0f80568c6e39c1b0ce335 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/RecursionLab.vcxproj @@ -0,0 +1,163 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {3b3cf74a-8e12-429a-806e-50836ddc0cf0} + RecursionLab + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/RecursionLab.vcxproj.filters b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/RecursionLab.vcxproj.filters new file mode 100644 index 0000000000000000000000000000000000000000..36392b27c6eb873e56a9ced9e72c36c6d168c7bd --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/RecursionLab.vcxproj.filters @@ -0,0 +1,66 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/log.html b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/log.html new file mode 100644 index 0000000000000000000000000000000000000000..a7fddd3f18fa05931bb338039bd10c3f8d4bf507 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/Project_VisualStudio/RecursionLab/log.html @@ -0,0 +1,74 @@ +LOG Sep 29 2020 + +
TIMELOCATIONMESSAGE
13:17:30Logger::SetupLogging Begins
13:17:30FileSystemManager::FileSystemManager21 files created
13:17:30FileSystemManager::FileSystemManagerFile 0: homework1.odt
13:17:30FileSystemManager::FileSystemManagerFile 1: homework2.odt
13:17:30FileSystemManager::FileSystemManagerFile 2: transcript.odt
13:17:30FileSystemManager::FileSystemManagerFile 3: references.odt
13:17:30FileSystemManager::FileSystemManagerFile 4: resume.odt
13:17:30FileSystemManager::FileSystemManagerFile 5: logfile.txt
13:17:30FileSystemManager::FileSystemManagerFile 6: complaints.csv
13:17:30FileSystemManager::FileSystemManagerFile 7: posting.txt
13:17:30FileSystemManager::FileSystemManagerFile 8: january.csv
13:17:30FileSystemManager::FileSystemManagerFile 9: february.csv
13:17:30FileSystemManager::FileSystemManagerFile 10: march.csv
13:17:30FileSystemManager::FileSystemManagerFile 11: todo.txt
13:17:30FileSystemManager::FileSystemManagerFile 12: done.txt
13:17:30FileSystemManager::FileSystemManagerFile 13: coolbaskets.jpg
13:17:30FileSystemManager::FileSystemManagerFile 14: esperanto.txt
13:17:30FileSystemManager::FileSystemManagerFile 15: ido.txt
13:17:30FileSystemManager::FileSystemManagerFile 16: tokipona.txt
13:17:30FileSystemManager::FileSystemManagerFile 17: laadan.txt
13:17:30FileSystemManager::FileSystemManagerFile 18: notpirated.mp4
13:17:30FileSystemManager::FileSystemManagerFile 19: notavirus.exe
13:17:30FileSystemManager::FileSystemManagerFile 20: screenshot.png
13:17:30FileSystemManager::FileSystemManager12 folders created
13:17:30FileSystemManager::FileSystemManagerFolder 0: home
13:17:30FileSystemManager::FileSystemManagerFolder 1: school
13:17:30FileSystemManager::FileSystemManagerFolder 2: cs235
13:17:30FileSystemManager::FileSystemManagerFolder 3: cs210
13:17:30FileSystemManager::FileSystemManagerFolder 4: work
13:17:30FileSystemManager::FileSystemManagerFolder 5: dayjob
13:17:30FileSystemManager::FileSystemManagerFolder 6: sidegig
13:17:30FileSystemManager::FileSystemManagerFolder 7: billing
13:17:30FileSystemManager::FileSystemManagerFolder 8: hobbies
13:17:30FileSystemManager::FileSystemManagerFolder 9: basketweaving
13:17:30FileSystemManager::FileSystemManagerFolder 10: constructed-languages
13:17:30FileSystemManager::FileSystemManagerFolder 11: files
13:17:32FileSystemManager::DisplayDisplay filesystem
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: home
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: school
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: cs235
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: homework1.odt
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: cs210
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: homework2.odt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: transcript.odt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: references.odt
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: work
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: dayjob
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: logfile.txt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: complaints.csv
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: sidegig
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: billing
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: january.csv
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: february.csv
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: march.csv
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: posting.txt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: resume.odt
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: hobbies
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: basketweaving
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: coolbaskets.jpg
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: constructed-languages
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: esperanto.txt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: ido.txt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: tokipona.txt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: laadan.txt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: todo.txt
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: done.txt
13:17:32FileSystemManager::Recursive_DisplayFolderDisplay folder: files
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: notpirated.mp4
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: notavirus.exe
13:17:32FileSystemManager::Recursive_DisplayFileDisplay file: screenshot.png
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TIMELOCATIONMESSAGE
12:42:53Logger::SetupLogging Begins
12:42:53FileSystemManager::FileSystemManager21 files created
12:42:53FileSystemManager::FileSystemManagerFile 0: homework1.odt
12:42:53FileSystemManager::FileSystemManagerFile 1: homework2.odt
12:42:53FileSystemManager::FileSystemManagerFile 2: transcript.odt
12:42:53FileSystemManager::FileSystemManagerFile 3: references.odt
12:42:53FileSystemManager::FileSystemManagerFile 4: resume.odt
12:42:53FileSystemManager::FileSystemManagerFile 5: logfile.txt
12:42:53FileSystemManager::FileSystemManagerFile 6: complaints.csv
12:42:53FileSystemManager::FileSystemManagerFile 7: posting.txt
12:42:53FileSystemManager::FileSystemManagerFile 8: january.csv
12:42:53FileSystemManager::FileSystemManagerFile 9: february.csv
12:42:53FileSystemManager::FileSystemManagerFile 10: march.csv
12:42:53FileSystemManager::FileSystemManagerFile 11: todo.txt
12:42:53FileSystemManager::FileSystemManagerFile 12: done.txt
12:42:53FileSystemManager::FileSystemManagerFile 13: coolbaskets.jpg
12:42:53FileSystemManager::FileSystemManagerFile 14: esperanto.txt
12:42:53FileSystemManager::FileSystemManagerFile 15: ido.txt
12:42:53FileSystemManager::FileSystemManagerFile 16: tokipona.txt
12:42:53FileSystemManager::FileSystemManagerFile 17: laadan.txt
12:42:53FileSystemManager::FileSystemManagerFile 18: notpirated.mp4
12:42:53FileSystemManager::FileSystemManagerFile 19: notavirus.exe
12:42:53FileSystemManager::FileSystemManagerFile 20: screenshot.png
12:42:53FileSystemManager::FileSystemManager12 folders created
12:42:53FileSystemManager::FileSystemManagerFolder 0: home
12:42:53FileSystemManager::FileSystemManagerFolder 1: school
12:42:53FileSystemManager::FileSystemManagerFolder 2: cs235
12:42:53FileSystemManager::FileSystemManagerFolder 3: cs210
12:42:53FileSystemManager::FileSystemManagerFolder 4: work
12:42:53FileSystemManager::FileSystemManagerFolder 5: dayjob
12:42:53FileSystemManager::FileSystemManagerFolder 6: sidegig
12:42:53FileSystemManager::FileSystemManagerFolder 7: billing
12:42:53FileSystemManager::FileSystemManagerFolder 8: hobbies
12:42:53FileSystemManager::FileSystemManagerFolder 9: basketweaving
12:42:53FileSystemManager::FileSystemManagerFolder 10: constructed-languages
12:42:53FileSystemManager::FileSystemManagerFolder 11: files
12:42:57FileSystemManager::FindFileFind file: todo.txt
12:42:57FileSystemManager::Recursive_FindFileSearching folder: home
12:42:57FileSystemManager::Recursive_FindFileSearching folder: school
12:42:57FileSystemManager::Recursive_FindFileSearching folder: cs235
12:42:57FileSystemManager::Recursive_FindFileSearching folder: cs210
12:42:57FileSystemManager::Recursive_FindFileSearching folder: work
12:42:57FileSystemManager::Recursive_FindFileSearching folder: dayjob
12:42:57FileSystemManager::Recursive_FindFileSearching folder: sidegig
12:42:57FileSystemManager::Recursive_FindFileSearching folder: billing
12:42:57FileSystemManager::Recursive_FindFileSearching folder: hobbies
12:43:00Logger::CleanupLogging Ends
+ diff --git a/labs/08 Recursion/Recursion_Lab_Starter/main.cpp b/labs/08 Recursion/Recursion_Lab_Starter/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d1e4f305ab0cfba61382c9c1dea4cc153ab023e --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/main.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +#include "FileSystemManager.hpp" +#include "utilities/Menu.hpp" +#include "utilities/Logger.hpp" + +int main() +{ + Logger::Setup(); + bool done = false; + FileSystemManager filesystem; + + while ( !done ) + { + Menu::ClearScreen(); + Menu::Header( "MAIN MENU" ); + + string choice = Menu::ShowStringMenuWithPrompt( { + "Search for file", + "Search for folder", + "View filesystem", + "Quit" + } ); + + if ( choice == "Search for file" ) + { + string name = Menu::GetStringChoice( "Enter file name:" ); + File* ptrFile = filesystem.FindFile( name ); + + cout << "Address: " << ptrFile << endl; + if ( ptrFile != nullptr ) + { + ptrFile->Details(); + } + } + else if ( choice == "Search for folder" ) + { + string name = Menu::GetStringChoice( "Enter folder name:" ); + Folder* ptrFolder = filesystem.FindFolder( name ); + + cout << "Address: " << ptrFolder << endl; + if ( ptrFolder != nullptr ) + { + ptrFolder->Details(); + } + } + else if ( choice == "View filesystem" ) + { + filesystem.Display(); + } + else if ( choice == "Quit" ) + { + done = true; + } + + Menu::Pause(); + } + + Logger::Cleanup(); + return 0; +} diff --git a/labs/08 Recursion/Recursion_Lab_Starter/utilities/Logger.cpp b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Logger.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cf1f6d4d028e93f233f1d47f387359872c7a6533 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Logger.cpp @@ -0,0 +1,197 @@ +#include "Logger.hpp" + +std::ofstream Logger::m_file; +time_t Logger::m_startTime; +time_t Logger::m_lastTimestamp; +int Logger::m_logLevel; +std::string Logger::m_categoryFilter; +int Logger::m_rowCount; +bool Logger::m_isLoud; + +/* + m_logLevel: + * 0: Mundane +*/ + +void Logger::Setup( bool isLoud ) +{ + m_rowCount = 0; + m_logLevel = 0; + m_file.open( "log.html" ); + m_startTime = GetTimestamp(); + m_lastTimestamp = m_startTime; + m_isLoud = isLoud; + + m_file << "LOG " << __DATE__ << "" << std::endl; + m_file << "" << std::endl; + m_file << "" << std::endl; + m_file << "" + << "" + << "" + << "" + << "" << std::endl; + Out( "Logging Begins", "Logger::Setup" ); +} + +void Logger::Setup( int logLevel, const std::string& filter ) +{ + Setup(); + SetLogLevel( logLevel ); + SetFilterWord( filter ); +} + +void Logger::SetLogLevel( int val ) +{ + m_logLevel = val; +} + +void Logger::SetFilterWord( const std::string& filter ) +{ + Out( "Setting filter to only display messages of category \"" + filter + "\"" ); + m_categoryFilter = filter; +} + +void Logger::Cleanup() +{ + Out( "Logging Ends", "Logger::Cleanup" ); + m_file << "
TIMELOCATIONMESSAGE
" << std::endl; + m_file << "" << std::endl; + m_file.close(); +} + +void Logger::OutHighlight( const std::string& message, const std::string& location, int color /* = 1 */ ) +{ + if ( m_isLoud ) + { + std::cout << GetFormattedTimestamp(); + if ( location != "" ) { std::cout << " @ " << location; } + std::cout << std::endl << " " << message << std::endl << std::endl; + } + + std::string loc = location; + if ( loc == "" ) { loc = "-"; } + + m_file << "" + << "" << GetFormattedTimestamp() << "" + << "" << loc << "" + << "" << message << "" + << "" << std::endl; + + m_rowCount++; +} + +void Logger::Out( const std::string& message, const std::string& location /* = "" */, const std::string& category /* = "" */, bool condition /* = true */, int level /* = 0 */ ) +{ + if ( m_categoryFilter.size() > 0 ) + { + // Filter is active + + if ( category.size() == 0 + || m_categoryFilter.find( category ) == std::string::npos ) + { + return; + } + } + + if ( level < m_logLevel ) + { + return; + } + + if ( condition ) + { + if ( m_isLoud ) + { + std::cout << GetFormattedTimestamp(); + if ( location != "" ) { std::cout << " @ " << location; } + std::cout << std::endl << " " << message << std::endl << std::endl; + } + + std::string loc = location; + if ( loc == "" ) { loc = "-"; } + + std::string rowClass = ( m_rowCount % 2 == 0 ) ? "" : "odd"; + + m_file << "" + << "" << GetFormattedTimestamp() << "" + << "" << loc << "" + << "" << message << "" + << "" << std::endl; + + m_rowCount++; + } +} + +void Logger::Error( const std::string& message, const std::string& location /* = "" */ ) +{ + std::cerr << "** " << GetTimestamp() << "\t" << message; + if ( location != "" ) { std::cerr << " @ " << location; } + std::cerr << "\t LINE " << __LINE__ << " FILE " << __FILE__ ; + std::cerr << std::endl; + + std::string loc = location; + if ( loc == "" ) { loc = "-"; } + + m_file << "" + << "" << GetFormattedTimestamp() << "" + << "" << loc << "" + << "" << message << "" + << "" << std::endl; +} + +std::string Logger::GetFormattedTimestamp() +{ + + #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + time_t timestamp = GetTimestamp(); + struct tm timeinfo; + char buffer[80]; + time( ×tamp ); + localtime_s ( &timeinfo, ×tamp ); + // http://www.cplusplus.com/reference/ctime/strftime/ + strftime( buffer, 80, "%H:%M:%S", &timeinfo ); + + std::string str( buffer ); + return str; + #else + time_t timestamp = GetTimestamp(); + struct tm* timeinfo; + char buffer[80]; + time( ×tamp ); + timeinfo = localtime ( ×tamp ); + // http://www.cplusplus.com/reference/ctime/strftime/ + strftime( buffer, 80, "%H:%M:%S", timeinfo ); + + std::string str( buffer ); + return str; + #endif + +} + +double Logger::GetTimestamp() +{ + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + return std::chrono::system_clock::to_time_t( now ); +} diff --git a/labs/08 Recursion/Recursion_Lab_Starter/utilities/Logger.hpp b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Logger.hpp new file mode 100755 index 0000000000000000000000000000000000000000..a00295aaf84e82bea7b7a04af29b7cb9d32187d1 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Logger.hpp @@ -0,0 +1,41 @@ +#ifndef _KUKO_LOGGER +#define _KUKO_LOGGER + +#include +#include +#include +#include +#include + +class Logger +{ + public: + static void Setup( bool loud = false ); + static void Setup( int logLevel, const std::string& filter ); + static void Cleanup(); + static void SetLogLevel( int val ); + static void SetFilterWord( const std::string& filter ); + + static void Out( const std::string& message, const std::string& location = "", const std::string& category = "", bool condition = true, int level = 0 ); + static void OutHighlight( const std::string& message, const std::string& location = "", int color = 1); + static void Error( const std::string& message, const std::string& location = "" ); + + static double GetTimestamp(); + static std::string GetFormattedTimestamp(); + + private: + static std::ofstream m_file; + static time_t m_startTime; + static time_t m_lastTimestamp; + static int m_logLevel; + static std::string m_categoryFilter; + static int m_rowCount; + static bool m_isLoud; +}; + +// Shortcut expressions for logger +#define LOG( msg ) dal::Logger::Out( msg ); +#define LOGLOC( msg, loc ) dal::Logger::Out( msg, loc ); +#define ERR( msg, loc ) dal::Logger::Error( msg, loc ); + +#endif diff --git a/labs/08 Recursion/Recursion_Lab_Starter/utilities/Menu.cpp b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Menu.cpp new file mode 100755 index 0000000000000000000000000000000000000000..65ad95045e36bb8831db9a46e5e82419efb612bd --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Menu.cpp @@ -0,0 +1,171 @@ +#include "Menu.hpp" + +void Menu::Header( const string& header ) +{ + DrawHorizontalBar( 80 ); + string head = "| " + header + " |"; + cout << " " << head << endl << " "; + DrawHorizontalBar( head.size() ); + cout << endl; +} + +void Menu::DrawHorizontalBar( int width, char symbol ) +{ + for ( int i = 0; i < width; i++ ) + { + cout << symbol; + } + cout << endl; +} + +// MENUS and INPUT/OUTPUT +void Menu::ShowMenu( const vector options, bool vertical ) +{ + if ( vertical ) + { + for ( unsigned int i = 0; i < options.size(); i++ ) + { + cout << " " << (i+1) << ".\t" << options[i] << endl; + } + } + else + { + for ( unsigned int i = 0; i < options.size(); i++ ) + { + cout << " " << (i+1) << ". " << options[i] << "\t"; + } + cout << endl; + } +} + +int Menu::ShowIntMenuWithPrompt( const vector options, bool vertical ) +{ + ShowMenu( options, vertical ); + int choice = GetValidChoice( 1, options.size() ); + return choice; +} + +string Menu::ShowStringMenuWithPrompt( const vector options, bool vertical ) +{ + ShowMenu( options, vertical ); + int choice = GetValidChoice( 1, options.size() ); + string value = options[ choice-1 ]; + return value; +} + +/* + * Call looks like this: + +Menu::ShowCallbackMenuWithPrompt( { + { "Set text", bind( &Program::Menu_SetText, this ) }, + { "Get text", bind( &Program::Menu_GetText, this ) } +} )(); +* + * */ +function Menu::ShowCallbackMenuWithPrompt( map > options, bool vertical ) +{ + vector menuOptions; + for ( auto& opt : options ) + { + menuOptions.push_back( opt.first ); + } + + ShowMenu( menuOptions, vertical ); + int choice = GetValidChoice( 1, options.size() ); + string key = menuOptions[ choice-1 ]; + + return options[ key ]; +} + +int Menu::GetValidChoice( int min, int max, const string& message ) +{ + if ( message != "" ) + { + cout << endl; + DrawHorizontalBar( message.size() + 2 ); + cout << " " << message << endl; + } + + int choice = GetIntChoice(); + + while ( choice < min || choice > max ) + { + cout << "Invalid selection. Try again." << endl; + choice = GetIntChoice(); + } + + return choice; +} + +string Menu::GetStringChoice( const string& message ) +{ + if ( message != "" ) + { + cout << " " << message << endl; + } + + cout << endl << " >> "; + string choice; + cin >> choice; + cin.ignore(); + cout << endl; + return choice; +} + +string Menu::GetStringLine( const string& message ) +{ + if ( message != "" ) + { + cout << " " << message << endl; + } + string choice; + cout << endl << " >> "; + getline( cin, choice ); + cout << endl; + return choice; +} + +int Menu::GetIntChoice( const string& message ) +{ + if ( message != "" ) + { + cout << " " << message << endl; + } + cout << endl << " >> "; + int choice; + cin >> choice; + cin.ignore(); + cout << endl; + return choice; +} + +// HANDY TRICKS +void Menu::ClearScreen() +{ + #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + system( "cls" ); + #else + system( "clear" ); + #endif +} + +void Menu::Pause() +{ + #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + system( "pause" ); + #else + cout << endl << " Press ENTER to continue..." << endl; + cin.ignore( std::numeric_limits ::max(), '\n' ); + #endif +} + +void Menu::PrintPwd() +{ + #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + system( "echo %cd%" ); + #else + system( "pwd" ); + #endif +} + + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/utilities/Menu.hpp b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Menu.hpp new file mode 100755 index 0000000000000000000000000000000000000000..5d1c4d6abdd000b1216c824f7ce98cd9b8e0ba9e --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/utilities/Menu.hpp @@ -0,0 +1,39 @@ +#ifndef _MENU +#define _MENU + +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +class Menu +{ + public: + // OUTPUT + static void Header( const string& header ); + static void DrawHorizontalBar( int width, char symbol = '-' ); + + // MENUS and INPUT/OUTPUT + static void ShowMenu( const vector options, bool vertical = true ); + static int ShowIntMenuWithPrompt( const vector options, bool vertical = true ); + static string ShowStringMenuWithPrompt( const vector options, bool vertical = true ); + static function ShowCallbackMenuWithPrompt( map > options, bool vertical = true ); + static int GetValidChoice( int min, int max, const string& message = "" ); + static string GetStringChoice( const string& message = "" ); + static string GetStringLine( const string& message = "" ); + static int GetIntChoice( const string& message = "" ); + + // HANDY TRICKS + static void ClearScreen(); + static void Pause(); + static void PrintPwd(); +}; + + + +#endif + diff --git a/labs/08 Recursion/Recursion_Lab_Starter/utilities/StringUtil.cpp b/labs/08 Recursion/Recursion_Lab_Starter/utilities/StringUtil.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ff93b8f2fcfd503854b91f39f150e83883f66d13 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/utilities/StringUtil.cpp @@ -0,0 +1,48 @@ +#include "StringUtil.hpp" + +#include +#include +using namespace std; + +int StringUtil::StringToInt( const string& str ) +{ + istringstream ss( str ); + int val; + ss >> val; + return val; +} + +string StringUtil::ToUpper( const string& val ) +{ + string upper = ""; + for ( unsigned int i = 0; i < val.size(); i++ ) + { + upper += toupper( val[i] ); + } + return upper; +} + +string StringUtil::ToLower( const string& val ) +{ + string upper = ""; + for ( unsigned int i = 0; i < val.size(); i++ ) + { + upper += tolower( val[i] ); + } + return upper; +} + +string StringUtil::ColumnText( int colWidth, const string& text ) +{ + string adjusted = text; + for ( int i = 0; i < colWidth - text.size(); i++ ) + { + adjusted += " "; + } + return adjusted; +} + +bool StringUtil::Contains( string haystack, string needle ) +{ + return ( haystack.find( needle ) != string::npos ); +} diff --git a/labs/08 Recursion/Recursion_Lab_Starter/utilities/StringUtil.hpp b/labs/08 Recursion/Recursion_Lab_Starter/utilities/StringUtil.hpp new file mode 100755 index 0000000000000000000000000000000000000000..4fe37502abd5137b469888153204c957f06f6f54 --- /dev/null +++ b/labs/08 Recursion/Recursion_Lab_Starter/utilities/StringUtil.hpp @@ -0,0 +1,28 @@ +#ifndef _STRINGS +#define _STRINGS + +#include +#include +using namespace std; + +class StringUtil +{ + public: + template + static string ToString( const T& value ); + static int StringToInt( const string& str ); + static string ToUpper( const string& val ); + static string ToLower( const string& val ); + static string ColumnText( int colWidth, const string& text ); + static bool Contains( string haystack, string needle ); +}; + +template +string StringUtil::ToString( const T& value ) +{ + stringstream ss; + ss << value; + return ss.str(); +} + +#endif