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
This commit is contained in:
Tomasz Sowa 2010-08-02 18:47:19 +00:00
parent 573d241dc1
commit 7554630395
2 changed files with 38 additions and 15 deletions

View File

@ -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; for(; *path && *file; ++path, ++file)
{
if( *path != *file )
return false;
}
// we do not allow a slash or backslash in the name if( *file != 0 )
for(i=0 ; name[i]!=0 ; ++i) return false;
if( name[i] == '/' || name[i] == '\\' )
// "\" 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; return false;
// "." is not allowed // looking for the next slash of backslash
if( i==1 && name[0]=='.' ) while( *name && *name!='\\' && *name!='/' )
return false; name += 1;
// ".." is not allowed too // skipping the slash (or backslash)
if( i==2 && name[0]=='.' && name[1]=='.' ) if( *name )
return false; name += 1;
}
return true; return true;
} }
/* /*
'name' must be a relative path - without a slash or backslash '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) std::string Pattern::ReadFile(const char * name)
{ {
if( !CheckFileName(name) ) if( !IsFileCorrect(name) )
return CreateMsg("incorrect file name:", name); return CreateMsg("incorrect file name:", name);
std::string result; std::string result;
@ -1321,7 +1345,6 @@ void Generator::MakeTextIfoneno(Pattern::Item & item)
void Generator::MakeTextIs(Pattern::Item & item) void Generator::MakeTextIs(Pattern::Item & item)
{ {
bool info_res1, info_res2; bool info_res1, info_res2;
bool res = false;
if( item.functions.size() != 2 ) if( item.functions.size() != 2 )
return; return;
@ -1339,7 +1362,6 @@ bool res = false;
void Generator::MakeTextIsno(Pattern::Item & item) void Generator::MakeTextIsno(Pattern::Item & item)
{ {
bool info_res1, info_res2; bool info_res1, info_res2;
bool res = false;
if( item.functions.size() != 2 ) if( item.functions.size() != 2 )
return; return;

View File

@ -153,7 +153,8 @@ private:
std::string ReadFile(const std::string & name); std::string ReadFile(const std::string & name);
std::string ReadFile(const char * 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); bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result);
int ReadCharInText(); int ReadCharInText();