ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/Host.cpp
(Generate patch)

Comparing HostUpdate/Host.cpp (file contents):
Revision 10 by Douglas Thrift, 2003-11-06T20:35:29-08:00 vs.
Revision 13 by Douglas Thrift, 2003-11-07T12:17:57-08:00

# Line 1 | Line 1
1 < // Host Update
2 < //
3 < // Douglas Thrift
4 < //
5 < // $Id$
6 <
7 < #include "Host.hpp"
8 <
9 < Host::Host(const string& host, const string& name, const string& address)
10 < {
11 < #ifdef _WIN32
12 <        if (count == 0)
13 <        {
14 <                if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
15 <                {
16 <                        cerr << "Host(): WSAStartup()\n";
17 <                        exit(1);
18 <                }
19 <        }
20 <
21 <        count++;
22 < #endif
23 <
24 <        this->address = new struct in_addr;
25 <
26 <        setHost(host);
27 <        if (name != "") setName(name, address == "");
28 <        if (address != "") setAddress(address, name == "");
29 < }
30 <
31 < Host::~Host()
32 < {
33 <        delete address;
34 <
35 < #ifdef _WIN32
36 <        count--;
37 <
38 <        if (count == 0)
39 <        {
40 <                if (WSACleanup() != 0)
41 <                {
42 <                        cerr << "~Host(): WSACleanup()\n";
43 <                        exit(1);
44 <                }
45 <        }
46 < #endif
47 < }
48 <
49 < void Host::setName(const string& name, bool lookup)
50 < {
51 <        this->name = name;
52 <
53 <        if (lookup)
54 <        {
55 <                struct hostent* ent = gethostbyname(this->name.c_str());
56 <
57 <                memcpy(address, *(ent->h_addr_list), ent->h_length);
58 <        }
59 < }
60 <
61 < void Host::setAddress(const string& address, bool lookup)
62 < {
63 <        in_addr_t value = inet_addr(address.c_str());
64 <
65 <        if (value == INADDR_NONE)
66 <        {
67 <                cerr << "Host.setAddress(): INADDR_NONE\n";
68 <                exit(1);
69 <        }
70 <        else
71 <        {
72 <                memcpy(this->address, &value, sizeof(in_addr_t));
73 <        }
74 <
75 <        if (lookup)
76 <        {
77 <                cerr << "Here!\n";
78 <                struct hostent* ent = gethostbyaddr((char*)(this->address),
79 <                        sizeof(in_addr), AF_INET);
80 <
81 <                name = ent->h_name;
82 <        }
83 < }
84 <
85 < void Host::operator++()
86 < {
87 <        ifstream fin(host.c_str());
88 <
89 <        getline(fin, name);
90 <
91 <        fin.read((char*)(address), sizeof(in_addr));
92 <        fin.close();
93 < }
94 <
95 < void Host::operator--()
96 < {
97 <        ofstream fout(host.c_str());
98 <
99 <        fout << name << '\n';
100 <
101 <        fout.write((char*)(address), sizeof(in_addr));
102 <        fout.close();
103 < }
104 <
105 < #ifdef _WIN32
106 <
107 < unsigned Host::count = 0;
108 < WSADATA Host::data;
109 <
110 < #endif
1 > // Host Update
2 > //
3 > // Douglas Thrift
4 > //
5 > // $Id$
6 >
7 > #include "Host.hpp"
8 >
9 > Host::Host(const string& host, const string& name, const string& address, const
10 >        string& platform)
11 > {
12 > #ifdef _WIN32
13 >        if (count == 0)
14 >        {
15 >                if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
16 >                {
17 >                        cerr << "Host(): WSAStartup()\n";
18 >                        exit(1);
19 >                }
20 >        }
21 >
22 >        count++;
23 > #endif
24 >
25 >        this->address = new struct in_addr;
26 >
27 >        setHost(host);
28 >        if (name != "") setName(name, address == "");
29 >        if (address != "") setAddress(address, name == "");
30 >        setPlatform(platform);
31 > }
32 >
33 > Host::~Host()
34 > {
35 >        delete address;
36 >
37 > #ifdef _WIN32
38 >        count--;
39 >
40 >        if (count == 0)
41 >        {
42 >                if (WSACleanup() != 0)
43 >                {
44 >                        cerr << "~Host(): WSACleanup()\n";
45 >                        exit(1);
46 >                }
47 >        }
48 > #endif
49 > }
50 >
51 > void Host::setName(const string& name, bool lookup)
52 > {
53 >        this->name = name;
54 >
55 >        if (lookup)
56 >        {
57 >                struct hostent* ent = gethostbyname(this->name.c_str());
58 >
59 >                memcpy(address, *(ent->h_addr_list), ent->h_length);
60 >        }
61 > }
62 >
63 > void Host::setAddress(const string& address, bool lookup)
64 > {
65 >        in_addr_t value = inet_addr(address.c_str());
66 >
67 >        if (value == INADDR_NONE)
68 >        {
69 >                cerr << "Host.setAddress(): INADDR_NONE\n";
70 >                exit(1);
71 >        }
72 >        else
73 >        {
74 >                memcpy(this->address, &value, sizeof(in_addr_t));
75 >        }
76 >
77 >        if (lookup)
78 >        {
79 >                struct hostent* ent = gethostbyaddr((char*)(this->address),
80 >                        sizeof(in_addr), AF_INET);
81 >
82 >                if (ent != NULL) name = ent->h_name;
83 >        }
84 > }
85 >
86 > bool Host::operator==(const Host& host) const
87 > {
88 >        if (this->host == host.host)
89 >        {
90 >                if (name == host.name)
91 >                {
92 >                        if (address == host.address)
93 >                        {
94 >                                return platform == host.platform;
95 >                        }
96 >                        else
97 >                        {
98 >                                return false;
99 >                        }
100 >                }
101 >                else
102 >                {
103 >                        return false;
104 >                }
105 >        }
106 >        else
107 >        {
108 >                return false;
109 >        }
110 > }
111 >
112 > Host Host::operator++()
113 > {
114 >        ifstream fin(host.c_str());
115 >
116 >        if (fin.is_open())
117 >        {
118 >                getline(fin, name);
119 >                fin.read((char*)(address), sizeof(in_addr));
120 >                getline(fin, platform);
121 >                fin.close();
122 >        }
123 >
124 >        return *this;
125 > }
126 >
127 > Host Host::operator++(int)
128 > {
129 >        Host old = *this;
130 >
131 >        ++*this;
132 >
133 >        return old;
134 > }
135 >
136 > Host Host::operator--()
137 > {
138 >        ofstream fout(host.c_str());
139 >
140 >        fout << name << '\n';
141 >
142 >        fout.write((char*)(address), sizeof(in_addr));
143 >
144 >        fout << platform;
145 >
146 >        fout.close();
147 >
148 >        return *this;
149 > }
150 >
151 > Host Host::operator--(int)
152 > {
153 >        Host old = *this;
154 >
155 >        --*this;
156 >
157 >        return old;
158 > }
159 >
160 > #ifdef _WIN32
161 >
162 > unsigned Host::count = 0;
163 > WSADATA Host::data;
164 >
165 > #endif

Comparing HostUpdate/Host.cpp (property svn:eol-style):
Revision 10 by Douglas Thrift, 2003-11-06T20:35:29-08:00 vs.
Revision 13 by Douglas Thrift, 2003-11-07T12:17:57-08:00

# Line 0 | Line 1
1 + native

Comparing HostUpdate/Host.cpp (property svn:keywords):
Revision 10 by Douglas Thrift, 2003-11-06T20:35:29-08:00 vs.
Revision 13 by Douglas Thrift, 2003-11-07T12:17:57-08:00

# Line 0 | Line 1
1 + Id

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines