| 1 | #include "fc_fileopener.h" |
| 2 | #include <cstdio> |
| 3 | #include <cctype> |
| 4 | #include <algorithm> |
| 5 | #include <wx/filename.h> |
| 6 | #include <wx/filefn.h> |
| 7 | |
| 8 | #ifdef __WXMSW__ |
| 9 | #define FC_PATH_SEP "\\" |
| 10 | #else |
| 11 | #define FC_PATH_SEP "/" |
| 12 | #endif |
| 13 | |
| 14 | fcFileOpener* fcFileOpener::ms_instance = 0; |
| 15 | |
| 16 | fcFileOpener::fcFileOpener() |
| 17 | : _depth(0) |
| 18 | , _maxDepth(20) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | fcFileOpener::~fcFileOpener() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | fcFileOpener* fcFileOpener::Get() |
| 27 | { |
| 28 | if (ms_instance == 0) { |
| 29 | ms_instance = new fcFileOpener(); |
| 30 | } |
| 31 | return ms_instance; |
| 32 | } |
| 33 | |
| 34 | void fcFileOpener::Release() |
| 35 | { |
| 36 | if (ms_instance) { |
| 37 | delete ms_instance; |
| 38 | } |
| 39 | ms_instance = 0; |
| 40 | } |
| 41 | |
| 42 | void fcFileOpener::AddSearchPath(const wxString& path) |
| 43 | { |
| 44 | wxFileName fn( path, "" ); |
| 45 | if ( !wxFileName::DirExists( fn.GetPath() ) ) |
| 46 | return; |
| 47 | _searchPath.push_back( fn.GetPath() ); |
| 48 | } |
| 49 | |
| 50 | FILE* fcFileOpener::OpenFile(const wxString& include_path, wxString &filepath) |
| 51 | { |
| 52 | filepath.Clear(); |
| 53 | if ( include_path.empty() ) { |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | wxString mod_path ( include_path ); |
| 58 | |
| 59 | static wxString trimString("\"<> \t"); |
| 60 | |
| 61 | mod_path.erase(0, mod_path.find_first_not_of(trimString)); |
| 62 | mod_path.erase(mod_path.find_last_not_of (trimString)+1); |
| 63 | |
| 64 | if ( _scannedfiles.find(mod_path) != _scannedfiles.end() ) { |
| 65 | // we already scanned this file |
| 66 | filepath.Clear(); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | FILE *fp (NULL); |
| 71 | |
| 72 | // first try to cwd |
| 73 | fp = try_open(_cwd, mod_path, filepath); |
| 74 | if ( fp ) { |
| 75 | return fp; |
| 76 | } |
| 77 | |
| 78 | // Now try the search directories |
| 79 | for (size_t i=0; i<_searchPath.size(); ++i) { |
| 80 | fp = try_open(_searchPath.at(i), mod_path, filepath); |
| 81 | if ( fp ) return fp; |
| 82 | } |
| 83 | |
| 84 | _scannedfiles.insert( mod_path ); |
| 85 | filepath.Clear(); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | FILE* fcFileOpener::try_open(const wxString &path, const wxString &name, wxString &filepath) |
| 90 | { |
| 91 | wxString fullpath ( path + FC_PATH_SEP + name ); |
| 92 | wxFileName fn(fullpath); |
| 93 | |
| 94 | fullpath = fn.GetFullPath(); |
| 95 | FILE *fp = wxFopen(fullpath, "rb"); |
| 96 | if ( fp ) { |
| 97 | |
| 98 | _scannedfiles.insert( name ); |
| 99 | wxString pathPart = fn.GetPath(); |
| 100 | |
| 101 | for(size_t i=0; i<_excludePaths.size(); ++i) { |
| 102 | if ( pathPart.StartsWith(_excludePaths.at(i) ) ) { |
| 103 | ::fclose( fp ); |
| 104 | return NULL; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | _matchedfiles.insert( fullpath ); |
| 109 | filepath = fullpath; |
| 110 | return fp; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | void fcFileOpener::AddExcludePath(const wxString& path) |
| 116 | { |
| 117 | wxFileName fn( path, "" ); |
| 118 | if ( !wxFileName::DirExists( fn.GetPath() ) ) |
| 119 | return; |
| 120 | _excludePaths.push_back( fn.GetPath() ); |
| 121 | } |
| 122 | |
| 123 | void fcFileOpener::AddNamespace(const char* ns) |
| 124 | { |
| 125 | _namespaces.insert(ns); |
| 126 | } |
| 127 | |
| 128 | void fcFileOpener::AddIncludeStatement(const wxString& statement) |
| 129 | { |
| 130 | if( std::find(_includeStatements.begin(), _includeStatements.end(), statement) == _includeStatements.end()) { |
| 131 | _includeStatements.push_back(statement); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | BufferState fcFileOpener::PopBufferState() |
| 136 | { |
| 137 | if ( _states.empty() ) { |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | fcState curstate = _states.back(); |
| 142 | BufferState state = curstate.buffer; |
| 143 | |
| 144 | // update the current directory |
| 145 | _cwd = wxFileName(curstate.filename).GetPath(); |
| 146 | |
| 147 | _states.pop_back(); |
| 148 | decDepth(); |
| 149 | return state; |
| 150 | } |
| 151 | |
| 152 | void fcFileOpener::PushBufferState(BufferState buffer, const wxString &filename) |
| 153 | { |
| 154 | fcState curstate; |
| 155 | curstate.buffer = buffer; |
| 156 | curstate.filename = filename; |
| 157 | _states.push_back( curstate ); |
| 158 | |
| 159 | // update the current directory |
| 160 | _cwd = wxFileName(curstate.filename).GetPath(); |
| 161 | incDepth(); |
| 162 | } |