ACE_Registry_ImpExp Class Reference

Configuration object that imports/exports data to a file formatted using the Win32 Registry file export format. This format looks like [Section] "key"="String Data" "key"=dword: numeric data in hexidecimal format "key"=hex: binary data. More...

#include <Configuration_Import_Export.h>

Inheritance diagram for ACE_Registry_ImpExp:

Inheritance graph
[legend]
Collaboration diagram for ACE_Registry_ImpExp:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Registry_ImpExp (ACE_Configuration &)
 Construction.
virtual ~ACE_Registry_ImpExp (void)
 Destruction.
virtual int import_config (const ACE_TCHAR *filename)
virtual int export_config (const ACE_TCHAR *filename)

Private Member Functions

int export_section (const ACE_Configuration_Section_Key &section, const ACE_TString &path, FILE *out)
int process_previous_line_format (ACE_TCHAR *buffer, ACE_Configuration_Section_Key &section)
 ACE_Registry_ImpExp (const ACE_Registry_ImpExp &)
ACE_Registry_ImpExpoperator= (const ACE_Registry_ImpExp &)

Detailed Description

Configuration object that imports/exports data to a file formatted using the Win32 Registry file export format. This format looks like [Section] "key"="String Data" "key"=dword: numeric data in hexidecimal format "key"=hex: binary data.

Todo:

Definition at line 94 of file Configuration_Import_Export.h.


Constructor & Destructor Documentation

ACE_Registry_ImpExp::ACE_Registry_ImpExp ( ACE_Configuration  ) 

Construction.

Definition at line 20 of file Configuration_Import_Export.cpp.

00021     : ACE_Config_ImpExp_Base (config)
00022 {
00023 }

ACE_Registry_ImpExp::~ACE_Registry_ImpExp ( void   )  [virtual]

Destruction.

Definition at line 25 of file Configuration_Import_Export.cpp.

00026 {
00027 }

ACE_Registry_ImpExp::ACE_Registry_ImpExp ( const ACE_Registry_ImpExp  )  [private]


Member Function Documentation

int ACE_Registry_ImpExp::export_config ( const ACE_TCHAR filename  )  [virtual]

This method exports the entire configuration database to filename. Once the file is opened this method calls export_section() passing the root section.

Implements ACE_Config_ImpExp_Base.

Definition at line 218 of file Configuration_Import_Export.cpp.

References ACE_TEXT, ACE_Config_ImpExp_Base::config_, export_section(), ACE_OS::fclose(), ACE_OS::fopen(), and ACE_Configuration::root_section().

Referenced by ACE_Configuration::export_config().

00219 {
00220   if (0 == filename)
00221     {
00222       errno = EINVAL;
00223       return -1;
00224     }
00225   int result = -1;
00226 
00227   FILE* out = ACE_OS::fopen (filename, ACE_TEXT ("w"));
00228   if (out)
00229     {
00230       result = this->export_section (config_.root_section (),
00231                                      ACE_TEXT (""),
00232                                      out);
00233       // The data may have been buffered and will be flush on close,
00234       // so we need to check that the close succeeds.
00235       if (ACE_OS::fclose (out) < 0)
00236         result = -7;
00237     }
00238   return result;
00239 }

int ACE_Registry_ImpExp::export_section ( const ACE_Configuration_Section_Key section,
const ACE_TString path,
FILE *  out 
) [private]

Definition at line 246 of file Configuration_Import_Export.cpp.

References ACE_TEXT, ACE_Configuration::BINARY, ACE_Config_ImpExp_Base::config_, ACE_Configuration::enumerate_sections(), ACE_Configuration::enumerate_values(), ACE_String_Base< CHAR >::fast_rep(), ACE_OS::fputs(), ACE_Configuration::get_binary_value(), ACE_Configuration::get_integer_value(), ACE_Configuration::get_string_value(), ACE_Configuration::INTEGER, ACE_Configuration::INVALID, ACE_String_Base< CHAR >::length(), ACE_Configuration::open_section(), ACE_OS::sprintf(), and ACE_Configuration::STRING.

Referenced by export_config().

00249 {
00250   // don't export the root
00251   if (path.length ())
00252     {
00253       // Write out the section header
00254       ACE_TString header = ACE_TEXT ("[");
00255       header += path;
00256       header += ACE_TEXT ("]");
00257       header += ACE_TEXT (" \n");
00258       if (ACE_OS::fputs (header.fast_rep (), out) < 0)
00259         return -1;
00260       // Write out each value
00261       int index = 0;
00262       ACE_TString name;
00263       ACE_Configuration::VALUETYPE type;
00264       ACE_TString line;
00265       ACE_TCHAR int_value[32];
00266       ACE_TCHAR bin_value[3];
00267       void* binary_data;
00268       size_t binary_length;
00269       ACE_TString string_value;
00270       while (!config_.enumerate_values (section, index, name, type))
00271         {
00272           line = ACE_TEXT ("\"") + name + ACE_TEXT ("\"=");
00273           switch (type)
00274             {
00275             case ACE_Configuration::INTEGER:
00276               {
00277                 u_int value;
00278                 if (config_.get_integer_value (section, name.fast_rep (), value))
00279                   return -2;
00280                 ACE_OS::sprintf (int_value, ACE_TEXT ("%08x"), value);
00281                 line += ACE_TEXT ("dword:");
00282                 line += int_value;
00283                 break;
00284               }
00285             case ACE_Configuration::STRING:
00286               {
00287                 if (config_.get_string_value (section,
00288                                               name.fast_rep (),
00289                                               string_value))
00290                   return -2;
00291                 line += ACE_TEXT ("\"");
00292                 line += string_value + ACE_TEXT ("\"");
00293                 break;
00294               }
00295 #ifdef _WIN32
00296             case ACE_Configuration::INVALID:
00297               break;  // JDO added break.  Otherwise INVALID is processed
00298               // like BINARY. If that's correct, please remove the
00299               // break and these comments
00300 #endif
00301             case ACE_Configuration::BINARY:
00302               {
00303                 // not supported yet - maybe use BASE64 codeing?
00304                 if (config_.get_binary_value (section,
00305                                               name.fast_rep (),
00306                                               binary_data,
00307                                               binary_length))
00308                   return -2;
00309                 line += ACE_TEXT ("hex:");
00310                 unsigned char* ptr = (unsigned char*)binary_data;
00311                 while (binary_length)
00312                   {
00313                     if (ptr != binary_data)
00314                       {
00315                         line += ACE_TEXT (",");
00316                       }
00317                     ACE_OS::sprintf (bin_value, ACE_TEXT ("%02x"), *ptr);
00318                     line += bin_value;
00319                     --binary_length;
00320                     ++ptr;
00321                   }
00322                 delete [] (char*) binary_data;
00323                 break;
00324               }
00325             default:
00326               return -3;
00327             }
00328           line += ACE_TEXT ("\n");
00329           if (ACE_OS::fputs (line.fast_rep (), out) < 0)
00330             return -4;
00331           ++index;
00332         }
00333     }
00334   // Export all sub sections
00335   int index = 0;
00336   ACE_TString name;
00337   ACE_Configuration_Section_Key sub_key;
00338   ACE_TString sub_section;
00339   while (!config_.enumerate_sections (section, index, name))
00340     {
00341       ACE_TString sub_section (path);
00342       if (path.length ())
00343         sub_section += ACE_TEXT ("\\");
00344       sub_section += name;
00345       if (config_.open_section (section, name.fast_rep (), 0, sub_key))
00346         return -5;
00347       if (export_section (sub_key, sub_section.fast_rep (), out))
00348         return -6;
00349       ++index;
00350     }
00351   return 0;
00352 }

int ACE_Registry_ImpExp::import_config ( const ACE_TCHAR filename  )  [virtual]

Imports the configuration database from filename. No existing data is removed.

Implements ACE_Config_ImpExp_Base.

Definition at line 32 of file Configuration_Import_Export.cpp.

References ACE_NEW_NORETURN, ACE_NEW_RETURN, ACE_TEXT, ACE_Config_ImpExp_Base::config_, ACE_Configuration::expand_path(), ACE_OS::fclose(), ACE_OS::fgets(), ACE_OS::fopen(), ACE_OS::memcpy(), process_previous_line_format(), ACE_Configuration::root_section(), ACE_Configuration::set_binary_value(), ACE_Configuration::set_integer_value(), ACE_Configuration::set_string_value(), ACE_OS::strchr(), ACE_OS::strlen(), ACE_OS::strncmp(), ACE_OS::strrchr(), and ACE_OS::strtoul().

Referenced by ACE_Configuration::import_config().

00033 {
00034   if (0 == filename)
00035     {
00036       errno = EINVAL;
00037       return -1;
00038     }
00039   FILE* in = ACE_OS::fopen (filename, ACE_TEXT ("r"));
00040   if (!in)
00041     return -1;
00042 
00043   u_int buffer_size = 4096;
00044   u_int read_pos = 0;
00045   ACE_TCHAR *buffer = 0;
00046   ACE_NEW_NORETURN (buffer, ACE_TCHAR[buffer_size]);
00047   if (!buffer)
00048     {
00049       ACE_Errno_Guard guard (errno);
00050       (void) ACE_OS::fclose (in);
00051       return -1;
00052     }
00053   ACE_Configuration_Section_Key section;
00054   ACE_TCHAR *end = 0;
00055 
00056   while (ACE_OS::fgets (buffer+read_pos, buffer_size - read_pos, in))
00057     {
00058       // Check if we got all the line.
00059       end = ACE_OS::strrchr (buffer + read_pos,
00060                              ACE_TEXT ('\n')); // look for end of line
00061       if (!end) // we havn't reach the end of the line yet
00062         {
00063           // allocate a new buffer - double size the previous one
00064           ACE_TCHAR *temp_buffer;
00065           ACE_NEW_NORETURN (temp_buffer, ACE_TCHAR[buffer_size * 2]);
00066           if (!temp_buffer)
00067             {
00068               ACE_Errno_Guard guard (errno);
00069               delete [] buffer;
00070               (void) ACE_OS::fclose (in);
00071               return -1;
00072             }
00073 
00074           // copy the beginnning of the line
00075           ACE_OS::memcpy (temp_buffer, buffer, buffer_size);
00076           read_pos = buffer_size - 1;
00077           buffer_size *= 2;
00078           delete [] buffer;
00079           buffer = temp_buffer;
00080           continue;
00081         }
00082       read_pos = 0;
00083 
00084       // Check for a comment
00085       if (buffer[0] == ACE_TEXT (';') || buffer[0] == ACE_TEXT ('#'))
00086         continue;
00087 
00088       if (buffer[0] == ACE_TEXT ('['))
00089         {
00090           // We have a new section here, strip out the section name
00091           end = ACE_OS::strrchr (buffer, ACE_TEXT (']'));
00092           if (!end)
00093             {
00094               ACE_OS::fclose (in);
00095               delete [] buffer;
00096               return -3;
00097             }
00098           *end = 0;
00099 
00100           if (config_.expand_path (config_.root_section (), buffer + 1, section, 1))
00101             {
00102               ACE_OS::fclose (in);
00103               delete [] buffer;
00104               return -3;
00105             }
00106           continue;
00107         }              // end if firs char is a [
00108 
00109       if (buffer[0] == ACE_TEXT ('"'))
00110         {
00111           // we have a value
00112           end = ACE_OS::strchr (buffer+1, '"');
00113           if (!end)  // no closing quote, not a value so just skip it
00114             continue;
00115 
00116           // null terminate the name
00117           *end = 0;
00118           ACE_TCHAR* name = buffer + 1;
00119           end+=2;
00120           // determine the type
00121           if (*end == '\"')
00122             {
00123               // string type
00124               // truncate trailing "
00125               ++end;
00126               ACE_TCHAR* trailing = ACE_OS::strrchr (end, '"');
00127               if (trailing)
00128                 *trailing = 0;
00129               if (config_.set_string_value (section, name, end))
00130                 {
00131                   ACE_OS::fclose (in);
00132                   delete [] buffer;
00133                   return -4;
00134                 }
00135             }
00136           else if (ACE_OS::strncmp (end, ACE_TEXT ("dword:"), 6) == 0)
00137             {
00138               // number type
00139               ACE_TCHAR* endptr = 0;
00140               unsigned long value = ACE_OS::strtoul (end + 6, &endptr, 16);
00141               if (config_.set_integer_value (section, name, value))
00142                 {
00143                   ACE_OS::fclose (in);
00144                   delete [] buffer;
00145                   return -4;
00146                 }
00147             }
00148           else if (ACE_OS::strncmp (end, ACE_TEXT ("hex:"), 4) == 0)
00149             {
00150               // binary type
00151               size_t string_length = ACE_OS::strlen (end + 4);
00152               // divide by 3 to get the actual buffer length
00153               size_t length = string_length / 3;
00154               size_t remaining = length;
00155               u_char* data = 0;
00156               ACE_NEW_RETURN (data,
00157                               u_char[length],
00158                               -1);
00159               u_char* out = data;
00160               ACE_TCHAR* inb = end + 4;
00161               ACE_TCHAR* endptr = 0;
00162               while (remaining)
00163                 {
00164                   u_char charin = (u_char) ACE_OS::strtoul (inb, &endptr, 16);
00165                   *out = charin;
00166                   ++out;
00167                   --remaining;
00168                   inb += 3;
00169                 }
00170               if (config_.set_binary_value (section, name, data, length))
00171                 {
00172                   ACE_OS::fclose (in);
00173                   delete [] data;
00174                   delete [] buffer;
00175                   return -4;
00176                 }
00177               else
00178                 delete [] data;
00179             }
00180           else
00181             {
00182               // invalid type, ignore
00183               continue;
00184             }
00185         }// end if first char is a "
00186       else
00187         {
00188           // if the first character is not a ", [, ;, or # we may be
00189           // processing a file in the old format.
00190           // Try and process the line as such and if it fails,
00191           // return an error
00192           int rc = process_previous_line_format (buffer, section);
00193           if (rc != 0)
00194             {
00195               ACE_OS::fclose (in);
00196               delete [] buffer;
00197               return rc;
00198             }
00199         }             // end if maybe old format
00200     }                 // end while fgets
00201 
00202   if (ferror (in))
00203     {
00204       ACE_OS::fclose (in);
00205       delete [] buffer;
00206       return -1;
00207     }
00208 
00209   ACE_OS::fclose (in);
00210   delete [] buffer;
00211   return 0;
00212 }

ACE_Registry_ImpExp& ACE_Registry_ImpExp::operator= ( const ACE_Registry_ImpExp  )  [private]

int ACE_Registry_ImpExp::process_previous_line_format ( ACE_TCHAR buffer,
ACE_Configuration_Section_Key section 
) [private]

Definition at line 358 of file Configuration_Import_Export.cpp.

References ACE_TEXT, ACE_OS::atoi(), ACE_Config_ImpExp_Base::config_, ACE_Configuration::set_integer_value(), ACE_Configuration::set_string_value(), ACE_OS::strchr(), and ACE_OS::strpbrk().

Referenced by import_config().

00360 {
00361   // Chop any cr/lf at the end of the line.
00362   ACE_TCHAR *endp = ACE_OS::strpbrk (buffer, ACE_TEXT ("\r\n"));
00363   if (endp != 0)
00364     *endp = '\0';
00365 
00366   // assume this is a value, read in the value name
00367   ACE_TCHAR* end = ACE_OS::strchr (buffer, '=');
00368   if (end)  // no =, not a value so just skip it
00369     {
00370       // null terminate the name
00371       *end = 0;
00372       ++end;
00373       // determine the type
00374       if (*end == '\"')
00375         {
00376           // string type
00377           if(config_.set_string_value (section, buffer, end + 1))
00378             return -4;
00379         }
00380       else if (*end == '#')
00381         {
00382           // number type
00383           u_int value = ACE_OS::atoi (end + 1);
00384           if (config_.set_integer_value (section, buffer, value))
00385             return -4;
00386         }
00387     }
00388   return 0;
00389 }                // end read_previous_line_format


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:31 2010 for ACE by  doxygen 1.4.7