Merge pull request 'rust_lang' (#1) from rust_lang into main

Reviewed-on: #1
This commit is contained in:
phoenix
2023-08-27 15:47:10 +00:00
11 changed files with 240 additions and 450 deletions
+1
View File
@@ -0,0 +1 @@
/target
-3
View File
@@ -1,3 +0,0 @@
[submodule "3rdparty/vcpkg"]
path = 3rdparty/vcpkg
url = https://github.com/microsoft/vcpkg
-1
Submodule 3rdparty/vcpkg deleted from f14984af37
-27
View File
@@ -1,27 +0,0 @@
cmake_minimum_required(VERSION 3.18.4)
if(NOT DEFINED VCPKG_ROOT_PATH)
set(VCPKG_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/vcpkg)
endif()
if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(TOOLCHAIN_PATH ${VCPKG_ROOT_PATH}/scripts/buildsystems/vcpkg.cmake)
set(CMAKE_TOOLCHAIN_FILE
"${TOOLCHAIN_PATH}"
CACHE STRING "Vcpkg toolchain file")
message("Using default toolchain file")
endif()
project(clean_files CXX)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
find_package(nlohmann_json CONFIG REQUIRED)
add_executable(clean_files src/main.cpp)
target_link_libraries(clean_files PRIVATE nlohmann_json nlohmann_json::nlohmann_json)
+13
View File
@@ -0,0 +1,13 @@
[package]
name = "clean_file"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.188", features = ["derive"] }
# serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
-31
View File
@@ -1,31 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "clean_file", "clean_file.vcxproj", "{27781E3A-B63A-4158-A915-7A4A9DA6D187}"
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
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x64.ActiveCfg = Debug|x64
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x64.Build.0 = Debug|x64
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x86.ActiveCfg = Debug|Win32
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x86.Build.0 = Debug|Win32
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x64.ActiveCfg = Release|x64
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x64.Build.0 = Release|x64
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x86.ActiveCfg = Release|Win32
{27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FD15EEA8-C66D-4B6F-A98D-227633B840D6}
EndGlobalSection
EndGlobal
-115
View File
@@ -1,115 +0,0 @@
<?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>17.0</VCProjectVersion>
<ProjectGuid>{27781E3A-B63A-4158-A915-7A4A9DA6D187}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
</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>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>$(ProjectDir)inculde;$(ProjectDir);$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>$(ProjectDir)inculde;$(ProjectDir);$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<TargetMachine>MachineX86</TargetMachine>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<TargetMachine>MachineX86</TargetMachine>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\number_parser.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
-232
View File
@@ -1,232 +0,0 @@
#ifndef NUMBER_PARSER_H_
#define NUMBER_PARSER_H_
#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <fstream>
#include "nlohmann/json.hpp"
namespace parser
{
class some_object
{
public:
some_object() = default;
some_object(std::string value) :
value(value)
{
}
~some_object() = default;
std::string value;
private:
};
template<typename Obj>
class number_parser
{
public:
number_parser(const std::string &filepath) :
filepath(filepath)
{ }
template<typename Con = std::vector<Obj>>
Con file_dump()
{
Con container;
std::fstream file(this->filepath.c_str(), std::ios::in);
if (!file.is_open())
{
std::cout << "File is not openned...\n";
std::cout << "Openning file: " << this->filepath << "\n";
file.open(filepath.c_str());
}
std::string line;
int i = 0;
while (std::getline(file, line))
{
if (line.size() > 1)
{
container.push_back(line);
}
}
file.close();
return container;
}
template<typename Con = std::vector<Obj>>
Con update_values(const Con &values)
{
Con container;
std::vector<char> remove_chars;
remove_chars.reserve(4);
remove_chars.push_back(')');
remove_chars.push_back('(');
remove_chars.push_back(32);
remove_chars.push_back('-');
auto prefix = "+1";
std::string parsed;
for (const auto &item : values)
{
parsed.assign(item.value);
if (parsed.size() < 2)
{
std::cout << parsed << " is an invalid number. Continuing with the next number\n";
continue;
}
auto pars = remove_some_data(parsed, remove_chars);
parsed.clear();
parsed.assign(pars);
add_prefix(parsed);
container.push_back(parsed);
parsed.clear();
}
return container;
}
template<typename Con = std::vector<Obj>>
void remove_duplicates(Con &container)
{
std::set<std::string> tmpContainer;
auto act = [&](Obj& ob)
{
tmpContainer.emplace(ob.value);
};
std::for_each(container.begin(), container.end(), act);
container.clear();
auto actReplace = [&](std::string value)
{
Obj o(value);
container.emplace_back(o);
};
std::for_each(tmpContainer.begin(), tmpContainer.end(), actReplace);
}
template<typename Con = std::vector<Obj>>
void save_file(const Con container)
{
std::set<std::string> my_set;
const std::string filepath("my_file.json");;
for (const auto &s : container)
{
my_set.insert(s.value);
}
nlohmann::json j(my_set);
std::cout << "Saving data to file: " << filepath << "\n";
std::fstream output(filepath.c_str(), std::ios::out);
output << j.dump(4);
output.close();
std::cout << "The file has been saved\n";
}
template<typename Con = std::vector<Obj>>
void print_values(const Con container)
{
if (!(container.size() > 0))
{
std::cout << "Empty container\n";
return;
}
std::cout << "Printing values\n";
for (const auto &item : container)
{
std::cout << "Value: " << item.value << "\n";
}
}
private:
std::string remove_some_data(const std::string &unparsed, const std::vector<char> &remove_chars)
{
std::string parsed;
std::cout << "Unparsed: " << unparsed << "\n";
for (auto i = 0; i < unparsed.size(); ++i)
{
char c = unparsed.at(i);
if (std::isdigit(c))
{
parsed.append(std::to_string(c-48));
}
}
std::cout << "Parsed: " << parsed << "\n";
return parsed;
}
void add_prefix(std::string &parsed)
{
auto last_char = parsed.at(parsed.size() - 1);
auto first_char = parsed.at(0);
auto second_char = parsed.at(1);
if (first_char == '1')
{
parsed.insert(0, "+");
}
else if (first_char != '+')
{
if (second_char != '1')
{
parsed.insert(0, "+1");
}
else
{
parsed.insert(0, "+1");
}
}
}
int add_prefix_if_not_found(std::string &parsed, const std::string &prefixes)
{
auto prefix_size = prefixes.size();
auto parsed_beginning = parsed.substr(0, prefix_size);
return 0;
}
std::string filepath;
};
}
#endif
-41
View File
@@ -1,41 +0,0 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include "number_parser.hpp"
using std::cout;
using std::string;
using std::vector;
using std::fstream;
using std::stringstream;
int main(int argc, char **argv)
{
cout << "clean_files\n";
if (argc < 2)
{
cout << "Provide path to file\n";
return -1;
}
const string filepath(argv[1]);
parser::number_parser<parser::some_object> file_parser(filepath);
auto raw_values = file_parser.file_dump();
auto parsed_values = file_parser.update_values(raw_values);
file_parser.remove_duplicates(parsed_values);
file_parser.print_values(parsed_values);
file_parser.save_file(parsed_values);
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
use std::env;
mod parser;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Provide path to file");
return;
}
let filepath: &String = args.get(1).unwrap();
println!("Filepath is: {:?}", filepath);
let myparser = parser::parser::NumberParser
{
filepath: filepath.clone()
};
println!("\nDumping files");
let rawvalues = myparser.filedump();
println!("\nUpdating values");
let parsed_values = myparser.updatevalues(&rawvalues);
println!("\nRemoving duplicates");
let newupdated = myparser.removedups(&parsed_values);
println!("\nPrinting values");
myparser.print_values(&newupdated);
println!("\nSaving file");
let _result = myparser.save_file(parsed_values);
}
+190
View File
@@ -0,0 +1,190 @@
pub mod parser {
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::prelude::*;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Deserialize, Serialize)]
pub struct SomeObject {
pub value: String
}
pub struct NumberParser {
pub filepath: String
}
impl NumberParser {
pub fn filedump(&self) -> Vec<SomeObject> {
let mut objs: Vec<SomeObject> = Vec::new();
if let Ok(lines) = self.read_lines(&self.filepath) {
for line in lines {
if let Ok(phonenumber) = line {
let myobj: SomeObject = SomeObject {
value: phonenumber
};
if !myobj.value.is_empty() {
objs.push(myobj);
}
}
}
}
return objs;
}
pub fn updatevalues(&self, vals: &Vec<SomeObject>) -> Vec<SomeObject> {
let mut updated: Vec<SomeObject> = Vec::new();
for a in vals {
let mut parsed: String = a.value.clone();
if a.value.len() < 2 {
println!("{} is an invalid number", a.value);
continue;
}
let pars = self.remove_some_data(&parsed);
parsed.clear();
parsed = pars;
println!("{}", parsed.clone());
let updated_parsed = self.add_prefix(parsed);
let myobj: SomeObject = SomeObject {
value: updated_parsed.clone()
};
updated.push(myobj);
}
return updated;
}
pub fn removedups(&self, vals: &Vec<SomeObject>) -> Vec<SomeObject> {
let mut tmp: HashSet<String> = HashSet::new();
let mut mm: Vec<SomeObject> = Vec::new();
mm.clone_from(vals);
mm.retain(|e| tmp.insert(e.value.clone()));
let mut updated: Vec<SomeObject> = Vec::new();
for t in tmp {
let so: SomeObject = SomeObject {
value: t.clone()
};
updated.push(so);
}
return updated
}
pub fn print_values(&self, vals: &Vec<SomeObject>) {
for o in vals {
println!("Value: {}", o.value);
}
}
pub fn save_file(&self, vals: Vec<SomeObject>) -> std::io::Result<()> {
let filename: String = String::from("numbers.json");
let json = serde_json::to_string(&vals).unwrap();
let mut file = File::create(filename)?;
file.write_all(json.as_bytes())?;
Ok(())
}
fn read_lines(&self, filename: &str) -> std::io::Result<std::io::Lines<BufReader<File>>> {
let file = File::open(filename)?;
Ok(BufReader::new(file).lines())
}
fn remove_some_data(&self, unparsed: &String) -> String {
println!("Unparsed: {}", unparsed);
let mut parsed: String = String::new();
for i in 0..unparsed.len() {
let mychar: char = unparsed.chars().nth(i).unwrap();
if mychar.is_numeric(){
parsed.push(mychar);
}
}
println!("Parsed: {}", parsed);
return parsed;
}
fn add_prefix(&self, raw: String) -> String {
let mut mychars = raw.chars().clone();
let first_char: char = mychars.nth(0).clone().unwrap();
let second_char: char = mychars.nth(1).clone().unwrap();
let mut parsed: String = raw.clone();
if first_char == '1' {
parsed.insert(0, '+');
} else if first_char != '+' {
if second_char != '1' {
parsed.insert(0, '+');
parsed.insert(1, '1');
} else {
parsed.insert(0, '+');
parsed.insert(1, '1');
}
}
return parsed
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn removing_text() {
let sometext: String = String::from("a1234567890");
let numberparser = NumberParser { filepath: String::from("")};
let removetext = numberparser.remove_some_data(&sometext);
let mut result = true;
for i in 0..removetext.len() {
let c = removetext.chars().nth(i).unwrap();
if !c.is_numeric() {
result = false;
break;
}
}
assert_eq!(result, true);
}
#[test]
fn adding_prefix() {
let sometext: String = String::from("1234567890");
let numberparser = NumberParser { filepath: String::from("")};
let modified = numberparser.add_prefix(sometext);
let chars = modified.chars().clone();
let firstcharcheck: char = chars.clone().nth(0).unwrap();
let secondcharcheck: char = chars.clone().nth(1).unwrap();
assert_eq!(firstcharcheck, '+');
assert_eq!(secondcharcheck, '1');
}
}
}