Config.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Header$
00004 
00005 */
00006 
00007 #include <stdio.h>
00008 #include <unistd.h>
00009 #include <fcntl.h>
00010 
00011 #include "Error.h"
00012 #include "Parser.h"
00013 
00014 #include "Config.h"
00015 
00016 // Public methods.
00017 
00018 int Config::save()
00019 {
00020   String xml;
00021   FILE *f;
00022   int res;
00023 
00024   if (FAILED(res = _xml->root()->toString(xml)))
00025     return ERROR_BACKTRACE(res);
00026 
00027   if ((f = fopen(CONFIG_PATH, "w")) == NULL) {
00028     WARNING(MSG_FILE_CANNOT_OPEN, CONFIG_PATH, strerror(errno));
00029     return OK;
00030   }
00031 
00032   fprintf(f, CSTRING(xml));
00033 
00034   if (fclose(f) != 0)
00035     return ERROR(MSG_FILE_CANNOT_CLOSE, CONFIG_PATH, strerror(errno));
00036 
00037   return OK;
00038 }
00039 
00040 int Config::remove(const String &name)
00041 {
00042   XmlElement *element;
00043   int res;
00044 
00045   if (FAILED(res = _xml->getElementById(name, &element)))
00046     return ERROR_BACKTRACE(res);
00047 
00048   if (FAILED(res = _xml->removeElement(element)))
00049     return ERROR_BACKTRACE(res);
00050 
00051   return OK;
00052 }
00053 
00054 // Private methods.
00055 
00056 Config::Config() : Object(), Singleton<Config>(),
00057   _xml(NULL)
00058 {
00059   CHECK_PARENT;
00060 
00061   int res;
00062 
00063   if (!(_xml = new XmlDocument())) {
00064     failure(ERROR(MSG_OBJECT_CANNOT_CREATE, "XmlDocument"));
00065     return;
00066   }
00067   if (FAILED(res = _xml->failureCode())) {
00068     failure(ERROR_BACKTRACE(res));
00069     return;
00070   }
00071 
00072   int f;
00073   int bytesRead;
00074   char buff[LIMIT_CSTRING_SIZE];
00075 
00076   if (!(f = open(CONFIG_PATH, O_RDONLY))) {
00077     WARNING(MSG_FILE_CANNOT_OPEN, CONFIG_PATH, strerror(errno));
00078     return;
00079   }
00080 
00081   while (true) {
00082     if ((bytesRead = read(f, (void *)buff, LIMIT_CSTRING_SIZE - 1)) < 0) {
00083       failure(ERROR(MSG_FILE_CANNOT_READ, CONFIG_PATH, strerror(errno)));
00084       break;
00085     } else
00086         if (FAILED(res = _xml->parse(buff, bytesRead, bytesRead == 0))) {
00087           ERROR_BACKTRACE(res);
00088           if (FAILED(res = _xml->reset())) {
00089             ERROR_BACKTRACE(res);
00090             return;
00091           }
00092           break;
00093         }
00094     if (bytesRead == 0)
00095       break;
00096   }
00097 
00098   if (close(f) < 0) {
00099     failure(ERROR(MSG_FILE_CANNOT_CLOSE, CONFIG_PATH, strerror(errno)));
00100     return;
00101   }
00102 
00103 #ifdef DEBUG_MODE
00104   print();
00105 #endif
00106 }
00107 
00108 Config::~Config()
00109 {
00110   if (_xml)
00111     delete _xml;
00112 }
00113 
00114 // Private methods.
00115 
00116 #ifdef DEBUG_MODE
00117 
00118 int Config::print() const
00119 {
00120   if (_xml && _xml->root()) {
00121     String xml;
00122     int res;
00123 
00124     if (FAILED(res = _xml->root()->toString(xml)))
00125       return ERROR_BACKTRACE(res);
00126 
00127     DEBUG("PYLON configuration");
00128     DEBUG("===================");
00129     DEBUG("%s%s", String::eol(), CSTRING(xml));
00130     DEBUG("===================");
00131   }
00132   return OK;
00133 }
00134 
00135 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1