ip2long

ip2long Commit Details


Date:2013-08-18 22:15:00 (11 years 4 months ago)
Author:Natalie Adams
Branch:default
Commit:8c8754db0e1e
Message:Migrating from google code

Changes:
Abuild.bat (full)
Aip2long.cpp (full)
Aip2long/DEBIAN/control (full)

File differences

build.bat
1
2
call "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
cl.exe /clr ip2long.cpp
ip2long.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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;
}
ip2long/DEBIAN/control
1
2
3
4
5
6
7
8
Package: ip2long
Version: 1
Section: base
Priority: optional
Architecture: all
Depends: bash
Maintainer: adamsna@datanethost.net
Description: Converts an IP to a long

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.43052s using 14 queries.