summaryrefslogtreecommitdiff
path: root/CodeLite/fc_fileopener.cpp
blob: 3e4e2ef99e163c29f7260bea191abe89a86d90aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 The CodeLite Team
// file name            : fc_fileopener.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "fc_fileopener.h"
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <wx/filename.h>
#include <wx/filefn.h>

#ifdef __WXMSW__
#define FC_PATH_SEP "\\"
#else
#define FC_PATH_SEP "/"
#endif

fcFileOpener* fcFileOpener::ms_instance = 0;

fcFileOpener::fcFileOpener()
    : _depth(0)
    , _maxDepth(20)
{
}

fcFileOpener::~fcFileOpener()
{
}

fcFileOpener* fcFileOpener::Get()
{
    if (ms_instance == 0) {
        ms_instance = new fcFileOpener();
    }
    return ms_instance;
}

void fcFileOpener::Release()
{
    if (ms_instance) {
        delete ms_instance;
    }
    ms_instance = 0;
}

void fcFileOpener::AddSearchPath(const wxString& path)
{
    wxFileName fn( path, "" );
    if ( !wxFileName::DirExists( fn.GetPath() ) )
        return;
    _searchPath.push_back( fn.GetPath() );
}

FILE* fcFileOpener::OpenFile(const wxString& include_path, wxString &filepath)
{
    filepath.Clear();
    if ( include_path.empty() ) {
        return NULL;
    }

    wxString mod_path ( include_path );

    static wxString trimString("\"<> \t");

    mod_path.erase(0, mod_path.find_first_not_of(trimString));
    mod_path.erase(mod_path.find_last_not_of    (trimString)+1);

    if ( _scannedfiles.find(mod_path) != _scannedfiles.end() ) {
        // we already scanned this file
        filepath.Clear();
        return NULL;
    }

    FILE *fp (NULL);
    
    // first try to cwd
    fp = try_open(_cwd, mod_path, filepath);
    if ( fp ) {
        return fp;
    }
    
    // Now try the search directories
    for (size_t i=0; i<_searchPath.size(); ++i) {
        fp = try_open(_searchPath.at(i), mod_path, filepath);
        if ( fp ) return fp;
    }
    
    _scannedfiles.insert( mod_path );
    filepath.Clear();
    return NULL;
}

FILE* fcFileOpener::try_open(const wxString &path, const wxString &name, wxString &filepath)
{
    wxString fullpath ( path + FC_PATH_SEP + name );
    wxFileName fn(fullpath);
    
    fullpath = fn.GetFullPath();
    FILE *fp = wxFopen(fullpath, "rb");
    if ( fp ) {

        _scannedfiles.insert( name );
        wxString pathPart = fn.GetPath();

        for(size_t i=0; i<_excludePaths.size(); ++i) {
            if ( pathPart.StartsWith(_excludePaths.at(i) ) ) {
                ::fclose( fp );
                return NULL;
            }
        }

        _matchedfiles.insert( fullpath );
        filepath = fullpath;
        return fp;
    }
    return NULL;
}

void fcFileOpener::AddExcludePath(const wxString& path)
{
    wxFileName fn( path, "" );
    if ( !wxFileName::DirExists( fn.GetPath() ) )
        return;
    _excludePaths.push_back( fn.GetPath() );
}

void fcFileOpener::AddNamespace(const char* ns)
{
    _namespaces.insert(ns);
}

void fcFileOpener::AddIncludeStatement(const wxString& statement)
{
    if( std::find(_includeStatements.begin(), _includeStatements.end(), statement) == _includeStatements.end()) {
        _includeStatements.push_back(statement);
    }
}

BufferState fcFileOpener::PopBufferState()
{
    if ( _states.empty() ) {
        return NULL;
    }
    
    fcState curstate = _states.back();
    BufferState state = curstate.buffer;
    
    // update the current directory
    _cwd = wxFileName(curstate.filename).GetPath();
    
    _states.pop_back();
    decDepth();
    return state;
}

void fcFileOpener::PushBufferState(BufferState buffer, const wxString &filename)
{
    fcState curstate;
    curstate.buffer = buffer;
    curstate.filename = filename;
    _states.push_back( curstate );
    
    // update the current directory
    _cwd = wxFileName(curstate.filename).GetPath();
    incDepth();
}