ACE_Ini_ImpExp Class Reference

Imports the configuration database from filename as strings. Allows non-typed values. (no #, dword: hex:, etc. prefixes) and skips whitespace (tabs and spaces) as in standard .ini and .conf files. Values (to right of equal sign) can be double quote delimited to embed tabs and spaces in the string. Caller must convert string to type. More...

#include <Configuration_Import_Export.h>

Inheritance diagram for ACE_Ini_ImpExp:

Inheritance graph
[legend]
Collaboration diagram for ACE_Ini_ImpExp:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Ini_ImpExp (ACE_Configuration &)
virtual ~ACE_Ini_ImpExp (void)
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)
ACE_TCHARsquish (ACE_TCHAR *src)
 ACE_Ini_ImpExp (const ACE_Ini_ImpExp &)
ACE_Ini_ImpExpoperator= (const ACE_Ini_ImpExp &)

Detailed Description

Imports the configuration database from filename as strings. Allows non-typed values. (no #, dword: hex:, etc. prefixes) and skips whitespace (tabs and spaces) as in standard .ini and .conf files. Values (to right of equal sign) can be double quote delimited to embed tabs and spaces in the string. Caller must convert string to type.

This method allows for lines in the .ini or .conf file like this:

TimeToLive = 100 Delay = FALSE Flags = FF34 Heading = "ACE - Adaptive Communication Environment"

(note leading whitespace (tabs) in examples below)

SeekIndex = 14 TraceLevel = 6 # Can comment lines like this Justification = left_justified

The caller can then retrieve the string with the regular <get_string_value> function and convert the string to the desired data type.

Todo:

Definition at line 162 of file Configuration_Import_Export.h.


Constructor & Destructor Documentation

ACE_Ini_ImpExp::ACE_Ini_ImpExp ( ACE_Configuration  ) 

Construction

Definition at line 392 of file Configuration_Import_Export.cpp.

00393     : ACE_Config_ImpExp_Base (config)
00394 {
00395 }

ACE_Ini_ImpExp::~ACE_Ini_ImpExp ( void   )  [virtual]

Destructor

Definition at line 397 of file Configuration_Import_Export.cpp.

00398 {
00399 }

ACE_Ini_ImpExp::ACE_Ini_ImpExp ( const ACE_Ini_ImpExp  )  [private]


Member Function Documentation

int ACE_Ini_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 503 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().

00504 {
00505   if (0 == filename)
00506     {
00507       errno = EINVAL;
00508       return -1;
00509     }
00510   int result = -1;
00511 
00512   FILE* out = ACE_OS::fopen (filename, ACE_TEXT ("w"));
00513   if (out)
00514     {
00515       result = this->export_section (config_.root_section (),
00516                                      ACE_TEXT (""),
00517                                      out);
00518       // The data may have been buffered and will be flush on close,
00519       // so we need to check that the close succeeds.
00520       if (ACE_OS::fclose (out) < 0)
00521         result = -7;
00522     }
00523   return result;
00524 }

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

Method provided by derived classes in order to write one section to the file specified. Called by export_config() when exporting the entire configuration object.

Definition at line 531 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().

00534 {
00535   // don't export the root
00536   if (path.length ())
00537     {
00538       // Write out the section header
00539       ACE_TString header = ACE_TEXT ("[");
00540       header += path;
00541       header += ACE_TEXT ("]\n");
00542       if (ACE_OS::fputs (header.fast_rep (), out) < 0)
00543         return -1;
00544       // Write out each value
00545       int index = 0;
00546       ACE_TString name;
00547       ACE_Configuration::VALUETYPE type;
00548       ACE_TString line;
00549       ACE_TCHAR int_value[32];
00550       ACE_TCHAR bin_value[3];
00551       void* binary_data;
00552       size_t binary_length;
00553       ACE_TString string_value;
00554       while (!config_.enumerate_values (section, index, name, type))
00555         {
00556           line = name + ACE_TEXT ("=");
00557           switch (type)
00558             {
00559             case ACE_Configuration::INTEGER:
00560               {
00561                 u_int value;
00562                 if (config_.get_integer_value (section, name.fast_rep (), value))
00563                   return -2;
00564                 ACE_OS::sprintf (int_value, ACE_TEXT ("%08x"), value);
00565                 line += int_value;
00566                 break;
00567               }
00568             case ACE_Configuration::STRING:
00569               {
00570                 if (config_.get_string_value (section,
00571                                               name.fast_rep (),
00572                                               string_value))
00573                   return -2;
00574                 line += string_value;
00575                 break;
00576               }
00577 #ifdef _WIN32
00578             case ACE_Configuration::INVALID:
00579               break;  // JDO added break.  Otherwise INVALID is processed
00580               // like BINARY. If that's correct, please remove the
00581               // break and these comments
00582 #endif
00583             case ACE_Configuration::BINARY:
00584               {
00585                 // not supported yet - maybe use BASE64 codeing?
00586                 if (config_.get_binary_value (section,
00587                                               name.fast_rep (),
00588                                               binary_data,
00589                                               binary_length))
00590                   return -2;
00591                 line += ACE_TEXT ("\"");
00592                 unsigned char* ptr = (unsigned char*)binary_data;
00593                 while (binary_length)
00594                   {
00595                     if (ptr != binary_data)
00596                       {
00597                         line += ACE_TEXT (",");
00598                       }
00599                     ACE_OS::sprintf (bin_value, ACE_TEXT ("%02x"), *ptr);
00600                     line += bin_value;
00601                     --binary_length;
00602                     ++ptr;
00603                   }
00604                 line += ACE_TEXT ("\"");
00605                 delete [] (char *) binary_data;
00606                 break;
00607               }
00608             default:
00609               return -3;
00610 
00611             }// end switch on type
00612 
00613           line += ACE_TEXT ("\n");
00614           if (ACE_OS::fputs (line.fast_rep (), out) < 0)
00615             return -4;
00616           ++index;
00617         }// end while enumerating values
00618     }
00619   // Export all sub sections
00620   int index = 0;
00621   ACE_TString name;
00622   ACE_Configuration_Section_Key sub_key;
00623   ACE_TString sub_section;
00624   while (!config_.enumerate_sections (section, index, name))
00625     {
00626       ACE_TString sub_section (path);
00627       if (path.length ())
00628         sub_section += ACE_TEXT ("\\");
00629       sub_section += name;
00630       if (config_.open_section (section, name.fast_rep (), 0, sub_key))
00631         return -5;
00632       if (export_section (sub_key, sub_section.fast_rep (), out))
00633         return -6;
00634       ++index;
00635     }
00636   return 0;
00637 
00638 }

int ACE_Ini_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 403 of file Configuration_Import_Export.cpp.

References ACE_TEXT, ACE_Config_ImpExp_Base::config_, ACE_Configuration::expand_path(), ACE_OS::fclose(), ACE_OS::fgets(), ACE_OS::fopen(), ACE_Configuration::root_section(), ACE_Configuration::set_string_value(), squish(), ACE_OS::strchr(), ACE_OS::strlen(), and ACE_OS::strrchr().

00404 {
00405   if (0 == filename)
00406     {
00407       errno = EINVAL;
00408       return -1;
00409     }
00410   FILE* in = ACE_OS::fopen (filename, ACE_TEXT ("r"));
00411   if (!in)
00412     return -1;
00413 
00414   // @@ Make this a dynamic size!
00415   ACE_TCHAR buffer[4096];
00416   ACE_Configuration_Section_Key section;
00417   while (ACE_OS::fgets (buffer, sizeof buffer, in))
00418     {
00419       ACE_TCHAR *line = this->squish (buffer);
00420       // Check for a comment and blank line
00421       if (line[0] == ACE_TEXT (';')  ||
00422           line[0] == ACE_TEXT ('#')  ||
00423           line[0] == '\0')
00424         continue;
00425 
00426       if (line[0] == ACE_TEXT ('['))
00427         {
00428           // We have a new section here, strip out the section name
00429           ACE_TCHAR* end = ACE_OS::strrchr (line, ACE_TEXT (']'));
00430           if (!end)
00431             {
00432               ACE_OS::fclose (in);
00433               return -3;
00434             }
00435           *end = 0;
00436 
00437           if (config_.expand_path (config_.root_section (),
00438                                    line + 1,
00439                                    section,
00440                                    1))
00441             {
00442               ACE_OS::fclose (in);
00443               return -3;
00444             }
00445 
00446           continue;
00447         }
00448 
00449       // We have a line; name ends at equal sign.
00450       ACE_TCHAR *end = ACE_OS::strchr (line, ACE_TEXT ('='));
00451       if (end == 0)                            // No '='
00452         {
00453           ACE_OS::fclose (in);
00454           return -3;
00455         }
00456       *end++ = '\0';
00457       ACE_TCHAR *name = this->squish (line);
00458 #if 0
00459       if (ACE_OS::strlen (name) == 0)          // No name; just an '='
00460         {
00461           ACE_OS::fclose (in);
00462           return -3;
00463         }
00464 #endif
00465       // Now find the start of the value
00466       ACE_TCHAR *value = this->squish (end);
00467       size_t value_len = ACE_OS::strlen (value);
00468       if (value_len > 0)
00469         {
00470           // ACE 5.2 (and maybe earlier) exported strings may be enclosed
00471           // in quotes. If string is quote-delimited, strip the quotes.
00472           // Newer exported files don't have quote delimiters.
00473           if (value[0] == ACE_TEXT ('"') &&
00474               value[value_len - 1] == ACE_TEXT ('"'))
00475             {
00476               // Strip quotes off both ends.
00477               value[value_len - 1] = '\0';
00478               ++value;
00479             }
00480         }
00481 
00482       if (config_.set_string_value (section, name, value))
00483         {
00484           ACE_OS::fclose (in);
00485           return -4;
00486         }
00487     }             // end while fgets
00488 
00489   if (ferror (in))
00490     {
00491       ACE_OS::fclose (in);
00492       return -1;
00493     }
00494 
00495   ACE_OS::fclose (in);
00496   return 0;
00497 }

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

ACE_TCHAR * ACE_Ini_ImpExp::squish ( ACE_TCHAR src  )  [private]

Method to squish leading and trailing whitespaces in a string. Whitespace is defined as: spaces (' '), tabs ('\t') or cr/lf. Returns a pointer to the first non-whitespace character in the buffer provided, or a pointer to the terminating null if the string is all whitespace. The terminating null is moved forward to the first character past the last non-whitespace.

Definition at line 648 of file Configuration_Import_Export.cpp.

References ACE_OS::ace_isspace(), and ACE_OS::strlen().

Referenced by import_config().

00649 {
00650   ACE_TCHAR *cp = 0;
00651 
00652   if (src == 0)
00653     return 0;
00654 
00655   // Start at the end and work backwards over all whitespace.
00656   for (cp = src + ACE_OS::strlen (src) - 1;
00657        cp != src;
00658        --cp)
00659     if (!ACE_OS::ace_isspace (*cp))
00660       break;
00661   cp[1] = '\0';          // Chop trailing whitespace
00662 
00663   // Now start at the beginning and move over all whitespace.
00664   for (cp = src; ACE_OS::ace_isspace (*cp); ++cp)
00665     continue;
00666 
00667   return cp;
00668 }


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