Matrix Science Mascot Parser toolkit
 
Loading...
Searching...
No Matches
common_error.cpp

Generic error handling example.

/*
##############################################################################
# file: common_error.cpp #
# 'msparser' toolkit example code #
##############################################################################
# COPYRIGHT NOTICE #
# Copyright 1998-2005 Matrix Science Limited All Rights Reserved. #
# #
##############################################################################
# $Source: parser/examples/test_cxx/common_error.cpp $ #
# $Author: villek@matrixscience.com $ #
# $Date: 2018-07-30 16:23:53 +0100 $ #
# $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_3_0_0-2024-09-24-0-g93ebaeb4f4 $ #
# $NoKeywords:: $ #
##############################################################################
*/
#include "msparser.hpp"
#include <iostream>
// All the classes are part of the matrix_science namespace
using namespace matrix_science;
/*****************************************************************************/
/* Local functions */
/* Any configuration file class can be cast to ms_errorsorigin */
/*****************************************************************************/
static void checkErrorHandler(ms_errors *obj);
/*****************************************************************************/
/* main */
/* Call this program with a single argument - the name of the .dat file */
/*****************************************************************************/
int main(int argc, char * argv[])
{
ms_datfile file;
if (argc == 2)
{
file.setFileName(argv[1]);
}
else
{
file.setFileName("wrong_name.txt");
}
file.read_file();
if (!file.isValid())
{
checkErrorHandler(&file);
}
else
{
std::cout << "The file has been read and parsed successfully. Congratulations!"
<< std::endl;
}
return 0;
}
/*****************************************************************************/
/* checkErrorHandler */
/* Outputs detailed information about all collected errors */
/*****************************************************************************/
static void checkErrorHandler(ms_errors *obj)
{
std::cout << "Last error description " << std::endl;
std::cout << "=========================================" << std::endl;
std::cout << "Error: " << obj->getLastErrorString() << std::endl;
std::cout << "=========================================" << std::endl;
std::cout << "Testing the error handling... " << std::endl;
std::cout << "=========================================" << std::endl;
const ms_errs *err = obj->getErrorHandler();
for (int i=1; i <= err->getNumberOfErrors(); i++)
{
std::cout << "Error number: "
<< err->getErrorNumber(i)
<< " ("
// one time happened and repeated after that zero or more times
<< (err->getErrorRepeats(i)+1)
<< " times) : "
<< err->getErrorString(i)
<< std::endl;
}
std::cout << std::endl;
}
/*
will give the output:
# common_error.cpp
Last error description
=========================================
Error: Cannot find file 'mascot.dat'. It contains the most important configuration parameters
=========================================
Testing the error handling...
=========================================
Error number: 1537 (1 times) : [F] Cannot find file 'mascot.dat'. It contains the most important configuration parameters
*/
Encapsulates the mascot.dat file that contains the most important parameters.
Definition: ms_datfile.hpp:47
void read_file()
Call this method in order to read the configuration information from the file.
Definition: ms_datfile.cpp:1857
void setFileName(const char *name)
Set the file name explicitly, before saving or loading.
Definition: ms_datfile.cpp:1670
This class is used as a base class for several Mascot Parser classes.
Definition: ms_errors.hpp:696
std::string getLastErrorString() const
Return the error description of the last error that occurred.
Definition: ms_errors.cpp:1488
const ms_errs * getErrorHandler() const
Retrive the error object using this function to get access to all errors and error parameters.
Definition: ms_errors.cpp:1518
bool isValid() const
Call this function to determine if there have been any errors.
Definition: ms_errors.cpp:1472
void clearAllErrors()
Remove all errors from the current list of errors.
Definition: ms_errors.cpp:1457
All errors are collected in an instance of this class.
Definition: ms_errors.hpp:37
int getErrorRepeats(const int num=-1) const
Returns a number of times the specified error has been repeated.
Definition: ms_errors.cpp:1171
int getErrorNumber(const int num=-1) const
Return a specific error number or ERR_NO_ERROR.
Definition: ms_errors.cpp:1044
std::string getErrorString(const int num) const
Returns a specific error as a string.
Definition: ms_errors.cpp:1094
int getNumberOfErrors() const
Return the number of errors since the last call to clearAllErrors().
Definition: ms_errors.cpp:1004