From 7e362f0ed975cdf2dbe7a554e4ad36ec07acff54 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 21 Feb 2024 22:27:49 -0500 Subject: [PATCH] Moved the private functions out of the anonymous namespace, because I need to use them in other places --- connect_code.cpp | 59 +++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/connect_code.cpp b/connect_code.cpp index 9245e78..5b83ff0 100644 --- a/connect_code.cpp +++ b/connect_code.cpp @@ -8,42 +8,39 @@ namespace connect_code { - /* Since these are private functions, I have wrapped them in an anonymous namespace, to prevent outside access to them */ - namespace { - /* Tokenizes a string, based on the given delimiter */ - std::vector tokenize_str(std::string str, std::string delim) { - std::vector result; - char* c_str = str.data(); - char* c_delim = delim.data(); - - char* tok = strtok(c_str, c_delim); - while (tok != NULL) { - result.push_back(std::string(tok)); - tok = strtok(NULL, c_delim); - } - - return result; + /* Tokenizes a string, based on the given delimiter */ + std::vector tokenize_str(std::string str, std::string delim) { + std::vector result; + char* c_str = str.data(); + char* c_delim = delim.data(); + + char* tok = strtok(c_str, c_delim); + while (tok != NULL) { + result.push_back(std::string(tok)); + tok = strtok(NULL, c_delim); } - /* Convert an IPv4 address from decimal to dotted decimal notation */ - std::string dec_to_dotted_dec(std::string addr) { - uint32_t addr_val = std::stoul(addr); /* 32 bit address */ - uint8_t addr_1 = (addr_val & (0xFF << 24)) >> 24; /* First octet (Bitwise AND the address with 255.0.0.0, and shift it to the right to obtain the first octet) */ - uint8_t addr_2 = (addr_val & (0xFF << 16)) >> 16; - uint8_t addr_3 = (addr_val & (0xFF << 8)) >> 8; - uint8_t addr_4 = (addr_val & 0xFF); + return result; + } - std::string ret_val = std::string(std::to_string(addr_1) + "." + std::to_string(addr_2) + "." + std::to_string(addr_3) + "." + std::to_string(addr_4)); + /* Convert an IPv4 address from decimal to dotted decimal notation */ + std::string dec_to_dotted_dec(std::string addr) { + uint32_t addr_val = std::stoul(addr); /* 32 bit address */ + uint8_t addr_1 = (addr_val & (0xFF << 24)) >> 24; /* First octet (Bitwise AND the address with 255.0.0.0, and shift it to the right to obtain the first octet) */ + uint8_t addr_2 = (addr_val & (0xFF << 16)) >> 16; + uint8_t addr_3 = (addr_val & (0xFF << 8)) >> 8; + uint8_t addr_4 = (addr_val & 0xFF); - return ret_val; - } + std::string ret_val = std::string(std::to_string(addr_1) + "." + std::to_string(addr_2) + "." + std::to_string(addr_3) + "." + std::to_string(addr_4)); - /* Convert an IPv4 address from dotted deecimal to decimal */ - std::string dotted_dec_to_dec(std::string addr) { - std::vector octets = tokenize_str(addr, "."); - uint32_t addr_val = (std::stoul(octets[0]) << 24) + (std::stoul(octets[1]) << 16) + (std::stoul(octets[2]) << 8) + (std::stoul(octets[3])); - return std::to_string(addr_val); - } + return ret_val; + } + + /* Convert an IPv4 address from dotted deecimal to decimal */ + std::string dotted_dec_to_dec(std::string addr) { + std::vector octets = tokenize_str(addr, "."); + uint32_t addr_val = (std::stoul(octets[0]) << 24) + (std::stoul(octets[1]) << 16) + (std::stoul(octets[2]) << 8) + (std::stoul(octets[3])); + return std::to_string(addr_val); }