| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : fc_fileopener.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include "fc_fileopener.h" |
| 27 |
#include <cstdio> |
| 28 |
#include <cctype> |
| 29 |
#include <algorithm> |
| 30 |
#include <wx/filename.h> |
| 31 |
#include <wx/filefn.h> |
| 32 |
|
| 33 |
#ifdef __WXMSW__ |
| 34 |
#define FC_PATH_SEP "\\" |
| 35 |
#else |
| 36 |
#define FC_PATH_SEP "/" |
| 37 |
#endif |
| 38 |
|
| 39 |
fcFileOpener* fcFileOpener::ms_instance = 0; |
| 40 |
|
| 41 |
fcFileOpener::fcFileOpener() |
| 42 |
: _depth(0) |
| 43 |
, _maxDepth(20) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
fcFileOpener::~fcFileOpener() {} |
| 48 |
|
| 49 |
fcFileOpener* fcFileOpener::Get() |
| 50 |
{ |
| 51 |
if(ms_instance == 0) { |
| 52 |
ms_instance = new fcFileOpener(); |
| 53 |
} |
| 54 |
return ms_instance; |
| 55 |
} |
| 56 |
|
| 57 |
void fcFileOpener::Release() |
| 58 |
{ |
| 59 |
if(ms_instance) { |
| 60 |
delete ms_instance; |
| 61 |
} |
| 62 |
ms_instance = 0; |
| 63 |
} |
| 64 |
|
| 65 |
void fcFileOpener::AddSearchPath(const wxString& path) |
| 66 |
{ |
| 67 |
wxFileName fn(path, ""); |
| 68 |
if(!wxFileName::DirExists(fn.GetPath())) return; |
| 69 |
_searchPath.push_back(fn.GetPath()); |
| 70 |
} |
| 71 |
|
| 72 |
FILE* fcFileOpener::OpenFile(const wxString& include_path, wxString& filepath) |
| 73 |
{ |
| 74 |
filepath.Clear(); |
| 75 |
if(include_path.empty()) { |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
wxString mod_path(include_path); |
| 80 |
|
| 81 |
static wxString trimString("\"<> \t"); |
| 82 |
|
| 83 |
mod_path.erase(0, mod_path.find_first_not_of(trimString)); |
| 84 |
mod_path.erase(mod_path.find_last_not_of(trimString) + 1); |
| 85 |
|
| 86 |
if(_scannedfiles.count(mod_path)) { |
| 87 |
// we already scanned this file |
| 88 |
filepath.Clear(); |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
|
| 92 |
FILE* fp(NULL); |
| 93 |
|
| 94 |
// first try to cwd |
| 95 |
fp = try_open(_cwd, mod_path, filepath); |
| 96 |
if(fp) { |
| 97 |
return fp; |
| 98 |
} |
| 99 |
|
| 100 |
// Now try the search directories |
| 101 |
for(size_t i = 0; i < _searchPath.size(); ++i) { |
| 102 |
fp = try_open(_searchPath.at(i), mod_path, filepath); |
| 103 |
if(fp) return fp; |
| 104 |
} |
| 105 |
|
| 106 |
_scannedfiles.insert(mod_path); |
| 107 |
filepath.Clear(); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
FILE* fcFileOpener::try_open(const wxString& path, const wxString& name, wxString& filepath) |
| 112 |
{ |
| 113 |
wxString fullpath(path + FC_PATH_SEP + name); |
| 114 |
wxFileName fn(fullpath); |
| 115 |
|
| 116 |
fullpath = fn.GetFullPath(); |
| 117 |
FILE* fp = wxFopen(fullpath, "rb"); |
| 118 |
if(fp) { |
| 119 |
|
| 120 |
_scannedfiles.insert(name); |
| 121 |
wxString pathPart = fn.GetPath(); |
| 122 |
|
| 123 |
for(size_t i = 0; i < _excludePaths.size(); ++i) { |
| 124 |
if(pathPart.StartsWith(_excludePaths.at(i))) { |
| 125 |
::fclose(fp); |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
_matchedfiles.insert(fullpath); |
| 131 |
filepath = fullpath; |
| 132 |
return fp; |
| 133 |
} |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
void fcFileOpener::AddExcludePath(const wxString& path) |
| 138 |
{ |
| 139 |
wxFileName fn(path, ""); |
| 140 |
if(!wxFileName::DirExists(fn.GetPath())) return; |
| 141 |
_excludePaths.push_back(fn.GetPath()); |
| 142 |
} |
| 143 |
|
| 144 |
void fcFileOpener::AddNamespace(const char* ns) { _namespaces.insert(ns); } |
| 145 |
|
| 146 |
void fcFileOpener::AddIncludeStatement(const wxString& statement) |
| 147 |
{ |
| 148 |
if(_includeStatements.count(statement) == 0) { |
| 149 |
_includeStatements.insert(statement); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
BufferState fcFileOpener::PopBufferState() |
| 154 |
{ |
| 155 |
if(_states.empty()) { |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
fcState curstate = _states.top(); |
| 160 |
BufferState state = curstate.buffer; |
| 161 |
|
| 162 |
// update the current directory |
| 163 |
_cwd = wxFileName(curstate.filename).GetPath(); |
| 164 |
|
| 165 |
_states.pop(); |
| 166 |
decDepth(); |
| 167 |
return state; |
| 168 |
} |
| 169 |
|
| 170 |
void fcFileOpener::PushBufferState(BufferState buffer, const wxString& filename) |
| 171 |
{ |
| 172 |
fcState curstate; |
| 173 |
curstate.buffer = buffer; |
| 174 |
curstate.filename = filename; |
| 175 |
_states.push(curstate); |
| 176 |
|
| 177 |
// update the current directory |
| 178 |
_cwd = wxFileName(curstate.filename).GetPath(); |
| 179 |
incDepth(); |
| 180 |
} |