Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Capabilities.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Capabilities.cpp,v 1.1.1.3.40.1 2003/03/13 19:44:20 chad Exp $
00003 
00004 #include "ace/Map_Manager.h"
00005 #include "ace/Capabilities.h"
00006 #include "ace/Log_Msg.h"
00007 
00008 #if !defined (__ACE_INLINE__)
00009 #include "ace/Capabilities.i"
00010 #endif /* !__ACE_INLINE__ */
00011 
00012 #define ACE_ESC ((ACE_TCHAR)0x1b)
00013 
00014 ACE_CapEntry::~ACE_CapEntry (void)
00015 {
00016 }
00017 
00018 ACE_Capabilities::ACE_Capabilities (void)
00019 {
00020 }
00021 
00022 ACE_Capabilities::~ACE_Capabilities (void)
00023 {
00024   this->resetcaps ();
00025 }
00026 
00027 const ACE_TCHAR *
00028 ACE_Capabilities::parse (const ACE_TCHAR *buf, ACE_TString &cap)
00029 {
00030   while (*buf != ACE_LIB_TEXT ('\0') && *buf != ACE_LIB_TEXT (','))
00031     {
00032       if (*buf == ACE_LIB_TEXT ('\\'))
00033         {
00034           buf++;
00035           if (*buf == ACE_LIB_TEXT ('E') || *buf == ACE_LIB_TEXT ('e'))
00036             {
00037               cap += ACE_ESC;
00038               buf++;
00039               continue;
00040             }
00041           else if (*buf == ACE_LIB_TEXT ('r'))
00042             {
00043               cap += ACE_LIB_TEXT ('\r');
00044               buf++;
00045               continue;
00046             }
00047           else if (*buf == ACE_LIB_TEXT ('n'))
00048             {
00049               cap += ACE_LIB_TEXT ('\n');
00050               buf++;
00051               continue;
00052             }
00053           else if (*buf == ACE_LIB_TEXT ('t'))
00054             {
00055               cap += ACE_LIB_TEXT ('\t');
00056               buf++;
00057               continue;
00058             }
00059           else if (*buf == ACE_LIB_TEXT ('\\'))
00060             {
00061               cap += *buf++;
00062               continue;
00063             }
00064           if (isdigit(*buf))
00065             {
00066               // @@ UNICODE Does this work with unicode?
00067               int oc = 0;
00068               for (int i = 0;
00069                    i < 3 && *buf && isdigit (*buf);
00070                    i++)
00071                 oc = oc * 8 + (*buf++ - ACE_LIB_TEXT ('0'));
00072 
00073               cap += (ACE_TCHAR) oc;
00074               continue;
00075             }
00076         }
00077       cap += *buf++;
00078     }
00079   return buf;
00080 }
00081 
00082 const ACE_TCHAR *
00083 ACE_Capabilities::parse (const ACE_TCHAR *buf, int &cap)
00084 {
00085   int n = 0;
00086 
00087   while (*buf && isdigit (*buf))
00088     n = n * 10 + (*buf++ - ACE_LIB_TEXT ('0'));
00089 
00090   cap = n;
00091 
00092   return buf;
00093 }
00094 
00095 void
00096 ACE_Capabilities::resetcaps (void)
00097 {
00098   for (ACE_Hash_Map_Iterator<ACE_TString, ACE_CapEntry *, ACE_Null_Mutex> iter (caps_);
00099        !iter.done ();
00100        iter.advance ())
00101     {
00102       ACE_Hash_Map_Entry<ACE_TString,ACE_CapEntry*> *entry;
00103       iter.next (entry);
00104       delete entry->int_id_;
00105     }
00106 
00107   caps_.close ();
00108   caps_.open ();
00109 }
00110 
00111 int
00112 ACE_Capabilities::fillent (const ACE_TCHAR *buf)
00113 {
00114   this->resetcaps ();
00115   while (*buf)
00116     {
00117       ACE_TString s;
00118       int n;
00119       ACE_TString name;
00120       ACE_CapEntry *ce;
00121 
00122       // Skip blanks
00123       while (*buf && isspace(*buf)) buf++;
00124       // If we get end of line return
00125 
00126       if (*buf == ACE_LIB_TEXT ('\0'))
00127         break;
00128 
00129       if (*buf == ACE_LIB_TEXT ('#'))
00130         {
00131           while (*buf && *buf != ACE_LIB_TEXT ('\n'))
00132             buf++;
00133           if (*buf == ACE_LIB_TEXT ('\n'))
00134             buf++;
00135           continue;
00136         }
00137       while(*buf && *buf != ACE_LIB_TEXT ('=')
00138             && *buf!= ACE_LIB_TEXT ('#')
00139             && *buf != ACE_LIB_TEXT (','))
00140         name += *buf++;
00141 
00142       // If name is null.
00143       switch (*buf)
00144         {
00145         case ACE_LIB_TEXT ('='):
00146           // String property
00147           buf = this->parse (buf + 1, s);
00148           ACE_NEW_RETURN (ce,
00149                           ACE_StringCapEntry (s),
00150                           -1);
00151           if (caps_.bind (name, ce) == -1)
00152             {
00153               delete ce;
00154               return -1;
00155             }
00156           break;
00157         case ACE_LIB_TEXT ('#'):
00158           // Integer property
00159           buf = this->parse (buf + 1, n);
00160           ACE_NEW_RETURN (ce,
00161                           ACE_IntCapEntry (n),
00162                           -1);
00163           if (caps_.bind (name, ce) == -1)
00164             {
00165               delete ce;
00166               return -1;
00167             }
00168           break;
00169         case ACE_LIB_TEXT (','):
00170           // Boolean
00171           ACE_NEW_RETURN (ce,
00172                           ACE_BoolCapEntry (1),
00173                           -1);
00174           if (caps_.bind (name, ce) == -1)
00175             {
00176               delete ce;
00177               return -1;
00178             }
00179           break;
00180         default:
00181           return 0;
00182         }
00183 
00184       if (*buf++ != ACE_LIB_TEXT (','))
00185         return -1;
00186     }
00187 
00188   return 0;
00189 }
00190 
00191 int
00192 ACE_Capabilities::is_entry (const ACE_TCHAR *name, const ACE_TCHAR *line)
00193 {
00194   for (;;)
00195     {
00196       // Skip blanks or irrelevant characters
00197       while (*line && isspace(*line))
00198         line++;
00199 
00200       // End of line reached
00201       if (*line == ACE_LIB_TEXT ('\0'))
00202         break;
00203 
00204       // Build the entry name
00205       ACE_TString nextname;
00206       while (*line && *line != ACE_LIB_TEXT ('|') && *line != ACE_LIB_TEXT (','))
00207         nextname += *line++;
00208 
00209       // We have found the required entry?
00210       if (ACE_OS::strcmp (nextname.c_str (), name) == 0)
00211         return 1;
00212 
00213       // Skip puntuaction char if neccesary.
00214       if (*line == ACE_LIB_TEXT ('|') || *line == ACE_LIB_TEXT (','))
00215         line++;
00216       else
00217         {
00218           ACE_DEBUG ((LM_DEBUG,
00219                       ACE_LIB_TEXT ("Invalid entry\n")));
00220           break;
00221         }
00222     }
00223   return 0;
00224 }
00225 
00226 int
00227 ACE_Capabilities::getline (FILE *fp, ACE_TString &line)
00228 {
00229   int ch;
00230 
00231   line.set (0, 0);
00232 
00233   while ((ch = fgetc (fp)) != EOF && ch != ACE_LIB_TEXT ('\n'))
00234     line += (ACE_TCHAR) ch;
00235 
00236   if (ch == EOF && line.length () == 0)
00237     return -1;
00238   else
00239     return 0;
00240 }
00241 
00242 int
00243 ACE_Capabilities::getval (const ACE_TCHAR *keyname, ACE_TString &val)
00244 {
00245   ACE_CapEntry* cap;
00246   if (caps_.find (keyname, cap) == -1)
00247     return -1;
00248 
00249   ACE_StringCapEntry *scap =
00250     ACE_dynamic_cast (ACE_StringCapEntry *, cap);
00251   if (scap == 0)
00252     return -1;
00253 
00254   val = scap->getval ();
00255   return 0;
00256 }
00257 
00258 int
00259 ACE_Capabilities::getval (const ACE_TCHAR *keyname, int &val)
00260 {
00261   ACE_CapEntry *cap;
00262   if (caps_.find (keyname, cap) == -1)
00263     return -1;
00264 
00265   ACE_IntCapEntry *icap =
00266     ACE_dynamic_cast (ACE_IntCapEntry *, cap);
00267   if (icap != 0)
00268     {
00269       val = icap->getval ();
00270       return 0;
00271     }
00272 
00273   ACE_BoolCapEntry *bcap =
00274     ACE_dynamic_cast (ACE_BoolCapEntry *, cap);
00275 
00276   if (bcap == 0)
00277     return -1;
00278 
00279   val = bcap->getval ();
00280   return 0;
00281 }
00282 
00283 #if !defined (ACE_IS_SPLITTING)
00284 static int
00285 is_empty (const ACE_TCHAR *line)
00286 {
00287   while (*line && isspace (*line))
00288     line++;
00289 
00290   return *line == ACE_LIB_TEXT ('\0') || *line == ACE_LIB_TEXT ('#');
00291 }
00292 
00293 static int
00294 is_line (const ACE_TCHAR *line)
00295 {
00296   while (*line && isspace (*line))
00297     line++;
00298 
00299   return *line != ACE_LIB_TEXT ('\0');
00300 }
00301 #endif /* !ACE_IS_SPLITTING */
00302 
00303 int
00304 ACE_Capabilities::getent (const ACE_TCHAR *fname, const ACE_TCHAR *name)
00305 {
00306   FILE *fp = ACE_OS::fopen (fname, ACE_LIB_TEXT ("r"));
00307 
00308   if (fp == 0)
00309     ACE_ERROR_RETURN ((LM_ERROR,
00310                        ACE_LIB_TEXT ("Can't open %s file\n"),
00311                        fname),
00312                       -1);
00313 
00314   int done;
00315   ACE_TString line;
00316 
00317   while (!(done = (this->getline (fp, line) == -1))
00318          && is_empty (line.c_str ()))
00319     continue;
00320 
00321   while (!done)
00322     {
00323       ACE_TString newline;
00324       ACE_TString description;
00325 
00326       while (!(done = (this->getline (fp, newline) == -1)))
00327         if (is_line (newline.c_str ()))
00328           description += newline;
00329         else
00330           break;
00331 
00332       if (this->is_entry (name, line.c_str()))
00333         {
00334           ACE_OS::fclose (fp);
00335           return this->fillent (description.c_str ());
00336         }
00337 
00338       line = newline;
00339       while (!done && is_empty (line.c_str ()))
00340         done = this->getline (fp, line) == -1;
00341     }
00342 
00343   ACE_OS::fclose (fp);
00344   return -1;
00345 }
00346 
00347 #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
00348 template class ACE_Hash_Map_Manager<ACE_TString,ACE_CapEntry*,ACE_Null_Mutex>;
00349 template class ACE_Hash_Map_Iterator<ACE_TString,ACE_CapEntry*,ACE_Null_Mutex>;
00350 template class ACE_Hash_Map_Reverse_Iterator<ACE_TString,ACE_CapEntry*,ACE_Null_Mutex>;
00351 template class ACE_Hash_Map_Entry<ACE_TString,ACE_CapEntry*>;
00352 template class ACE_Hash_Map_Manager_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>;
00353 template class ACE_Hash_Map_Iterator_Base_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>;
00354 template class ACE_Hash_Map_Iterator_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>;
00355 template class ACE_Hash_Map_Reverse_Iterator_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>;
00356 template class ACE_Hash<ACE_TString>;
00357 template class ACE_Equal_To<ACE_TString>;
00358 #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
00359 #pragma instantiate ACE_Hash_Map_Manager<ACE_TString,ACE_CapEntry*,ACE_Null_Mutex>
00360 #pragma instantiate ACE_Hash_Map_Iterator<ACE_TString,ACE_CapEntry*,ACE_Null_Mutex>
00361 #pragma instantiate ACE_Hash_Map_Reverse_Iterator<ACE_TString,ACE_CapEntry*,ACE_Null_Mutex>
00362 #pragma instantiate ACE_Hash_Map_Entry<ACE_TString,ACE_CapEntry*>
00363 #pragma instantiate ACE_Hash_Map_Manager_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>
00364 #pragma instantiate ACE_Hash_Map_Iterator_Base_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>
00365 #pragma instantiate ACE_Hash_Map_Iterator_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>
00366 #pragma instantiate ACE_Hash_Map_Reverse_Iterator_Ex<ACE_TString,ACE_CapEntry*,ACE_Hash<ACE_TString>,ACE_Equal_To<ACE_TString>,ACE_Null_Mutex>
00367 #pragma instantiate ACE_Hash<ACE_TString>
00368 #pragma instantiate ACE_Equal_To<ACE_TString>
00369 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

Generated on Mon Jun 16 11:19:19 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002