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

Comparing HostUpdate/Host.cpp (property svn:eol-style):
Revision 10 by Douglas Thrift, 2003-11-06T20:35:29-08:00 vs.
Revision 19 by Douglas Thrift, 2003-11-10T19:46:19-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 19 by Douglas Thrift, 2003-11-10T19:46:19-08:00

# Line 0 | Line 1
1 + Id

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines