header.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright, 2000 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #include "stdgeorges.h"
00025 
00026 #include "header.h"
00027 #include "nel/misc/thread.h"
00028 #include "nel/misc/i_xml.h"
00029 #include "nel/misc/common.h"
00030 
00031 using namespace NLMISC;
00032 
00033 namespace NLGEORGES
00034 {
00035 
00036 // ***************************************************************************
00037 
00038 void warning (bool exception, const char *format, ... );
00039 
00040 // ***************************************************************************
00041 
00042 CFileHeader::CFileHeader ()
00043 {
00044     MajorVersion = 0;
00045     MinorVersion = 0;
00046     State = Modified;
00047     Revision = "$R";
00048     Revision += "evision$";
00049 }
00050 
00051 // ***************************************************************************
00052 
00053 void CFileHeader::write (xmlNodePtr node, bool georges4CVS) const
00054 {
00055     // Version for CVS ?
00056     if (georges4CVS)
00057     {
00058         // Georges version system
00059         xmlSetProp (node, (const xmlChar*)"Revision", (const xmlChar*)Revision.c_str ());
00060     }
00061     else
00062     {
00063         // Georges version system
00064         char tmp[512];
00065         smprintf (tmp, 512, "%d.%d", MajorVersion, MinorVersion);
00066         xmlSetProp (node, (const xmlChar*)"Version", (const xmlChar*)tmp);
00067     }
00068 
00069     // State
00070     if (State == Modified)
00071         xmlSetProp (node, (const xmlChar*)"State", (const xmlChar*)"modified");
00072     else
00073         xmlSetProp (node, (const xmlChar*)"State", (const xmlChar*)"checked");
00074 
00075     // Comments of the form
00076     if (!Comments.empty ())
00077     {
00078         // Create a new node
00079         xmlNodePtr child = xmlNewChild ( node, NULL, (const xmlChar*)"COMMENTS", NULL);
00080         xmlNodePtr textNode = xmlNewText ((const xmlChar *)Comments.c_str());
00081         xmlAddChild (child, textNode);
00082     }
00083 
00084     // Logs
00085     if (!Log.empty ())
00086     {
00087         // Create a new node
00088         xmlNodePtr child = xmlNewChild ( node, NULL, (const xmlChar*)"LOG", NULL);
00089         xmlNodePtr textNode = xmlNewText ((const xmlChar *)Log.c_str());
00090         xmlAddChild (child, textNode);
00091     }
00092 }
00093 
00094 // ***************************************************************************
00095 
00096 void CFileHeader::addLog (const char *log)
00097 {
00098     time_t t;
00099     time (&t);
00100     if (!Log.empty())
00101         Log += "\n";
00102     Log += ctime(&t);
00103     Log.resize (Log.size()-1);
00104     Log += " (";
00105     Log += IThread::getCurrentThread ()->getUserName ();
00106     Log += ") ";
00107     Log += log;
00108 }
00109 
00110 // ***************************************************************************
00111 
00112 void CFileHeader::setComments (const char *comments)
00113 {
00114     Comments = comments;
00115 }
00116 
00117 // ***************************************************************************
00118 
00119 void CFileHeader::read (xmlNodePtr root)
00120 {
00121     // Get the version
00122     const char *value = (const char*)xmlGetProp (root, (xmlChar*)"Version");
00123     if (value)
00124     {
00125         // Read the version
00126         if (sscanf (value, "%d.%d", &MajorVersion, &MinorVersion) != 2)
00127         {
00128             // Delete the value
00129             xmlFree ((void*)value);
00130 
00131             // Throw exception
00132             warning (true, "read", "XML Syntax error in TYPE block line %d, the Version argument is invalid.",
00133                 (ptrdiff_t)root->content);
00134         }
00135 
00136         // Delete the value
00137         xmlFree ((void*)value);
00138     }
00139     else
00140     {
00141         // Set default
00142         MajorVersion = 0;
00143         MinorVersion = 0;
00144     }
00145 
00146     // Get the revision
00147     value = (const char*)xmlGetProp (root, (xmlChar*)"Revision");
00148     if (value)
00149     {
00150         // Set the value
00151         Revision = value;
00152 
00153         // Delete the value
00154         xmlFree ((void*)value);
00155     }
00156     else
00157     {
00158         // Set default
00159         Revision = "$R";
00160         Revision += "evision$";
00161     }
00162 
00163     // Get the version
00164     value = (const char*)xmlGetProp (root, (xmlChar*)"State");
00165     if (value)
00166     {
00167         // Read the version
00168         if (strcmp (value, "modified") == 0)
00169         {
00170             State = Modified;
00171         }
00172         else if (strcmp (value, "checked") == 0)
00173         {
00174             State = Checked;
00175         }
00176         else
00177         {
00178             // Delete the value
00179             xmlFree ((void*)value);
00180 
00181             // Throw exception
00182             warning (true, "read", "XML Syntax error in TYPE block line %d, the State argument is invalid.",
00183                 (ptrdiff_t)root->content);
00184         }
00185 
00186         // Delete the value
00187         xmlFree ((void*)value);
00188     }
00189     else
00190     {
00191         // Set default
00192         State = Modified;
00193     }
00194 
00195     // Look for the comment node
00196     Comments = "";
00197     xmlNodePtr node = CIXml::getFirstChildNode (root, "COMMENTS");
00198     if (node)
00199     {
00200         // Get a text node
00201         node = CIXml::getFirstChildNode (node, XML_TEXT_NODE);
00202 
00203         if (node)
00204         {
00205             // Get content
00206             const char *comments = (const char*)xmlNodeGetContent (node);
00207             if (comments)
00208             {
00209                 Comments = comments;
00210 
00211                 // Delete the value
00212                 xmlFree ((void*)comments);
00213             }
00214         }
00215     }
00216 
00217     // Look for the log node
00218     Log = "";
00219     node = CIXml::getFirstChildNode (root, "LOG");
00220     if (node)
00221     {
00222         // Get a text node
00223         node = CIXml::getFirstChildNode (node, XML_TEXT_NODE);
00224 
00225         if (node)
00226         {
00227             // Get content
00228             const char *log = (const char*)xmlNodeGetContent (node);
00229             if (log)
00230             {
00231                 Log = log;
00232 
00233                 // Delete the value
00234                 xmlFree ((void*)log);
00235             }
00236         }
00237     }
00238 }
00239 
00240 // ***************************************************************************
00241 
00242 const char *CFileHeader::getStateString (TState state)
00243 {
00244     if (state == Modified)
00245         return "Modified";
00246     else
00247         return "Checked";
00248 }
00249 
00250 // ***************************************************************************
00251 
00252 void CFileHeader::warning (bool exception, const char *function, const char *format, ... ) const
00253 {
00254     // Make a buffer string
00255     va_list args;
00256     va_start( args, format );
00257     char buffer[1024];
00258     vsnprintf( buffer, 1024, format, args );
00259     va_end( args );
00260 
00261     // Set the warning
00262     NLGEORGES::warning (exception, "(CFileHeader::%s) : %s", function, buffer);
00263 }
00264 
00265 // ***************************************************************************
00266 
00267 } // NLGEORGES

Generated on Thu Jan 7 08:26:32 2010 for NeL by  doxygen 1.6.1