Changeset 18018 for trunk/Mars/mbase


Ignore:
Timestamp:
11/17/14 18:42:01 (10 years ago)
Author:
dneise
Message:
ticket:#11 MEnv, will now check if the last line ends with a \n ,and if not, it will throw an exception, print an explanation and stop fatally. This will hopefully make the user to add an empty line to the file and be happy.
Location:
trunk/Mars/mbase
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mbase/MEnv.cc

    r9345 r18018  
    5151#include "MEnv.h"
    5252
     53#include <fstream>
     54#include <exception>
     55
    5356#include <Gtypes.h>
    5457#include <TObjString.h>
     
    97100
    98101    SetRcName(fname);
     102   
     103    if (!IsTEnvCompliant(name)){
     104        throw "This file can't be correctly parsed by TEnv";
     105        return;
     106    }
    99107
    100108    // No file found
     
    189197
    190198    return 0;
     199}
     200
     201//---------------------------------------------------------------------------
     202//
     203// Check if the input file is compatible to the way TEnv parses a resource file.
     204//
     205// Returns true in case everything is file and
     206//         false in case there is a problem with the file.
     207//
     208// Background:
     209//      This check has been introduced as a workaround for a ROOT feature.
     210//      TEnv will not see the last line in a text file, when this last line
     211//      does not end with a \n.
     212//
     213//      In addition we check for the occurence of \r, since some editors would
     214//      place \r instead of \n (unlike windows that uses \n\r), which would
     215//      also result in problems.
     216//      FIXME: So currently we also complain about "\n\r" and "\r\n" as well as
     217//      "\r  \t   \n" or "\n      \t\t\t\r", which would in fact not be a problem at
     218//      all.
     219Bool_t MEnv::IsTEnvCompliant(const char *fname)
     220{
     221    ifstream ifs(fname);
     222    bool printable_char_since_last_lf = false;
     223    char c;
     224    while (ifs.good()){
     225        ifs.get(c);
     226
     227        if (c==13){
     228            gLog << err << "Menv::IsTEnvCompliant - file:" << fname
     229                << " contains \\r this might lead to parsing errors. "
     230                <<"Please exchange \\r by \\n everywhere in that file."
     231                << endl;
     232            return false;
     233        }
     234        if (c=='\n'){
     235            printable_char_since_last_lf = false;
     236        }
     237        if (isgraph(c)){
     238            printable_char_since_last_lf = true;
     239        }
     240    }
     241    if (printable_char_since_last_lf){
     242        gLog << err << "Menv::IsTEnvCompliant - file:" << fname
     243            << " must have \\n at the end of the file. "
     244            <<"Please make sure this is the case."
     245            << endl;
     246        return false;
     247    }
     248    return true;
    191249}
    192250
  • trunk/Mars/mbase/MEnv.h

    r9518 r18018  
    2727    TString Compile(TString str, const char *post) const;
    2828    Int_t   ReadInclude();
     29    Bool_t  IsTEnvCompliant(const char *fname);
    2930
    3031public:
Note: See TracChangeset for help on using the changeset viewer.