From 69bd57f4281ac51d74b63213ed14054441a8df81 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 27 Jan 2007 13:52:18 +0000 Subject: [PATCH] added Ezc::SplitUnixDirectory(...) it's possible to give the path into the working directory in the Pattern::ParseFile(...) without using the Pattern::Directory(...) added [#] (commentary) [include] now can open a file only in a specific directory git-svn-id: svn://ttmath.org/publicrep/cgi/ezc/trunk@9 e52654a7-88a9-db11-a3e9-0013d4bc506e --- COPYRIGHT | 2 +- src/ezc.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++---------- src/ezc.h | 18 +++++++-- 3 files changed, 99 insertions(+), 25 deletions(-) diff --git a/COPYRIGHT b/COPYRIGHT index a99db80..cb07a6e 100644 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -1,4 +1,4 @@ -Copyright (c) 2006-2007, Tomasz Sowa +Copyright (c) 2007, Tomasz Sowa All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/src/ezc.cpp b/src/ezc.cpp index 23d5073..2b5da31 100644 --- a/src/ezc.cpp +++ b/src/ezc.cpp @@ -56,6 +56,52 @@ return buffer.str(); } +/* + this method splits the input file name into a directory and a file name + e.g + if 'name' is "/this/is/dummy/file.ezc" + the result will be: + dir = "/this/is/dummy" + file = "file.ezc" +*/ +void SplitUnixDirectory(const char * name, std::string & dir, std::string & file) +{ + dir.clear(); + file.clear(); + + int i; + for( i=0 ; name[i] != 0 ; ++i ); + + if( i == 0 ) + return; + + for( --i ; i>=0 && name[i]!='\\' && name[i]!='/' ; --i ); + + if( i < 0 ) + { + file.assign(name); + } + else + { + if( i == 0 ) + // we're leaving one '/' in the directory + // (we have only the root directory) + dir.assign(name, 1); + else + dir.assign(name, i); + + file.assign(name + i + 1 ); + } + +return; +} + +void SplitUnixDirectory(const std::string & name, std::string & dir, std::string & file) +{ + SplitUnixDirectory(name.c_str(), dir, file); +} + + /* * @@ -64,20 +110,26 @@ return buffer.str(); * */ -void Pattern::ParseFile(std::string file_name) +void Pattern::ParseFile(const std::string & file_name) { - file_name.insert(file_name.begin(), '\"'); - - item_root.directives.clear(); - item_root.directives.push_back( file_name ); - - CreateTreeReadInclude(item_root); + ParseFile( file_name.c_str() ); } - void Pattern::ParseFile(const char * file_name) { - ParseFile( std::string(file_name) ); +std::string file; + + if( directory.empty() ) + SplitUnixDirectory(file_name, directory, file); + else + file = file_name; + + file.insert(file.begin(), '\"'); + + item_root.directives.clear(); + item_root.directives.push_back( file ); + + CreateTreeReadInclude(item_root); } @@ -107,6 +159,9 @@ return true; } +/* + 'name' must be a relative path +*/ std::string Pattern::ReadFile(const char * name) { // for security reason we can't open a file which has two dots @@ -114,16 +169,10 @@ std::string Pattern::ReadFile(const char * name) if( !CheckFileName(name) ) return CreateMsg("incorrect file name:", name); - std::string file_name; - - if( name[0]!='\\' && name[0]!='/' ) - { - // name is a relative path - file_name += directory; - - if( !file_name.empty() ) - file_name += '/'; - } + std::string file_name(directory); + + if( !file_name.empty() ) + file_name += '/'; file_name += name; @@ -173,7 +222,7 @@ std::string directive; while( (*itext>='a' && *itext<='z') || (*itext>='A' && *itext<='Z') || (*itext>='0' && *itext<='9') || - *itext=='_' || *itext=='-' || *itext=='.' ) + *itext=='_' || *itext=='-' || *itext=='.' || *itext=='#' ) { directive += *itext; @@ -326,6 +375,15 @@ void Pattern::ReadDirectiveFor(Item & item) } +void Pattern::ReadDirectiveComment(Item & item) +{ + item.type = Item::item_comment; + + while( *itext && *itext!=']' && *itext!='\n' ) + ++itext; +} + + void Pattern::ReadDirectiveInclude(Item & item) { item.type = Item::item_include; @@ -369,6 +427,9 @@ void Pattern::CreateTreeReadItemDirective(Item & item) if( directive == "include" ) ReadDirectiveInclude(item); else + if( directive == "#" ) + ReadDirectiveComment(item); + else { // user defined item.directives.push_back(directive); @@ -462,6 +523,9 @@ void Pattern::CreateTree(Item & item) while( CreateTreeReadItem(item_temp) ) { + if( item_temp.type == Item::item_comment ) + continue; + Item * pitem = item.AddItem(item_temp); if( item_temp.type==Item::item_end || item_temp.type==Item::item_else ) diff --git a/src/ezc.h b/src/ezc.h index 993cfea..a762fdd 100644 --- a/src/ezc.h +++ b/src/ezc.h @@ -35,6 +35,12 @@ * THE POSSIBILITY OF SUCH DAMAGE. */ +/* + * version 0.9.0 + * + */ + + #ifndef headerfileezc #define headerfileezc @@ -51,12 +57,14 @@ namespace Ezc { std::string CreateMsg(const char * type, const char * arg = 0); +void SplitUnixDirectory(const char * name, std::string & dir, std::string & file); +void SplitUnixDirectory(const std::string & name, std::string & dir, std::string & file); class Pattern { public: - void ParseFile(std::string file_name); + void ParseFile(const std::string & file_name); void ParseFile(const char * file_name); void Directory(const char * d); void Directory(const std::string & d); @@ -65,8 +73,9 @@ public: { enum ItemType { - item_none, item_container, item_text, item_ifany, item_for, item_else, - item_end, item_err, item_normal, item_ifindex, item_include, item_is, item_ifone + item_none, item_container, item_text, item_ifany, item_for, + item_else, item_end, item_err, item_normal, item_ifindex, + item_include, item_is, item_ifone, item_comment }; ItemType type; @@ -112,7 +121,9 @@ private: void ReadDirectiveIs(Item & item); void ReadDirectiveIfindex(Item & item); void ReadDirectiveFor(Item & item); + void ReadDirectiveComment(Item & item); void ReadDirectiveInclude(Item & item); + void CreateTreeReadItemDirective(Item & item); void CreateTreeReadItemText(Item & item); @@ -163,7 +174,6 @@ private: UserInfoTable user_info_table; std::string otext; - bool Find(const std::string & key, UserInfo ** user_info); void MakeTextIf_go(Pattern::Item & item, bool result); bool MakeTextIfindexnumber(Pattern::Item & item, UserInfo * user_info, bool & result);