#include <file_system.hh>
Inheritance diagram for lestes::lang::cplus::lex::file_system:

Public Types | |
| enum | name_type { NM_INVALID, NM_FILE_REL, NM_FILE_ABS, NM_PATH_REL, NM_PATH_ABS } |
| The type of name for classification. More... | |
Public Member Functions | |
| ptr< named_istream > | open_file (const lstring &name) |
| Opens a file stream, using only working directory. | |
| ptr< named_istream > | find_file (const lstring &path, const lstring &name, bool system) |
| Opens a file stream, searching given paths. | |
| bool | add_search_path (const lstring &path) |
| Adds search path. | |
Static Public Member Functions | |
| static ptr< file_system > | create (void) |
| Returns new instance. | |
| static name_type | classify_name (const lstring &name) |
| Classifies path and file name. | |
Protected Member Functions | |
| file_system (void) | |
| Creates the object. | |
| virtual void | gc_mark (void) |
| Marks the object. | |
Private Types | |
| typedef ::lestes::std::vector< lstring > | lstrings_type |
| Type of sequence of strings, used for search path list. | |
Private Member Functions | |
| file_system (const file_system &) | |
| Hides copy constructor. | |
| file_system & | operator= (const file_system &) |
| Hides assignment operator. | |
Static Private Member Functions | |
| static ptr< lstrings_type > | split (const lstring &str, hchar delimiter) |
| Splits string into parts. | |
Private Attributes | |
| srp< lstrings_type > | search_paths |
| System search paths. | |
Provides acces to the file system. Enables opening file streams and include path search.
Definition at line 54 of file file_system.hh.
typedef ::lestes::std::vector<lstring> lestes::lang::cplus::lex::file_system::lstrings_type [private] |
Type of sequence of strings, used for search path list.
Definition at line 90 of file file_system.hh.
The type of name for classification.
| NM_INVALID | Invalid name. |
| NM_FILE_REL | Relative file name. |
| NM_FILE_ABS | Absolute file name. |
| NM_PATH_REL | Relative path. |
| NM_PATH_ABS | Absolute path. |
Definition at line 57 of file file_system.hh.
00057 { 00058 //! Invalid name. 00059 NM_INVALID, 00060 //! Relative file name. 00061 NM_FILE_REL, 00062 //! Absolute file name. 00063 NM_FILE_ABS, 00064 //! Relative path. 00065 NM_PATH_REL, 00066 //! Absolute path. 00067 NM_PATH_ABS 00068 } name_type;
| lestes::lang::cplus::lex::file_system::file_system | ( | void | ) | [protected] |
Creates the object.
Creates the object.
Definition at line 53 of file file_system.cc.
References search_paths.
Referenced by create().
00053 : 00054 search_paths(lstrings_type::create()) 00055 { 00056 // an extra path starting from base 00057 search_paths->push_back(lstring("")); 00058 }
| lestes::lang::cplus::lex::file_system::file_system | ( | const file_system & | ) | [private] |
Hides copy constructor.
| ptr< named_istream > lestes::lang::cplus::lex::file_system::open_file | ( | const lstring & | name | ) |
Opens a file stream, using only working directory.
Opens a stream for the given file name or standard input. Relative file name is searched for in the working directory.
| name | Absolute or relative file name, "" used for stdin. |
Definition at line 135 of file file_system.cc.
References classify_name(), lestes::lang::cplus::lex::named_istream::create(), NM_FILE_ABS, and NM_FILE_REL.
Referenced by find_file().
00136 { 00137 if (name.length() == 0) { 00138 ptr<istream_wrapper> iw = istream_wrapper::create(&::std::cin,false); 00139 return named_istream::create(iw,"","<stdin>"); 00140 } 00141 00142 name_type nt = classify_name(name); 00143 00144 if (nt != NM_FILE_REL && nt != NM_FILE_ABS) 00145 return NULL; 00146 00147 fstream *f = new fstream(); 00148 bool error = false; 00149 try { 00150 f->open(name.c_str(),ios_base::in); 00151 error = f->fail(); 00152 } catch (...) { 00153 error = true; 00154 } 00155 00156 if (error) { 00157 delete f; 00158 return NULL; 00159 } 00160 00161 ptr<istream_wrapper> iw = istream_wrapper::create(f,true); 00162 00163 return named_istream::create(iw,name,name); 00164 }
| ptr< named_istream > lestes::lang::cplus::lex::file_system::find_file | ( | const lstring & | base_path, | |
| const lstring & | name, | |||
| bool | system | |||
| ) |
Opens a file stream, searching given paths.
Opens a stream for the given file name. Relative names are searched for in search paths if system is set to true, otherwise in the given path, and in the search paths in this order.
| base_path | Absolute or relative path for the search. | |
| name | Absolute or relative file name of the file to open. | |
| system | Flag set to true for system header. |
Definition at line 197 of file file_system.cc.
References classify_name(), lestes::lang::cplus::lex::named_istream::create(), lassert, NM_FILE_ABS, NM_FILE_REL, NM_PATH_ABS, NM_PATH_REL, open_file(), and search_paths.
00198 { 00199 name_type nt = classify_name(base_path); 00200 lassert(nt == NM_PATH_REL || nt == NM_PATH_ABS); 00201 00202 nt = classify_name(name); 00203 00204 if (nt == NM_FILE_ABS) 00205 return open_file(name); 00206 00207 if (nt != NM_FILE_REL) 00208 return NULL; 00209 00210 lstrings_type::iterator it = search_paths->begin(), end = search_paths->end(); 00211 00212 if (system) { 00213 // skip the placeholder 00214 ++it; 00215 } else { 00216 // extra entry to enable uniform processing 00217 (*search_paths)[0] = base_path; 00218 } 00219 00220 fstream *f = new fstream(); 00221 00222 for ( ; it != end; ++it) { 00223 00224 lstring full_name(*it); 00225 full_name.append(name); 00226 00227 bool error = false; 00228 00229 try { 00230 f->open(full_name.c_str(),ios_base::in); 00231 error = f->fail(); 00232 } catch (...) { 00233 error = true; 00234 } 00235 00236 if (!error) { 00237 ptr<istream_wrapper> iw = istream_wrapper::create(f,true); 00238 return named_istream::create(iw,full_name,full_name); 00239 } 00240 } 00241 00242 // the resource is not in use 00243 delete f; 00244 00245 // failure 00246 return NULL; 00247 }
| bool lestes::lang::cplus::lex::file_system::add_search_path | ( | const lstring & | a_path | ) |
Adds search path.
Adds search path for file searching.
| a_path | A path to add. |
Definition at line 254 of file file_system.cc.
References classify_name(), lassert2, NM_FILE_ABS, NM_FILE_REL, NM_INVALID, NM_PATH_ABS, NM_PATH_REL, and search_paths.
00255 { 00256 name_type nt = classify_name(a_path); 00257 lstring path(a_path); 00258 00259 switch (nt) { 00260 case NM_INVALID: 00261 return false; 00262 case NM_FILE_REL: 00263 case NM_FILE_ABS: 00264 path.append("/"); 00265 // fall through 00266 case NM_PATH_REL: 00267 case NM_PATH_ABS: 00268 search_paths->push_back(path); 00269 break; 00270 default: 00271 lassert2(false,"You should never get here"); 00272 } 00273 return true; 00274 }
| ptr< file_system > lestes::lang::cplus::lex::file_system::create | ( | void | ) | [static] |
Returns new instance.
Returns new instance.
Definition at line 289 of file file_system.cc.
References file_system().
Referenced by lestes::lang::cplus::syn::do_it().
00290 { 00291 return new file_system(); 00292 }
| file_system::name_type lestes::lang::cplus::lex::file_system::classify_name | ( | const lstring & | name | ) | [static] |
Classifies path and file name.
Classifies the name according to its properties. Distinguishes relative and absolute path and file names.
| str | The string to classify. |
Definition at line 106 of file file_system.cc.
References NM_FILE_ABS, NM_FILE_REL, NM_INVALID, NM_PATH_ABS, NM_PATH_REL, and split().
Referenced by add_search_path(), find_file(), and open_file().
00107 { 00108 ptr<lstrings_type> parts = split(name,'/'); 00109 ulint len = parts->size(); 00110 00111 if (!len) return NM_PATH_REL; 00112 00113 bool absolute = parts->at(0).length() == 0; 00114 bool pathname = parts->at(len - 1).length() == 0; 00115 00116 // check nonempty directory names 00117 for (ulint i = 1; i < len - 1; i++) { 00118 if (parts->at(i).length() == 0) 00119 return NM_INVALID; 00120 } 00121 00122 if (absolute) { 00123 return pathname ? NM_PATH_ABS : NM_FILE_ABS; 00124 } 00125 00126 return pathname ? NM_PATH_REL : NM_FILE_REL; 00127 }
| void lestes::lang::cplus::lex::file_system::gc_mark | ( | void | ) | [protected, virtual] |
Marks the object.
Marks the object.
Reimplemented from lestes::std::mem::keystone.
Definition at line 279 of file file_system.cc.
References lestes::std::mem::keystone::gc_mark(), and search_paths.
00280 { 00281 search_paths.gc_mark(); 00282 ::lestes::std::object::gc_mark(); 00283 }
| ptr< file_system::lstrings_type > lestes::lang::cplus::lex::file_system::split | ( | const lstring & | str, | |
| hchar | delimiter | |||
| ) | [static, private] |
Splits string into parts.
Splits string into sequence of parts separated by delimiter. If delimiter is found at the beginning, or at the end of the string, or two delimiters are adjacent, an empty string is added to the appropriate place. So empty string results in empty sequence, while string containing sole delimiter results in sequence containing two empty strings.
| str | The string to split. | |
| delimiter | The delimiter to separate the parts. |
Definition at line 70 of file file_system.cc.
References lestes::std::vector< T >::create().
Referenced by classify_name().
00071 { 00072 ptr< lstrings_type > v = lstrings_type::create(); 00073 00074 lstring::size_type len = str.length(); 00075 00076 // no data means no output 00077 if (!len) return v; 00078 00079 lstring::size_type i, j; 00080 00081 i = 0; 00082 00083 // the condition is set so that find is called even after 00084 // the delimiter was found at the end of the string 00085 while (i <= len) { 00086 j = str.find(delimiter,i); 00087 if (j == lstring::npos) 00088 // pretend delimiter just after the end 00089 j = len; 00090 00091 // the part starts at i and ends at j - 1 00092 v->push_back(str.substr(i,j-i)); 00093 // skip the delimiter 00094 i = j + 1; 00095 } 00096 00097 return v; 00098 }
| file_system& lestes::lang::cplus::lex::file_system::operator= | ( | const file_system & | ) | [private] |
Hides assignment operator.
srp<lstrings_type> lestes::lang::cplus::lex::file_system::search_paths [private] |
System search paths.
Definition at line 94 of file file_system.hh.
Referenced by add_search_path(), file_system(), find_file(), and gc_mark().
1.5.1-20070107