2010-04-21 Ramon Novoa <rnovoa@artica.es>

* main.cc: Updated headers after tinyxml removal.

        * modules/pandora_module_list.cc,
          modules/pandora_module_factory.cc: Added support for plugin modules.

        * modules/pandora_module_exec.cc, modules/pandora_module.h,
          modules/pandora_module.cc: Added the module_timeout configuration
          token and replaced tinyxml related code.

        * bin/pandora_agent.conf: Added a sample plugin module. Added the
          group configuration token (was missing).

        * modules/pandora_module_plugin.cc,
          modules/pandora_module_plugin.h: Added to repository. Plugin module.

        * bin/util/df.vbs, bin/util/ps.vbs: Added to repository. Sample
          VBScript plugins.

        * pandora_windows_service.cc, pandora_windows_service.h,
          modules/pandora_module_inventory.cc, ssh/pandora_ssh_test.cc,
          ftp/pandora_ftp_test.cc, modules/pandora_module_inventory.h: Replaced
          tinyxml related code.

        * tinyxml, tinyxml/tinyxmlerror.cpp, tinyxml/tinystr.h,
          tinyxml/tinyxmlparser.cpp, tinyxml/tinyxml.cpp,
          tinyxml/tinyxml.h, tinyxml/tinystr.cpp: Removed from repository. No
          longer used.

        * installer/pandora.mpi, pandora.cc: Updated build to RC1.

        * Makefile.am: Updated sources. Removed tinyxml and added the plugin
          module.




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2584 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Ramon Novoa 2010-04-21 20:00:31 +00:00
parent 2696b815e5
commit b363210cba
27 changed files with 460 additions and 5662 deletions

View File

@ -1,3 +1,38 @@
2010-04-21 Ramon Novoa <rnovoa@artica.es>
* main.cc: Updated headers after tinyxml removal.
* modules/pandora_module_list.cc,
modules/pandora_module_factory.cc: Added support for plugin modules.
* modules/pandora_module_exec.cc, modules/pandora_module.h,
modules/pandora_module.cc: Added the module_timeout configuration
token and replaced tinyxml related code.
* bin/pandora_agent.conf: Added a sample plugin module. Added the
group configuration token (was missing).
* modules/pandora_module_plugin.cc,
modules/pandora_module_plugin.h: Added to repository. Plugin module.
* bin/util/df.vbs, bin/util/ps.vbs: Added to repository. Sample
VBScript plugins.
* pandora_windows_service.cc, pandora_windows_service.h,
modules/pandora_module_inventory.cc, ssh/pandora_ssh_test.cc,
ftp/pandora_ftp_test.cc, modules/pandora_module_inventory.h: Replaced
tinyxml related code.
* tinyxml, tinyxml/tinyxmlerror.cpp, tinyxml/tinystr.h,
tinyxml/tinyxmlparser.cpp, tinyxml/tinyxml.cpp,
tinyxml/tinyxml.h, tinyxml/tinystr.cpp: Removed from repository. No
longer used.
* installer/pandora.mpi, pandora.cc: Updated build to RC1.
* Makefile.am: Updated sources. Removed tinyxml and added the plugin
module.
2010-04-16 Ramon Novoa <rnovoa@artica.es>
* windows/pandora_wmi.cc: fixed getOSVersion. Crashed in Windows 2008

View File

@ -1,8 +1,8 @@
bin_PROGRAMS = PandoraAgent
if DEBUG
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_odbc.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc tinyxml/tinyxmlerror.cpp tinyxml/tinyxml.cpp tinyxml/tinyxmlparser.cpp tinyxml/tinystr.cpp misc/md5.c windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc debug_new.cpp
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_plugin.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_odbc.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc misc/md5.c windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc debug_new.cpp
PandoraAgent_CXXFLAGS=-g -O0 -DCURL_STATICLIB -D__DEBUG__
else
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_odbc.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc tinyxml/tinyxmlerror.cpp tinyxml/tinyxml.cpp tinyxml/tinyxmlparser.cpp tinyxml/tinystr.cpp misc/md5.c windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_plugin.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_odbc.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc misc/md5.c windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc
PandoraAgent_CXXFLAGS=-O2 -DCURL_STATICLIB
endif

View File

@ -26,6 +26,9 @@ temporal "$AgentTemp$"
#agent_name My_Custom_Agent_name
# Group assigned for this agent (descriptive, p.e: Servers)
#group Servers
# This limits operation if temporal dir has not enough free disk.
#temporal_min_size 1024
@ -184,3 +187,6 @@ module_end
#process_firefox_stop killall firefox
#service_messenger 1
# Example plugin to retrieve drive usage
# module_plugin cscript.exe //B "C:\Program Files\pandora_agent\util\df.vbs"

View File

@ -0,0 +1,32 @@
' df.vbs
' Returns free space for avaible drives.
' --------------------------------------
Option Explicit
On Error Resume Next
' Variables
Dim objWMIService, objItem, colItems, argc, argv, i
' Parse command line parameters
argc = Wscript.Arguments.Count
Set argv = CreateObject("Scripting.Dictionary")
For i = 0 To argc - 1
argv.Add Wscript.Arguments(i), i
Next
' Get drive information
Set objWMIService = GetObject ("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objItem in colItems
If argc = 0 Or argv.Exists(objItem.Name) Then
If objItem.FreeSpace <> "" Then
Wscript.StdOut.WriteLine "<module>"
Wscript.StdOut.WriteLine " <name><![CDATA[" & objItem.Name & "]]></name>"
Wscript.StdOut.WriteLine " <description><![CDATA[Drive " & objItem.Name & " free space in MB]]></description>"
Wscript.StdOut.WriteLine " <data><![CDATA[" & Int(objItem.FreeSpace /1048576) & "]]></data>"
Wscript.StdOut.WriteLine "</module>"
End If
End If
Next

View File

@ -0,0 +1,33 @@
' ps.vbs
' Returns the status of the given processes.
' -----------------------------------------
Option Explicit
'On Error Resume Next
' Variables
Dim objWMIService, objItem, colItems, argc, ps, i
' Get and hash process information
Set objWMIService = GetObject ("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process")
Set ps = CreateObject("Scripting.Dictionary")
For Each objItem in colItems
if Not ps.Exists(objItem.Name) Then
ps.Add objItem.Name, 1
End If
Next
' Parse command line parameters and check each process
argc = Wscript.Arguments.Count
For i = 0 To argc - 1
Wscript.StdOut.WriteLine "<module>"
Wscript.StdOut.WriteLine " <name><![CDATA[" & Wscript.Arguments(i) & "]]></name>"
Wscript.StdOut.WriteLine " <description><![CDATA[Process " & Wscript.Arguments(i) & " status]]></description>"
If argc = 0 Or ps.Exists(Wscript.Arguments(i)) Then
Wscript.StdOut.WriteLine " <data><![CDATA[" & 1 & "]]></data>"
Else
Wscript.StdOut.WriteLine " <data><![CDATA[" & 0 & "]]></data>"
End If
Wscript.StdOut.WriteLine "</module>"
Next

View File

@ -19,7 +19,6 @@
*/
#include "pandora_ftp_test.h"
#include "../tinyxml/tinyxml.h"
#include "../misc/pandora_file.h"
#include <iostream>
#include <dir.h>
@ -73,13 +72,13 @@ Pandora_FTP_Test::~Pandora_FTP_Test () {
*/
void
Pandora_FTP_Test::test () {
string data_xml;
string password, tmp_filename;
string remote_host, remote_filepath, tmp_filepath;
TiXmlDocument *doc;
TiXmlDeclaration *decl;
bool saved;
char *err;
DIR *dir;
FILE *conf_fh = NULL;
remote_host = this->conf->getValue ("server_ip");
cout << "Connecting with " << remote_host << "..." << endl;
@ -115,24 +114,17 @@ Pandora_FTP_Test::test () {
}
tmp_filepath += tmp_filename;
decl = new TiXmlDeclaration( "1.0", "ISO-8859-1", "" );
doc = new TiXmlDocument (tmp_filepath);
doc->InsertEndChild (*decl);
saved = doc->SaveFile();
if (!saved) {
Pandora_Exception e;
data_xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
conf_fh = fopen (tmp_filepath.c_str (), "w");
if (conf_fh == NULL) {
Pandora::Pandora_Exception e;
cout << "Error when saving the XML in " << tmp_filepath << endl;
if (doc->Error ()) {
cout << "Reason: " << doc->ErrorDesc () << endl;
}
cout << "Check the configuration file" << endl;
delete doc;
delete ftp_client;
throw e;
}
delete doc;
fprintf (conf_fh, "%s", data_xml.c_str ());
fclose (conf_fh);
cout << "Created a blank XML file in " << tmp_filepath<< endl;
remote_filepath = conf->getValue ("server_path");

View File

@ -3,7 +3,7 @@ AllowLanguageSelection
{Yes}
AppName
{Pandora FMS Windows Agent v3.0}
{Pandora FMS Windows Agent v3.1RC1}
ApplicationID
{17E3D2CF-CA02-406B-8A80-9D31C17BD08F}
@ -36,7 +36,7 @@ CompressionMethod
{zlib}
Copyright
{2004-2009}
{2004-2010}
DefaultDirectoryLocation
{}
@ -75,7 +75,7 @@ InstallPassword
{}
InstallVersion
{3.0.3.999}
{3.1.0.0}
Language,ca
{No}
@ -186,7 +186,7 @@ UpgradeApplicationID
{}
Version
{091218}
{100421}
ViewReadme
{Yes}
@ -1033,7 +1033,7 @@ array set Properties {
{<%InstallDir%>}
946FC43B-0E32-4C38-86EF-4F9252C67E16,FileSize
{9043742}
{8990281}
946FC43B-0E32-4C38-86EF-4F9252C67E16,Name
{Program Files}

View File

@ -18,6 +18,7 @@
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <iostream>
#include "pandora.h"
#include "pandora_windows_service.h"
#include "ssh/pandora_ssh_test.h"

View File

@ -37,6 +37,7 @@ Pandora_Module::Pandora_Module (string name) {
this->module_name = name;
this->executions = 0;
this->module_interval = 1;
this->module_timeout = 15000;
this->max = 0;
this->min = 0;
this->has_limits = false;
@ -155,6 +156,8 @@ Pandora_Module::parseModuleKindFromString (string kind) {
return MODULE_TCPCHECK;
} else if (kind == module_regexp_str) {
return MODULE_REGEXP;
} else if (kind == module_plugin_str) {
return MODULE_PLUGIN;
} else {
return MODULE_0;
}
@ -353,103 +356,76 @@ Pandora_Module::run () {
* @return A pointer to the TiXmlElement if successful which has to be
* freed by the caller. NULL if the XML could not be created.
*/
TiXmlElement *
string
Pandora_Module::getXml () {
TiXmlElement *root;
TiXmlElement *element;
TiXmlElement *data_list_element;
TiXmlElement *data_element;
TiXmlText *text;
string item_clean, data_clean, desc_clean;
string module_xml, data_clean;
Pandora_Data *data;
pandoraDebug ("%s getXML begin", module_name.c_str ());
/* No data */
if (!this->has_output || this->data_list == NULL) {
return NULL;
return "";
}
root = new TiXmlElement ("module");
element = new TiXmlElement ("name");
text = new TiXmlText (this->module_name);
element->InsertEndChild (*text);
root->InsertEndChild (*element);
delete element;
delete text;
element = new TiXmlElement ("type");
text = new TiXmlText (this->module_type_str);
element->InsertEndChild (*text);
root->InsertEndChild (*element);
delete element;
delete text;
/* Compose the module XML */
module_xml = "<module>\n\t<name><![CDATA[";
module_xml += this->module_name;
module_xml += "]]></name>\n\t<type><![CDATA[";
module_xml += this->module_type_str;
module_xml += "]]></type>\n";
if (this->module_description != "") {
module_xml += "\t<description><![CDATA[";
module_xml += this->module_description;
module_xml += "]]></description>\n";
}
/* Write module data */
if (this->data_list && this->data_list->size () > 1) {
list<Pandora_Data *>::iterator iter;
data_list_element = new TiXmlElement ("datalist");
module_xml += "\t<datalist>\n";
iter = this->data_list->begin ();
for (iter = this->data_list->begin ();
iter != this->data_list->end ();
iter++) {
data = *iter;
data_element = new TiXmlElement ("data");
element = new TiXmlElement ("value");
try {
data_clean = strreplace (this->getDataOutput (data),
"%", "%%" );
} catch (Output_Error e) {
delete element;
continue;
}
text = new TiXmlText (data_clean);
text->SetCDATA (true);
element->InsertEndChild (*text);
data_element->InsertEndChild (*element);
delete text;
delete element;
element = new TiXmlElement ("timestamp");
text = new TiXmlText (data->getTimestamp ());
element->InsertEndChild (*text);
data_element->InsertEndChild (*element);
delete text;
delete element;
data_list_element->InsertEndChild (*data_element);
delete data_element;
module_xml += "\t\t<data>\n\t\t\t<value><![CDATA[";
module_xml += data_clean;
module_xml += "]]></value>\n\t\t\t<timestamp><![CDATA[";
module_xml += data->getTimestamp ();
module_xml += "]]></timestamp>\n\t\t</data>\n";
}
root->InsertEndChild (*data_list_element);
delete data_list_element;
module_xml += "\t</datalist>\n";
} else {
data = data_list->front ();
element = new TiXmlElement ("data");
try {
data_clean = strreplace (this->getDataOutput (data), "%", "%%" );
text = new TiXmlText (data_clean);
element->InsertEndChild (*text);
root->InsertEndChild (*element);
delete text;
} catch (Output_Error e) {
}
delete element;
data_clean = strreplace (this->getDataOutput (data), "%", "%%" );
module_xml += "\t<data><![CDATA[";
module_xml += data_clean;
module_xml += "]]></data>\n";
} catch (Output_Error e) {
}
}
element = new TiXmlElement ("description");
text = new TiXmlText (this->module_description);
element->InsertEndChild (*text);
root->InsertEndChild (*element);
delete text;
delete element;
/* Close the module tag */
module_xml += "</module>\n";
/* Clean up */
this->cleanDataList ();
pandoraDebug ("%s getXML end", module_name.c_str ());
return root;
return module_xml;
}
/**
@ -523,6 +499,22 @@ Pandora_Module::setInterval (int interval) {
this->module_interval = interval;
}
/**
* Set the execution timeout.
*
* @param timeout Execution timeout.
*/
void
Pandora_Module::setTimeout (int timeout) {
if (timeout < 0) {
return;
}
/* WaitForSingleObject expects milliseconds */
this->module_timeout = timeout * 1000;
}
/**
* Get the execution interval.
*
@ -533,6 +525,16 @@ Pandora_Module::getInterval () {
return this->module_interval;
}
/**
* Get the execution timeout.
*
* @return The execution timeout.
*/
int
Pandora_Module::getTimeout () {
return this->module_timeout;
}
/**
* Set the module description.
*

View File

@ -23,7 +23,6 @@
#include "../pandora.h"
#include "pandora_data.h"
#include "../tinyxml/tinyxml.h"
#include <list>
#include <string>
@ -84,7 +83,8 @@ namespace Pandora_Modules {
MODULE_WMIQUERY, /**< The module runs WQL queries */
MODULE_PERFCOUNTER, /**< The module reads performance counters */
MODULE_TCPCHECK, /**< The module checks whether a tcp port is open */
MODULE_REGEXP /**< The module searches a file for matches of a regular expression */
MODULE_REGEXP, /**< The module searches a file for matches of a regular expression */
MODULE_PLUGIN /**< Plugin */
} Module_Kind;
const string module_exec_str = "module_exec";
@ -102,6 +102,7 @@ namespace Pandora_Modules {
const string module_perfcounter_str = "module_perfcounter";
const string module_tcpcheck_str = "module_tcpcheck";
const string module_regexp_str = "module_regexp";
const string module_plugin_str = "module_plugin";
/**
* Pandora module super-class exception.
@ -131,15 +132,18 @@ namespace Pandora_Modules {
class Pandora_Module {
private:
int module_interval;
int module_timeout;
int executions;
int max, min;
bool has_limits;
Module_Type module_type;
string module_kind_str;
Module_Kind module_kind;
list<Pandora_Data *> *data_list;
protected:
list<Pandora_Data *> *data_list;
/**
* Indicates if the module generated output in
* his last execution.
@ -185,8 +189,10 @@ namespace Pandora_Modules {
void setInterval (int interval);
int getInterval ();
void setTimeout (int timeout);
int getTimeout ();
virtual TiXmlElement *getXml ();
virtual string getXml ();
virtual void run ();

View File

@ -122,8 +122,7 @@ Pandora_Module_Exec::run () {
ResumeThread (pi.hThread);
/* Wait until process exits. */
/* TODO: The time should be an attribute*/
WaitForSingleObject (pi.hProcess, 15000);
WaitForSingleObject (pi.hProcess, this->getTimeout ());
GetExitCodeProcess (pi.hProcess, &retval);
if (retval != 0) {
@ -131,9 +130,15 @@ Pandora_Module_Exec::run () {
pandoraLog ("TerminateJobObject failed. (error %d)",
GetLastError ());
}
pandoraLog ("Pandora_Module_Exec: %s did not executed well (retcode: %d)",
this->module_name.c_str (), retval);
/* STILL_ACTIVE */
if (retval == 259) {
pandoraLog ("Pandora_Module_Exec: %s timed out (retcode: 259)",
this->module_name.c_str ());
} else {
pandoraLog ("Pandora_Module_Exec: %s did not executed well (retcode: %d)",
this->module_name.c_str (), retval);
}
this->has_output = false;
}

View File

@ -35,6 +35,7 @@
#include "pandora_module_perfcounter.h"
#include "pandora_module_tcpcheck.h"
#include "pandora_module_regexp.h"
#include "pandora_module_plugin.h"
#include "../pandora_strutils.h"
#include <list>
@ -78,6 +79,7 @@ using namespace Pandora_Strutils;
#define TOKEN_PORT ("module_port ")
#define TOKEN_TIMEOUT ("module_timeout ")
#define TOKEN_REGEXP ("module_regexp ")
#define TOKEN_PLUGIN ("module_plugin ")
string
parseLine (string line, string token) {
@ -120,6 +122,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
string module_retries, module_startdelay, module_retrydelay;
string module_perfcounter, module_tcpcheck;
string module_port, module_timeout, module_regexp;
string module_plugin;
Pandora_Module *module;
bool numeric;
Module_Type type;
@ -155,6 +158,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module_port = "";
module_timeout = "";
module_regexp = "";
module_plugin = "";
stringtok (tokens, definition, "\n");
@ -273,6 +277,9 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
if (module_regexp == "") {
module_regexp = parseLine (line, TOKEN_REGEXP);
}
if (module_plugin == "") {
module_plugin = parseLine (line, TOKEN_PLUGIN);
}
iter++;
}
@ -281,6 +288,10 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
if (module_exec != "") {
module = new Pandora_Module_Exec (module_name,
module_exec);
if (module_timeout != "") {
module->setTimeout (atoi (module_timeout.c_str ()));
}
} else if (module_proc != "") {
module = new Pandora_Module_Proc (module_name,
module_proc);
@ -358,6 +369,8 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module = new Pandora_Module_Tcpcheck (module_name, module_tcpcheck, module_port, module_timeout);
} else if (module_regexp != "") {
module = new Pandora_Module_Regexp (module_name, module_regexp, module_pattern);
} else if (module_plugin != "") {
module = new Pandora_Module_Plugin (module_name, module_plugin);
} else {
return NULL;
}
@ -370,32 +383,38 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module->setAsync (true);
}
type = Pandora_Module::parseModuleTypeFromString (module_type);
switch (type) {
case TYPE_GENERIC_DATA:
case TYPE_GENERIC_DATA_INC:
case TYPE_GENERIC_PROC:
case TYPE_ASYNC_DATA:
case TYPE_ASYNC_PROC:
module->setType (module_type);
numeric = true;
break;
case TYPE_GENERIC_DATA_STRING:
case TYPE_ASYNC_STRING:
module->setType (module_type);
/* Plugins do not have a module type */
if (module_plugin == "") {
type = Pandora_Module::parseModuleTypeFromString (module_type);
switch (type) {
case TYPE_GENERIC_DATA:
case TYPE_GENERIC_DATA_INC:
case TYPE_GENERIC_PROC:
case TYPE_ASYNC_DATA:
case TYPE_ASYNC_PROC:
module->setType (module_type);
numeric = true;
break;
case TYPE_GENERIC_DATA_STRING:
case TYPE_ASYNC_STRING:
module->setType (module_type);
numeric = false;
break;
default:
pandoraDebug ("Bad module type \"%s\" while parsing %s module",
module_type.c_str (), module_name.c_str ());
delete module;
return NULL;
}
} else {
module->setType ("generic_data_string");
numeric = false;
break;
default:
pandoraDebug ("Bad module type \"%s\" while parsing %s module",
module_type.c_str (), module_name.c_str ());
delete module;
return NULL;
}
if (numeric) {
if (module_max != "") {
try {

View File

@ -300,23 +300,18 @@ Pandora_Module_Inventory::setOutput (string output, string data_origin) {
* @overrides TiXmlElement* Pandora_Module::getXml()
*/
TiXmlElement *
string
Pandora_Module_Inventory::getXml() {
TiXmlElement *root;
TiXmlElement *element, *inventory_module;
TiXmlElement *inventory_list_element = NULL;
TiXmlElement *data_element;
TiXmlText *text;
string item_clean, data_clean, desc_clean, submodule;
string module_xml, data_clean, current_module, prev_module;
Pandora_Data *data;
pandoraDebug ("Pandora_Module_Inventory::getXML begin\n");
if (!this->has_output || this->inventory_list == NULL) {
return NULL;
return "";
}
root = new TiXmlElement ("inventory");
inventory_module = new TiXmlElement("inventory_module");
module_xml = "\t<inventory>\n";
if (this->inventory_list && this->inventory_list->size () > 1) {
list<Pandora_Data *>::iterator iter;
@ -325,82 +320,45 @@ Pandora_Module_Inventory::getXml() {
iter != this->inventory_list->end ();
iter++) {
data = *iter;
if (submodule != data->getDataOrigin()){
if (inventory_list_element != NULL)
{
inventory_module->InsertEndChild (*inventory_list_element);
delete inventory_list_element;
inventory_list_element = NULL;
root->InsertEndChild(*inventory_module);
delete inventory_module;
inventory_module = new TiXmlElement("inventory_module");
}
submodule = data->getDataOrigin();
element = new TiXmlElement ("name");
text = new TiXmlText (submodule);
element->InsertEndChild (*text);
inventory_module->InsertEndChild (*element);
delete element;
delete text;
current_module = data->getDataOrigin();
if (current_module != prev_module) {
element = new TiXmlElement ("type");
text = new TiXmlText (this->module_type_str);
element->InsertEndChild (*text);
inventory_module->InsertEndChild (*element);
delete element;
delete text;
/* Close the previous datalist */
if (prev_module != "") {
module_xml += "\t\t\t</datalist>\n";
}
module_xml += "\t\t<inventory_module>\n\t\t\t<name><![CDATA[";
module_xml += data->getDataOrigin();
module_xml += "]]></name>\n";
module_xml += "\t\t\t<type><![CDATA[";
module_xml += this->module_type_str;
module_xml += "]]></type>\n";
inventory_list_element = new TiXmlElement ("datalist");
}
data_element = new TiXmlElement ("data");
try {
data_clean = strreplace (this->getDataOutput (data),
"%", "%%" );
} catch (Output_Error e) {
delete data_element;
continue;
module_xml += "\t\t\t<datalist>\n";
} else {
try {
data_clean = strreplace (this->getDataOutput (data),
"%", "%%" );
} catch (Output_Error e) {
continue;
}
module_xml += "\t\t\t\t<data><![CDATA[";
module_xml += data_clean;
module_xml += "]]></data>\n";
}
text = new TiXmlText (data_clean);
data_element->InsertEndChild (*text);
delete text;
inventory_list_element->InsertEndChild (*data_element);
delete data_element;
prev_module = current_module;
}
if (inventory_list_element != NULL)
{
inventory_module->InsertEndChild (*inventory_list_element);
delete inventory_list_element;
inventory_list_element = NULL;
}
} else {
data = inventory_list->front ();
element = new TiXmlElement ("data");
try {
data_clean = strreplace (this->getDataOutput (data), "%", "%%" );
text = new TiXmlText (data_clean);
element->InsertEndChild (*text);
inventory_module->InsertEndChild (*element);
delete text;
} catch (Output_Error e) {
}
delete element;
}
element = new TiXmlElement ("description");
text = new TiXmlText (this->module_description);
element->InsertEndChild (*text);
inventory_module->InsertEndChild (*element);
delete text;
delete element;
root->InsertEndChild(*inventory_module);
delete inventory_module;
/* Clean up */
this->cleanDataList ();
pandoraDebug ("%s Pandora_Module_Inventory::getXML end", module_name.c_str ());
return root;
return module_xml;
}

View File

@ -47,7 +47,7 @@ namespace Pandora_Modules {
Pandora_Module_Inventory (string name, string options);
void run ();
TiXmlElement *getXml ();
string getXml ();
void setOutput (string output, string data_origin);
void setOutput (string output);
};

View File

@ -35,6 +35,7 @@
#include "pandora_module_perfcounter.h"
#include "pandora_module_tcpcheck.h"
#include "pandora_module_regexp.h"
#include "pandora_module_plugin.h"
#include <fstream>
using namespace std;
@ -65,6 +66,8 @@ Pandora_Modules::Pandora_Module_List::Pandora_Module_List (string filename) {
/* Ignore blank or commented lines */
if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0') {
/* Module */
pos = buffer.find ("module_begin");
if (pos != string::npos) {
string str_module = buffer + "\n";
@ -80,8 +83,17 @@ Pandora_Modules::Pandora_Module_List::Pandora_Module_List (string filename) {
str_module += buffer + "\n";
}
this->parseModuleDefinition (str_module);
this->parseModuleDefinition (str_module);
continue;
}
/* Plugin */
pos = buffer.find ("module_plugin");
if (pos != string::npos) {
this->parseModuleDefinition (buffer);
continue;
}
}
}
file.close ();
@ -148,7 +160,8 @@ Pandora_Modules::Pandora_Module_List::parseModuleDefinition (string definition)
Pandora_Module_WMIQuery *module_wmiquery;
Pandora_Module_Perfcounter *module_perfcounter;
Pandora_Module_Tcpcheck *module_tcpcheck;
Pandora_Module_Regexp *module_regexp;
Pandora_Module_Regexp *module_regexp;
Pandora_Module_Plugin *module_plugin;
module = Pandora_Module_Factory::getModuleFromDefinition (definition);
@ -224,6 +237,10 @@ Pandora_Modules::Pandora_Module_List::parseModuleDefinition (string definition)
module_regexp = (Pandora_Module_Regexp *) module;
modules->push_back (module_regexp);
break;
case MODULE_PLUGIN:
module_plugin = (Pandora_Module_Plugin *) module;
modules->push_back (module_plugin);
break;
default:
break;
}

View File

@ -0,0 +1,62 @@
/* Pandora exec module. These modules exec a command.
Copyright (C) 2010 Artica ST.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "pandora_module_plugin.h"
#include "../pandora_strutils.h"
#include <windows.h>
using namespace Pandora;
using namespace Pandora_Strutils;
using namespace Pandora_Modules;
/**
* Creates a Pandora_Module_Plugin object.
*
* @param name Module name
* @param plugin Command to be executed.
*/
Pandora_Module_Plugin::Pandora_Module_Plugin (string name, string plugin)
: Pandora_Module_Exec ("plugin", plugin) {
this->setKind (module_plugin_str);
}
/**
* Get the plugin output.
*
* @return A pointer to the TiXmlElement if successful which has to be
* freed by the caller. NULL if the XML could not be created.
*/
string
Pandora_Module_Plugin::getXml () {
string value;
Pandora_Data *data = NULL;
pandoraDebug ("%s getXML begin", module_name.c_str ());
if (this->data_list) {
data = data_list->front ();
if (data != NULL) {
value = data->getValue ();
}
}
this->cleanDataList ();
pandoraDebug ("%s getXML end", module_name.c_str ());
return value;
}

View File

@ -0,0 +1,40 @@
/* Pandora exec module. These modules exec a command
Copyright (C) 2010 Artica ST.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __PANDORA_MODULE_PLUGIN_H__
#define __PANDORA_MODULE_PLUGIN_H__
#include "pandora_module_exec.h"
namespace Pandora_Modules {
/**
* Module to execute a custom command using the Windows command
* line.
*
* Any custom order that want to be executed can be put in
* the <code>util</code> directory into the Pandora agent path.
*/
class Pandora_Module_Plugin : public Pandora_Module_Exec {
public:
Pandora_Module_Plugin (string name, string plugin);
virtual string getXml ();
};
}
#endif

View File

@ -30,7 +30,7 @@ using namespace Pandora;
using namespace Pandora_Strutils;
#define PATH_SIZE _MAX_PATH+1
#define PANDORA_VERSION ("3.0(Build 091218)")
#define PANDORA_VERSION ("3.1RC1(Build 100421)")
string pandora_path;
string pandora_dir;

View File

@ -180,53 +180,48 @@ Pandora_Windows_Service::pandora_init () {
}
}
TiXmlElement *
string
Pandora_Windows_Service::getXmlHeader () {
TiXmlElement *agent;
char timestamp[20];
string value;
string agent_name, os_name, os_version, encoding, value;
time_t ctime;
struct tm *ctime_tm = NULL;
agent = new TiXmlElement ("agent_data");
value = conf->getValue ("agent_name");
if (value == "") {
value = Pandora_Windows_Info::getSystemName ();
// Get agent name
agent_name = conf->getValue ("agent_name");
if (agent_name == "") {
agent_name = Pandora_Windows_Info::getSystemName ();
}
agent->SetAttribute ("agent_name", value);
value = conf->getValue ("description");
agent->SetAttribute ("description", value);
agent->SetAttribute ("version", getPandoraAgentVersion ());
// Get current time
// Get timestamp
ctime = time(0);
ctime_tm = localtime(&ctime);
value = conf->getValue ("autotime");
if (value != "1") {
sprintf (timestamp, "%d-%02d-%02d %02d:%02d:%02d", ctime_tm->tm_year + 1900,
ctime_tm->tm_mon + 1, ctime_tm->tm_mday, ctime_tm->tm_hour,
ctime_tm->tm_min, ctime_tm->tm_sec);
agent->SetAttribute ("timestamp", timestamp);
}
value = conf->getValue ("interval");
agent->SetAttribute ("interval", value);
value = Pandora_Windows_Info::getOSName ();
agent->SetAttribute ("os_name", value);
value = value + Pandora_Windows_Info::getOSVersion ();
agent->SetAttribute ("os_version", value);
value = conf->getValue ("group");
agent->SetAttribute ("group", value);
// Get OS name and version
os_name = Pandora_Windows_Info::getOSName ();
os_version = os_name + Pandora_Windows_Info::getOSVersion ();
return agent;
// Get encoding
encoding = conf->getValue ("encoding");
if (encoding == "") {
encoding = "ISO-8859-1";
}
return "<?xml version=\"1.0\" encoding=\"" + encoding + "\" ?>\n" +
"<agent_data agent_name=\"" + agent_name +
"\" description=\"" + conf->getValue ("description") +
"\" version=\"" + getPandoraAgentVersion () +
"\" timestamp=\"" + timestamp +
"\" interval=\"" + conf->getValue ("interval") +
"\" os_name=\"" + os_name +
"\" os_version=\"" + os_version +
"\" group=\"" + conf->getValue ("group") + "\">\n";
}
int
@ -705,17 +700,15 @@ Pandora_Windows_Service::checkConfig () {
int
Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
int rc = 0, xml_buffer;
TiXmlDeclaration *decl;
TiXmlDocument *doc;
TiXmlElement *local_xml, *agent;
string data_xml;
string xml_filename, random_integer;
string tmp_filename, tmp_filepath;
string encoding;
bool saved;
static HANDLE mutex = 0;
ULARGE_INTEGER free_bytes;
double min_free_bytes = 0;
Pandora_Agent_Conf *conf = NULL;
FILE *conf_fh = NULL;
conf = this->getConf ();
min_free_bytes = 1024 * atoi (conf->getValue ("temporal_min_size").c_str ());
@ -728,26 +721,24 @@ Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
/* Wait for the mutex to be opened */
WaitForSingleObject (mutex, INFINITE);
agent = getXmlHeader ();
data_xml = getXmlHeader ();
/* Write module data */
if (modules != NULL) {
modules->goFirst ();
while (! modules->isLast ()) {
Pandora_Module *module;
module = modules->getCurrentValue ();
local_xml = module->getXml ();
if (local_xml != NULL) {
agent->InsertEndChild (*local_xml);
delete local_xml;
}
module = modules->getCurrentValue ();
data_xml += module->getXml ();
modules->goNext ();
}
}
/* Close the XML header */
data_xml += "</agent_data>";
/* Generate temporal filename */
random_integer = inttostr (rand());
tmp_filename = conf->getValue ("agent_name");
@ -763,27 +754,16 @@ Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
tmp_filepath = xml_filename + tmp_filename;
/* Copy the XML to temporal file */
encoding = conf->getValue ("encoding");
if (encoding == "") {
encoding = "ISO-8859-1";
}
pandoraDebug ("Copying XML on %s", tmp_filepath.c_str ());
decl = new TiXmlDeclaration ("1.0", encoding.c_str(), "");
doc = new TiXmlDocument (tmp_filepath);
doc->InsertEndChild (*decl);
doc->InsertEndChild (*agent);
saved = doc->SaveFile();
delete doc;
delete decl;
delete agent;
if (!saved) {
conf_fh = fopen (tmp_filepath.c_str (), "w");
if (conf_fh == NULL) {
pandoraLog ("Error when saving the XML in %s",
tmp_filepath.c_str ());
ReleaseMutex (mutex);
return PANDORA_EXCEPTION;
}
fprintf (conf_fh, "%s", data_xml.c_str ());
fclose (conf_fh);
/* Only send if debug is not activated */
if (getPandoraDebug () == false) {

View File

@ -23,7 +23,6 @@
#include <list>
#include "windows_service.h"
#include "tinyxml/tinyxml.h"
#include "pandora_agent_conf.h"
#include "modules/pandora_module_list.h"
#include "ssh/pandora_ssh_client.h"
@ -46,7 +45,7 @@ namespace Pandora {
bool started;
void *udp_server;
TiXmlElement *getXmlHeader ();
string getXmlHeader ();
int copyDataFile (string filename);
int copyTentacleDataFile (string host,
string filename,

View File

@ -19,7 +19,6 @@
*/
#include "pandora_ssh_test.h"
#include "../tinyxml/tinyxml.h"
#include "../misc/pandora_file.h"
#include <iostream>
@ -70,12 +69,12 @@ Pandora_SSH_Test::~Pandora_SSH_Test () {
*/
void
Pandora_SSH_Test::test () {
string data_xml;
string pubkey_file, privkey_file, tmp_filename;
string remote_host, remote_filepath, tmp_filepath;
TiXmlDocument *doc;
TiXmlDeclaration *decl;
bool saved;
FILE *conf_fh = NULL;
pubkey_file = Pandora::getPandoraInstallDir ();
pubkey_file += "key\\id_dsa.pub";
if (! Pandora_File::fileExists (pubkey_file)) {
@ -138,17 +137,16 @@ Pandora_SSH_Test::test () {
}
tmp_filepath += tmp_filename;
decl = new TiXmlDeclaration( "1.0", "ISO-8859-1", "" );
doc = new TiXmlDocument (tmp_filepath);
doc->InsertEndChild (*decl);
saved = doc->SaveFile();
delete doc;
if (!saved) {
data_xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
conf_fh = fopen (tmp_filepath.c_str (), "w");
if (conf_fh == NULL) {
Pandora::Pandora_Exception e;
cout << "Error when saving the XML in " << tmp_filepath << endl;
cout << "Check the configuration file" << endl;
throw e;
}
fprintf (conf_fh, "%s", data_xml.c_str ());
fclose (conf_fh);
cout << "Created a blank XML file in " << tmp_filepath<< endl;

View File

@ -1,115 +0,0 @@
/*
www.sourceforge.net/projects/tinyxml
Original file by Yves Berquin.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
/*
* THIS FILE WAS ALTERED BY Tyge Løvset, 7. April 2005.
*/
#ifndef TIXML_USE_STL
#include "tinystr.h"
// Error value for find primitive
const TiXmlString::size_type TiXmlString::npos = static_cast< size_type >(-1);
// Null rep.
TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, '\0' };
void TiXmlString::reserve (size_type cap)
{
if (cap > capacity())
{
TiXmlString tmp;
tmp.init(length(), cap);
memcpy(tmp.start(), data(), length());
swap(tmp);
}
}
TiXmlString& TiXmlString::assign(const char* str, size_type len)
{
size_type cap = capacity();
if (len > cap || cap > 3*(len + 8))
{
TiXmlString tmp;
tmp.init(len);
memcpy(tmp.start(), str, len);
swap(tmp);
}
else
{
memmove(start(), str, len);
set_size(len);
}
return *this;
}
TiXmlString& TiXmlString::append(const char* str, size_type len)
{
size_type newsize = length() + len;
if (newsize > capacity())
{
reserve (newsize + capacity());
}
memmove(finish(), str, len);
set_size(newsize);
return *this;
}
TiXmlString operator + (const TiXmlString & a, const TiXmlString & b)
{
TiXmlString tmp;
tmp.reserve(a.length() + b.length());
tmp += a;
tmp += b;
return tmp;
}
TiXmlString operator + (const TiXmlString & a, const char* b)
{
TiXmlString tmp;
TiXmlString::size_type b_len = static_cast<TiXmlString::size_type>( strlen(b) );
tmp.reserve(a.length() + b_len);
tmp += a;
tmp.append(b, b_len);
return tmp;
}
TiXmlString operator + (const char* a, const TiXmlString & b)
{
TiXmlString tmp;
TiXmlString::size_type a_len = static_cast<TiXmlString::size_type>( strlen(a) );
tmp.reserve(a_len + b.length());
tmp.append(a, a_len);
tmp += b;
return tmp;
}
#endif // TIXML_USE_STL

View File

@ -1,319 +0,0 @@
/*
www.sourceforge.net/projects/tinyxml
Original file by Yves Berquin.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
/*
* THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
*
* - completely rewritten. compact, clean, and fast implementation.
* - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
* - fixed reserve() to work as per specification.
* - fixed buggy compares operator==(), operator<(), and operator>()
* - fixed operator+=() to take a const ref argument, following spec.
* - added "copy" constructor with length, and most compare operators.
* - added swap(), clear(), size(), capacity(), operator+().
*/
#ifndef TIXML_USE_STL
#ifndef TIXML_STRING_INCLUDED
#define TIXML_STRING_INCLUDED
#include <assert.h>
#include <string.h>
/* The support for explicit isn't that universal, and it isn't really
required - it is used to check that the TiXmlString class isn't incorrectly
used. Be nice to old compilers and macro it here:
*/
#if defined(_MSC_VER) && (_MSC_VER >= 1200 )
// Microsoft visual studio, version 6 and higher.
#define TIXML_EXPLICIT explicit
#elif defined(__GNUC__) && (__GNUC__ >= 3 )
// GCC version 3 and higher.s
#define TIXML_EXPLICIT explicit
#else
#define TIXML_EXPLICIT
#endif
/*
TiXmlString is an emulation of a subset of the std::string template.
Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
Only the member functions relevant to the TinyXML project have been implemented.
The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
a string and there's no more room, we allocate a buffer twice as big as we need.
*/
class TiXmlString
{
public :
// The size type used
typedef size_t size_type;
// Error value for find primitive
static const size_type npos; // = -1;
// TiXmlString empty constructor
TiXmlString () : rep_(&nullrep_)
{
}
// TiXmlString copy constructor
TiXmlString ( const TiXmlString & copy)
{
init(copy.length());
memcpy(start(), copy.data(), length());
}
// TiXmlString constructor, based on a string
TIXML_EXPLICIT TiXmlString ( const char * copy)
{
init( static_cast<size_type>( strlen(copy) ));
memcpy(start(), copy, length());
}
// TiXmlString constructor, based on a string
TIXML_EXPLICIT TiXmlString ( const char * str, size_type len)
{
init(len);
memcpy(start(), str, len);
}
// TiXmlString destructor
~TiXmlString ()
{
quit();
}
// = operator
TiXmlString& operator = (const char * copy)
{
return assign( copy, (size_type)strlen(copy));
}
// = operator
TiXmlString& operator = (const TiXmlString & copy)
{
return assign(copy.start(), copy.length());
}
// += operator. Maps to append
TiXmlString& operator += (const char * suffix)
{
return append(suffix, static_cast<size_type>( strlen(suffix) ));
}
// += operator. Maps to append
TiXmlString& operator += (char single)
{
return append(&single, 1);
}
// += operator. Maps to append
TiXmlString& operator += (const TiXmlString & suffix)
{
return append(suffix.data(), suffix.length());
}
// Convert a TiXmlString into a null-terminated char *
const char * c_str () const { return rep_->str; }
// Convert a TiXmlString into a char * (need not be null terminated).
const char * data () const { return rep_->str; }
// Return the length of a TiXmlString
size_type length () const { return rep_->size; }
// Alias for length()
size_type size () const { return rep_->size; }
// Checks if a TiXmlString is empty
bool empty () const { return rep_->size == 0; }
// Return capacity of string
size_type capacity () const { return rep_->capacity; }
// single char extraction
const char& at (size_type index) const
{
assert( index < length() );
return rep_->str[ index ];
}
// [] operator
char& operator [] (size_type index) const
{
assert( index < length() );
return rep_->str[ index ];
}
// find a char in a string. Return TiXmlString::npos if not found
size_type find (char lookup) const
{
return find(lookup, 0);
}
// find a char in a string from an offset. Return TiXmlString::npos if not found
size_type find (char tofind, size_type offset) const
{
if (offset >= length()) return npos;
for (const char* p = c_str() + offset; *p != '\0'; ++p)
{
if (*p == tofind) return static_cast< size_type >( p - c_str() );
}
return npos;
}
void clear ()
{
//Lee:
//The original was just too strange, though correct:
// TiXmlString().swap(*this);
//Instead use the quit & re-init:
quit();
init(0,0);
}
/* Function to reserve a big amount of data when we know we'll need it. Be aware that this
function DOES NOT clear the content of the TiXmlString if any exists.
*/
void reserve (size_type cap);
TiXmlString& assign (const char* str, size_type len);
TiXmlString& append (const char* str, size_type len);
void swap (TiXmlString& other)
{
Rep* r = rep_;
rep_ = other.rep_;
other.rep_ = r;
}
private:
void init(size_type sz) { init(sz, sz); }
void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
char* start() const { return rep_->str; }
char* finish() const { return rep_->str + rep_->size; }
struct Rep
{
size_type size, capacity;
char str[1];
};
void init(size_type sz, size_type cap)
{
if (cap)
{
// Lee: the original form:
// rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
// doesn't work in some cases of new being overloaded. Switching
// to the normal allocation, although use an 'int' for systems
// that are overly picky about structure alignment.
const size_type bytesNeeded = sizeof(Rep) + cap;
const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
rep_->str[ rep_->size = sz ] = '\0';
rep_->capacity = cap;
}
else
{
rep_ = &nullrep_;
}
}
void quit()
{
if (rep_ != &nullrep_)
{
// The rep_ is really an array of ints. (see the allocator, above).
// Cast it back before delete, so the compiler won't incorrectly call destructors.
delete [] ( reinterpret_cast<int*>( rep_ ) );
}
}
Rep * rep_;
static Rep nullrep_;
} ;
inline bool operator == (const TiXmlString & a, const TiXmlString & b)
{
return ( a.length() == b.length() ) // optimization on some platforms
&& ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
}
inline bool operator < (const TiXmlString & a, const TiXmlString & b)
{
return strcmp(a.c_str(), b.c_str()) < 0;
}
inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
TiXmlString operator + (const TiXmlString & a, const char* b);
TiXmlString operator + (const char* a, const TiXmlString & b);
/*
TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
Only the operators that we need for TinyXML have been developped.
*/
class TiXmlOutStream : public TiXmlString
{
public :
// TiXmlOutStream << operator.
TiXmlOutStream & operator << (const TiXmlString & in)
{
*this += in;
return *this;
}
// TiXmlOutStream << operator.
TiXmlOutStream & operator << (const char * in)
{
*this += in;
return *this;
}
} ;
#endif // TIXML_STRING_INCLUDED
#endif // TIXML_USE_STL

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,52 +0,0 @@
/*
www.sourceforge.net/projects/tinyxml
Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinyxml.h"
// The goal of the seperate error file is to make the first
// step towards localization. tinyxml (currently) only supports
// english error messages, but the could now be translated.
//
// It also cleans up the code a bit.
//
const char* TiXmlBase::errorString[ TIXML_ERROR_STRING_COUNT ] =
{
"No error",
"Error",
"Failed to open file",
"Memory allocation failed.",
"Error parsing Element.",
"Failed to read Element name",
"Error reading Element value.",
"Error reading Attributes.",
"Error: empty tag.",
"Error reading end tag.",
"Error parsing Unknown.",
"Error parsing Comment.",
"Error parsing Declaration.",
"Error document empty.",
"Error null (0) or unexpected EOF found in input stream.",
"Error parsing CDATA.",
};

File diff suppressed because it is too large Load Diff