Fixed typos
[ACE_TAO.git] / ACE / ace / Capabilities.cpp
blob9ffceda2a912c8cec1c0e071d5caff59903eb806
1 #include "ace/Capabilities.h"
2 #include "ace/OS_NS_ctype.h"
3 #include "ace/OS_Memory.h"
4 #include "ace/OS_NS_string.h"
6 #if !defined (__ACE_INLINE__)
7 #include "ace/Capabilities.inl"
8 #endif /* !__ACE_INLINE__ */
10 #include "ace/OS_NS_stdio.h"
12 #define ACE_ESC ((ACE_TCHAR)0x1b)
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 ACE_CapEntry::~ACE_CapEntry (void)
20 ACE_Capabilities::ACE_Capabilities (void)
21 : caps_ ()
25 ACE_Capabilities::~ACE_Capabilities (void)
27 this->resetcaps ();
30 const ACE_TCHAR *
31 ACE_Capabilities::parse (const ACE_TCHAR *buf, ACE_TString &cap)
33 while (*buf != ACE_TEXT ('\0') && *buf != ACE_TEXT (','))
35 if (*buf == ACE_TEXT ('\\'))
37 ++buf;
38 if (*buf == ACE_TEXT ('E') || *buf == ACE_TEXT ('e'))
40 cap += ACE_ESC;
41 ++buf;
42 continue;
44 else if (*buf == ACE_TEXT ('r'))
46 cap += ACE_TEXT ('\r');
47 ++buf;
48 continue;
50 else if (*buf == ACE_TEXT ('n'))
52 cap += ACE_TEXT ('\n');
53 ++buf;
54 continue;
56 else if (*buf == ACE_TEXT ('t'))
58 cap += ACE_TEXT ('\t');
59 ++buf;
60 continue;
62 else if (*buf == ACE_TEXT ('\\'))
64 cap += *buf++;
65 continue;
67 if (ACE_OS::ace_isdigit(*buf))
69 // @@ UNICODE Does this work with unicode?
70 int oc = 0;
71 for (int i = 0;
72 i < 3 && *buf && ACE_OS::ace_isdigit (*buf);
73 i++)
74 oc = oc * 8 + (*buf++ - ACE_TEXT ('0'));
76 cap += (ACE_TCHAR) oc;
77 continue;
80 cap += *buf++;
82 return buf;
85 const ACE_TCHAR *
86 ACE_Capabilities::parse (const ACE_TCHAR *buf, int &cap)
88 int n = 0;
90 while (*buf && ACE_OS::ace_isdigit (*buf))
91 n = n * 10 + (*buf++ - ACE_TEXT ('0'));
93 cap = n;
95 return buf;
98 void
99 ACE_Capabilities::resetcaps (void)
101 for (CAPABILITIES_MAP::ITERATOR iter (this->caps_);
102 !iter.done ();
103 iter.advance ())
105 CAPABILITIES_MAP::ENTRY *entry = 0;
106 iter.next (entry);
107 delete entry->int_id_;
110 this->caps_.close ();
111 this->caps_.open ();
115 ACE_Capabilities::fillent (const ACE_TCHAR *buf)
117 this->resetcaps ();
118 while (*buf)
120 ACE_TString s;
121 int n;
122 ACE_TString name;
123 ACE_CapEntry *ce;
125 // Skip blanks
126 while (*buf && ACE_OS::ace_isspace(*buf)) buf++;
127 // If we get end of line return
129 if (*buf == ACE_TEXT ('\0'))
130 break;
132 if (*buf == ACE_TEXT ('#'))
134 while (*buf && *buf != ACE_TEXT ('\n'))
135 buf++;
136 if (*buf == ACE_TEXT ('\n'))
137 buf++;
138 continue;
140 while(*buf && *buf != ACE_TEXT ('=')
141 && *buf!= ACE_TEXT ('#')
142 && *buf != ACE_TEXT (','))
143 name += *buf++;
145 // If name is null.
146 switch (*buf)
148 case ACE_TEXT ('='):
149 // String property
150 buf = this->parse (buf + 1, s);
151 ACE_NEW_RETURN (ce,
152 ACE_StringCapEntry (s),
153 -1);
154 if (this->caps_.bind (name, ce) == -1)
156 delete ce;
157 return -1;
159 break;
160 case ACE_TEXT ('#'):
161 // Integer property
162 buf = this->parse (buf + 1, n);
163 ACE_NEW_RETURN (ce,
164 ACE_IntCapEntry (n),
165 -1);
166 if (this->caps_.bind (name, ce) == -1)
168 delete ce;
169 return -1;
171 break;
172 case ACE_TEXT (','):
173 // Boolean
174 ACE_NEW_RETURN (ce,
175 ACE_BoolCapEntry (1),
176 -1);
177 if (this->caps_.bind (name, ce) == -1)
179 delete ce;
180 return -1;
182 break;
183 default:
184 return 0;
187 if (*buf++ != ACE_TEXT (','))
188 return -1;
191 return 0;
195 ACE_Capabilities::is_entry (const ACE_TCHAR *name, const ACE_TCHAR *line)
197 for (;;)
199 // Skip blanks or irrelevant characters
200 while (*line && ACE_OS::ace_isspace(*line))
201 ++line;
203 // End of line reached
204 if (*line == ACE_TEXT ('\0'))
205 break;
207 // Build the entry name
208 ACE_TString nextname;
209 while (*line && *line != ACE_TEXT ('|') && *line != ACE_TEXT (','))
210 nextname += *line++;
212 // We have found the required entry?
213 if (ACE_OS::strcmp (nextname.c_str (), name) == 0)
214 return 1;
216 // Skip puntuaction char if neccesary.
217 if (*line == ACE_TEXT ('|') || *line == ACE_TEXT (','))
218 ++line;
219 else
221 ACELIB_DEBUG ((LM_DEBUG,
222 ACE_TEXT ("Invalid entry\n")));
223 break;
226 return 0;
230 ACE_Capabilities::getline (FILE *fp, ACE_TString &line)
232 int ch;
234 line.set (0, 0);
236 while ((ch = ACE_OS::fgetc (fp)) != EOF && ch != ACE_TEXT ('\n'))
237 line += (ACE_TCHAR) ch;
239 if (ch == EOF && line.length () == 0)
240 return -1;
241 else
242 return 0;
246 ACE_Capabilities::getval (const ACE_TCHAR *keyname, ACE_TString &val)
248 ACE_CapEntry* cap = 0;
249 if (this->caps_.find (keyname, cap) == -1)
250 return -1;
252 ACE_StringCapEntry *scap =
253 dynamic_cast<ACE_StringCapEntry *> (cap);
254 if (scap == 0)
255 return -1;
257 val = scap->getval ();
258 return 0;
262 ACE_Capabilities::getval (const ACE_TCHAR *keyname, int &val)
264 ACE_CapEntry *cap = 0;
265 if (this->caps_.find (keyname, cap) == -1)
266 return -1;
268 ACE_IntCapEntry *icap =
269 dynamic_cast<ACE_IntCapEntry *> (cap);
270 if (icap != 0)
272 val = icap->getval ();
273 return 0;
276 ACE_BoolCapEntry *bcap =
277 dynamic_cast<ACE_BoolCapEntry *> (cap);
279 if (bcap == 0)
280 return -1;
282 val = bcap->getval ();
283 return 0;
286 static int
287 is_empty (const ACE_TCHAR *line)
289 while (*line && ACE_OS::ace_isspace (*line))
290 ++line;
292 return *line == ACE_TEXT ('\0') || *line == ACE_TEXT ('#');
295 static int
296 is_line (const ACE_TCHAR *line)
298 while (*line && ACE_OS::ace_isspace (*line))
299 ++line;
301 return *line != ACE_TEXT ('\0');
305 ACE_Capabilities::getent (const ACE_TCHAR *fname, const ACE_TCHAR *name)
307 FILE *fp = ACE_OS::fopen (fname, ACE_TEXT ("r"));
309 if (fp == 0)
310 ACELIB_ERROR_RETURN ((LM_ERROR,
311 ACE_TEXT ("Can't open %s file\n"),
312 fname),
313 -1);
315 int done;
316 ACE_TString line;
318 while (0 == (done = (this->getline (fp, line) == -1))
319 && is_empty (line.c_str ()))
320 continue;
322 while (!done)
324 ACE_TString newline;
325 ACE_TString description;
327 while (0 == (done = (this->getline (fp, newline) == -1)))
328 if (is_line (newline.c_str ()))
329 description += newline;
330 else
331 break;
333 if (this->is_entry (name, line.c_str()))
335 ACE_OS::fclose (fp);
336 return this->fillent (description.c_str ());
339 line = newline;
340 while (!done && is_empty (line.c_str ()))
341 done = this->getline (fp, line) == -1;
344 ACE_OS::fclose (fp);
345 return -1;
348 ACE_ALLOC_HOOK_DEFINE(ACE_StringCapEntry)
349 ACE_ALLOC_HOOK_DEFINE(ACE_IntCapEntry)
350 ACE_ALLOC_HOOK_DEFINE(ACE_BoolCapEntry)
352 ACE_END_VERSIONED_NAMESPACE_DECL