OpenMS
Loading...
Searching...
No Matches
File.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Chris Bielow $
6// $Authors: Andreas Bertsch, Chris Bielow, Marc Sturm $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12#include <OpenMS/config.h>
13#include <cstdlib>
14#include <mutex>
15
16
17namespace OpenMS
18{
19 class Param;
20 class TOPPBase;
21
27 class OPENMS_DLLAPI File
28 {
29public:
34 class OPENMS_DLLAPI TempDir
35 {
36 public:
37
40 TempDir(bool keep_dir = false);
41
47 TempDir(const std::string& base_dir, bool keep_dir = false);
48
51
53 TempDir(const TempDir&) = delete;
54 TempDir& operator=(const TempDir&) = delete;
55 TempDir(TempDir&&) = delete;
57
59 const std::string& getPath() const;
60
61 private:
62 std::string temp_dir_;
64 };
65
70 static std::string getExecutablePath();
71
73 static bool exists(const std::string& file);
74
76 static bool empty(const std::string& file);
77
79 static bool executable(const std::string& file);
80
82 static UInt64 fileSize(const std::string& file);
83
93 static Int64 getModificationTime(const std::string& file);
94
109 static bool rename(const std::string& from, const std::string& to, bool overwrite_existing = true, bool verbose = true);
110
127 enum class CopyOptions {OVERWRITE,SKIP,CANCEL};
128 static bool copyDirRecursively(const std::string& from_dir, const std::string& to_dir, File::CopyOptions option = CopyOptions::OVERWRITE);
129
131 static bool copy(const std::string& from, const std::string& to);
132
138 static bool remove(const std::string& file);
139
141 static bool removeDirRecursively(const std::string& dir_name);
142
144 static bool removeDir(const std::string& dir_name);
145
148 static bool makeDir(const std::string& dir_name);
149
151 static std::string absolutePath(const std::string& file);
152
156 static std::string basename(const std::string& file);
157
163 static std::string stemName(const std::string& file);
164
169 static std::string extension(const std::string& file);
170
173 static StringList listDirectories(const std::string& dir);
174
179 static std::string path(const std::string& file);
180
182 static bool readable(const std::string& file);
183
185 static bool writable(const std::string& file);
186
188 static bool isDirectory(const std::string& path);
189
200 static std::string find(const std::string& filename, StringList directories = StringList());
201
208 static bool fileList(const std::string& dir, const std::string& file_pattern, StringList& output, bool full_path = false);
209
226 static std::string findDoc(const std::string& filename);
227
234 static std::string getUniqueName(bool include_hostname = true);
235
238 static std::string getOpenMSDataPath();
239
245 static const std::string& getOpenMSDataPathSource();
246
248 static std::string getOpenMSHomePath();
249
256 static std::string getOpenMSConfigDir();
257
263 static std::string getTempDirectory();
264
270 static std::string getUserDirectory();
271
278
282 static std::string findDatabase(const std::string& db_name);
283
295
303 static StringList getPathLocations(const std::string& path);
304
318 static bool findExecutable(std::string& exe_filename);
319
326 static std::string findSiblingTOPPExecutable(const std::string& toolName);
327
344 static std::string getTemporaryFile(const std::string& alternative_file = "");
345
346
348 {
349 MATCH = 0, // Everything matches perfectly
350 ORDER_MISMATCH = 1, // Same set of files but in wrong order
351 SET_MISMATCH = 2 // Different sets of files (including size mismatch)
352 };
353
370 const StringList& sl2,
371 bool basename = true,
372 bool ignore_extension = true);
373
374private:
375
378
380 static bool isOpenMSDataPath_(const std::string& path);
381
384 {
385 std::string path;
386 std::string source;
387 };
388
391
392#ifdef OPENMS_WINDOWSPLATFORM
402 static StringList executableExtensions_();
403
411 static StringList executableExtensions_(const std::string& ext);
412#endif
413
418 {
419 public:
420 TemporaryFiles_(const TemporaryFiles_&) = delete; // copy is forbidden
424 std::string newFile();
425
427 private:
429 std::mutex mtx_;
430 };
431
432
435 };
436}
Class representing a temporary directory.
Definition File.h:35
TempDir(const std::string &base_dir, bool keep_dir=false)
~TempDir()
Destroy temporary folder (can be prohibited in Constructor)
const std::string & getPath() const
Return path to temporary folder.
TempDir(bool keep_dir=false)
TempDir(const TempDir &)=delete
delete all means to copy or move a TempDir
TempDir(TempDir &&)=delete
std::string temp_dir_
Definition File.h:62
TempDir & operator=(const TempDir &)=delete
bool keep_dir_
Definition File.h:63
TempDir & operator=(TempDir &&)=delete
Internal helper class, which holds temporary filenames and deletes these files at program exit.
Definition File.h:418
StringList filenames_
Definition File.h:428
TemporaryFiles_ & operator=(const TemporaryFiles_ &)=delete
std::string newFile()
create a new filename and queue internally for deletion
TemporaryFiles_(const TemporaryFiles_ &)=delete
std::mutex mtx_
Definition File.h:429
Basic file handling operations.
Definition File.h:28
static std::string findDoc(const std::string &filename)
Resolves a partial file name to a documentation file in the doc-folder.
static std::string findSiblingTOPPExecutable(const std::string &toolName)
Searches for an executable with the given name.
static std::string find(const std::string &filename, StringList directories=StringList())
Looks up the location of the file filename.
std::string source
human-readable origin, e.g. "the OPENMS_DATA_PATH environment variable"
Definition File.h:386
static std::string getOpenMSConfigDir()
Returns the per-user OpenMS configuration directory (the directory that holds OpenMS....
static bool executable(const std::string &file)
Method used to test if a file is executable.
static bool removeDir(const std::string &dir_name)
Removes a directory and all its contents (absolute path). Returns true if successful.
static std::string getUserDirectory()
static bool writable(const std::string &file)
Return true if the file is writable.
CopyOptions
Copy directory recursively.
Definition File.h:127
static Param getSystemParameterDefaults_()
get defaults for the system's Temp-path, user home directory etc.
static std::string findDatabase(const std::string &db_name)
static StringList listDirectories(const std::string &dir)
static bool remove(const std::string &file)
Removes a file (if it exists).
std::string path
the resolved shared-data directory
Definition File.h:385
static std::string getTempDirectory()
static std::string basename(const std::string &file)
static bool fileList(const std::string &dir, const std::string &file_pattern, StringList &output, bool full_path=false)
Retrieves a list of files matching file_pattern in directory dir (returns filenames without paths unl...
static StringList getPathLocations()
Extract list of directories from a concatenated string (usually $PATH).
static MatchingFileListsStatus validateMatchingFileNames(const StringList &sl1, const StringList &sl2, bool basename=true, bool ignore_extension=true)
Helper function to test if filenames provided in two StringLists match.
static bool empty(const std::string &file)
Return true if the file does not exist or the file is empty.
static bool readable(const std::string &file)
Return true if the file exists and is readable.
static std::string extension(const std::string &file)
static UInt64 fileSize(const std::string &file)
The filesize in bytes (or -1 on error, e.g. if the file does not exist)
static bool makeDir(const std::string &dir_name)
static std::string getTemporaryFile(const std::string &alternative_file="")
Obtain a temporary filename, ensuring automatic deletion upon exit.
static std::string absolutePath(const std::string &file)
Replaces the relative path in the argument with the absolute path.
static std::string getUniqueName(bool include_hostname=true)
Returns a string, consisting of date, time, hostname, process id, and a incrementing number....
static std::string path(const std::string &file)
static const std::string & getOpenMSDataPathSource()
Returns a human-readable description of where getOpenMSDataPath() resolved from.
static bool findExecutable(std::string &exe_filename)
Searches for an executable with the given name (similar to where (Windows) or which (Linux/MacOS)
static bool isOpenMSDataPath_(const std::string &path)
Check if the given path is a valid OPENMS_DATA_PATH.
MatchingFileListsStatus
Definition File.h:348
static bool removeDirRecursively(const std::string &dir_name)
Removes a directory and all its contents recursively (absolute path). Returns true if successful.
static std::string stemName(const std::string &file)
static std::string getExecutablePath()
static bool copyDirRecursively(const std::string &from_dir, const std::string &to_dir, File::CopyOptions option=CopyOptions::OVERWRITE)
static bool exists(const std::string &file)
Method used to test if a file exists.
static bool copy(const std::string &from, const std::string &to)
Copy a file (if it exists). Returns true if successful.
static std::string getOpenMSDataPath()
static const OpenMSDataPath_ & resolveOpenMSDataPath_()
Resolve (once, thread-safe) and return the OpenMS data path together with where it was found.
static std::string getOpenMSHomePath()
Returns the OpenMS home path (environment variable overwrites the default home path)
static Param getSystemParameters()
static Int64 getModificationTime(const std::string &file)
Last modification time of file, in seconds since the Unix epoch (or -1 on error).
static TemporaryFiles_ temporary_files_
private list of temporary filenames, which are deleted upon program exit
Definition File.h:434
static bool isDirectory(const std::string &path)
Return true if the given path specifies a directory.
static StringList getPathLocations(const std::string &path)
Extract list of directories from an explicit concatenated path string.
static bool rename(const std::string &from, const std::string &to, bool overwrite_existing=true, bool verbose=true)
Rename a file.
Bundles the resolved OpenMS data path with a human-readable description of where it was found (for di...
Definition File.h:384
Management and storage of parameters / INI files.
Definition Param.h:46
int64_t Int64
Signed integer type (64bit)
Definition Types.h:40
uint64_t UInt64
Unsigned integer type (64bit)
Definition Types.h:47
std::vector< std::string > StringList
Vector of String.
Definition TypeAliases.h:39
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19