fixed: when Pattern::allow_include was false then nothing was read

added: ezc can use the logger from winix
added: we check how many [include] directive was called
       (if more than 100 then we break - supposing infinite loop)
added: in Pattern we read from two directories
       if a file is not in the first directory then we try to read
       from the other one




git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@292 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2010-02-15 00:19:31 +00:00
parent bb00f23f29
commit f65178dd0e
4 changed files with 92 additions and 25 deletions

View File

@ -15,7 +15,7 @@ ezc.a: $(o)
depend:
makedepend -Y. -f- *.cpp > Makefile.dep
makedepend $(CXXFLAGS) -Y. -f- *.cpp > Makefile.dep
echo -n "o = " > Makefile.o.dep
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep

View File

@ -1,3 +1,3 @@
# DO NOT DELETE
ezc.o: ezc.h
edanticezc.o: ezc.h /home/tomek/roboczy/winix/core/log.h

View File

@ -37,6 +37,9 @@
#include "ezc.h"
#ifdef EZC_USE_WINIX_LOGGER
#include "core/log.h"
#endif
namespace Ezc
{
@ -149,7 +152,9 @@ Item::Directive file;
item_root.directives.clear();
item_root.directives.push_back( file );
CreateTreeReadInclude(item_root);
include_level = 0;
CreateTreeReadIncludeSkipAllowFlag(item_root);
}
@ -157,6 +162,7 @@ Item::Directive file;
void Pattern::ParseString(const std::string & str)
{
itext = str.c_str();
include_level = 0;
CreateTree(item_root);
}
@ -164,20 +170,33 @@ void Pattern::ParseString(const std::string & str)
void Pattern::ParseString(const char * str)
{
itext = str;
include_level = 0;
CreateTree(item_root);
}
void Pattern::Directory(const char * d)
void Pattern::Directory(const char * dir, const char * dir2)
{
directory = d;
directory = dir;
if( !dir2 )
directory2.clear();
else
directory2 = dir2;
}
void Pattern::Directory(const std::string & d)
void Pattern::Directory(const std::string & dir)
{
directory = d;
directory = dir;
directory2.clear();
}
void Pattern::Directory(const std::string & dir, const std::string & dir2)
{
directory = dir;
directory2 = dir2;
}
@ -220,30 +239,45 @@ std::string Pattern::ReadFile(const std::string & name)
*/
std::string Pattern::ReadFile(const char * name)
{
// for security reason we can't open a file which has two dots
// for security reasons we can't open a file which has two dots
// somewhere in its name '..'
if( !CheckFileName(name) )
return CreateMsg("incorrect file name:", name);
std::string file_name(directory);
std::string result;
if( !file_name.empty() )
file_name += '/';
if( !ReadFileFromDir(directory, name, result) )
if( !ReadFileFromDir(directory2, name, result) )
return CreateMsg("can't open: ", name);
return result;
}
/*
'name' must be a relative path
*/
bool Pattern::ReadFileFromDir(const std::string & dir, const char * name, std::string & result)
{
if( dir.empty() )
return false;
std::string file_name(dir);
file_name += '/';
file_name += name;
std::ifstream file(file_name.c_str());
if( !file )
return CreateMsg("can't open:", name);
// or we can give the whole path here (file_name)
else
{
std::string result;
std::getline(file, result, '\0');
return false;
return result;
}
std::getline(file, result, '\0');
#ifdef EZC_USE_WINIX_LOGGER
log << log3 << "EZC: read pattern: " << file_name << logend;
#endif
return true;
}
@ -629,15 +663,36 @@ return false;
void Pattern::CreateTreeReadInclude(Item & item)
{
if( !allow_include || item.directives.empty() || !item.directives[0].is_text || item.directives[0].name.empty() )
if( !allow_include )
return;
CreateTreeReadIncludeSkipAllowFlag(item);
}
void Pattern::CreateTreeReadIncludeSkipAllowFlag(Item & item)
{
if( item.directives.empty() || !item.directives[0].is_text || item.directives[0].name.empty() )
return;
if( include_level >= 100 )
{
#ifdef EZC_USE_WINIX_LOGGER
log << log1 << "EZC: infinite loop in \"include\" directive" << logend;
#endif
return;
}
++include_level;
std::string file_text = ReadFile( item.directives[0].name );
const char * itext_old = itext;
itext = file_text.c_str();
CreateTree(item);
itext = itext_old;
--include_level;
}
@ -1354,7 +1409,6 @@ void Generator::MakeText(Pattern::Item & item)
return;
}
switch( item.type )
{
case Pattern::Item::item_text:

View File

@ -69,8 +69,14 @@ public:
void ParseFile(const char * file_name);
void ParseString(const std::string & str);
void ParseString(const char * str);
void Directory(const char * d);
void Directory(const std::string & d);
// first we're trying to read a file from directory dir
// if there is no such a file there then we try read from dir2
// (the second dir2 can be empty - it will not be used)
void Directory(const char * dir, const char * dir2 = 0);
void Directory(const std::string & dir);
void Directory(const std::string & dir, const std::string & dir2);
void Clear();
struct Item
@ -124,12 +130,18 @@ public:
private:
const char * itext;
std::string directory;
// first we're trying to read a file from 'directory'
// if there is no such a file there then we try read from 'directory2'
// we read from these directories only if they are not empty
std::string directory, directory2;
int include_level;
bool CheckFileName(const char * name);
std::string ReadFile(const std::string & name);
std::string ReadFile(const char * name);
bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result);
int ReadCharInText();
bool IsWhite(int c);
@ -163,6 +175,7 @@ private:
void CreateTree(Item & item);
void CreateTreeReadInclude(Item & item);
void CreateTreeReadIncludeSkipAllowFlag(Item & item);
}; // Pattern