Initial commit for WordsSorting
This commit is contained in:
39
WordsSorting/FileTools.cpp
Normal file
39
WordsSorting/FileTools.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "FileTools.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
void FileTools::ExtractWordsFromFile(const string& iFilePath, vector<string>& oWords)
|
||||
{
|
||||
ifstream wordList;
|
||||
wordList.open(iFilePath);
|
||||
if (wordList.is_open()) {
|
||||
string word;
|
||||
while (getline(wordList, word)) {
|
||||
// TODO basic checks on the words to validate/sanitise the input
|
||||
if (!word.empty()) {
|
||||
oWords.push_back(word);
|
||||
}
|
||||
}
|
||||
wordList.close();
|
||||
}
|
||||
}
|
||||
|
||||
void FileTools::SaveRseult(const vector<string>& iWords, const string& iPath, bool isReverse)
|
||||
{
|
||||
fstream fileOut;
|
||||
fileOut.open(iPath, ios_base::out);
|
||||
if (fileOut.is_open()) {
|
||||
if (isReverse) {
|
||||
for (auto wordsIt = iWords.rbegin(); wordsIt != iWords.rend(); ++wordsIt) {
|
||||
fileOut << *wordsIt << "\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (auto wordsIt = iWords.begin(); wordsIt != iWords.end(); ++wordsIt) {
|
||||
fileOut << *wordsIt << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
fileOut.close();
|
||||
}
|
||||
20
WordsSorting/FileTools.h
Normal file
20
WordsSorting/FileTools.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class FileTools
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* brief: Read the file provided and store each word/line in the output
|
||||
*/
|
||||
static void ExtractWordsFromFile(const string& iFilePath, vector<string>& oWords);
|
||||
|
||||
/*
|
||||
* brief: Save the porcessed words to an output file
|
||||
*/
|
||||
static void SaveRseult(const vector<string>& iWords, const string& iPath, bool isReverse);
|
||||
};
|
||||
|
||||
12
WordsSorting/Options.h
Normal file
12
WordsSorting/Options.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// List of hardcoded options strings
|
||||
struct Options {
|
||||
const std::string kHelp = "help";
|
||||
const std::string kReverse = "reverse";
|
||||
const std::string kUnique = "unique";
|
||||
const std::string kInput = "input";
|
||||
};
|
||||
43
WordsSorting/WordProcessing.cpp
Normal file
43
WordsSorting/WordProcessing.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "WordProcessing.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
// Comparator struct for alphabetical sorting
|
||||
// TODO support non ASCII characters in a graceful manner (accents, other alphabets, etc)
|
||||
struct AlphbeticalStringOrdering
|
||||
{
|
||||
bool operator()(string iLeft, string iRight) const
|
||||
{
|
||||
|
||||
string lowerLeft = iLeft, lowerRight = iRight;
|
||||
transform(lowerLeft.begin(), lowerLeft.end(), lowerLeft.begin(), ::tolower);
|
||||
transform(lowerRight.begin(), lowerRight.end(), lowerRight.begin(), ::tolower);
|
||||
|
||||
/* If the strings are the same in case a case insensitive comparisonthen we can keep the default/basic
|
||||
* because we will only compare the case of the letters
|
||||
* If they are different in case insensitive we use this as our basis
|
||||
* because we do not want to risk having a string starting with a 'Z' before a word starting with an 'a'
|
||||
*/
|
||||
bool leftSmaller = iLeft < iRight;
|
||||
if (lowerLeft != lowerRight)
|
||||
{
|
||||
leftSmaller = lowerLeft < lowerRight;
|
||||
}
|
||||
|
||||
return leftSmaller;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void WordProcessing::ProcessWords(bool isUnique, vector<string>& ioWords)
|
||||
{
|
||||
std::function<bool(string, string)> sorter = AlphbeticalStringOrdering();
|
||||
sort(ioWords.begin(), ioWords.end(), sorter);
|
||||
|
||||
if (isUnique)
|
||||
{
|
||||
auto eraseIt = unique(ioWords.begin(), ioWords.end());
|
||||
ioWords.erase(eraseIt, ioWords.end());
|
||||
}
|
||||
}
|
||||
15
WordsSorting/WordProcessing.h
Normal file
15
WordsSorting/WordProcessing.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class WordProcessing
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* brief: Sort and apply the possible unicity option to the words
|
||||
*/
|
||||
static void ProcessWords(bool isUnique, vector<string>& ioWords);
|
||||
};
|
||||
|
||||
83
WordsSorting/WordsSorting.cpp
Normal file
83
WordsSorting/WordsSorting.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "FileTools.h"
|
||||
#include "Options.h"
|
||||
#include "WordProcessing.h"
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
namespace po = boost::program_options;
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
int main(int ac, char* av[])
|
||||
{
|
||||
try {
|
||||
Options options;
|
||||
|
||||
// Visible options that are possible to use
|
||||
po::options_description visible("Allowed options");
|
||||
visible.add_options()
|
||||
(options.kHelp.c_str(), "produce help message")
|
||||
(options.kReverse.c_str(), "sort word list in reverse alphbetical order")
|
||||
(options.kUnique.c_str(), "delete duplicates from the word list");
|
||||
|
||||
// Hidden parameter for the source file
|
||||
po::options_description hidden("Hidden options");
|
||||
hidden.add_options()
|
||||
(options.kInput.c_str(), po::value<string>(), "input file");
|
||||
|
||||
po::options_description cmdline_options;
|
||||
cmdline_options.add(visible).add(hidden);
|
||||
|
||||
// Only expect 1 filename for the hidden parameter
|
||||
po::positional_options_description p;
|
||||
p.add(options.kInput.c_str(), 1);
|
||||
|
||||
po::variables_map vm;
|
||||
po::store(po::command_line_parser(ac, av).
|
||||
options(cmdline_options).positional(p).run(), vm);
|
||||
po::notify(vm);
|
||||
|
||||
// Only display the help if asked
|
||||
if (vm.count(options.kHelp)) {
|
||||
cout << "Usage: WordsSorting [OPTIONS] FILE\n"
|
||||
<< "Sort the list of words provided in the input file\n"
|
||||
<< "Example: WordsSorting --reverse test.txt\n\n"
|
||||
<< visible << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Input file is required
|
||||
string inputFile;
|
||||
if (vm.count(options.kInput)) {
|
||||
inputFile = vm[options.kInput].as<string>();
|
||||
}
|
||||
else {
|
||||
cerr << "Input file missing\n";
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Setting up the flags for the proccessing
|
||||
const bool isReverse = vm.count(options.kReverse);
|
||||
const bool isUnique = vm.count(options.kUnique);
|
||||
|
||||
vector<string> words;
|
||||
|
||||
FileTools::ExtractWordsFromFile(inputFile, words);
|
||||
WordProcessing::ProcessWords(isUnique, words);
|
||||
// TODO have an output parameter
|
||||
FileTools::SaveRseult(words, "out.txt", isReverse);
|
||||
|
||||
}
|
||||
catch (exception& e) {
|
||||
cerr << "error: " << e.what() << "\n";
|
||||
cout << "Use --help to see how to use this tool\n";
|
||||
return 1;
|
||||
}
|
||||
catch (...) {
|
||||
cerr << "Exception of unknown type!\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
160
WordsSorting/WordsSorting.vcxproj
Normal file
160
WordsSorting/WordsSorting.vcxproj
Normal file
@@ -0,0 +1,160 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{e0b8ade8-e356-44d8-a6b9-d26e957e1d02}</ProjectGuid>
|
||||
<RootNamespace>WordsSorting</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalIncludeDirectories>D:\Dev\libs\boost_1_76_0</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>D:\Dev\libs\boost_1_76_0\stage\lib;D:\Dev\libs\boost_1_76_0\bin.v2\libs;D:\Dev\libs\boost_1_76_0\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalIncludeDirectories>D:\Dev\libs\boost_1_76_0</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>D:\Dev\libs\boost_1_76_0\stage\lib;D:\Dev\libs\boost_1_76_0\bin.v2\libs;D:\Dev\libs\boost_1_76_0\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="FileTools.cpp" />
|
||||
<ClCompile Include="WordProcessing.cpp" />
|
||||
<ClCompile Include="WordsSorting.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Options.h" />
|
||||
<ClInclude Include="FileTools.h" />
|
||||
<ClInclude Include="WordProcessing.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
39
WordsSorting/WordsSorting.vcxproj.filters
Normal file
39
WordsSorting/WordsSorting.vcxproj.filters
Normal file
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="WordsSorting.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="FileTools.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="WordProcessing.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Options.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="FileTools.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="WordProcessing.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user