Skip to content
Open
7 changes: 4 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- otus_dz_2
- feature/github_actions

jobs:
Expand Down Expand Up @@ -35,6 +36,6 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./helloworld-0.0.${{ github.run_number }}-Linux.deb
asset_name: helloworld-0.0.${{ github.run_number }}-Linux.deb
asset_content_type: application/vnd.debian.binary-package
asset_path: ./ipfilter-0.0.${{ github.run_number }}-Linux.deb
asset_name: ipfilter-0.0.${{ github.run_number }}-Linux.deb
asset_content_type: application/vnd.debian.binary-package
26 changes: 13 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ cmake_minimum_required(VERSION 3.12)
set(PATCH_VERSION "1" CACHE INTERNAL "Patch version")
set(PROJECT_VESRION 0.0.${PATCH_VERSION})

project(helloworld VERSION ${PROJECT_VESRION})
project(ipfilter VERSION ${PROJECT_VESRION})

option(WITH_BOOST_TEST "Whether to build Boost test" ON)

configure_file(version.h.in version.h)

add_executable(helloworld_cli main.cpp)
add_library(helloworld lib.cpp)
add_executable(ipfilter_cli ip_filter.cpp)
add_library(ipfilter lib.cpp)

set_target_properties(helloworld_cli helloworld PROPERTIES
set_target_properties(ipfilter_cli ipfilter PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
)

target_include_directories(helloworld
target_include_directories(ipfilter
PRIVATE "${CMAKE_BINARY_DIR}"
)

target_link_libraries(helloworld_cli PRIVATE
helloworld
target_link_libraries(ipfilter_cli PRIVATE
ipfilter
)

if(WITH_BOOST_TEST)
Expand All @@ -41,15 +41,15 @@ if(WITH_BOOST_TEST)

target_link_libraries(test_version
${Boost_LIBRARIES}
helloworld
ipfilter
)
endif()

if (MSVC)
target_compile_options(helloworld_cli PRIVATE
target_compile_options(ipfilter_cli PRIVATE
/W4
)
target_compile_options(helloworld PRIVATE
target_compile_options(ipfilter PRIVATE
/W4
)
if(WITH_BOOST_TEST)
Expand All @@ -58,10 +58,10 @@ if (MSVC)
)
endif()
else ()
target_compile_options(helloworld_cli PRIVATE
target_compile_options(ipfilter_cli PRIVATE
-Wall -Wextra -pedantic -Werror
)
target_compile_options(helloworld PRIVATE
target_compile_options(ipfilter PRIVATE
-Wall -Wextra -pedantic -Werror
)
if(WITH_BOOST_TEST)
Expand All @@ -71,7 +71,7 @@ else ()
endif()
endif()

install(TARGETS helloworld_cli RUNTIME DESTINATION bin)
install(TARGETS ipfilter_cli RUNTIME DESTINATION bin)

set(CPACK_GENERATOR DEB)
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
Expand Down
163 changes: 163 additions & 0 deletions ip_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include <iostream>
#include <string>
#include <vector>
#include <cstdint>
#include <sstream>
#include <algorithm>

#include "lib.h"

class IpAddress
{
uint16_t first;
uint16_t second;
uint16_t third;
uint16_t fourth;

public:

IpAddress()
{
this->first = 0;
this->second = 0;
this->third = 0;
this->fourth = 0;
}

IpAddress(std::string ip_address)
{
sscanf(ip_address.c_str(), "%hu.%hu.%hu.%hu", &this->first, &this->second, &this->third, &this->fourth);
}

void print() const
{
std::cout << this->first << "." << this->second << "." << this->third << "." << this->fourth << std::endl;
}

std::string getIpString() const
{
std::ostringstream ip;

ip << static_cast<unsigned>(first) << '.'
<< static_cast<unsigned>(second) << '.'
<< static_cast<unsigned>(third) << '.'
<< static_cast<unsigned>(fourth);

return ip.str();
}


bool operator==(const IpAddress& other)
{
return first == other.getFirst() && second == other.getSecond() && third == other.getThird() && fourth == other.getFourth();
}

bool operator<(const IpAddress& other)
{
if(first != other.getFirst()) return other.getFirst() < first;
if(second != other.getSecond()) return other.getSecond() < second;
if(third != other.getThird()) return other.getThird() < third;
return other.getFourth() < fourth;
}

uint16_t getFirst() const
{
return first;
}


uint16_t getSecond() const
{
return second;
}

uint16_t getThird() const
{
return third;
}

uint16_t getFourth() const
{
return fourth;
}
};

std::string parseIpStr(std::string str)
{
size_t pos = str.find('\t');
if (pos == str.npos)
{
return str;
}
return str.substr(0, pos);
}

bool isFirstByteEqualTo(const IpAddress& ip, uint16_t value)
{
return ip.getFirst() == value;
}

bool isFirstAndSecondBytesEqualTo(const IpAddress& ip, uint16_t first_value, uint16_t second_value)
{
return ip.getFirst() == first_value && ip.getSecond() == second_value;
}

bool isAnyByteEqualTo(const IpAddress& ip, uint16_t value)
{
return ip.getFirst() == value || ip.getSecond() == value || ip.getThird() == value || ip.getFourth() == value;
}

int main(int,char **)
{
std::string input;
std::vector<IpAddress> ips;

std::vector<std::string> ips_cond1;
std::vector<std::string> ips_cond2;
std::vector<std::string> ips_cond3;


while(std::getline(std::cin, input))
{
ips.push_back(IpAddress{parseIpStr(input)});
}

std::sort(ips.begin(), ips.end());

for(auto& ip : ips)
{
ip.print();

if (isFirstAndSecondBytesEqualTo(ip,46,70))
{
ips_cond2.push_back(ip.getIpString());
}
else if (isFirstByteEqualTo(ip,1))
{
ips_cond1.push_back(ip.getIpString());
}


if (isAnyByteEqualTo(ip,46))
{
ips_cond3.push_back(ip.getIpString());
}
}

for(auto& ip : ips_cond1)
{
std::cout << ip << std::endl;
}

for(auto& ip : ips_cond2)
{
std::cout << ip << std::endl;
}

for(auto& ip : ips_cond3)
{
std::cout << ip << std::endl;
}

return 0;
}
Loading