Matrix Science header

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_2_8_1-0-gea32989045 $                                                         #
# $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;
    obj->clearAllErrors();
}




/*

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

*/

Copyright © 2022 Matrix Science Ltd.  All Rights Reserved. Generated on Thu Mar 31 2022 01:12:29