| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : fileutils.h |
| 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 |
#ifndef FILEUTILS_H |
| 26 |
#define FILEUTILS_H |
| 27 |
|
| 28 |
#include "asyncprocess.h" |
| 29 |
#include "codelite_exports.h" |
| 30 |
#include "macros.h" |
| 31 |
#include "wx/filename.h" |
| 32 |
|
| 33 |
#include <wx/filename.h> |
| 34 |
#include <wx/log.h> |
| 35 |
|
| 36 |
#define clRemoveFile(filename) FileUtils::RemoveFile(filename, (wxString() << __FILE__ << ":" << __LINE__)) |
| 37 |
|
| 38 |
class WXDLLIMPEXP_CL FileUtils |
| 39 |
{ |
| 40 |
public: |
| 41 |
class Deleter |
| 42 |
{ |
| 43 |
wxFileName m_filename; |
| 44 |
|
| 45 |
public: |
| 46 |
Deleter(const wxFileName& filename) |
| 47 |
: m_filename(filename) |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
~Deleter() |
| 52 |
{ |
| 53 |
if(m_filename.Exists()) { |
| 54 |
clRemoveFile(m_filename); |
| 55 |
} |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
public: |
| 60 |
static bool ReadFileContent(const wxFileName& fn, wxString& data, const wxMBConv& conv = wxConvUTF8); |
| 61 |
static bool ReadFileContentRaw(const wxFileName& fn, std::string& data); |
| 62 |
|
| 63 |
/** |
| 64 |
* @brief attempt to read up to bufferSize from the beginning of file |
| 65 |
*/ |
| 66 |
static bool ReadBufferFromFile(const wxFileName& fn, wxString& data, size_t bufferSize); |
| 67 |
|
| 68 |
/** |
| 69 |
* @brief set the file content (replacing it) |
| 70 |
*/ |
| 71 |
static bool WriteFileContent(const wxFileName& fn, const wxString& content, const wxMBConv& conv = wxConvUTF8); |
| 72 |
/** |
| 73 |
* @brief append content to end of the file |
| 74 |
*/ |
| 75 |
static bool AppendFileContent(const wxFileName& fn, const wxString& content, const wxMBConv& conv = wxConvUTF8); |
| 76 |
/** |
| 77 |
* @brief open file explorer at given path |
| 78 |
*/ |
| 79 |
static void OpenFileExplorer(const wxString& path); |
| 80 |
|
| 81 |
/** |
| 82 |
* @brief open file explorer at given path and select the input file |
| 83 |
*/ |
| 84 |
static void OpenFileExplorerAndSelect(const wxFileName& filename); |
| 85 |
|
| 86 |
/** |
| 87 |
* @brief launch the OS default terminal at a given path |
| 88 |
*/ |
| 89 |
static void OpenTerminal(const wxString& path, const wxString& user_command = "", bool pause_when_exit = false); |
| 90 |
|
| 91 |
/** |
| 92 |
* @brief open the built-in terminal |
| 93 |
*/ |
| 94 |
static void OpenBuiltInTerminal(const wxString& wd, const wxString& user_command, bool pause_when_exit = false); |
| 95 |
|
| 96 |
/** |
| 97 |
* @brief open ssh terminal |
| 98 |
* @param sshClient ssh client to use (putty, ssh etc) |
| 99 |
* @param connectString e.g. eran@host |
| 100 |
* @param password the password |
| 101 |
* @param port ssh port |
| 102 |
*/ |
| 103 |
static void OpenSSHTerminal(const wxString& sshClient, const wxString& connectString, const wxString& password, |
| 104 |
int port = 22); |
| 105 |
|
| 106 |
/** |
| 107 |
* @brief OSX only: open Terminal and return its TTY |
| 108 |
* @param path working directory |
| 109 |
* @param [output] tty the TTY of the launched terminal |
| 110 |
*/ |
| 111 |
static void OSXOpenDebuggerTerminalAndGetTTY(const wxString& path, const wxString& appname, wxString& tty, |
| 112 |
long& pid); |
| 113 |
|
| 114 |
/** |
| 115 |
* @brief return the command needed to open OSX terminal at a given directory and launch a command |
| 116 |
*/ |
| 117 |
static wxString GetOSXTerminalCommand(const wxString& command, const wxString& workingDirectory); |
| 118 |
|
| 119 |
/** |
| 120 |
* @brief file masking search |
| 121 |
*/ |
| 122 |
static bool WildMatch(const wxString& mask, const wxString& filename); |
| 123 |
/** |
| 124 |
* @brief file masking search |
| 125 |
*/ |
| 126 |
static bool WildMatch(const wxString& mask, const wxFileName& filename); |
| 127 |
|
| 128 |
/** |
| 129 |
* @brief file masking search |
| 130 |
*/ |
| 131 |
static bool WildMatch(const wxArrayString& masks, const wxString& filename); |
| 132 |
|
| 133 |
/** |
| 134 |
* @brief return true if needle exists in the haystack. Supports fuzzy search |
| 135 |
* @param needle the pattern to search |
| 136 |
* @param haystack the string to search on |
| 137 |
*/ |
| 138 |
static bool FuzzyMatch(const wxString& needle, const wxString& haystack); |
| 139 |
|
| 140 |
/** |
| 141 |
* @brief an efficient way to tokenize string into words (separated by SPACE and/or TAB) |
| 142 |
* @code |
| 143 |
* wxString str = "My String That Requires Tokenize"; |
| 144 |
* wxString word; // The output |
| 145 |
* size_t offset = 0; |
| 146 |
* while(clNextWord(str, offset, word)) { |
| 147 |
* // Do something with "word" here |
| 148 |
* } |
| 149 |
* @codeend |
| 150 |
* @param str the string to tokenize |
| 151 |
* @param offset used internally, allocate one on the stack and initialise it to 0 |
| 152 |
* @param word [output] |
| 153 |
* @return true if a word was found |
| 154 |
*/ |
| 155 |
static bool NextWord(const wxString& str, size_t& offset, wxString& word, bool makeLower = false); |
| 156 |
|
| 157 |
/** |
| 158 |
* @brief split a string by whitespace |
| 159 |
*/ |
| 160 |
static size_t SplitWords(const wxString& str, wxStringSet_t& outputSet, bool makeLower = false); |
| 161 |
|
| 162 |
/** |
| 163 |
* @brief decode URI using percent encoding |
| 164 |
*/ |
| 165 |
static wxString DecodeURI(const wxString& uri); |
| 166 |
|
| 167 |
/** |
| 168 |
* @brief encode URI using percent encoding |
| 169 |
*/ |
| 170 |
static wxString EncodeURI(const wxString& uri); |
| 171 |
|
| 172 |
/** |
| 173 |
* @brief escape string. Each space and double quotes marker is escaped with backslash |
| 174 |
*/ |
| 175 |
static wxString EscapeString(const wxString& str); |
| 176 |
|
| 177 |
/** |
| 178 |
* @brief is the file or folder a hidden file? |
| 179 |
*/ |
| 180 |
static bool IsHidden(const wxFileName& path); |
| 181 |
/** |
| 182 |
* @brief is the file or folder a hidden file? |
| 183 |
*/ |
| 184 |
static bool IsHidden(const wxString& path); |
| 185 |
|
| 186 |
/** |
| 187 |
* @brief return true if filename is a symlink |
| 188 |
*/ |
| 189 |
static bool IsSymlink(const wxFileName& filename) { return IsSymlink(filename.GetFullPath()); } |
| 190 |
/** |
| 191 |
* @brief return true if filename is a symlink |
| 192 |
*/ |
| 193 |
static bool IsSymlink(const wxString& filename); |
| 194 |
|
| 195 |
/** |
| 196 |
* @brief return true if filename is a symlink |
| 197 |
*/ |
| 198 |
static bool IsDirectory(const wxFileName& filename) { return IsDirectory(filename.GetFullPath()); } |
| 199 |
/** |
| 200 |
* @brief return true if filename is a symlink |
| 201 |
*/ |
| 202 |
static bool IsDirectory(const wxString& filename); |
| 203 |
|
| 204 |
/** |
| 205 |
* @brief is `filename` an executable? |
| 206 |
* on Linux/macOS, we check if the file is an ELF file, on Windows, we check if the |
| 207 |
* file has the extension `.exe` or `.dll` |
| 208 |
*/ |
| 209 |
static bool IsBinaryExecutable(const wxString& filename); |
| 210 |
static bool IsBinaryExecutable(const wxFileName& filename) { return IsBinaryExecutable(filename.GetFullPath()); } |
| 211 |
|
| 212 |
/** |
| 213 |
* @brief set permissions to filename |
| 214 |
*/ |
| 215 |
static bool SetFilePermissions(const wxFileName& filename, mode_t perm); |
| 216 |
|
| 217 |
/** |
| 218 |
* @brief get file permissions |
| 219 |
*/ |
| 220 |
static bool GetFilePermissions(const wxFileName& filename, mode_t& perm); |
| 221 |
|
| 222 |
/** |
| 223 |
* @brief return the file modification time |
| 224 |
*/ |
| 225 |
static time_t GetFileModificationTime(const wxFileName& filename); |
| 226 |
|
| 227 |
/** |
| 228 |
* @brief return the file size, in bytes |
| 229 |
*/ |
| 230 |
static size_t GetFileSize(const wxFileName& filename); |
| 231 |
|
| 232 |
/** |
| 233 |
* @brief replace any unwanted characters with underscore |
| 234 |
* The chars that we replace are: |
| 235 |
* @-^%&$#@!(){}[]+=;,. |
| 236 |
* @param name |
| 237 |
* @return modified name excluding the above chars (will be replaced with _) |
| 238 |
*/ |
| 239 |
static wxString NormaliseName(const wxString& name); |
| 240 |
|
| 241 |
/** |
| 242 |
* @brief remove a file from the file system |
| 243 |
* @param filename file name to remove |
| 244 |
* @param context unique context string which will be logged to the log file |
| 245 |
*/ |
| 246 |
static bool RemoveFile(const wxFileName& filename, const wxString& context = "") |
| 247 |
{ |
| 248 |
return RemoveFile(filename.GetFullPath(), context); |
| 249 |
} |
| 250 |
/** |
| 251 |
* @brief same as above |
| 252 |
*/ |
| 253 |
static bool RemoveFile(const wxString& filename, const wxString& context = ""); |
| 254 |
|
| 255 |
static unsigned int UTF8Length(const wchar_t* uptr, unsigned int tlen); |
| 256 |
|
| 257 |
/** |
| 258 |
* @brief (on Linux) makes-absolute filepath, and dereferences it and any symlinked dirs in the path |
| 259 |
*/ |
| 260 |
static wxString RealPath(const wxString& filepath); |
| 261 |
|
| 262 |
/** |
| 263 |
* @brief convert string into std::string |
| 264 |
*/ |
| 265 |
static std::string ToStdString(const wxString& str); |
| 266 |
|
| 267 |
/** |
| 268 |
* @brief create an environment list from string in the format of: |
| 269 |
* ... |
| 270 |
* key=value |
| 271 |
* key1=value2 |
| 272 |
* ... |
| 273 |
*/ |
| 274 |
static clEnvList_t CreateEnvironment(const wxString& envstr); |
| 275 |
|
| 276 |
/** |
| 277 |
* @brief Check if the file 'name' is findable on the user's system |
| 278 |
* @param name the name of the file to locate |
| 279 |
* @param exepath will contain its filepath if successfully located |
| 280 |
* @param hint extra paths to search |
| 281 |
* @param list of suffixes. On Linux, some files may have number attached to them like: lldb-10, lldb-9 |
| 282 |
* passing suffix_list = {"-10", "-9"...} will also check for these files (in order) |
| 283 |
* @return true if a filepath was found |
| 284 |
*/ |
| 285 |
static bool FindExe(const wxString& name, wxFileName& exepath, const wxArrayString& hint = {}, |
| 286 |
const wxArrayString& suffix_list = {}); |
| 287 |
|
| 288 |
/** |
| 289 |
* @brief create a temporary file *name* in a given folder with a given name prefix and an extension |
| 290 |
* note that this function does not actually create the file, but only generates a name |
| 291 |
*/ |
| 292 |
static wxFileName CreateTempFileName(const wxString& folder, const wxString& prefix, const wxString& ext); |
| 293 |
|
| 294 |
/** |
| 295 |
* @brief find a file(s) with similar name and path, but with a different extension |
| 296 |
* @param filename |
| 297 |
* @param extensions |
| 298 |
* @param [out] the files found |
| 299 |
*/ |
| 300 |
static size_t FindSimilar(const wxFileName& filename, const std::vector<wxString>& extensions, |
| 301 |
std::vector<wxFileName>& vout); |
| 302 |
|
| 303 |
/** |
| 304 |
* @brief given URI, parse it into its basic parts |
| 305 |
*/ |
| 306 |
static bool ParseURI(const wxString& uri, wxString& path, wxString& scheme, wxString& user, wxString& host, |
| 307 |
wxString& port); |
| 308 |
|
| 309 |
/** |
| 310 |
* @brief covnert path to uri |
| 311 |
* /home/eran/file.cpp -> file:///home/eran/file.cpp |
| 312 |
* file:///home/eran/file.cpp -> file:///home/eran/file.cpp |
| 313 |
*/ |
| 314 |
static wxString FilePathToURI(const wxString& filepath); |
| 315 |
|
| 316 |
/** |
| 317 |
* @brief convert uri to file path |
| 318 |
* file:///home/eran/file.cpp -> /home/eran/file.cpp |
| 319 |
* /home/eran/file.cpp -> /home/eran/file.cpp |
| 320 |
*/ |
| 321 |
static wxString FilePathFromURI(const wxString& uri); |
| 322 |
|
| 323 |
/** |
| 324 |
* @brief calculate checksum of a given file |
| 325 |
*/ |
| 326 |
static bool GetChecksum(const wxString& filepath, size_t* checksum); |
| 327 |
}; |
| 328 |
#endif // FILEUTILS_H |