Browse Source

changed: testing file names

now it is allowed to use a slash (we can use directories)
only ".." is not allowed (you cannot go up in a directory structure)


git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@299 e52654a7-88a9-db11-a3e9-0013d4bc506e
master
Tomasz Sowa 12 years ago
parent
commit
7554630395
  1. 50
      src/ezc.cpp
  2. 3
      src/ezc.h

50
src/ezc.cpp

@ -186,27 +186,51 @@ void Pattern::Clear()
bool Pattern::CheckFileName(const char * name)
bool Pattern::HasFileAtBeginning(const char * path, const char * file)
{
size_t i;
// we do not allow a slash or backslash in the name
for(i=0 ; name[i]!=0 ; ++i)
if( name[i] == '/' || name[i] == '\\' )
for(; *path && *file; ++path, ++file)
{
if( *path != *file )
return false;
}
// "." is not allowed
if( i==1 && name[0]=='.' )
if( *file != 0 )
return false;
// ".." is not allowed too
if( i==2 && name[0]=='.' && name[1]=='.' )
return false;
// "\" is from a dos path syntax
if( *path==0 || *path=='\\' || *path=='/' )
return true;
return false;
}
/*
".." is not allowed in the file path
you cannot go up from your template directory
*/
bool Pattern::IsFileCorrect(const char * name)
{
while( *name )
{
if( HasFileAtBeginning(name, "..") )
return false;
// looking for the next slash of backslash
while( *name && *name!='\\' && *name!='/' )
name += 1;
// skipping the slash (or backslash)
if( *name )
name += 1;
}
return true;
}
/*
'name' must be a relative path - without a slash or backslash
*/
@ -221,7 +245,7 @@ std::string Pattern::ReadFile(const std::string & name)
*/
std::string Pattern::ReadFile(const char * name)
{
if( !CheckFileName(name) )
if( !IsFileCorrect(name) )
return CreateMsg("incorrect file name:", name);
std::string result;
@ -1321,7 +1345,6 @@ void Generator::MakeTextIfoneno(Pattern::Item & item)
void Generator::MakeTextIs(Pattern::Item & item)
{
bool info_res1, info_res2;
bool res = false;
if( item.functions.size() != 2 )
return;
@ -1339,7 +1362,6 @@ bool res = false;
void Generator::MakeTextIsno(Pattern::Item & item)
{
bool info_res1, info_res2;
bool res = false;
if( item.functions.size() != 2 )
return;

3
src/ezc.h

@ -153,7 +153,8 @@ private:
std::string ReadFile(const std::string & name);
std::string ReadFile(const char * name);
bool CheckFileName(const char * name);
bool HasFileAtBeginning(const char * path, const char * file);
bool IsFileCorrect(const char * name);
bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result);
int ReadCharInText();

Loading…
Cancel
Save