Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / nestea_i.cpp
blobf2e7dddaf24757dd0e12e3e4e103bbe1df00d205
1 #include "nestea_i.h"
2 #include "tao/debug.h"
3 #include "ace/ACE.h"
4 #include "ace/FILE_Addr.h"
5 #include "ace/FILE_Connector.h"
6 #include "ace/FILE_IO.h"
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_string.h"
10 const size_t MAX_UINT32_STR_LEN = 11; // Largest UINT32 is 8589934591 + NUL is 11 characters
13 Nestea_i::Nestea_i (CORBA::ORB_ptr orb, const ACE_TCHAR *filename)
14 : cans_ (0)
16 orb_ = CORBA::ORB::_duplicate(orb);
18 this->data_filename_ = ACE::strnew (filename);
20 // @@ This should probably be called from somewhere else
21 this->load_data ();
25 Nestea_i::~Nestea_i ()
27 delete [] this->data_filename_;
31 // Add <cans> number of cans to the bookshelf.
33 void
34 Nestea_i::drink (CORBA::Long cans)
36 if (TAO_debug_level)
37 ACE_DEBUG ((LM_DEBUG, "Nestea_i::drink %d cans\n", cans));
39 this->cans_ += cans;
41 this->save_data ();
45 // Removes <cans> number of cans from the bookshelf.
47 void
48 Nestea_i::crush (CORBA::Long cans)
50 if (TAO_debug_level)
51 ACE_DEBUG ((LM_DEBUG, "Nestea_i::crush %d cans\n", cans));
53 if (static_cast<ACE_UINT32> (cans) > this->cans_)
54 this->cans_ = 0;
55 else
56 this->cans_ -= cans;
58 this->save_data ();
62 // Returns the number of cans in the bookshelf.
64 CORBA::Long
65 Nestea_i::bookshelf_size ()
67 if (TAO_debug_level)
68 ACE_DEBUG ((LM_DEBUG, "Nestea_i::bookshelf_size\n"));
70 return this->cans_;
73 // Returns comments about your collection.
75 char *
76 Nestea_i::get_praise ()
78 if (TAO_debug_level)
79 ACE_DEBUG ((LM_DEBUG, "Nestea_i::get_praise\n"));
81 if (this->cans_ > 500)
82 return CORBA::string_dup ("Man, that is one excellent Nestea Collection!");
83 else if (this->cans_ > 250)
84 return CORBA::string_dup ("We are getting into the big leagues now!");
85 else if (this->cans_ > 100)
86 return CORBA::string_dup ("Things are looking up!");
87 else if (this->cans_ > 0)
88 return CORBA::string_dup ("Well, it is a start. Drink more Nestea!");
89 else
90 return CORBA::string_dup ("No cans, no praise.");
93 void
94 Nestea_i::shutdown ()
96 if (TAO_debug_level)
97 ACE_DEBUG ((LM_DEBUG, "Nestea_i::shutdown\n"));
99 orb_->shutdown(0);
102 // Saves bookshelf data to a file.
105 Nestea_i::save_data ()
107 ACE_FILE_IO file;
108 ACE_FILE_Connector connector;
110 if (connector.connect (file,
111 ACE_FILE_Addr (this->data_filename_),
113 ACE_Addr::sap_any) == -1)
114 ACE_ERROR_RETURN ((LM_ERROR,
115 "%p\n to %s",
116 "connect",
117 this->data_filename_),
118 -1);
120 char str[MAX_UINT32_STR_LEN];
122 ACE_OS::sprintf (str, "%d", this->cans_);
124 return file.send_n (str, ACE_OS::strlen (str) + 1);
128 // Loads bookshelf data from a file.
131 Nestea_i::load_data ()
133 ACE_FILE_IO file;
134 ACE_FILE_Connector connector;
136 if (connector.connect (file,
137 ACE_FILE_Addr (this->data_filename_),
139 ACE_Addr::sap_any) == -1)
140 ACE_ERROR_RETURN ((LM_ERROR,
141 "%p\n to %s",
142 "connect",
143 this->data_filename_),
144 -1);
146 char str[MAX_UINT32_STR_LEN];
148 int len = file.recv (str, MAX_UINT32_STR_LEN);
149 str[len] = 0;
151 if (len > 0)
152 this->cans_ = ACE_OS::atoi (str);
153 else
154 this->cans_ = 0;
155 return 0;