Skip to content

Commit

Permalink
add namespace ipaddress to the cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mabels committed Oct 5, 2016
1 parent 6d9ad8d commit 186c639
Show file tree
Hide file tree
Showing 34 changed files with 111 additions and 60 deletions.
4 changes: 4 additions & 0 deletions cpp/src/crunchy.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "crunchy.hpp"

namespace ipaddress {

std::ostream& operator<<(std::ostream &o, const Crunchy &crunchy) {
o << crunchy.toString();
return o;
Expand All @@ -17,3 +19,5 @@ static auto _two = Crunchy::from_8bit({2});
const Crunchy& Crunchy::two(){
return _two;
}

}
2 changes: 2 additions & 0 deletions cpp/src/crunchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "result.hpp"

namespace ipaddress {
/**
* based
* Crunch - Arbitrary-precision integer arithmetic library
Expand Down Expand Up @@ -217,5 +218,6 @@ class Crunchy {

std::ostream& operator<<(std::ostream &o, const Crunchy &crunchy);

}

#endif
3 changes: 3 additions & 0 deletions cpp/src/ip_bits.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "ip_bits.hpp"

namespace ipaddress {

static std::string ipv4_as_compressed(const IpBits *ip_bits, const Crunchy &host_address) {
std::stringstream ret;
Expand Down Expand Up @@ -83,3 +84,5 @@ const IpBits* IpBits::v6() {
Crunchy::zero());
return v6Ptr;
}

}
6 changes: 5 additions & 1 deletion cpp/src/ip_bits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <functional>
#include <vector>

namespace ipaddress {


class IpBits {
public:
typedef std::function<std::string(const IpBits *source, const Crunchy &num)> ToString;
Expand All @@ -33,7 +36,7 @@ class IpBits {
rev_domain(rev_domain), part_mod(part_mod), host_ofs(host_ofs) {
}
public:

const IpBits* clone() const {
return this;
// IpBits my;
Expand Down Expand Up @@ -84,5 +87,6 @@ class IpBits {
};

std::ostream& operator<<(std::ostream &o, const IpBits &ip_bits);
}

#endif
3 changes: 3 additions & 0 deletions cpp/src/ip_version.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#ifndef __IP_VERSION__
#define __IP_VERSION__

namespace ipaddress {

typedef enum {
V4 = 4,
V6 = 6
} IpVersion;

}
#endif
31 changes: 6 additions & 25 deletions cpp/src/ipaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ipv6.hpp"
#include "ipv6_mapped.hpp"

namespace ipaddress {

std::ostream& operator<<(std::ostream &o, const IPAddress &ipaddress) {
o << "<ipaddress@"
Expand Down Expand Up @@ -110,52 +111,30 @@ std::vector<IPAddress> IPAddress::aggregate(const std::vector<IPAddress> &networ
// &stack[i].to_string_uncompressed());
// }
for (ssize_t pos = 0; true;) {
if (pos < 0) {
pos = 0;
}
if (pos < 0) { pos = 0; }
auto stack_len = static_cast<ssize_t>(stack.size()); // borrow checker
// println!("loop:{}:{}", pos, stack_len);
// if stack_len == 1 {
// println!("exit 1");
// break;
// }
if (pos >= stack_len) {
// println!("exit first:{}:{}", stack_len, pos);
break;
}
if (pos >= stack_len) { break; }
auto first = IPAddress::pos_to_idx(pos, stack_len);
pos = pos + 1;
if (pos >= stack_len) {
// println!("exit second:{}:{}", stack_len, pos);
break;
}
if (pos >= stack_len) { break; }
auto second = IPAddress::pos_to_idx(pos, stack_len);
pos = pos + 1;
//auto firstUnwrap = first;
if (stack[first].includes(stack[second])) {
pos = pos - 2;
// println!("remove:1:{}:{}:{}=>{}", first, second, stack_len, pos + 1);
auto pidx = IPAddress::pos_to_idx(pos + 1, stack_len);
// std::cout << "-1:" << pidx << ":" << stack.size() << std::endl;
stack.erase(stack.begin() + pidx);
} else {
stack[first].prefix = stack[first].prefix.sub(1).unwrap();
// println!("complex:{}:{}:{}:{}:P1:{}:P2:{}", pos, stack_len,
// first, second,
// stack[first].to_string(), stack[second].to_string());
if ((stack[first].prefix.num + 1) == stack[second].prefix.num &&
stack[first].includes(stack[second])) {
pos = pos - 2;
auto idx = IPAddress::pos_to_idx(pos, stack_len);
stack[idx] = stack[first].clone(); // kaputt
auto pidx = IPAddress::pos_to_idx(pos + 1, stack_len);
// std::cout << "-1:" << pidx << ":" << stack.size() << std::endl;
stack.erase(stack.begin() + pidx);
// println!("remove-2:{}:{}", pos + 1, stack_len);
pos = pos - 1; // backtrack
} else {
stack[first].prefix = stack[first].prefix.add(1).unwrap(); //reset prefix
// println!("easy:{}:{}=>{}", pos, stack_len, stack[first].to_string());
pos = pos - 1; // do it with second as first
}
}
Expand All @@ -164,3 +143,5 @@ std::vector<IPAddress> IPAddress::aggregate(const std::vector<IPAddress> &networ
//return stack.erase(0, stack.size());
return std::vector<IPAddress>(stack.begin(), stack.end());
}

}
4 changes: 3 additions & 1 deletion cpp/src/ipaddress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "result.hpp"
#include "option.hpp"


namespace ipaddress {

class ResultCrunchyParts {
public:
Expand Down Expand Up @@ -1295,4 +1295,6 @@ class IPAddress {

std::ostream& operator<<(std::ostream &o, const IPAddress &ipa);

}

#endif
4 changes: 4 additions & 0 deletions cpp/src/ipv4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "ipv6.hpp"
#include "prefix128.hpp"

namespace ipaddress {

IPAddress Ipv4::to_ipv6(const IPAddress &ia) {
return IPAddress(
IpBits::v6(),
Expand All @@ -14,3 +16,5 @@ IPAddress Ipv4::to_ipv6(const IPAddress &ia) {
Ipv6::to_ipv6
);
}

}
4 changes: 3 additions & 1 deletion cpp/src/ipv4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "ip_bits.hpp"
#include "prefix128.hpp"
// #include "ipv6.hpp"

namespace ipaddress {
class Ipv4 {
public:
static Result<IPAddress> from_number(const Crunchy &addr, size_t prefix_num) {
Expand Down Expand Up @@ -181,4 +181,6 @@ class Ipv4 {
return Ok(ip);
}
};

}
#endif
2 changes: 2 additions & 0 deletions cpp/src/ipv6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <string>

namespace ipaddress {
class Ipv6 {
public:
// =Name
Expand Down Expand Up @@ -209,4 +210,5 @@ class Ipv6 {
}

};
}
#endif
3 changes: 2 additions & 1 deletion cpp/src/ipv6_loopback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ipv6.hpp"
#include "crunchy.hpp"

namespace ipaddress {

class Ipv6Loopback {
public:
Expand Down Expand Up @@ -55,5 +56,5 @@ class Ipv6Loopback {
return Ipv6::from_int(Crunchy::one(), 128).unwrap();
}
};

}
#endif
3 changes: 3 additions & 0 deletions cpp/src/ipv6_mapped.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ipv6_mapped.hpp"

namespace ipaddress {

Result<IPAddress> Ipv6Mapped::create(const std::string &str) {
// console.log("mapped-1");
auto i = IPAddress::split_at_slash(str);
Expand Down Expand Up @@ -87,3 +89,4 @@ Result<IPAddress> Ipv6Mapped::create(const std::string &str) {
// console.log("mapped-9");
return Err<IPAddress>("not a valid mapped string");
}
}
3 changes: 3 additions & 0 deletions cpp/src/ipv6_mapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "ip_bits.hpp"
#include "ipaddress.hpp"

namespace ipaddress {

class Ipv6Mapped {
public:
// Ac
Expand Down Expand Up @@ -85,4 +87,5 @@ class Ipv6Mapped {
//
static Result<IPAddress> create(const std::string &str);
};
}
#endif
3 changes: 2 additions & 1 deletion cpp/src/ipv6_unspec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define __IPV6_UNSPEC__

#include "ipaddress.hpp"

namespace ipaddress {
class Ipv6Unspec {
public:
// The address with all zero bits is called the +unspecified+ address
Expand Down Expand Up @@ -59,4 +59,5 @@ class Ipv6Unspec {
}
// class IPv6::Unspecified
};
}
#endif
2 changes: 2 additions & 0 deletions cpp/src/option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <memory>

namespace ipaddress {
class OptionError : public std::exception {
const std::string msg;
public:
Expand Down Expand Up @@ -72,5 +73,6 @@ std::ostream& operator<<(std::ostream &o, const Option<T> &option) {
}
return o;
}
}

#endif
3 changes: 2 additions & 1 deletion cpp/src/prefix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "ip_bits.hpp"
#include "crunchy.hpp"


namespace ipaddress {
class Prefix {
public:
typedef std::function<Result<Prefix>(const size_t num)> From;
Expand Down Expand Up @@ -171,4 +171,5 @@ class Prefix {
};

std::ostream& operator<<(std::ostream &o, const Prefix &prefix);
}
#endif
4 changes: 4 additions & 0 deletions cpp/src/prefix128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "result.hpp"

namespace ipaddress {

class Prefix128 {
public:
// #[derive(Ord,PartialOrd,Eq,PartialEq,Debug,Copy,Clone)]
Expand Down Expand Up @@ -41,4 +43,6 @@ class Prefix128 {
}
};

}

#endif
2 changes: 2 additions & 0 deletions cpp/src/prefix32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "prefix.hpp"
#include "ip_bits.hpp"

namespace ipaddress {
class Prefix32 {
public:
static Result<Prefix> from(size_t num) {
Expand All @@ -30,5 +31,6 @@ class Prefix32 {
}

};
}

#endif
4 changes: 3 additions & 1 deletion cpp/src/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "option.hpp"

namespace ipaddress {

class ResultError : public std::exception {
const char *msg;
public:
Expand Down Expand Up @@ -59,5 +61,5 @@ template<typename T> Result<T> Ok(T &t) {
template<typename T> Result<T> Err(const char *msg) {
return Result<T>(0, msg);
}

}
#endif
3 changes: 3 additions & 0 deletions cpp/src/rle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "rle.hpp"

namespace ipaddress{

std::ostream& operator<<(std::ostream &o, const Rle &rle) {
o << rle.toString();
return o;
Expand Down Expand Up @@ -68,3 +70,4 @@ std::vector<Rle> Rle::code(const std::vector<size_t> &parts) {
last.handle_last();
return last.ret;
}
}
3 changes: 2 additions & 1 deletion cpp/src/rle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <sstream>

namespace ipaddress {

class Rle {
public:
Expand Down Expand Up @@ -50,5 +51,5 @@ class Rle {
};

std::ostream& operator<<(std::ostream &o, const Rle &rle);

}
#endif
2 changes: 1 addition & 1 deletion cpp/test/mocha.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static int okCount = 0;


int exit() {
std::cout << std::endl;

std::stringstream s2;
s2 << std::dec << "Total " << okCount+failCount
<< " Ok " << okCount
Expand Down
1 change: 1 addition & 0 deletions cpp/test/test_crunchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "chai.hpp"
#include "mocha.hpp"

using namespace ipaddress;
int main(int, char **) {

describe("#compare", []() {
Expand Down
Loading

0 comments on commit 186c639

Please sign in to comment.