diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cef3170..e77de9e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,8 +4,16 @@ on: push: branches: - master + - test - feature/github_actions +permissions: + contents: write + actions: write + packages: write + id-token: write + pull-requests: write + jobs: build: runs-on: ubuntu-latest @@ -17,6 +25,7 @@ jobs: - run: cmake . -DPATCH_VERSION=${{ github.run_number }} -DWITH_BOOST_TEST=ON - run: cmake --build . - run: cmake --build . --target package + - run: ls -l ./ - name: Create Release id: create_release uses: actions/create-release@v1 @@ -34,6 +43,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./ip_filter-0.0.${{ github.run_number }}-Linux.deb - asset_name: ip_filter-0.0.${{ github.run_number }}-Linux.deb + asset_path: ./ip_filter-0.1.${{ github.run_number }}-Linux.deb + asset_name: ip_filter-0.1.${{ github.run_number }}-Linux.deb asset_content_type: application/vnd.debian.binary-package \ No newline at end of file diff --git a/ip_filter.cpp b/ip_filter.cpp index c9c4076..183f7fa 100644 --- a/ip_filter.cpp +++ b/ip_filter.cpp @@ -1,64 +1,56 @@ #include +#include #include #include #include +using ip_address = std::array; +using ip_pool_type = std::vector; -using my_vec = std::vector>; - -void OutPutIP(const std::vector& vec){ - for (auto ip_part = vec.cbegin(); ip_part != vec.cend(); ++ip_part) - { - if (ip_part != vec.cbegin()) std::cout << "."; - std::cout << *ip_part; +void OutPutIP(const ip_address& ip) { + for (auto it = ip.cbegin(); it != ip.cend(); ++it) { + if (it != ip.cbegin()) std::cout << "."; + std::cout << *it; } + std::cout << std::endl; } -void FilterAny(my_vec& vector, const std::string& any) { - for (auto& row : vector) { - if (std::find(row.begin(), row.end(), any) != row.end()) { - OutPutIP(row); - std::cout << std::endl; +void FilterAny(const ip_pool_type& ip_pool, int any) { + for (const auto& ip : ip_pool) { + if (std::find(ip.begin(), ip.end(), any) != ip.end()) { + OutPutIP(ip); } } } -void FilterOutput(my_vec& vector, std::string fst) { - for (auto& row : vector) - if (row[0] == fst) { - OutPutIP(row); - std::cout << std::endl; +void FilterOutput(const ip_pool_type& ip_pool, int fst) { + for (const auto& ip : ip_pool) { + if (ip[0] == fst) { + OutPutIP(ip); } + } } -void FilterOutput(my_vec& vector, std::string fst, std::string snd) { - for (auto& row : vector) - if (row[0] == fst && row[1] == snd) { - OutPutIP(row); - std::cout << std::endl; +void FilterOutput(const ip_pool_type& ip_pool, int fst, int snd) { + for (const auto& ip : ip_pool) { + if (ip[0] == fst && ip[1] == snd) { + OutPutIP(ip); } + } } - struct MySort { - bool operator()(const std::vector& vec1, const std::vector& vec2) const { - for (size_t i = 0; i < 4; ++i) { - int num1 = std::stoi(vec1[i]); - int num2 = std::stoi(vec2[i]); - - if (num1 != num2) - return num1 < num2; - } - return false; + bool operator()(const ip_address& ip1, const ip_address& ip2) const { + return ip1 > ip2; } }; -auto Split(const std::string &str, char d) -> std::vector { +auto SplitStr(const std::string& str, char d) -> std::vector { std::vector r; std::string::size_type start = 0; std::string::size_type stop = str.find_first_of(d); - while(stop != std::string::npos) + while (stop != std::string::npos) { r.push_back(str.substr(start, stop - start)); @@ -71,45 +63,47 @@ auto Split(const std::string &str, char d) -> std::vector { return r; } -int main() -{ - try - { - my_vec ip_pool; +ip_address Split(const std::string& str, char d) { + ip_address result{}; + std::string::size_type start = 0; + std::string::size_type stop = str.find_first_of(d); + + for (int i = 0; i < 4; ++i) { + result[i] = std::stoi(str.substr(start, stop - start)); + start = stop + 1; + stop = str.find_first_of(d, start); + } + + return result; +} + +int main() { + try { + ip_pool_type ip_pool; - for(std::string line; std::getline(std::cin, line);) - { - std::vector v = Split(line, '\t'); + for (std::string line; std::getline(std::cin, line);) { + std::vector v = SplitStr(line, '\t'); ip_pool.push_back(Split(v.at(0), '.')); } - // TODO reverse lexicographically sort DONE std::sort(ip_pool.begin(), ip_pool.end(), MySort()); - for(const auto & ip : ip_pool) - { - for(auto ip_part = ip.cbegin(); ip_part != ip.cend(); ++ip_part) - { - if (ip_part != ip.cbegin()) - { - std::cout << "."; - - } - std::cout << *ip_part; - } - std::cout << std::endl; + + for (const auto& ip : ip_pool) { + OutPutIP(ip); } - //std::cout << "Filter by 1st byte"<< std::endl; - FilterOutput(ip_pool, "1"); - //std::cout << "Filter by 1st and 2nd byte"<< std::endl; - FilterOutput(ip_pool, "46", "70"); - //std::cout << "Filter any"<< std::endl; - FilterAny(ip_pool, "46"); + std::cout << "Filter by 1st byte (1):" << std::endl; + FilterOutput(ip_pool, 1); + + std::cout << "Filter by 1st and 2nd byte (46, 70):" << std::endl; + FilterOutput(ip_pool, 46, 70); + + std::cout << "Filter any (46):" << std::endl; + FilterAny(ip_pool, 46); } - catch(const std::exception &e) - { + catch (const std::exception& e) { std::cerr << e.what() << std::endl; } return 0; -} +} \ No newline at end of file