| #include <iostream>␊ |
| #include <sstream>␊ |
| #include <stdlib.h> //atoi␊ |
| #include <bitset>␊ |
| #include <vector>␊ |
| #include <exception>␊ |
| #include <string>␊ |
| ␊ |
| using namespace std;␊ |
| ␊ |
| typedef std::string::size_type (std::string::*find_t)(const std::string& delim,␊ |
| std::string::size_type offset) const;␊ |
| ␊ |
| template <class T>␊ |
| inline string ToString(const T& t)␊ |
| {␊ |
| ␉stringstream ss;␊ |
| ␉ss << t;␊ |
| ␉return ss.str();␊ |
| }␊ |
| ␊ |
| string str_pad(string s, unsigned int num, char pad)␊ |
| {␊ |
| ␉string tmppad;␊ |
| ␉if (s.length() <= num)␊ |
| ␉␉for(unsigned int i = (num - s.length()); i > 0; i--)␊ |
| ␉␉␉tmppad = tmppad + pad;␊ |
| ␉return tmppad + s;␊ |
| }␊ |
| ␊ |
| vector<string> split(const string& s,␊ |
| const string& match,␊ |
| bool removeEmpty=false,␊ |
| bool fullMatch=false)␊ |
| {␊ |
| vector<string> result; // return container for tokens␊ |
| string::size_type start = 0, // starting position for searches␊ |
| skip = 1; // positions to skip after a match␊ |
| find_t pfind = &string::find_first_of; // search algorithm for matches␊ |
| ␊ |
| if (fullMatch)␊ |
| {␊ |
| // use the whole match string as a key␊ |
| // instead of individual characters␊ |
| // skip might be 0. see search loop comments␊ |
| skip = match.length();␊ |
| pfind = &string::find;␊ |
| }␊ |
| ␊ |
| while (start != string::npos)␊ |
| {␊ |
| // get a complete range [start..end)␊ |
| string::size_type end = (s.*pfind)(match, start);␊ |
| ␊ |
| // null strings always match in string::find, but␊ |
| // a skip of 0 causes infinite loops. pretend that␊ |
| // no tokens were found and extract the whole string␊ |
| if (skip == 0) end = string::npos;␊ |
| ␊ |
| string token = s.substr(start, end - start);␊ |
| ␊ |
| if (!(removeEmpty && token.empty()))␊ |
| {␊ |
| // extract the token and add it to the result list␊ |
| result.push_back(token);␊ |
| }␊ |
| ␊ |
| // start the next range␊ |
| if ((start = end) != string::npos) start += skip;␊ |
| }␊ |
| ␊ |
| return result;␊ |
| }␊ |
| ␊ |
| string tobin(int dec)␊ |
| {␊ |
| if (dec > 0)␊ |
| return tobin(dec/2) + ToString(dec%2);␊ |
| return "";␊ |
| }␊ |
| ␊ |
| int main(int argc,char *argv[])␊ |
| {␊ |
| ␉vector<string> ip_addr_arr;␊ |
| ␉int oct1, oct2, oct3, oct4;␊ |
| ␉unsigned int addr;␊ |
| ␉string combinedoct;␊ |
| ␉string ip_addr;␊ |
| ␉string nlopt = "";␊ |
| ␉if (argc == 2)␊ |
| ␉␉nlopt = argv[1];␊ |
| ␉try ␊ |
| ␉{␊ |
| ␉␉cin >> ip_addr;␊ |
| ␉ip_addr_arr = split(ip_addr, ".");␊ |
| ␊ |
| ␉␉oct1 = atoi(ip_addr_arr.at(0).c_str());␊ |
| ␉␉oct2 = atoi(ip_addr_arr.at(1).c_str());␊ |
| ␉␉oct3 = atoi(ip_addr_arr.at(2).c_str());␊ |
| ␉␉oct4 = atoi(ip_addr_arr.at(3).c_str());␊ |
| ␊ |
| ␉␉combinedoct = str_pad(tobin(oct1), 8, '0');␊ |
| ␉␉combinedoct += str_pad(tobin(oct2), 8, '0');␊ |
| ␉␉combinedoct += str_pad(tobin(oct3), 8, '0');␊ |
| ␉␉combinedoct += str_pad(tobin(oct4), 8, '0');␊ |
| ␊ |
| ␉␉addr = bitset<32>(combinedoct).to_ulong();␊ |
| ␉␉cout << addr;␊ |
| ␉␉if (argc == 2 && nlopt == "-n")␊ |
| ␉␉␉cout << endl;␊ |
| ␉} catch (exception& e) {␊ |
| ␉␉cout << e.what() << endl;␊ |
| ␉}␊ |
| ␉return 0;␊ |
| }␊ |