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 11 by Douglas Thrift, 2003-11-06T20:37:52-08:00 vs.
Revision 192 by Douglas Thrift, 2004-08-22T21:23:29-07:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines