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 function and convert the string to the desired data type.

Todo:
-Strings with embedded newlines cause the import to fail -Strings with embedded quotes " cause the import to fail -Importing/exporting for values in the root section does not work -Add dynamic buffer when importing. currently it will not allow importing of values greater than a fixed ammount (4096 bytes)

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 375 of file Configuration_Import_Export.cpp.

00376     : ACE_Config_ImpExp_Base (config)
00377 {
00378 }

ACE_Ini_ImpExp::~ACE_Ini_ImpExp void   )  [virtual]
 

Destructor

Definition at line 380 of file Configuration_Import_Export.cpp.

00381 {
00382 }

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 486 of file Configuration_Import_Export.cpp.

References ACE_LIB_TEXT, ACE_TCHAR, export_section(), ACE_OS::fclose(), ACE_OS::fopen(), and ACE_Configuration::root_section().

00487 {
00488   if (0 == filename)
00489     {
00490       errno = EINVAL;
00491       return -1;
00492     }
00493   int result = -1;
00494 
00495   FILE* out = ACE_OS::fopen (filename, ACE_LIB_TEXT ("w"));
00496   if (out)
00497     {
00498       result = this->export_section (config_.root_section (),
00499                                      ACE_LIB_TEXT (""),
00500                                      out);
00501       ACE_OS::fclose (out);
00502     }
00503   return result;
00504 }

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 511 of file Configuration_Import_Export.cpp.

References ACE_LIB_TEXT, ACE_TCHAR, ACE_TString, 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_String_Base< CHAR >::length(), ACE_Configuration::open_section(), and ACE_OS::sprintf().

Referenced by export_config().

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

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 386 of file Configuration_Import_Export.cpp.

References ACE_LIB_TEXT, ACE_TCHAR, 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().

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

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 628 of file Configuration_Import_Export.cpp.

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

Referenced by import_config().

00629 {
00630   ACE_TCHAR *cp = 0;
00631 
00632   if (src == 0)
00633     return 0;
00634 
00635   // Start at the end and work backwards over all whitespace.
00636   for (cp = src + ACE_OS::strlen (src) - 1;
00637        cp != src;
00638        --cp)
00639     if (!ACE_OS::ace_isspace (*cp))
00640       break;
00641   cp[1] = '\0';          // Chop trailing whitespace
00642 
00643   // Now start at the beginning and move over all whitespace.
00644   for (cp = src; ACE_OS::ace_isspace (*cp); ++cp)
00645     continue;
00646 
00647   return cp;
00648 }


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:23:31 2006 for ACE by doxygen 1.3.6