Public Member Functions | Private Member Functions

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:
  • Add dynamic buffer when importing. currently it will not allow importing of values greater than a fixed ammount (4096 bytes)

Definition at line 94 of file Configuration_Import_Export.h.


Constructor & Destructor Documentation

ACE_Registry_ImpExp::ACE_Registry_ImpExp ( ACE_Configuration config  ) 

Construction.

Definition at line 20 of file Configuration_Import_Export.cpp.

    : ACE_Config_ImpExp_Base (config)
{
}

ACE_Registry_ImpExp::~ACE_Registry_ImpExp ( void   )  [virtual]

Destruction.

Definition at line 25 of file Configuration_Import_Export.cpp.

{
}

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.

{
  if (0 == filename)
    {
      errno = EINVAL;
      return -1;
    }
  int result = -1;

  FILE* out = ACE_OS::fopen (filename, ACE_TEXT ("w"));
  if (out)
    {
      result = this->export_section (config_.root_section (),
                                     ACE_TEXT (""),
                                     out);
      // The data may have been buffered and will be flush on close,
      // so we need to check that the close succeeds.
      if (ACE_OS::fclose (out) < 0)
        result = -7;
    }
  return result;
}

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.

{
  // don't export the root
  if (path.length ())
    {
      // Write out the section header
      ACE_TString header = ACE_TEXT ("[");
      header += path;
      header += ACE_TEXT ("]");
      header += ACE_TEXT ("\n");
      if (ACE_OS::fputs (header.fast_rep (), out) < 0)
        return -1;
      // Write out each value
      int index = 0;
      ACE_TString name;
      ACE_Configuration::VALUETYPE type;
      ACE_TString line;
      ACE_TCHAR int_value[32];
      ACE_TCHAR bin_value[3];
      void* binary_data;
      size_t binary_length;
      ACE_TString string_value;
      while (!config_.enumerate_values (section, index, name, type))
        {
          line = ACE_TEXT ("\"") + name + ACE_TEXT ("\"=");
          switch (type)
            {
            case ACE_Configuration::INTEGER:
              {
                u_int value;
                if (config_.get_integer_value (section, name.fast_rep (), value))
                  return -2;
                ACE_OS::sprintf (int_value, ACE_TEXT ("%08x"), value);
                line += ACE_TEXT ("dword:");
                line += int_value;
                break;
              }
            case ACE_Configuration::STRING:
              {
                if (config_.get_string_value (section,
                                              name.fast_rep (),
                                              string_value))
                  return -2;
                line += ACE_TEXT ("\"");
                line += string_value + ACE_TEXT ("\"");
                break;
              }
#ifdef _WIN32
            case ACE_Configuration::INVALID:
              break;  // JDO added break.  Otherwise INVALID is processed
              // like BINARY. If that's correct, please remove the
              // break and these comments
#endif
            case ACE_Configuration::BINARY:
              {
                // not supported yet - maybe use BASE64 codeing?
                if (config_.get_binary_value (section,
                                              name.fast_rep (),
                                              binary_data,
                                              binary_length))
                  return -2;
                line += ACE_TEXT ("hex:");
                unsigned char* ptr = (unsigned char*)binary_data;
                while (binary_length)
                  {
                    if (ptr != binary_data)
                      {
                        line += ACE_TEXT (",");
                      }
                    ACE_OS::sprintf (bin_value, ACE_TEXT ("%02x"), *ptr);
                    line += bin_value;
                    --binary_length;
                    ++ptr;
                  }
                delete [] (char*) binary_data;
                break;
              }
            default:
              return -3;
            }
          line += ACE_TEXT ("\n");
          if (ACE_OS::fputs (line.fast_rep (), out) < 0)
            return -4;
          ++index;
        }
    }
  // Export all sub sections
  int index = 0;
  ACE_TString name;
  ACE_Configuration_Section_Key sub_key;
  ACE_TString sub_section;
  while (!config_.enumerate_sections (section, index, name))
    {
      ACE_TString sub_section (path);
      if (path.length ())
        sub_section += ACE_TEXT ("\\");
      sub_section += name;
      if (config_.open_section (section, name.fast_rep (), 0, sub_key))
        return -5;
      if (export_section (sub_key, sub_section.fast_rep (), out))
        return -6;
      ++index;
    }
  return 0;
}

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.

{
  if (0 == filename)
    {
      errno = EINVAL;
      return -1;
    }
  FILE* in = ACE_OS::fopen (filename, ACE_TEXT ("r"));
  if (!in)
    return -1;

  u_int buffer_size = 4096;
  u_int read_pos = 0;
  ACE_TCHAR *buffer = 0;
  ACE_NEW_NORETURN (buffer, ACE_TCHAR[buffer_size]);
  if (!buffer)
    {
      ACE_Errno_Guard guard (errno);
      (void) ACE_OS::fclose (in);
      return -1;
    }
  ACE_Configuration_Section_Key section;
  ACE_TCHAR *end = 0;

  while (ACE_OS::fgets (buffer+read_pos, buffer_size - read_pos, in))
    {
      // Check if we got all the line.
      end = ACE_OS::strrchr (buffer + read_pos,
                             ACE_TEXT ('\n')); // look for end of line
      if (!end) // we havn't reach the end of the line yet
        {
          // allocate a new buffer - double size the previous one
          ACE_TCHAR *temp_buffer;
          ACE_NEW_NORETURN (temp_buffer, ACE_TCHAR[buffer_size * 2]);
          if (!temp_buffer)
            {
              ACE_Errno_Guard guard (errno);
              delete [] buffer;
              (void) ACE_OS::fclose (in);
              return -1;
            }

          // copy the beginnning of the line
          ACE_OS::memcpy (temp_buffer, buffer, buffer_size);
          read_pos = buffer_size - 1;
          buffer_size *= 2;
          delete [] buffer;
          buffer = temp_buffer;
          continue;
        }
      read_pos = 0;

      // Check for a comment
      if (buffer[0] == ACE_TEXT (';') || buffer[0] == ACE_TEXT ('#'))
        continue;

      if (buffer[0] == ACE_TEXT ('['))
        {
          // We have a new section here, strip out the section name
          end = ACE_OS::strrchr (buffer, ACE_TEXT (']'));
          if (!end)
            {
              ACE_OS::fclose (in);
              delete [] buffer;
              return -3;
            }
          *end = 0;

          if (config_.expand_path (config_.root_section (), buffer + 1, section, 1))
            {
              ACE_OS::fclose (in);
              delete [] buffer;
              return -3;
            }
          continue;
        }              // end if firs char is a [

      if (buffer[0] == ACE_TEXT ('"'))
        {
          // we have a value
          end = ACE_OS::strchr (buffer+1, '"');
          if (!end)  // no closing quote, not a value so just skip it
            continue;

          // null terminate the name
          *end = 0;
          ACE_TCHAR* name = buffer + 1;
          end+=2;
          // determine the type
          if (*end == '\"')
            {
              // string type
              // truncate trailing "
              ++end;
              ACE_TCHAR* trailing = ACE_OS::strrchr (end, '"');
              if (trailing)
                *trailing = 0;
              if (config_.set_string_value (section, name, end))
                {
                  ACE_OS::fclose (in);
                  delete [] buffer;
                  return -4;
                }
            }
          else if (ACE_OS::strncmp (end, ACE_TEXT ("dword:"), 6) == 0)
            {
              // number type
              ACE_TCHAR* endptr = 0;
              unsigned long value = ACE_OS::strtoul (end + 6, &endptr, 16);
              if (config_.set_integer_value (section, name, value))
                {
                  ACE_OS::fclose (in);
                  delete [] buffer;
                  return -4;
                }
            }
          else if (ACE_OS::strncmp (end, ACE_TEXT ("hex:"), 4) == 0)
            {
              // binary type
              size_t string_length = ACE_OS::strlen (end + 4);
              // divide by 3 to get the actual buffer length
              size_t length = string_length / 3;
              size_t remaining = length;
              u_char* data = 0;
              ACE_NEW_RETURN (data,
                              u_char[length],
                              -1);
              u_char* out = data;
              ACE_TCHAR* inb = end + 4;
              ACE_TCHAR* endptr = 0;
              while (remaining)
                {
                  u_char charin = (u_char) ACE_OS::strtoul (inb, &endptr, 16);
                  *out = charin;
                  ++out;
                  --remaining;
                  inb += 3;
                }
              if (config_.set_binary_value (section, name, data, length))
                {
                  ACE_OS::fclose (in);
                  delete [] data;
                  delete [] buffer;
                  return -4;
                }
              else
                delete [] data;
            }
          else
            {
              // invalid type, ignore
              continue;
            }
        }// end if first char is a "
      else
        {
          // if the first character is not a ", [, ;, or # we may be
          // processing a file in the old format.
          // Try and process the line as such and if it fails,
          // return an error
          int rc = process_previous_line_format (buffer, section);
          if (rc != 0)
            {
              ACE_OS::fclose (in);
              delete [] buffer;
              return rc;
            }
        }             // end if maybe old format
    }                 // end while fgets

  if (ferror (in))
    {
      ACE_OS::fclose (in);
      delete [] buffer;
      return -1;
    }

  ACE_OS::fclose (in);
  delete [] buffer;
  return 0;
}

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.

{
  // Chop any cr/lf at the end of the line.
  ACE_TCHAR *endp = ACE_OS::strpbrk (buffer, ACE_TEXT ("\r\n"));
  if (endp != 0)
    *endp = '\0';

  // assume this is a value, read in the value name
  ACE_TCHAR* end = ACE_OS::strchr (buffer, '=');
  if (end)  // no =, not a value so just skip it
    {
      // null terminate the name
      *end = 0;
      ++end;
      // determine the type
      if (*end == '\"')
        {
          // string type
          if(config_.set_string_value (section, buffer, end + 1))
            return -4;
        }
      else if (*end == '#')
        {
          // number type
          u_int value = ACE_OS::atoi (end + 1);
          if (config_.set_integer_value (section, buffer, value))
            return -4;
        }
    }
  return 0;
}                // end read_previous_line_format


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines