Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Capabilities.cpp
blob4efd172c886a0ce624676dfcc873bedc235c5f2d
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_Capabilities::ACE_Capabilities ()
17 : caps_ ()
21 ACE_Capabilities::~ACE_Capabilities ()
23 this->resetcaps ();
26 const ACE_TCHAR *
27 ACE_Capabilities::parse (const ACE_TCHAR *buf, ACE_TString &cap)
29 while (*buf != ACE_TEXT ('\0') && *buf != ACE_TEXT (','))
31 if (*buf == ACE_TEXT ('\\'))
33 ++buf;
34 if (*buf == ACE_TEXT ('E') || *buf == ACE_TEXT ('e'))
36 cap += ACE_ESC;
37 ++buf;
38 continue;
40 else if (*buf == ACE_TEXT ('r'))
42 cap += ACE_TEXT ('\r');
43 ++buf;
44 continue;
46 else if (*buf == ACE_TEXT ('n'))
48 cap += ACE_TEXT ('\n');
49 ++buf;
50 continue;
52 else if (*buf == ACE_TEXT ('t'))
54 cap += ACE_TEXT ('\t');
55 ++buf;
56 continue;
58 else if (*buf == ACE_TEXT ('\\'))
60 cap += *buf++;
61 continue;
63 if (ACE_OS::ace_isdigit(*buf))
65 // @@ UNICODE Does this work with unicode?
66 int oc = 0;
67 for (int i = 0;
68 i < 3 && *buf && ACE_OS::ace_isdigit (*buf);
69 i++)
70 oc = oc * 8 + (*buf++ - ACE_TEXT ('0'));
72 cap += (ACE_TCHAR) oc;
73 continue;
76 cap += *buf++;
78 return buf;
81 const ACE_TCHAR *
82 ACE_Capabilities::parse (const ACE_TCHAR *buf, int &cap)
84 int n = 0;
86 while (*buf && ACE_OS::ace_isdigit (*buf))
87 n = n * 10 + (*buf++ - ACE_TEXT ('0'));
89 cap = n;
91 return buf;
94 void
95 ACE_Capabilities::resetcaps ()
97 for (CAPABILITIES_MAP::ITERATOR iter (this->caps_);
98 !iter.done ();
99 iter.advance ())
101 CAPABILITIES_MAP::ENTRY *entry = 0;
102 iter.next (entry);
103 delete entry->int_id_;
106 this->caps_.close ();
107 this->caps_.open ();
111 ACE_Capabilities::fillent (const ACE_TCHAR *buf)
113 this->resetcaps ();
114 while (*buf)
116 ACE_TString s;
117 int n;
118 ACE_TString name;
119 ACE_CapEntry *ce;
121 // Skip blanks
122 while (*buf && ACE_OS::ace_isspace(*buf)) buf++;
123 // If we get end of line return
125 if (*buf == ACE_TEXT ('\0'))
126 break;
128 if (*buf == ACE_TEXT ('#'))
130 while (*buf && *buf != ACE_TEXT ('\n'))
131 buf++;
132 if (*buf == ACE_TEXT ('\n'))
133 buf++;
134 continue;
136 while(*buf && *buf != ACE_TEXT ('=')
137 && *buf!= ACE_TEXT ('#')
138 && *buf != ACE_TEXT (','))
139 name += *buf++;
141 // If name is null.
142 switch (*buf)
144 case ACE_TEXT ('='):
145 // String property
146 buf = this->parse (buf + 1, s);
147 ACE_NEW_RETURN (ce,
148 ACE_StringCapEntry (s),
149 -1);
150 if (this->caps_.bind (name, ce) == -1)
152 delete ce;
153 return -1;
155 break;
156 case ACE_TEXT ('#'):
157 // Integer property
158 buf = this->parse (buf + 1, n);
159 ACE_NEW_RETURN (ce,
160 ACE_IntCapEntry (n),
161 -1);
162 if (this->caps_.bind (name, ce) == -1)
164 delete ce;
165 return -1;
167 break;
168 case ACE_TEXT (','):
169 // Boolean
170 ACE_NEW_RETURN (ce,
171 ACE_BoolCapEntry (1),
172 -1);
173 if (this->caps_.bind (name, ce) == -1)
175 delete ce;
176 return -1;
178 break;
179 default:
180 return 0;
183 if (*buf++ != ACE_TEXT (','))
184 return -1;
187 return 0;
191 ACE_Capabilities::is_entry (const ACE_TCHAR *name, const ACE_TCHAR *line)
193 for (;;)
195 // Skip blanks or irrelevant characters
196 while (*line && ACE_OS::ace_isspace(*line))
197 ++line;
199 // End of line reached
200 if (*line == ACE_TEXT ('\0'))
201 break;
203 // Build the entry name
204 ACE_TString nextname;
205 while (*line && *line != ACE_TEXT ('|') && *line != ACE_TEXT (','))
206 nextname += *line++;
208 // We have found the required entry?
209 if (ACE_OS::strcmp (nextname.c_str (), name) == 0)
210 return 1;
212 // Skip puntuaction char if neccesary.
213 if (*line == ACE_TEXT ('|') || *line == ACE_TEXT (','))
214 ++line;
215 else
217 ACELIB_DEBUG ((LM_DEBUG,
218 ACE_TEXT ("Invalid entry\n")));
219 break;
222 return 0;
226 ACE_Capabilities::getline (FILE *fp, ACE_TString &line)
228 int ch;
230 line.set (0, 0);
232 while ((ch = ACE_OS::fgetc (fp)) != EOF && ch != ACE_TEXT ('\n'))
233 line += (ACE_TCHAR) ch;
235 if (ch == EOF && line.length () == 0)
236 return -1;
237 else
238 return 0;
242 ACE_Capabilities::getval (const ACE_TCHAR *keyname, ACE_TString &val)
244 ACE_CapEntry* cap = 0;
245 if (this->caps_.find (keyname, cap) == -1)
246 return -1;
248 ACE_StringCapEntry *scap =
249 dynamic_cast<ACE_StringCapEntry *> (cap);
250 if (scap == 0)
251 return -1;
253 val = scap->getval ();
254 return 0;
258 ACE_Capabilities::getval (const ACE_TCHAR *keyname, int &val)
260 ACE_CapEntry *cap = 0;
261 if (this->caps_.find (keyname, cap) == -1)
262 return -1;
264 ACE_IntCapEntry *icap =
265 dynamic_cast<ACE_IntCapEntry *> (cap);
266 if (icap != 0)
268 val = icap->getval ();
269 return 0;
272 ACE_BoolCapEntry *bcap =
273 dynamic_cast<ACE_BoolCapEntry *> (cap);
275 if (bcap == 0)
276 return -1;
278 val = bcap->getval ();
279 return 0;
282 static int
283 is_empty (const ACE_TCHAR *line)
285 while (*line && ACE_OS::ace_isspace (*line))
286 ++line;
288 return *line == ACE_TEXT ('\0') || *line == ACE_TEXT ('#');
291 static int
292 is_line (const ACE_TCHAR *line)
294 while (*line && ACE_OS::ace_isspace (*line))
295 ++line;
297 return *line != ACE_TEXT ('\0');
301 ACE_Capabilities::getent (const ACE_TCHAR *fname, const ACE_TCHAR *name)
303 FILE *fp = ACE_OS::fopen (fname, ACE_TEXT ("r"));
305 if (fp == 0)
306 ACELIB_ERROR_RETURN ((LM_ERROR,
307 ACE_TEXT ("Can't open %s file\n"),
308 fname),
309 -1);
311 int done;
312 ACE_TString line;
314 while (0 == (done = (this->getline (fp, line) == -1))
315 && is_empty (line.c_str ()))
316 continue;
318 while (!done)
320 ACE_TString newline;
321 ACE_TString description;
323 while (0 == (done = (this->getline (fp, newline) == -1)))
324 if (is_line (newline.c_str ()))
325 description += newline;
326 else
327 break;
329 if (this->is_entry (name, line.c_str()))
331 ACE_OS::fclose (fp);
332 return this->fillent (description.c_str ());
335 line = newline;
336 while (!done && is_empty (line.c_str ()))
337 done = this->getline (fp, line) == -1;
340 ACE_OS::fclose (fp);
341 return -1;
344 ACE_ALLOC_HOOK_DEFINE(ACE_StringCapEntry)
345 ACE_ALLOC_HOOK_DEFINE(ACE_IntCapEntry)
346 ACE_ALLOC_HOOK_DEFINE(ACE_BoolCapEntry)
348 ACE_END_VERSIONED_NAMESPACE_DECL