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: /vol/cvsroot/parser/examples/test_cxx/common_error.cpp,v $ #
#     $Author: davidc $                                                      #
#       $Date: 2016/04/11 15:14:35 $                                         #
#   $Revision: 1.3 $                                                         #
# $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 © 2016 Matrix Science Ltd.  All Rights Reserved. Generated on Fri Jun 2 2017 01:44:50