File: fileextmanager.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136,244 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (407 lines) | stat: -rw-r--r-- 14,346 bytes parent folder | download | duplicates (2)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : fileextmanager.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 "fileextmanager.h"

#include "JSON.h"
#include "file_logger.h"
#include "fileutils.h"

#include <wx/filename.h>
#include <wx/regex.h>
#include <wx/thread.h>
#include <wx/tokenzr.h>
#include <wx/xml/xml.h>

struct Matcher {
    SmartPtr<wxRegEx> m_regex;
    wxString m_exactMatch;
    FileExtManager::FileType m_fileType;

    Matcher(const wxString& pattern, FileExtManager::FileType fileType, bool regex = true)
        : m_fileType(fileType)
    {
        if(regex) {
            m_regex = new wxRegEx(pattern, wxRE_ADVANCED | wxRE_ICASE);
        } else {
            m_exactMatch = pattern;
        }
    }

    bool Matches(const wxString& in) const
    {
        auto lines = ::wxStringTokenize(in, "\r\n", wxTOKEN_STRTOK);
        bool use_regex = m_regex;
        for(const auto& line : lines) {
            if(use_regex && m_regex->Matches(line)) {
                return true;
            } else if(!use_regex && line.Find(m_exactMatch) != wxNOT_FOUND) {
                return true;
            }
        }
        return false;
    }
};
namespace
{
std::unordered_map<wxString, FileExtManager::FileType> m_map;
std::unordered_map<wxString, std::vector<FileExtManager::FileType>> m_language_bundle;
std::unordered_map<int, wxString> m_file_type_to_lang;
std::vector<Matcher> m_matchers;
bool init_done = false;
}; // namespace

void FileExtManager::Init()
{
    if(!init_done) {
        init_done = true;
        m_map[wxT("cc")] = TypeSourceCpp;
        m_map[wxT("cpp")] = TypeSourceCpp;
        m_map[wxT("cxx")] = TypeSourceCpp;
        m_map[wxT("c++")] = TypeSourceCpp;
        m_map[wxT("as")] = TypeSourceCpp;  // AngelScript files are handled as C++ source files in CodeLite
        m_map[wxT("ino")] = TypeSourceCpp; // Arduino sketches

        m_map[wxT("c")] = TypeSourceC;
        m_map[wxT("h")] = TypeHeader;
        m_map[wxT("hpp")] = TypeHeader;
        m_map[wxT("hxx")] = TypeHeader;
        m_map[wxT("hh")] = TypeHeader;
        m_map[wxT("h++")] = TypeHeader;
        m_map[wxT("inl")] = TypeHeader;

        m_map[wxT("rc")] = TypeResource;
        m_map[wxT("res")] = TypeResource;

        m_map[wxT("y")] = TypeYacc;

        m_map[wxT("l")] = TypeLex;

        m_map[wxT("ui")] = TypeQtForm;
        m_map[wxT("qrc")] = TypeQtResource;
        m_map[wxT("qml")] = TypeJS;

        m_map[wxT("project")] = TypeProject;
        m_map[wxT("workspace")] = TypeWorkspace;
        m_map[wxT("fbp")] = TypeFormbuilder;

        m_map[wxT("cdp")] = TypeCodedesigner;
        m_map[wxT("erd")] = TypeErd;

        m_map[wxT("php")] = TypePhp;
        m_map[wxT("php5")] = TypePhp;
        m_map[wxT("inc")] = TypePhp;
        m_map[wxT("phtml")] = TypePhp;
        m_map[wxT("ctp")] = TypePhp;

        m_map[wxT("xml")] = TypeXml;
        m_map[wxT("xrc")] = TypeXRC;
        m_map[wxT("css")] = TypeCSS;
        m_map[wxT("less")] = TypeCSS;
        m_map[wxT("sass")] = TypeCSS;
        m_map[wxT("js")] = TypeJS;
        m_map[wxT("javascript")] = TypeJS;
        m_map[wxT("ts")] = TypeJS; // TypeScript, but we consider this a JavaScript
        m_map[wxT("py")] = TypePython;
        m_map["json"] = TypeJSON;
        m_map["conf"] = TypeJSON; // CodeLite configuration files are marked as "conf"

        // Java file
        m_map[wxT("java")] = TypeJava;

        m_map[wxT("exe")] = TypeExe;
        m_map[wxT("html")] = TypeHtml;
        m_map[wxT("htm")] = TypeHtml;

        m_map[wxT("tar")] = TypeArchive;
        m_map[wxT("a")] = TypeArchive;
        m_map[wxT("lib")] = TypeArchive;
        m_map[wxT("zip")] = TypeArchive;
        m_map[wxT("rar")] = TypeArchive;
        m_map[wxT("targz")] = TypeArchive;

        m_map[wxT("dll")] = TypeDll;
        m_map[wxT("so")] = TypeDll;
        m_map[wxT("dylib")] = TypeDll;

        m_map[wxT("bmp")] = TypeBmp;
        m_map[wxT("jpeg")] = TypeBmp;
        m_map[wxT("jpg")] = TypeBmp;
        m_map[wxT("png")] = TypeBmp;
        m_map[wxT("ico")] = TypeBmp;
        m_map[wxT("xpm")] = TypeBmp;
        m_map[wxT("svg")] = TypeSvg;

        m_map[wxT("mk")] = TypeMakefile;

        m_map[wxT("log")] = TypeText;
        m_map[wxT("txt")] = TypeText;
        m_map[wxT("ini")] = TypeText;

        m_map[wxT("script")] = TypeShellScript;
        m_map[wxT("sh")] = TypeShellScript;
        m_map[wxT("bat")] = TypeShellScript;
        m_map[wxT("bash")] = TypeShellScript;

        m_map[wxT("wxcp")] = TypeWxCrafter;
        m_map[wxT("xrc")] = TypeXRC;

        m_map[wxT("sql")] = TypeSQL;
        m_map[wxT("sqlite")] = TypeSQL;
        m_map[wxT("phpwsp")] = TypeWorkspacePHP;
        m_map[wxT("phptags")] = TypeWorkspacePHPTags;

        m_map["pro"] = TypeQMake;
        m_map["pri"] = TypeQMake;
        m_map["cmake"] = TypeCMake;
        m_map["s"] = TypeAsm;
        m_map["yaml"] = TypeYAML;
        m_map["yml"] = TypeYAML;
        m_map["clangd"] = TypeYAML;
        m_map["db"] = TypeDatabase;
        m_map["tags"] = TypeDatabase;
        m_map["lua"] = TypeLua;
        m_map["rs"] = TypeRust;

        m_map["patch"] = TypePatch;
        m_map["diff"] = TypeDiff;

        m_map["rb"] = TypeRuby;
        m_map["md"] = TypeMarkdown;
        m_map["dart"] = TypeDart;

        m_language_bundle.insert({ "C/C++", { TypeSourceCpp, TypeSourceC, TypeHeader } });
        m_language_bundle.insert({ "Windows resource files", { TypeResource } });
        m_language_bundle.insert({ "Yacc", { TypeYacc } });
        m_language_bundle.insert({ "Lex", { TypeLex } });
        m_language_bundle.insert({ "Xml", { TypeProject, TypeWorkspace, TypeXml, TypeXRC, TypeSvg, TypeFormbuilder } });
        m_language_bundle.insert({ "Yaml", { TypeYAML } });
        m_language_bundle.insert({ "Json",
                                   { TypeJSON, TypeWorkspaceFileSystem, TypeWxCrafter, TypeWorkspaceDocker,
                                     TypeWorkspaceNodeJS, TypeWorkspacePHP } });
        m_language_bundle.insert({ "Rust", { TypeRust } });
        m_language_bundle.insert({ "Ruby", { TypeRuby } });
        m_language_bundle.insert({ "Shell script", { TypeShellScript } });
        m_language_bundle.insert({ "Java", { TypeJava } });
        m_language_bundle.insert({ "Javascript/Typescript", { TypeJS } });
        m_language_bundle.insert({ "Python", { TypePython } });
        m_language_bundle.insert({ "PHP", { TypePhp } });
        m_language_bundle.insert({ "CMake", { TypeCMake } });

        // build the reverse search table: file type -> language
        for(auto vt : m_language_bundle) {
            for(auto t : vt.second) {
                m_file_type_to_lang.insert({ (int)t, vt.first });
            }
        }

        // Initialize regexes:
        m_matchers.push_back(Matcher("#[ \t]*!(.*?)sh", TypeShellScript));
        m_matchers.push_back(Matcher("#[ \t]*!(.*?)python", TypePython));
        m_matchers.push_back(Matcher("#[ \t]*!(.*?)ruby", TypeRuby));
        m_matchers.push_back(Matcher("#[ \t]*!(.*?)node", TypeJS));
        m_matchers.push_back(Matcher("<?xml", TypeXml, false));
        m_matchers.push_back(Matcher("<?php", TypePhp, false));
        m_matchers.push_back(Matcher("SQLite format 3", TypeDatabase, false));

        // STL sources places "-*- C++ -*-" at the top of their headers
        m_matchers.push_back(Matcher("-*- C++ -*-", TypeSourceCpp, false));

        // #ifndef WORD
        m_matchers.push_back(Matcher("#ifndef[ \t]+[a-zA-Z0-9_]+", TypeSourceCpp));

        // vim modlines
        m_matchers.push_back(Matcher("/\\* \\-\\*\\- Mode:[ \t]+c\\+\\+", TypeSourceCpp));
        m_matchers.push_back(Matcher("# \\-\\*\\- Mode:[ \t]+python", TypePython));

        // #include <
        m_matchers.push_back(Matcher("#include[ \t]+[\\<\"]", TypeSourceCpp));
    }
}
std::unordered_map<wxString, FileExtManager::FileType> FileExtManager::GetAllSupportedFileTypes()
{
    Init();
    return m_map;
}

std::unordered_map<wxString, std::vector<FileExtManager::FileType>> FileExtManager::GetLanguageBundles()
{
    Init();
    return m_language_bundle;
}

FileExtManager::FileType FileExtManager::GetType(const wxString& filename, FileExtManager::FileType defaultType)
{
    Init();

    wxFileName fn(filename);
    if(fn.IsOk() == false) {
        return defaultType;
    }

    wxString e(fn.GetExt());
    e.MakeLower();
    e.Trim().Trim(false);

    auto iter = m_map.find(e);
    if(iter == m_map.end()) {
        // try to see if the file is a makefile
        if(fn.GetFullName().CmpNoCase(wxT("makefile")) == 0) {
            return TypeMakefile;
        } else if(fn.GetFullName().Lower() == "dockerfile") {
            return TypeDockerfile;
        } else if(fn.GetFullName().CmpNoCase("README") == 0) {
            return TypeMarkdown;
        } else if(fn.GetFullName().CmpNoCase(".clangd") == 0) {
            return TypeYAML;
        } else {
            // try auto detecting
            FileType autoDetectType = defaultType;
            if(AutoDetectByContent(filename, autoDetectType)) {
                return autoDetectType;
            }
        }
        return defaultType;
    } else if((iter->second == TypeText) && (fn.GetFullName().CmpNoCase("CMakeLists.txt") == 0)) {
        return TypeCMake;
    }

    FileExtManager::FileType type = iter->second;
    if(fn.Exists() && (type == TypeWorkspace)) {
        wxString content;
        if(FileUtils::ReadFileContent(fn, content)) {
            if(content.Contains("<CodeLite_Workspace")) {
                return TypeWorkspace;
            } else {
                JSON root(content);
                if(!root.isOk())
                    return TypeWorkspace;
                if(root.toElement().hasNamedObject("NodeJS")) {
                    return TypeWorkspaceNodeJS;
                } else if(root.toElement().hasNamedObject("Docker")) {
                    return TypeWorkspaceDocker;
                } else if(root.toElement().namedObject("workspace_type").toString() == "File System Workspace") {
                    return TypeWorkspaceFileSystem;
                } else if(root.toElement().namedObject("metadata").namedObject("type").toString() == "php") {
                    return TypeWorkspacePHP;
                } else {
                    return TypeWorkspace;
                }
            }
        } else {
            return TypeWorkspace;
        }
    }
    return iter->second;
}

bool FileExtManager::IsCxxFile(const wxString& filename)
{

    FileType ft = GetType(filename);
    if(ft == TypeOther) {
        // failed to detect the type
        if(!AutoDetectByContent(filename, ft)) {
            return false;
        }
    }
    return (ft == TypeSourceC) || (ft == TypeSourceCpp) || (ft == TypeHeader);
}

bool FileExtManager::AutoDetectByContent(const wxString& filename, FileExtManager::FileType& fileType)
{
    wxString fileContent;
    if(!FileUtils::ReadBufferFromFile(filename, fileContent, 1024)) {
        clWARNING() << "Failed to read file's content" << endl;
        return false;
    }
    return GetContentType(fileContent, fileType);
}

bool FileExtManager::GetContentType(const wxString& string_content, FileExtManager::FileType& fileType)
{
    for(size_t i = 0; i < m_matchers.size(); ++i) {
        if(m_matchers[i].Matches(string_content)) {
            LOG_IF_TRACE
            {
                if(m_matchers[i].m_regex) {
                    clDEBUG1() << "Matching part is:" << m_matchers[i].m_regex->GetMatch(string_content, 0) << endl;
                }
            }
            fileType = m_matchers[i].m_fileType;
            return true;
        }
    }
    return false;
}

bool FileExtManager::IsFileType(const wxString& filename, FileExtManager::FileType type)
{

    FileType ft = GetType(filename);
    if(ft == TypeOther) {
        // failed to detect the type
        if(!AutoDetectByContent(filename, ft)) {
            return false;
        }
    }
    return (ft == type);
}

bool FileExtManager::IsJavascriptFile(const wxString& filename) { return FileExtManager::IsFileType(filename, TypeJS); }

bool FileExtManager::IsPHPFile(const wxString& filename) { return FileExtManager::IsFileType(filename, TypePhp); }

bool FileExtManager::IsJavaFile(const wxString& filename) { return FileExtManager::IsFileType(filename, TypeJava); }

FileExtManager::FileType FileExtManager::GetTypeFromExtension(const wxFileName& filename)
{

    std::unordered_map<wxString, FileExtManager::FileType>::iterator iter = m_map.find(filename.GetExt().Lower());
    if(iter == m_map.end())
        return TypeOther;
    return iter->second;
}

bool FileExtManager::IsSymlinkFile(const wxString& filename)
{
    return wxFileName::Exists(filename, wxFILE_EXISTS_NO_FOLLOW | wxFILE_EXISTS_SYMLINK) &&
           wxFileName::FileExists(filename);
}

bool FileExtManager::IsSymlinkFolder(const wxString& filename)
{
    return wxFileName::Exists(filename, wxFILE_EXISTS_NO_FOLLOW | wxFILE_EXISTS_SYMLINK) &&
           wxFileName::DirExists(filename);
}

wxString FileExtManager::GetLanguageFromType(FileExtManager::FileType file_type)
{
    if(m_file_type_to_lang.count((int)file_type) == 0) {
        return wxEmptyString;
    }

    return m_file_type_to_lang.find((int)file_type)->second;
}