Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / apps / JAWS3 / jaws3 / Config_File.cpp
blob9c73c9da1546ef17baa407cc832a790c79cdd4dc
1 #include "ace/OS_NS_stdlib.h"
2 #include "ace/OS_NS_string.h"
3 #include "ace/FILE_Connector.h"
4 #include "ace/Message_Block.h"
5 #include "ace/Singleton.h"
6 #include "ace/Unbounded_Queue.h"
8 #ifndef JAWS_BUILD_DLL
9 #define JAWS_BUILD_DLL
10 #endif
12 #include "jaws3/Config_File.h"
13 #include "jaws3/Symbol_Table.h"
15 // = Helper class to manage "constant" strings.
17 class JAWS_strings
19 public:
20 ~JAWS_strings ()
22 void *p;
23 while (this->queue_.dequeue_head (p) != -1)
24 ACE_OS::free (p);
27 const ACE_TCHAR * duplicate (const ACE_TCHAR *s)
29 void **x;
30 const ACE_TCHAR *d = 0;
31 ACE_Unbounded_Queue_Iterator<void *> iter (this->queue_);
33 while (iter.next (x))
35 d = (const ACE_TCHAR *) *x;
36 if (ACE_OS::strcmp (d, s) == 0)
37 break;
38 d = 0;
39 iter.advance ();
42 if (d == 0)
44 d = ACE_OS::strdup (s);
45 this->queue_.enqueue_tail ((void *) d);
48 return d;
51 private:
52 ACE_Unbounded_Queue<void *> queue_;
56 // = Underlying implementation class.
59 class JAWS_Config_File_Impl
61 public:
62 JAWS_Config_File_Impl (const ACE_TCHAR *config_file);
63 ~JAWS_Config_File_Impl ();
64 int find (const ACE_TCHAR *key, const ACE_TCHAR *&value);
66 void parse_file ();
67 void reset ();
68 void dump ();
70 enum { JAWS_CONFIG_FILE_SYMBOL_TABLE_SIZE = 211 };
72 private:
73 ACE_FILE_Addr faddr_;
74 JAWS_strings *strings_;
75 JAWS_Symbol_Table *symbols_;
78 JAWS_Config_File_Impl::JAWS_Config_File_Impl (const ACE_TCHAR *config_file)
79 : faddr_ (config_file)
80 , strings_ (0)
81 , symbols_ (0)
83 this->strings_ = new JAWS_strings;
84 this->symbols_ = new JAWS_Symbol_Table (JAWS_CONFIG_FILE_SYMBOL_TABLE_SIZE);
85 this->parse_file ();
88 JAWS_Config_File_Impl::~JAWS_Config_File_Impl ()
90 delete this->symbols_;
91 this->symbols_ = 0;
92 delete this->strings_;
93 this->strings_ = 0;
96 int
97 JAWS_Config_File_Impl::find (const ACE_TCHAR *key, const ACE_TCHAR *&value)
99 return this->symbols_->find (key, value);
102 void
103 JAWS_Config_File_Impl::parse_file ()
105 ACE_FILE_Connector fconnector;
106 ACE_FILE_IO fio;
108 if (fconnector.connect ( fio
109 , this->faddr_
111 , ACE_Addr::sap_any
113 , O_RDONLY
114 ) == -1)
115 return;
117 ACE_Message_Block buffer (8192);
118 ACE_Message_Block line (4096);
119 ssize_t count = 0;
120 const ACE_TCHAR *sym_name;
121 const ACE_TCHAR *sym_value;
122 int last_line_was_read = 0;
123 ACE_TCHAR *end_of_current_line = 0;
124 ACE_TCHAR *p = 0;
126 while (last_line_was_read
127 || (count = fio.recv (buffer.wr_ptr (), buffer.space () - 2)) >= 0)
129 end_of_current_line = 0;
131 // Make sure input is newline terminated if it is the last line,
132 // and always null terminated.
133 if (! last_line_was_read)
135 if (count > 0)
137 buffer.wr_ptr (count);
138 // Scan forward for at least one newline character
139 p = buffer.rd_ptr ();
140 while (p != buffer.wr_ptr ())
142 if (*p == '\n')
143 break;
144 p++;
147 if (p == buffer.wr_ptr ())
148 continue;
150 end_of_current_line = p;
152 else
154 if (buffer.wr_ptr ()[-1] != '\n')
156 buffer.wr_ptr ()[0] = '\n';
157 buffer.wr_ptr (1);
160 last_line_was_read = 1;
163 buffer.wr_ptr ()[0] = '\0';
166 if (end_of_current_line == 0)
168 end_of_current_line = buffer.rd_ptr ();
169 while (*end_of_current_line != '\n')
170 end_of_current_line++;
173 // If buffer is not pointing to a continuation line, or there is
174 // no more input, then can commit the scanned configuration
175 // line.
176 if (line.length () != 0
177 && ((last_line_was_read && buffer.length () == 0)
178 || (buffer.rd_ptr ()[0] != ' '
179 && buffer.rd_ptr ()[0] != '\t')))
181 ACE_TCHAR *name = 0;
182 ACE_TCHAR *value = 0;
184 name = line.rd_ptr ();
185 for (p = name; *p != '\0'; p++)
187 if (*p == '=')
189 line.rd_ptr (p+1);
190 while (p != name && (p[-1] == ' ' || p[-1] == '\t'))
191 p--;
192 *p = '\0';
196 if (*name)
198 value = line.rd_ptr ();
199 while (*value == ' ' || *value == '\t')
200 value++;
201 p = line.wr_ptr ();
202 while (p != value && (p[-1] == ' ' || p[-1] == '\t'))
203 p--;
204 *p = '\0';
206 sym_name = this->strings_->duplicate (name);
207 sym_value = this->strings_->duplicate (value);
208 this->symbols_->rebind (sym_name, sym_value);
211 line.reset ();
214 // If we are done, we are done!
215 if (last_line_was_read && buffer.length () == 0)
216 break;
218 // If the buffer is pointing at a comment line, ignore it.
219 if (buffer.rd_ptr ()[0] == '#'
220 || buffer.rd_ptr ()[0] == '\n'
221 || (buffer.rd_ptr ()[0] == '\r' && buffer.rd_ptr ()[1] == '\n'))
223 buffer.rd_ptr (end_of_current_line + 1);
224 buffer.crunch ();
225 continue;
228 // Whatever is left is either the start of a name-value-pair or a
229 // continuation of one.
230 line.copy (buffer.rd_ptr (),
231 end_of_current_line - buffer.rd_ptr ());
232 p = line.wr_ptr ();
233 while (p != line.rd_ptr () && (p[-1] == ' ' || p[-1] == '\t'))
234 p--;
235 line.wr_ptr (p);
236 line.wr_ptr ()[0] = '\0';
237 buffer.rd_ptr (end_of_current_line + 1);
238 buffer.crunch ();
241 fio.close ();
244 void
245 JAWS_Config_File_Impl::reset ()
247 delete this->symbols_;
248 delete this->strings_;
249 this->strings_ = new JAWS_strings;
250 this->symbols_ = new JAWS_Symbol_Table (JAWS_CONFIG_FILE_SYMBOL_TABLE_SIZE);
251 this->parse_file ();
254 void
255 JAWS_Config_File_Impl::dump ()
257 JAWS_SYMBOL_TABLE_ITERATOR iter (*this->symbols_);
258 JAWS_SYMBOL_TABLE_ENTRY *entry = 0;
260 while (iter.next (entry))
262 ACE_DEBUG ((LM_DEBUG, "[%D|%t] %s=%s\n",
263 entry->ext_id_,
264 entry->int_id_));
265 iter.advance ();
269 JAWS_Config_File::JAWS_Config_File (const ACE_TCHAR *config_file,
270 const ACE_TCHAR *config_dir)
272 ACE_TCHAR filename[MAXPATHLEN];
273 ACE_OS::strcpy (filename, config_dir);
274 ACE_OS::strcat (filename, config_file);
276 this->impl_ = new JAWS_Config_File_Impl (filename);
280 JAWS_Config_File::find (const ACE_TCHAR *key, const ACE_TCHAR *&value)
282 return this->impl_->find (key, value);
285 void
286 JAWS_Config_File::reset ()
288 this->impl_->reset ();
291 void
292 JAWS_Config_File::dump ()
294 this->impl_->dump ();