| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : fileextmanager.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 "fileextmanager.h" |
| 27 |
|
| 28 |
#include "JSON.h" |
| 29 |
#include "file_logger.h" |
| 30 |
#include "fileutils.h" |
| 31 |
|
| 32 |
#include <wx/filename.h> |
| 33 |
#include <wx/regex.h> |
| 34 |
#include <wx/thread.h> |
| 35 |
#include <wx/tokenzr.h> |
| 36 |
#include <wx/xml/xml.h> |
| 37 |
|
| 38 |
struct Matcher { |
| 39 |
SmartPtr<wxRegEx> m_regex; |
| 40 |
wxString m_exactMatch; |
| 41 |
FileExtManager::FileType m_fileType; |
| 42 |
|
| 43 |
Matcher(const wxString& pattern, FileExtManager::FileType fileType, bool regex = true) |
| 44 |
: m_fileType(fileType) |
| 45 |
{ |
| 46 |
if(regex) { |
| 47 |
m_regex = new wxRegEx(pattern, wxRE_ADVANCED | wxRE_ICASE); |
| 48 |
} else { |
| 49 |
m_exactMatch = pattern; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
bool Matches(const wxString& in) const |
| 54 |
{ |
| 55 |
auto lines = ::wxStringTokenize(in, "\r\n", wxTOKEN_STRTOK); |
| 56 |
bool use_regex = m_regex; |
| 57 |
for(const auto& line : lines) { |
| 58 |
if(use_regex && m_regex->Matches(line)) { |
| 59 |
return true; |
| 60 |
} else if(!use_regex && line.Find(m_exactMatch) != wxNOT_FOUND) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
} |
| 64 |
return false; |
| 65 |
} |
| 66 |
}; |
| 67 |
namespace |
| 68 |
{ |
| 69 |
std::unordered_map<wxString, FileExtManager::FileType> m_map; |
| 70 |
std::unordered_map<wxString, std::vector<FileExtManager::FileType>> m_language_bundle; |
| 71 |
std::unordered_map<int, wxString> m_file_type_to_lang; |
| 72 |
std::vector<Matcher> m_matchers; |
| 73 |
bool init_done = false; |
| 74 |
}; // namespace |
| 75 |
|
| 76 |
void FileExtManager::Init() |
| 77 |
{ |
| 78 |
if(!init_done) { |
| 79 |
init_done = true; |
| 80 |
m_map[wxT("cc")] = TypeSourceCpp; |
| 81 |
m_map[wxT("cpp")] = TypeSourceCpp; |
| 82 |
m_map[wxT("cxx")] = TypeSourceCpp; |
| 83 |
m_map[wxT("c++")] = TypeSourceCpp; |
| 84 |
m_map[wxT("as")] = TypeSourceCpp; // AngelScript files are handled as C++ source files in CodeLite |
| 85 |
m_map[wxT("ino")] = TypeSourceCpp; // Arduino sketches |
| 86 |
|
| 87 |
m_map[wxT("c")] = TypeSourceC; |
| 88 |
m_map[wxT("h")] = TypeHeader; |
| 89 |
m_map[wxT("hpp")] = TypeHeader; |
| 90 |
m_map[wxT("hxx")] = TypeHeader; |
| 91 |
m_map[wxT("hh")] = TypeHeader; |
| 92 |
m_map[wxT("h++")] = TypeHeader; |
| 93 |
m_map[wxT("inl")] = TypeHeader; |
| 94 |
|
| 95 |
m_map[wxT("rc")] = TypeResource; |
| 96 |
m_map[wxT("res")] = TypeResource; |
| 97 |
|
| 98 |
m_map[wxT("y")] = TypeYacc; |
| 99 |
|
| 100 |
m_map[wxT("l")] = TypeLex; |
| 101 |
|
| 102 |
m_map[wxT("ui")] = TypeQtForm; |
| 103 |
m_map[wxT("qrc")] = TypeQtResource; |
| 104 |
m_map[wxT("qml")] = TypeJS; |
| 105 |
|
| 106 |
m_map[wxT("project")] = TypeProject; |
| 107 |
m_map[wxT("workspace")] = TypeWorkspace; |
| 108 |
m_map[wxT("fbp")] = TypeFormbuilder; |
| 109 |
|
| 110 |
m_map[wxT("cdp")] = TypeCodedesigner; |
| 111 |
m_map[wxT("erd")] = TypeErd; |
| 112 |
|
| 113 |
m_map[wxT("php")] = TypePhp; |
| 114 |
m_map[wxT("php5")] = TypePhp; |
| 115 |
m_map[wxT("inc")] = TypePhp; |
| 116 |
m_map[wxT("phtml")] = TypePhp; |
| 117 |
m_map[wxT("ctp")] = TypePhp; |
| 118 |
|
| 119 |
m_map[wxT("xml")] = TypeXml; |
| 120 |
m_map[wxT("xrc")] = TypeXRC; |
| 121 |
m_map[wxT("css")] = TypeCSS; |
| 122 |
m_map[wxT("less")] = TypeCSS; |
| 123 |
m_map[wxT("sass")] = TypeCSS; |
| 124 |
m_map[wxT("js")] = TypeJS; |
| 125 |
m_map[wxT("javascript")] = TypeJS; |
| 126 |
m_map[wxT("ts")] = TypeJS; // TypeScript, but we consider this a JavaScript |
| 127 |
m_map[wxT("py")] = TypePython; |
| 128 |
m_map["json"] = TypeJSON; |
| 129 |
m_map["conf"] = TypeJSON; // CodeLite configuration files are marked as "conf" |
| 130 |
|
| 131 |
// Java file |
| 132 |
m_map[wxT("java")] = TypeJava; |
| 133 |
|
| 134 |
m_map[wxT("exe")] = TypeExe; |
| 135 |
m_map[wxT("html")] = TypeHtml; |
| 136 |
m_map[wxT("htm")] = TypeHtml; |
| 137 |
|
| 138 |
m_map[wxT("tar")] = TypeArchive; |
| 139 |
m_map[wxT("a")] = TypeArchive; |
| 140 |
m_map[wxT("lib")] = TypeArchive; |
| 141 |
m_map[wxT("zip")] = TypeArchive; |
| 142 |
m_map[wxT("rar")] = TypeArchive; |
| 143 |
m_map[wxT("targz")] = TypeArchive; |
| 144 |
|
| 145 |
m_map[wxT("dll")] = TypeDll; |
| 146 |
m_map[wxT("so")] = TypeDll; |
| 147 |
m_map[wxT("dylib")] = TypeDll; |
| 148 |
|
| 149 |
m_map[wxT("bmp")] = TypeBmp; |
| 150 |
m_map[wxT("jpeg")] = TypeBmp; |
| 151 |
m_map[wxT("jpg")] = TypeBmp; |
| 152 |
m_map[wxT("png")] = TypeBmp; |
| 153 |
m_map[wxT("ico")] = TypeBmp; |
| 154 |
m_map[wxT("xpm")] = TypeBmp; |
| 155 |
m_map[wxT("svg")] = TypeSvg; |
| 156 |
|
| 157 |
m_map[wxT("mk")] = TypeMakefile; |
| 158 |
|
| 159 |
m_map[wxT("log")] = TypeText; |
| 160 |
m_map[wxT("txt")] = TypeText; |
| 161 |
m_map[wxT("ini")] = TypeText; |
| 162 |
|
| 163 |
m_map[wxT("script")] = TypeShellScript; |
| 164 |
m_map[wxT("sh")] = TypeShellScript; |
| 165 |
m_map[wxT("bat")] = TypeShellScript; |
| 166 |
m_map[wxT("bash")] = TypeShellScript; |
| 167 |
|
| 168 |
m_map[wxT("wxcp")] = TypeWxCrafter; |
| 169 |
m_map[wxT("xrc")] = TypeXRC; |
| 170 |
|
| 171 |
m_map[wxT("sql")] = TypeSQL; |
| 172 |
m_map[wxT("sqlite")] = TypeSQL; |
| 173 |
m_map[wxT("phpwsp")] = TypeWorkspacePHP; |
| 174 |
m_map[wxT("phptags")] = TypeWorkspacePHPTags; |
| 175 |
|
| 176 |
m_map["pro"] = TypeQMake; |
| 177 |
m_map["pri"] = TypeQMake; |
| 178 |
m_map["cmake"] = TypeCMake; |
| 179 |
m_map["s"] = TypeAsm; |
| 180 |
m_map["yaml"] = TypeYAML; |
| 181 |
m_map["yml"] = TypeYAML; |
| 182 |
m_map["clangd"] = TypeYAML; |
| 183 |
m_map["db"] = TypeDatabase; |
| 184 |
m_map["tags"] = TypeDatabase; |
| 185 |
m_map["lua"] = TypeLua; |
| 186 |
m_map["rs"] = TypeRust; |
| 187 |
|
| 188 |
m_map["patch"] = TypePatch; |
| 189 |
m_map["diff"] = TypeDiff; |
| 190 |
|
| 191 |
m_map["rb"] = TypeRuby; |
| 192 |
m_map["md"] = TypeMarkdown; |
| 193 |
m_map["dart"] = TypeDart; |
| 194 |
|
| 195 |
m_language_bundle.insert({ "C/C++", { TypeSourceCpp, TypeSourceC, TypeHeader } }); |
| 196 |
m_language_bundle.insert({ "Windows resource files", { TypeResource } }); |
| 197 |
m_language_bundle.insert({ "Yacc", { TypeYacc } }); |
| 198 |
m_language_bundle.insert({ "Lex", { TypeLex } }); |
| 199 |
m_language_bundle.insert({ "Xml", { TypeProject, TypeWorkspace, TypeXml, TypeXRC, TypeSvg, TypeFormbuilder } }); |
| 200 |
m_language_bundle.insert({ "Yaml", { TypeYAML } }); |
| 201 |
m_language_bundle.insert({ "Json", |
| 202 |
{ TypeJSON, TypeWorkspaceFileSystem, TypeWxCrafter, TypeWorkspaceDocker, |
| 203 |
TypeWorkspaceNodeJS, TypeWorkspacePHP } }); |
| 204 |
m_language_bundle.insert({ "Rust", { TypeRust } }); |
| 205 |
m_language_bundle.insert({ "Ruby", { TypeRuby } }); |
| 206 |
m_language_bundle.insert({ "Shell script", { TypeShellScript } }); |
| 207 |
m_language_bundle.insert({ "Java", { TypeJava } }); |
| 208 |
m_language_bundle.insert({ "Javascript/Typescript", { TypeJS } }); |
| 209 |
m_language_bundle.insert({ "Python", { TypePython } }); |
| 210 |
m_language_bundle.insert({ "PHP", { TypePhp } }); |
| 211 |
m_language_bundle.insert({ "CMake", { TypeCMake } }); |
| 212 |
|
| 213 |
// build the reverse search table: file type -> language |
| 214 |
for(auto vt : m_language_bundle) { |
| 215 |
for(auto t : vt.second) { |
| 216 |
m_file_type_to_lang.insert({ (int)t, vt.first }); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// Initialize regexes: |
| 221 |
m_matchers.push_back(Matcher("#[ \t]*!(.*?)sh", TypeShellScript)); |
| 222 |
m_matchers.push_back(Matcher("#[ \t]*!(.*?)python", TypePython)); |
| 223 |
m_matchers.push_back(Matcher("#[ \t]*!(.*?)ruby", TypeRuby)); |
| 224 |
m_matchers.push_back(Matcher("#[ \t]*!(.*?)node", TypeJS)); |
| 225 |
m_matchers.push_back(Matcher("<?xml", TypeXml, false)); |
| 226 |
m_matchers.push_back(Matcher("<?php", TypePhp, false)); |
| 227 |
m_matchers.push_back(Matcher("SQLite format 3", TypeDatabase, false)); |
| 228 |
|
| 229 |
// STL sources places "-*- C++ -*-" at the top of their headers |
| 230 |
m_matchers.push_back(Matcher("-*- C++ -*-", TypeSourceCpp, false)); |
| 231 |
|
| 232 |
// #ifndef WORD |
| 233 |
m_matchers.push_back(Matcher("#ifndef[ \t]+[a-zA-Z0-9_]+", TypeSourceCpp)); |
| 234 |
|
| 235 |
// vim modlines |
| 236 |
m_matchers.push_back(Matcher("/\\* \\-\\*\\- Mode:[ \t]+c\\+\\+", TypeSourceCpp)); |
| 237 |
m_matchers.push_back(Matcher("# \\-\\*\\- Mode:[ \t]+python", TypePython)); |
| 238 |
|
| 239 |
// #include < |
| 240 |
m_matchers.push_back(Matcher("#include[ \t]+[\\<\"]", TypeSourceCpp)); |
| 241 |
} |
| 242 |
} |
| 243 |
std::unordered_map<wxString, FileExtManager::FileType> FileExtManager::GetAllSupportedFileTypes() |
| 244 |
{ |
| 245 |
Init(); |
| 246 |
return m_map; |
| 247 |
} |
| 248 |
|
| 249 |
std::unordered_map<wxString, std::vector<FileExtManager::FileType>> FileExtManager::GetLanguageBundles() |
| 250 |
{ |
| 251 |
Init(); |
| 252 |
return m_language_bundle; |
| 253 |
} |
| 254 |
|
| 255 |
FileExtManager::FileType FileExtManager::GetType(const wxString& filename, FileExtManager::FileType defaultType) |
| 256 |
{ |
| 257 |
Init(); |
| 258 |
|
| 259 |
wxFileName fn(filename); |
| 260 |
if(fn.IsOk() == false) { |
| 261 |
return defaultType; |
| 262 |
} |
| 263 |
|
| 264 |
wxString e(fn.GetExt()); |
| 265 |
e.MakeLower(); |
| 266 |
e.Trim().Trim(false); |
| 267 |
|
| 268 |
auto iter = m_map.find(e); |
| 269 |
if(iter == m_map.end()) { |
| 270 |
// try to see if the file is a makefile |
| 271 |
if(fn.GetFullName().CmpNoCase(wxT("makefile")) == 0) { |
| 272 |
return TypeMakefile; |
| 273 |
} else if(fn.GetFullName().Lower() == "dockerfile") { |
| 274 |
return TypeDockerfile; |
| 275 |
} else if(fn.GetFullName().CmpNoCase("README") == 0) { |
| 276 |
return TypeMarkdown; |
| 277 |
} else if(fn.GetFullName().CmpNoCase(".clangd") == 0) { |
| 278 |
return TypeYAML; |
| 279 |
} else { |
| 280 |
// try auto detecting |
| 281 |
FileType autoDetectType = defaultType; |
| 282 |
if(AutoDetectByContent(filename, autoDetectType)) { |
| 283 |
return autoDetectType; |
| 284 |
} |
| 285 |
} |
| 286 |
return defaultType; |
| 287 |
} else if((iter->second == TypeText) && (fn.GetFullName().CmpNoCase("CMakeLists.txt") == 0)) { |
| 288 |
return TypeCMake; |
| 289 |
} |
| 290 |
|
| 291 |
FileExtManager::FileType type = iter->second; |
| 292 |
if(fn.Exists() && (type == TypeWorkspace)) { |
| 293 |
wxString content; |
| 294 |
if(FileUtils::ReadFileContent(fn, content)) { |
| 295 |
if(content.Contains("<CodeLite_Workspace")) { |
| 296 |
return TypeWorkspace; |
| 297 |
} else { |
| 298 |
JSON root(content); |
| 299 |
if(!root.isOk()) |
| 300 |
return TypeWorkspace; |
| 301 |
if(root.toElement().hasNamedObject("NodeJS")) { |
| 302 |
return TypeWorkspaceNodeJS; |
| 303 |
} else if(root.toElement().hasNamedObject("Docker")) { |
| 304 |
return TypeWorkspaceDocker; |
| 305 |
} else if(root.toElement().namedObject("workspace_type").toString() == "File System Workspace") { |
| 306 |
return TypeWorkspaceFileSystem; |
| 307 |
} else if(root.toElement().namedObject("metadata").namedObject("type").toString() == "php") { |
| 308 |
return TypeWorkspacePHP; |
| 309 |
} else { |
| 310 |
return TypeWorkspace; |
| 311 |
} |
| 312 |
} |
| 313 |
} else { |
| 314 |
return TypeWorkspace; |
| 315 |
} |
| 316 |
} |
| 317 |
return iter->second; |
| 318 |
} |
| 319 |
|
| 320 |
bool FileExtManager::IsCxxFile(const wxString& filename) |
| 321 |
{ |
| 322 |
|
| 323 |
FileType ft = GetType(filename); |
| 324 |
if(ft == TypeOther) { |
| 325 |
// failed to detect the type |
| 326 |
if(!AutoDetectByContent(filename, ft)) { |
| 327 |
return false; |
| 328 |
} |
| 329 |
} |
| 330 |
return (ft == TypeSourceC) || (ft == TypeSourceCpp) || (ft == TypeHeader); |
| 331 |
} |
| 332 |
|
| 333 |
bool FileExtManager::AutoDetectByContent(const wxString& filename, FileExtManager::FileType& fileType) |
| 334 |
{ |
| 335 |
wxString fileContent; |
| 336 |
if(!FileUtils::ReadBufferFromFile(filename, fileContent, 1024)) { |
| 337 |
clWARNING() << "Failed to read file's content" << endl; |
| 338 |
return false; |
| 339 |
} |
| 340 |
return GetContentType(fileContent, fileType); |
| 341 |
} |
| 342 |
|
| 343 |
bool FileExtManager::GetContentType(const wxString& string_content, FileExtManager::FileType& fileType) |
| 344 |
{ |
| 345 |
for(size_t i = 0; i < m_matchers.size(); ++i) { |
| 346 |
if(m_matchers[i].Matches(string_content)) { |
| 347 |
LOG_IF_TRACE |
| 348 |
{ |
| 349 |
if(m_matchers[i].m_regex) { |
| 350 |
clDEBUG1() << "Matching part is:" << m_matchers[i].m_regex->GetMatch(string_content, 0) << endl; |
| 351 |
} |
| 352 |
} |
| 353 |
fileType = m_matchers[i].m_fileType; |
| 354 |
return true; |
| 355 |
} |
| 356 |
} |
| 357 |
return false; |
| 358 |
} |
| 359 |
|
| 360 |
bool FileExtManager::IsFileType(const wxString& filename, FileExtManager::FileType type) |
| 361 |
{ |
| 362 |
|
| 363 |
FileType ft = GetType(filename); |
| 364 |
if(ft == TypeOther) { |
| 365 |
// failed to detect the type |
| 366 |
if(!AutoDetectByContent(filename, ft)) { |
| 367 |
return false; |
| 368 |
} |
| 369 |
} |
| 370 |
return (ft == type); |
| 371 |
} |
| 372 |
|
| 373 |
bool FileExtManager::IsJavascriptFile(const wxString& filename) { return FileExtManager::IsFileType(filename, TypeJS); } |
| 374 |
|
| 375 |
bool FileExtManager::IsPHPFile(const wxString& filename) { return FileExtManager::IsFileType(filename, TypePhp); } |
| 376 |
|
| 377 |
bool FileExtManager::IsJavaFile(const wxString& filename) { return FileExtManager::IsFileType(filename, TypeJava); } |
| 378 |
|
| 379 |
FileExtManager::FileType FileExtManager::GetTypeFromExtension(const wxFileName& filename) |
| 380 |
{ |
| 381 |
|
| 382 |
std::unordered_map<wxString, FileExtManager::FileType>::iterator iter = m_map.find(filename.GetExt().Lower()); |
| 383 |
if(iter == m_map.end()) |
| 384 |
return TypeOther; |
| 385 |
return iter->second; |
| 386 |
} |
| 387 |
|
| 388 |
bool FileExtManager::IsSymlinkFile(const wxString& filename) |
| 389 |
{ |
| 390 |
return wxFileName::Exists(filename, wxFILE_EXISTS_NO_FOLLOW | wxFILE_EXISTS_SYMLINK) && |
| 391 |
wxFileName::FileExists(filename); |
| 392 |
} |
| 393 |
|
| 394 |
bool FileExtManager::IsSymlinkFolder(const wxString& filename) |
| 395 |
{ |
| 396 |
return wxFileName::Exists(filename, wxFILE_EXISTS_NO_FOLLOW | wxFILE_EXISTS_SYMLINK) && |
| 397 |
wxFileName::DirExists(filename); |
| 398 |
} |
| 399 |
|
| 400 |
wxString FileExtManager::GetLanguageFromType(FileExtManager::FileType file_type) |
| 401 |
{ |
| 402 |
if(m_file_type_to_lang.count((int)file_type) == 0) { |
| 403 |
return wxEmptyString; |
| 404 |
} |
| 405 |
|
| 406 |
return m_file_type_to_lang.find((int)file_type)->second; |
| 407 |
} |