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 12 by Douglas Thrift, 2003-11-06T23:43:01-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 > void Host::operator++()
87 > {
88 >        ifstream fin(host.c_str());
89 >
90 >        getline(fin, name);
91 >        fin.read((char*)(address), sizeof(in_addr));
92 >        getline(fin, platform);
93 >        fin.close();
94 > }
95 >
96 > void Host::operator--()
97 > {
98 >        ofstream fout(host.c_str());
99 >
100 >        fout << name << '\n';
101 >
102 >        fout.write((char*)(address), sizeof(in_addr));
103 >
104 >        fout << platform;
105 >
106 >        fout.close();
107 > }
108 >
109 > #ifdef _WIN32
110 >
111 > unsigned Host::count = 0;
112 > WSADATA Host::data;
113 >
114 > #endif

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

# Line 0 | Line 1
1 + Id

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines