From f81442ebcc2ef577850b8c64cbeae9570acba687 Mon Sep 17 00:00:00 2001 From: Alessandro Maraschini Date: Fri, 23 Jul 2004 07:46:54 +0000 Subject: [PATCH] New Approach Exception work in progress --- .../interface/Makefile.am | 3 +- .../interface/glite/wmsutils/exception/Exception.h | 120 ++++++++++ .../glite/wmsutils/exception/Exceptions.h | 165 -------------- .../glite/wmsutils/exception/exception_codes.h | 56 +++-- .../glite/wmsutils/exception/result_codes.h | 54 ----- org.glite.wms-utils.exception/src/Exceptions.cpp | 171 -------------- org.glite.wms-utils.exception/src/Makefile.am | 7 +- org.glite.wms-utils.exception/src/utils.cpp | 249 --------------------- org.glite.wms-utils.exception/src/utils.h | 86 ------- 9 files changed, 149 insertions(+), 762 deletions(-) create mode 100644 org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exception.h delete mode 100755 org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exceptions.h delete mode 100755 org.glite.wms-utils.exception/interface/glite/wmsutils/exception/result_codes.h delete mode 100755 org.glite.wms-utils.exception/src/Exceptions.cpp delete mode 100755 org.glite.wms-utils.exception/src/utils.cpp delete mode 100755 org.glite.wms-utils.exception/src/utils.h diff --git a/org.glite.wms-utils.exception/interface/Makefile.am b/org.glite.wms-utils.exception/interface/Makefile.am index 5f552a2..3c4ccb7 100755 --- a/org.glite.wms-utils.exception/interface/Makefile.am +++ b/org.glite.wms-utils.exception/interface/Makefile.am @@ -9,7 +9,6 @@ exceptiondir = $(includedir) nobase_exception_HEADERS = \ glite/wmsutils/exception/exception_codes.h \ - glite/wmsutils/exception/Exceptions.h \ - glite/wmsutils/exception/result_codes.h + glite/wmsutils/exception/Exception.h MAINTAINERCLEANFILES = Makefile.in diff --git a/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exception.h b/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exception.h new file mode 100644 index 0000000..bbe23d2 --- /dev/null +++ b/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exception.h @@ -0,0 +1,120 @@ +#ifndef GLITE_WMS_UTILS_EXCEPTION_EXCEPTION_H +#define GLITE_WMS_UTILS_EXCEPTION_EXCEPTION_H + +/* + * Exception.h + * Copyright (c) 2001 The European Datagrid Project - IST programme, all rights reserved. + * Contributors are mentioned in the code where appropriate. + */ + +#include +#include +//#include +#include // For logging exceptions to log file +#include // list the exception codes +#include +#include // base ancestor stl::exception + + +namespace glite { + namespace wmsutils { + namespace exception { + +extern pthread_mutex_t METHOD_MUTEX; // used in order to store info into a file (rather then syslog) +#define GLITE_STACK_TRY(method_name) std::string METHOD = method_name ; try { +#define GLITE_STACK_CATCH() } catch (glite::wmsutils::exception::Exception &exc){ throw glite::wmsutils::exception::Exception ( __FILE__ , METHOD , &exc) ; } + +/** + * The Exception base classe contains attributes into which are placed exception information and provides + * constructor that beyond the error code take parameters specifying the source file and line number + * (e.g. through __FILE__ and __LINE__) where the error has been generated and string messages, + * allowing an easy way of storing the origin of the exception. + * Moreover it provides methods for getting all the exception information and for logging them either + * in a log file or to the syslog daemon. + * Each of the derived types may contain its private attributes describing the actual error instance in detail. + * Moreover each exception has an attribute representing the exception identifier that is set by the + * class constructor and allows the identification of the original exception. + * + * @version 0.1 + * @date 22 July 2004 + * @author Alessandro Maraschini +*/ + +class Exception : public std::exception{ + public: + /** + * Constructor Update all mandatory fields + * @param method the name of the method that raised the exception + * @param source The source that raised the exception (could be the file path, the class Name, etc etc) + * @param exc the previous exception as in the stack trace */ + Exception ( const std::string& source, const std::string& method, Exception *exc); + /** + * Constructor Update all mandatory fields + * @param code the code representing the thrown exception + * @param exception the name of the thrown exception + * @param method the name of the method that raised the exception + * @param source The source that raised the exception (could be the file path, the class Name, etc etc) + * @param line_number the number of the line in the file that raised the exception(if the source has been given as a file) */ + Exception ( const std::string& source, const std::string& method, int code, const std::string& exception); + + /** + * Constructor Update all mandatory fields + * @param source the path of the file that raised the exception + * @param line_number the number of the line in the file that raised the exception + * @param method the name of the method that raised the exception + * @param code the code representing the thrown exception + * @param exception the name of the thrown exception */ + Exception (const std::string& source, int line_number, const std::string& method, int code, const std::string& exception); + /** + * Default Destructor + */ + virtual ~Exception() throw (); + /** + * Return a string debug message containing information about Exception thrown + * Debug message contains all the attributes stored in an exception instance such as the method, the file and the line + * that threw the exception. + *@return the debug message string representation + */ + virtual std::string dbgMessage(); + /** + * Return the error code + * @return The integer representing the code of the error that generated the exception + */ + virtual int getCode(); + + /** + * return the Error Message associated to the Exception + * @return The Exception string message representation + */ + virtual const char* what() const throw (); + + /** + * Print Exception error information into a log file + * @param logfile the file where to log exception information + */ + virtual void log(const std::string& logfile = ""); + /** + * Retrieve the Exception name + * @return the name of the Exception thrown + */ + virtual std::string getExceptionName(); + + /** + * Retrieve the Stack of the exception as a list of previous generated exceptions + *@return the string representation of the stack trace: each line correspond to an exception message + */ + virtual std::string printStackTrace() ; + + protected: + Exception(); + int error_code; + std::string error_message ; + int line; + std::string source_file; + std::string exception_name; + std::string method_name ; + std::string stack; + std::string ancestor ; +}; //End Exception Class +}}} // Closing namespace +#endif diff --git a/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exceptions.h b/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exceptions.h deleted file mode 100755 index 30ada55..0000000 --- a/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/Exceptions.h +++ /dev/null @@ -1,165 +0,0 @@ -#ifndef GLITE_WMSUTILS_EXCEPTION_EXCEPTIONS_H -#define GLITE_WMSUTILS_EXCEPTION_EXCEPTIONS_H - -/* - * Exceptions.h - * Copyright (c) 2001 The European Datagrid Project - IST programme, all rights reserved. - * Contributors are mentioned in the code where appropriate. - */ - -#include -#include -//#include -#include // For logging exceptions to log file -#include // list the exception codes -#include -#include // base ancestor - -#include "glite/wmsutils/exception/result_codes.h" //base result codes - -namespace glite { -namespace wmsutils { -namespace exception { - -extern pthread_mutex_t METHOD_MUTEX; // used in order to store info into a file (rather then syslog) -#define EDG_STACK_TRY(method_name) std::string METHOD = method_name ; try { -#define EDG_STACK_CATCH() } catch (glite::wms::common::utilities::Exception &exc){ throw glite::wms::common::utilities::Exception ( __FILE__ , METHOD , &exc) ; } - -/** - * The Exception classes contains attributes into which are placed exception information and provides - * constructors that beyond the error code take parameters specifying the source file and line number - * (e.g. through __FILE__ and __LINE__) where the error has been generated and string messages, - * allowing an easy way of storing the origin of the exception. - * Moreover it provides methods for getting all the exception information and for logging them either - * in a log file or to the syslog daemon. - * Each of the derived types may contain its private attributes describing the actual error instance in detail. - * For example a JdlSyntaxException thrown when parsing the job description could contain additional - * details indicating exactly on which attributes the error occurred. - * Moreover each exception has an attribute representing the exception identifier that is set by the - * class constructor and allows the identification of the original exception. - * - * @ingroup Common - * @version 0.1 - * @date 15 April 2002 - * @author Alessandro Maraschini -*/ - -class Exception : public std::exception{ - public: - Exception ( const std::string& source, - const std::string& method, - Exception *e); - /** - * Constructor Update all mandatory fields - * @param code the code representing the thrown exception - * @param exception the name of the thrown exception - * @param method the name of the method that raised the exception - * @param source The source that raised the exception (could be the file path, the class Name, etc etc) - * @param line_number the number of the line in the file that raised the exception(if the source has been given as a file) */ - - Exception ( const std::string& source, - const std::string& method, - int code, - const std::string& exception); - - /** - * Constructor Update all mandatory fields - * @param source the path of the file that raised the exception - * @param line_number the number of the line in the file that raised the exception - * @param method the name of the method that raised the exception - * @param code the code representing the thrown exception - * @param exception the name of the thrown exception */ - Exception (const std::string& source, - int line_number, - const std::string& method, - int code, - const std::string& exception); - - /** - * Destructor - */ - virtual ~Exception() throw (); - /** - * Return a string debug message containing information about Exception thrown - */ - virtual std::string dbgMessage(); - /** - * Return the Code number - */ - virtual int getCode(); - - /** - * return the Error Message associated to the Exception - */ - virtual const char* what() const throw (); - - /** - * Print Exception error information into a log file - */ - virtual void log(const std::string& logfile = ""); - /** - * Return the name of the Exception raised - */ - virtual std::string getExceptionName(); - - /** - * Return the list of methods that caused the Exception - */ - virtual std::string printStackTrace() ; - - protected: - /** - * Empty Default Constructor - */ - Exception(); - //Mandatory Information: - int error_code; - std::string error_message ; - int line; - std::string source_file; - std::string exception_name; - std::string method_name ; - std::string stack; - std::string ancestor ; - // Exception* exc ; -}; //End Exception Class - -/** - * This Exception is thrown when a unespected fatal error is found - */ -class FatalErrorException : public Exception { -public: - FatalErrorException(const std::string& file, - int line, - const std::string& method); -};//End CLass FatalErrorException - -/** - * StdException remap the Exception thrown by std::exception class into the JSUI WL standard format - */ -class StdException : public Exception { -public: - StdException(const std::string& file, - int line, - const std::string& method, - int code, - const std::string& exception_name); -};//End CLass StdException - -/** - * StdException remap the Exception thrown by std::exception class into the JSUI WL standard format - */ -class ThreadException : public Exception { -public: - ThreadException(const std::string& file, - int line, - const std::string& method, - int code, - int jobNumber); -};//End CLass StdException - -} // exception namespace -} // wmsutils namespace -} // glite namespace - -#endif diff --git a/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/exception_codes.h b/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/exception_codes.h index e784833..7597e08 100755 --- a/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/exception_codes.h +++ b/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/exception_codes.h @@ -1,7 +1,6 @@ -#ifndef GLITE_WMSUTILS_EXCEPTION_CODES_H -#define GLITE_WMSUTILS_EXCEPTION_CODES_H - -// pure C-style code: +#ifndef GLITE_WMSUTILS_EXCEPTION_CODES_H +#define GLITE_WMSUTILS_EXCEPTION_CODES_H +// pure C-style code (needed by some libraries) #define GLITE_WMS_COMMON_ERROR_BASE 900 #define GLITE_WMS_USERINTERFACE_ERROR_BASE 1000 #define GLITE_WMS_NETWORKSERVER_ERROR_BASE 1200 @@ -11,37 +10,32 @@ #define GLITE_WMS_REQUESTAD_ERROR_BASE 1500 #define GLITE_WMS_CHECKPOINT_ERROR_BASE 1600 #define GLITE_WMS_CONFIGURATION_ERROR_BASE 1800 - #ifdef __cplusplus - -namespace glite { -namespace wmsutils { +namespace glite { +namespace wmsutils { namespace exception { - -/** - * The Error Code -*/ -enum { - WMS_COMMON_BASE = GLITE_WMS_COMMON_ERROR_BASE, - THREAD_INIT , // pthread_attr_init method failed - THREAD_DETACH , // pthread_attr_setdetachstate method failed - THREAD_CREATE , // pthread_create method failed - THREAD_JOIN, - THREAD_SSL, - WMS_FATAL_ERROR, - WMS_UI_ERROR_BASE = GLITE_WMS_USERINTERFACE_ERROR_BASE, - WMS_NS_ERROR_BASE = GLITE_WMS_NETWORKSERVER_ERROR_BASE, - WMS_SOCKET_ERROR_BASE = GLITE_WMS_SOCKET_ERROR_BASE, - WMS_LDAP_ERROR_BASE = GLITE_WMS_LDAP_ERROR_BASE, - WMS_LB_ERROR_BASE = GLITE_WMS_LOGGING_ERROR_BASE , - WMS_REQUESTAD_ERROR_BASE = GLITE_WMS_REQUESTAD_ERROR_BASE, - WMS_CHKPT_ERROR_BASE = GLITE_WMS_CHECKPOINT_ERROR_BASE, - WMS_CONFIGURATION_ERROR_BASE = GLITE_WMS_CONFIGURATION_ERROR_BASE -}; - + /** + * The Error Code + */ + enum { + WMS_COMMON_BASE = GLITE_WMS_COMMON_ERROR_BASE, + WMS_THREAD_INIT , // pthread_attr_init method failed + WMS_THREAD_DETACH , // pthread_attr_setdetachstate method failed + WMS_THREAD_CREATE , // pthread_create method failed + WMS_THREAD_JOIN, + WMS_THREAD_SSL, + WMS_FATAL_ERROR, + WMS_UI_ERROR_BASE = GLITE_WMS_USERINTERFACE_ERROR_BASE, + WMS_NS_ERROR_BASE = GLITE_WMS_NETWORKSERVER_ERROR_BASE, + WMS_SOCKET_ERROR_BASE = GLITE_WMS_SOCKET_ERROR_BASE, + WMS_LDAP_ERROR_BASE = GLITE_WMS_LDAP_ERROR_BASE, + WMS_LB_ERROR_BASE = GLITE_WMS_LOGGING_ERROR_BASE , + WMS_REQUESTAD_ERROR_BASE = GLITE_WMS_REQUESTAD_ERROR_BASE, + WMS_CHKPT_ERROR_BASE = GLITE_WMS_CHECKPOINT_ERROR_BASE, + WMS_CONFIGURATION_ERROR_BASE = GLITE_WMS_CONFIGURATION_ERROR_BASE + }; } // exception namespace } // wmsutils namespace } // glite namespace - #endif //ifdef c++ #endif diff --git a/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/result_codes.h b/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/result_codes.h deleted file mode 100755 index ace9326..0000000 --- a/org.glite.wms-utils.exception/interface/glite/wmsutils/exception/result_codes.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef GLITE_WMSUTILS_EXCEPTION_RESULT_CODES_H -#define GLITE_WMSUTILS_EXCEPTION_RESULT_CODES_H - -namespace glite { -namespace wmsutils { -namespace exception { -/* - * exception_codes.h - * Copyright (c) 2001 The European Datagrid Project - IST programme, all rights reserved. - * Contributors are mentioned in the code where appropriate. - */ - - -/** - * Result Code -*/ -enum ResultCode { - SUCCESS, // The requested operation has been completed successfully - ACCEPTED, // The requested operation has been accepted - - SUBMISSION_FAILURE, // API failed, general RB Exc remapping - CANCEL_FAILURE, // API failed, general RB Exc remapping - GETOUTPUT_FAILURE, // API failed, general RB Exc remapping - STATUS_FAILURE, // API failed, general RB Exc remapping - - GETOUTPUT_FORBIDDEN, //When trying to retrieve output from a not submitted job - CANCEL_FORBIDDEN, //When trying to cancel a not submitted job - STATUS_FORBIDDEN, //When trying to retrieve status from a not submitted job - ALREADY_SUBMITTED, //submit skipped because Job has been already submitted - - JOIN_FAILED, //When a pthread_join is waiting for a cored thread - - OUTPUT_NOT_READY, //JobNotDoneException - FILE_TRANSFER_ERROR, //SandboxIOException - JOB_NOT_FOUND, //JobNotFoundException - - MARKED_FOR_REMOVAL, //Cancel Method Result - GENERIC_FAILURE, //Cancel Method Result - CONDOR_FAILURE, //Cancel Method Result - - GLOBUS_JOBMANAGER_FAILURE, - - JOB_ALREADY_DONE, - JOB_ABORTED, - JOB_CANCELLING, - JOB_NOT_OWNER - -}; - -} // exception namespace -} // wmsutils namespace -} // glite namespace - -#endif diff --git a/org.glite.wms-utils.exception/src/Exceptions.cpp b/org.glite.wms-utils.exception/src/Exceptions.cpp deleted file mode 100755 index 30a19cc..0000000 --- a/org.glite.wms-utils.exception/src/Exceptions.cpp +++ /dev/null @@ -1,171 +0,0 @@ -/* ************************************************************************** -* filename : Exceptions.cpp -* author : Alessandro Maraschini -* copyright : (C) 2002 by DATAMAT -***************************************************************************/ - -#include "glite/wmsutils/exception/Exceptions.h" -#include "glite/wmsutils/exception/exception_codes.h" -#include "utils.h" - -namespace glite { -namespace wmsutils { -namespace exception { - -using namespace std ; -using namespace glite::wmsutils::exception ; - -pthread_mutex_t METHOD_MUTEX ; // This mutex is used in order to lock the file for writing log infornation -/* ********************************* -* Exception Class Implementation -************************************/ -//Constructor/Destructor - -Exception::Exception () { - //exc = NULL ; - stack= ""; - line = 0; -} ; - -Exception::~Exception() throw(){ } -/** -* Exception chainig -*/ -Exception::Exception ( const string& source, - const string& method, - Exception *e){ - source_file = source ; - method_name = method; - error_message = ""; - stack= e->printStackTrace(); - ancestor = e->what() ; - line = 0; - error_code= 0; -}; -Exception::Exception( const std::string& file, - int line_number, - const std::string& method, - int code, - const std::string& name) - : error_code(code), exception_name(name){ - source_file = file; - line = line_number; - method_name = method; - stack= ""; -}; -Exception::Exception (const string& source, - const string& method, - int code, - const string& exception) - : error_code(code), exception_name(exception){ - source_file = source; - method_name = method; - stack= ""; - line = 0; -}; -int Exception::getCode(){ - if (error_code != 0) - return error_code ; - else - return WMS_COMMON_BASE; -}; -const char* Exception::what() const throw(){ - if (!ancestor.empty() ) - return ancestor.c_str() ; - if ( error_message != "") - return error_message.c_str() ; - else - return "" ; - -}; -string Exception::getExceptionName(){ - if (exception_name!= "") - return exception_name; - else - return "" ; -}; -void Exception::log(const std::string& logfile) -{ - if ( logfile == "") - syslog ( LOG_PERROR, (char *) (dbgMessage()).c_str() ); - else{ - pthread_mutex_lock( &METHOD_MUTEX); // LOCK - //TBD : test if file exist-->>Create HEADER ?? - ofstream fout ((char *) logfile.c_str() , ios::app ); //Open the file for writing (if it doesn't exist then it will be created) - fout << "\n" << dbgMessage() ; //write (append) the message - fout.close(); //close the file - pthread_mutex_unlock( &METHOD_MUTEX); // UNLOCK - } -}; -string Exception::printStackTrace(){ -return stack + "\n" + dbgMessage(); -}; -string Exception::dbgMessage(){ - // cout << "\nDBM callded for :" << this; - string result ; - //Adding exception Name - if ( exception_name!="") - result = exception_name ; - //Adding error msg - if (error_message!="") - result +=": " + string(what()); - if (result != "") - result+="\n"; - //Adding Source - result +=" at " + source_file ; - //Adding line number - if (line!=0) - result += " Line: " + inTo(line); - result +=" " ; - //Adding Method Name - if (method_name != "") - result += "Method: " +method_name ; - return result; -} -FatalErrorException::FatalErrorException(const std::string& file, - int line, - const std::string& method) - : Exception(file, line, method, WMS_FATAL_ERROR, "FatalErrorException") -{ - error_message = "Fatal Error found: system is unable to continue"; -} -StdException::StdException(const std::string& file, - int line, - const std::string& method, - int code, - const std::string& exception_name) - : Exception(file, line, method, code, "StdException") -{ - error_message = "std::exception Fatal Error thrown: " + exception_name; -} -ThreadException::ThreadException(const std::string& file, - int line, - const std::string& method, - int code, - int jobNumber) - : Exception(file, line, method, code, "ThreadException") -{ - - switch (code){ - case THREAD_INIT: - error_message = "pthread_attr_init"; - break; - case THREAD_DETACH : - error_message = "pthread_attr_setdetachstate"; - break; - case THREAD_SSL : - error_message = "SSL multi thread procedure"; - break; - case THREAD_CREATE : - error_message = "pthread_create"; - break; - default: //THREAD_JOIN - error_message = "pthread_join"; - break; - } - error_message += "pthread Fatal Error thrown for: " + error_message ; -} - -} // exception namespace -} // wmsutils namespace -} // glite namespace diff --git a/org.glite.wms-utils.exception/src/Makefile.am b/org.glite.wms-utils.exception/src/Makefile.am index 09a4a8b..0ce42c9 100755 --- a/org.glite.wms-utils.exception/src/Makefile.am +++ b/org.glite.wms-utils.exception/src/Makefile.am @@ -6,11 +6,10 @@ ## * ## ********************************************************************* -lib_LTLIBRARIES = libglite_wmsutils_exceptions.la +lib_LTLIBRARIES = libglite_wmsutils_exception.la -libglite_wmsutils_exceptions_la_SOURCES = \ - Exceptions.cpp \ - utils.cpp +libglite_wmsutils_exception_la_SOURCES = \ + Exception.cpp utilitiesincludedir = $(includedir)/glite/wmsutils/exception utilitiesinclude_HEADERS = \ diff --git a/org.glite.wms-utils.exception/src/utils.cpp b/org.glite.wms-utils.exception/src/utils.cpp deleted file mode 100755 index 75016ac..0000000 --- a/org.glite.wms-utils.exception/src/utils.cpp +++ /dev/null @@ -1,249 +0,0 @@ -/* ************************************************************************** -* filename : utils.cpp -* author : Alessandro Maraschini -* copyright : (C) 2002 by DATAMAT -***************************************************************************/ -#include "utils.h" -#include -#include - -using namespace std; - -namespace glite { -namespace wmsutils { -namespace exception { - -/****************************************************************** - method : isInt - field: integer & string class - check if str is an integer -*******************************************************************/ -int isInt (const string& str) -{ - int len, bCode,eCode; - char code ; - const char * strCh ; - len = str.size() ; - strCh = str.c_str(); - bCode= '0'; - eCode= '9'; - for (int i=0; ieCode) ) //It's not a digit number - return 1; - } - return 0; -} - - -/****************************************************************** - methods : toInt , toHex - field: integer & string class - Converts a string str to an Integer value sum - returns 0/1 ---> success/failure - STRING--->INTEGER -*******************************************************************/ -int toHex (const string& str,int &sum) -{ - int len, bCode,eCode,dec=1; - char code ; - const char * strCh ; - - len = str.size() ; - strCh = str.c_str(); - bCode= '0'; - eCode= '9'; - sum=0; - for (int i=0; i= bCode) && (code <=eCode) ){ //It's a digit number - sum = sum + dec * (code-bCode) ; - //cout << dec <<" Int added\n" ; - } - else if ( (code >= 'A') && (code <= 'F') ){ //It's a hex allowed char - sum = sum + dec * (code- 'A' +10) ; - //cout << dec << " Hex Added\n" ; - } - else { - return 1; - //cout << sum << " Azz Added\n" ; - } - } - return 0; - -}; - -int toInt (const string& str,int &sum) -{ - int len, bCode,eCode,dec=1; - char code ; - const char * strCh ; - - len = str.size() ; - strCh = str.c_str(); - bCode= '0'; - eCode= '9'; - sum=0; - for (int i=0; ieCode) ) //It's not a digit number - return 1; - else - sum = sum + dec * (code-bCode) ; - } - return 0; -} -/****************************************************************** - method : inTo - field: integer & string class - Converts an integer to a string - INTEGER --->STRING -*******************************************************************/ -string inTo(const int number){ - char buffer [CHAR_BUFFER_SIZE] ; - sprintf (buffer, "%i" , number) ; - return std::string(buffer); -}; - -/****************************************************************** - method : count - field: string class - Counts number of sub-string occurrences in str -*******************************************************************/ -int count(const string& strMain,const string& sep){ - int n=0,found ; - int l; - //string str=strMain ; - l = sep.length(); - found = strMain.find(sep) ; - while (found!= -1){ - found = strMain.find(sep , found + l) ; - ++n ; - } - return n ; -}; - -/****************************************************************** - method : split - field: string class - Splits a string into a vector of substring - maxLenght: max dimension of vector (the last item of the vector will be the lasting part) - trough: if trough == 1 the separator will be copied at the end of the left-substring -*******************************************************************/ -vector split(const string& str, const string& sep, int maxLength, int trough) -{ - vector strList ; - int n,l,end, begin; - string tmpStr=str ; - l= sep.length(); - // Optional trough variable indicates that the separator - // string remain in the array string - if (trough) trough = l ; - n = count(tmpStr,sep); - if (n>maxLength) n = maxLength ; - //n (or maxLength) is the number of separator to be found - begin = 0; - //cout << "->"<< str << " <--------> " << sep << ":\n"; - end = tmpStr.find(sep); - for (int i=0; ibeg>end>" << tmpStr.length() ; - strList.push_back(tmpStr.substr(begin, tmpStr.length() - begin ) ) ; - //cout << "||"<< tmpStr.substr( begin,tmpStr.length() - begin )<<"||\n" ; - return strList ; - -}; - -/****************************************************************** - method : sp - field: string class - used to create a checkString -*******************************************************************/ -string sp(const string& separator) - { - const string sep = "_SEP_" ; - return sep + separator + sep ; - } - -/****************************************************************** - method : checkFormat - field: string class - Chek the format of a string - The format should be given as: - TYPE+sep+SEPARATOR+sep+TYPE+sep+SEPARATOR+sep+ TYPE +... - Returns: 0 No Error - 1 Error Found -*******************************************************************/ -int checkFormat (const string& format, const string& str) -{ - string type , separator, tmpStr=str ; - vector form ; - int len, pos, i; - const string sep = "_SEP_" ; - const string sepStr = "$STR$" ; - const string sepInt = "$INT$" ; - form =split (format,sep); - len=form.size(); - for (i =1;i -#endif - -#ifdef HAVE_STRINGSTREAM -#include -#else -#include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#define CHAR_BUFFER_SIZE 1024 - -namespace glite { -namespace wmsutils { -namespace exception { - -/** - * std:string useful JSUI common utilized methods - * - * @ingroup Common - * @version 0.1 - * @date 15 April 2002 - * @author Alessandro Maraschini -*/ - -/** -* Check if a string can be converted into an integer -* @ingroup Common -*/ -int isInt (const std::string& str); -/** -* Converts a string into an Integer value -* @ingroup Common -*/ -int toInt (const std::string& str, int &sum); -/** -* Converts a string into an Hexasdecimal value -* @ingroup Common -*/ -int toHex (const std::string& str, int &sum); -/** -* Converts an integer into a string -* @ingroup Common -*/ -std::string inTo(int i); -/** -* Counts number of sub-string occurrences in a string -* @ingroup Common -*/ -int count(const std::string& strMain, const std::string& sep); -/** -* Splits a string into a vector of substring -* @param str - The string to be parsed -* @param sep - The separator to be found -* @param maxLength - max dimension of vector (the last item of the vector will be the lasting part) -* @param trough - if trough == 1 the separator will be copied at the end of the left-substring -* @ingroup Common -*/ -std::vector split(const std::string& str, const std::string& sep, int maxLength =1000, int trough = 0); -std::string sp(const std::string& separator); -int checkFormat (const std::string& format, const std::string& str); - -void -replace(std::string& where, const std::string& what, const std::string& with); - -} // exception namespace -} // wmsglite namespace -} // glite namespace - -#endif -- 1.8.2.3