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 12 by Douglas Thrift, 2003-11-06T23:43:01-08:00 vs.
Revision 15 by Douglas Thrift, 2003-11-09T16:51:42-08:00

# Line 6 | Line 6
6  
7   #include "Host.hpp"
8  
9 + #ifndef _WIN32
10 +
11 + static char slash = '/';
12 +
13 + #else
14 +
15 + static char slash = '\\';
16 +
17 + #endif
18 +
19   Host::Host(const string& host, const string& name, const string& address, const
20          string& platform)
21   {
# Line 22 | Line 32 | Host::Host(const string& host, const str
32          count++;
33   #endif
34  
25        this->address = new struct in_addr;
26
35          setHost(host);
36          if (name != "") setName(name, address == "");
37          if (address != "") setAddress(address, name == "");
# Line 32 | Line 40 | Host::Host(const string& host, const str
40  
41   Host::~Host()
42   {
35        delete address;
36
43   #ifdef _WIN32
44          count--;
45  
# Line 56 | Line 62 | void Host::setName(const string& name, b
62          {
63                  struct hostent* ent = gethostbyname(this->name.c_str());
64  
65 <                memcpy(address, *(ent->h_addr_list), ent->h_length);
65 >                memcpy(&address, *(ent->h_addr_list), ent->h_length);
66          }
67   }
68  
# Line 71 | Line 77 | void Host::setAddress(const string& addr
77          }
78          else
79          {
80 <                memcpy(this->address, &value, sizeof(in_addr_t));
80 >                memcpy(&this->address, &value, sizeof(in_addr_t));
81          }
82  
83          if (lookup)
84          {
85 <                struct hostent* ent = gethostbyaddr((char*)(this->address),
85 >                struct hostent* ent = gethostbyaddr((char*)(&this->address),
86                          sizeof(in_addr), AF_INET);
87  
88                  if (ent != NULL) name = ent->h_name;
89          }
90   }
91  
92 < void Host::operator++()
92 > bool Host::operator==(const Host& host) const
93   {
94 <        ifstream fin(host.c_str());
94 >        if (this->host == host.host)
95 >        {
96 >                if (name == host.name)
97 >                {
98 >                        if (string(inet_ntoa(address)) == inet_ntoa(host.address))
99 >                        {
100 >                                return platform == host.platform;
101 >                        }
102 >                        else
103 >                        {
104 >                                return false;
105 >                        }
106 >                }
107 >                else
108 >                {
109 >                        return false;
110 >                }
111 >        }
112 >        else
113 >        {
114 >                return false;
115 >        }
116 > }
117 >
118 > Host Host::operator++()
119 > {
120 >        string file = string("hosts") + slash + host;
121 >
122 >        ifstream fin(file.c_str());
123  
124 <        getline(fin, name);
125 <        fin.read((char*)(address), sizeof(in_addr));
126 <        getline(fin, platform);
127 <        fin.close();
124 >        if (fin.is_open())
125 >        {
126 >                getline(fin, name);
127 >
128 >                string address;
129 >                
130 >                getline(fin, address);
131 >
132 >                in_addr_t value = inet_addr(address.c_str());
133 >
134 >                if (value == INADDR_NONE)
135 >                {
136 >                        cerr << "Host.operator++(): INADDR_NONE\n";
137 >                        exit(1);
138 >                }
139 >                else
140 >                {
141 >                        memcpy(&this->address, &value, sizeof(in_addr_t));
142 >                }
143 >
144 >                getline(fin, platform);
145 >                fin.close();
146 >        }
147 >
148 >        return *this;
149   }
150  
151 < void Host::operator--()
151 > Host Host::operator++(int)
152   {
153 <        ofstream fout(host.c_str());
153 >        Host old = *this;
154  
155 <        fout << name << '\n';
155 >        ++*this;
156  
157 <        fout.write((char*)(address), sizeof(in_addr));
157 >        return old;
158 > }
159  
160 <        fout << platform;
160 > Host Host::operator--()
161 > {
162 >        string file = string("hosts") + slash + host;
163 >        
164 >        ofstream fout(file.c_str());
165 >
166 >        fout << name << '\n'
167 >                << inet_ntoa(address) << '\n'
168 >                << platform << '\n';
169  
170          fout.close();
171 +
172 +        return *this;
173 + }
174 +
175 + Host Host::operator--(int)
176 + {
177 +        Host old = *this;
178 +
179 +        --*this;
180 +
181 +        return old;
182   }
183  
184   #ifdef _WIN32

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines