Geogram  Version 1.9.0
A programming library of geometric algorithms
file_system.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_FILE_SYSTEM
41 #define GEOGRAM_BASIC_FILE_SYSTEM
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
45 #include <geogram/basic/counted.h>
47 
48 #include <string>
49 #include <vector>
50 #include <map>
51 
57 namespace GEO {
58 
62  namespace FileSystem {
63 
69  class GEOGRAM_API Node : public Counted {
70  public:
74  Node();
75 
79  ~Node() override;
80 
81  /************************** OS-dependent **************************/
82 
89  virtual bool is_file(const std::string& path);
90 
97  virtual bool is_directory(const std::string& path);
98 
108  virtual bool create_directory(const std::string& path);
109 
118  virtual bool delete_directory(const std::string& path);
119 
126  virtual bool delete_file(const std::string& path);
127 
138  virtual bool get_directory_entries(
139  const std::string& path, std::vector<std::string>& result
140  );
141 
146  virtual std::string get_current_working_directory();
147 
156  const std::string& path
157  );
158 
171  virtual bool rename_file(
172  const std::string& old_name, const std::string& new_name
173  );
174 
180  virtual Numeric::uint64 get_time_stamp(const std::string& path);
181 
189  virtual bool set_executable_flag(const std::string& filename);
190 
191 
198  virtual bool touch(const std::string& filename);
199 
208  virtual std::string normalized_path(const std::string& path);
209 
210 
216  virtual std::string home_directory();
217 
227  virtual std::string documents_directory();
228 
229 
235  virtual std::string load_file_as_string(const std::string& path);
236 
237  /************************ OS-independent **************************/
238 
256  virtual std::string extension(const std::string& path);
257 
277  virtual std::string base_name(
278  const std::string& path, bool remove_extension = true
279  );
280 
296  virtual std::string dir_name(const std::string& path);
297 
310  virtual void get_directory_entries(
311  const std::string& path,
312  std::vector<std::string>& result, bool recursive
313  );
314 
328  virtual void get_files(
329  const std::string& path,
330  std::vector<std::string>& result, bool recursive = false
331  );
332 
346  virtual void get_subdirectories(
347  const std::string& path,
348  std::vector<std::string>& result, bool recursive = false
349  );
350 
357  virtual void flip_slashes(std::string& path);
358 
366  virtual bool copy_file(
367  const std::string& from, const std::string& to
368  );
369  };
370 
374  class GEOGRAM_API MemoryNode : public Node {
375  public:
376 
381  MemoryNode(const std::string& path="/") : path_(path) {
382  }
383 
385  bool copy_file(
386  const std::string& from, const std::string& to
387  ) override ;
388 
390  std::string load_file_as_string(const std::string& path) override;
391 
393  virtual bool is_file(const std::string& path) override;
394 
396  virtual bool is_directory(const std::string& path) override;
397 
399  virtual bool create_directory(const std::string& path) override;
400 
402  virtual bool delete_directory(const std::string& path) override;
403 
405  virtual bool delete_file(const std::string& path) override;
406 
409  const std::string& path, std::vector<std::string>& result
410  ) override;
411 
412 
415  const std::string& old_name, const std::string& new_name
416  ) override;
417 
423  const char* get_file_contents(const std::string& path);
424 
430  bool create_file(const std::string& path, const char* content);
431 
432  protected:
440  static void split_path(
441  const std::string& path, std::string& leadingsubdir,
442  std::string& rest
443  );
444 
445  private:
446  std::string path_;
447  std::map<std::string, SmartPointer<MemoryNode> > subnodes_;
448  std::map<std::string, const char*> files_;
449  };
450 
452 
453  /**********************************************************/
454 
461  void GEOGRAM_API initialize();
462 
469  void GEOGRAM_API terminate();
470 
472  bool GEOGRAM_API is_file(const std::string& path);
473 
475  bool GEOGRAM_API is_directory(const std::string& path);
476 
484  bool GEOGRAM_API can_read_directory(const std::string& path);
485 
494  bool GEOGRAM_API can_write_directory(
495  const std::string& path, bool create_missing_directories = false
496  );
497 
499  bool GEOGRAM_API create_directory(const std::string& path);
500 
502  bool GEOGRAM_API delete_directory(const std::string& path);
503 
505  bool GEOGRAM_API delete_file(const std::string& path);
506 
508  bool GEOGRAM_API get_directory_entries(
509  const std::string& path, std::vector<std::string>& result
510  );
511 
513  std::string GEOGRAM_API get_current_working_directory();
514  bool GEOGRAM_API set_current_working_directory(
515  const std::string& path
516  );
517 
519  bool GEOGRAM_API rename_file(
520  const std::string& old_name, const std::string& new_name
521  );
522 
525  const std::string& path
526  );
527 
529  std::string GEOGRAM_API extension(const std::string& path);
530 
532  std::string GEOGRAM_API base_name(
533  const std::string& path, bool remove_extension = true
534  );
535 
537  std::string GEOGRAM_API dir_name(const std::string& path);
538 
540  void GEOGRAM_API get_directory_entries(
541  const std::string& path,
542  std::vector<std::string>& result, bool recursive
543  );
544 
546  void GEOGRAM_API get_files(
547  const std::string& path,
548  std::vector<std::string>& result, bool recursive = false
549  );
550 
552  void GEOGRAM_API get_subdirectories(
553  const std::string& path,
554  std::vector<std::string>& result, bool recursive = false
555  );
556 
558  void GEOGRAM_API flip_slashes(std::string& path);
559 
561  bool GEOGRAM_API copy_file(
562  const std::string& from, const std::string& to
563  );
564 
566  bool GEOGRAM_API set_executable_flag(const std::string& filename);
567 
569  bool GEOGRAM_API touch(const std::string& filename);
570 
572  std::string GEOGRAM_API normalized_path(const std::string& path);
573 
575  std::string GEOGRAM_API absolute_path(const std::string& path);
576 
578  std::string GEOGRAM_API home_directory();
579 
581  std::string GEOGRAM_API documents_directory();
582 
588  void GEOGRAM_API get_root(Node*& root);
589 
590 #ifdef GEO_OS_EMSCRIPTEN
598  void set_file_system_changed_callback(void(*callback)());
599 #endif
600 
601  }
602 }
603 
604 #endif
605 
Base class for reference-counted objects.
Definition: counted.h:71
Implementation of a file system stored in memory.
Definition: file_system.h:374
bool create_file(const std::string &path, const char *content)
Creates a file.
static void split_path(const std::string &path, std::string &leadingsubdir, std::string &rest)
Splits a path.
const char * get_file_contents(const std::string &path)
Gets the contents of a file.
virtual bool delete_file(const std::string &path) override
Deletes a file.
virtual bool is_directory(const std::string &path) override
Checks if a path is a directory.
virtual bool delete_directory(const std::string &path) override
Deletes a directory.
bool copy_file(const std::string &from, const std::string &to) override
Copies a file.
virtual bool is_file(const std::string &path) override
Checks if a path is a regular file.
MemoryNode(const std::string &path="/")
MemoryNode constructor.
Definition: file_system.h:381
virtual bool create_directory(const std::string &path) override
Creates a directory.
bool rename_file(const std::string &old_name, const std::string &new_name) override
Renames or moves a file.
bool get_directory_entries(const std::string &path, std::vector< std::string > &result) override
Lists directory contents.
std::string load_file_as_string(const std::string &path) override
Load file contents in a string.
A Node in a FileSystem.
Definition: file_system.h:69
virtual bool copy_file(const std::string &from, const std::string &to)
Copies a file.
virtual void get_subdirectories(const std::string &path, std::vector< std::string > &result, bool recursive=false)
Lists sub-directories in a directory.
virtual bool rename_file(const std::string &old_name, const std::string &new_name)
Renames or moves a file.
virtual std::string load_file_as_string(const std::string &path)
Load file contents in a string.
virtual void flip_slashes(std::string &path)
Converts a path to Unix format.
virtual std::string extension(const std::string &path)
Gets a path extension.
virtual bool delete_file(const std::string &path)
Deletes a file.
virtual bool touch(const std::string &filename)
Modifies the last modification time of a file.
virtual Numeric::uint64 get_time_stamp(const std::string &path)
Gets a file last modification time.
virtual std::string home_directory()
Gets the current user's home directory.
virtual bool set_executable_flag(const std::string &filename)
Marks a filename as executable.
virtual std::string base_name(const std::string &path, bool remove_extension=true)
Gets a path base name.
virtual void get_directory_entries(const std::string &path, std::vector< std::string > &result, bool recursive)
Lists directory contents.
virtual bool is_file(const std::string &path)
Checks if a path is a regular file.
virtual std::string dir_name(const std::string &path)
Gets a path directory.
virtual void get_files(const std::string &path, std::vector< std::string > &result, bool recursive=false)
Lists files in a directory.
Node()
Node constructor.
virtual bool set_current_working_directory(const std::string &path)
Sets the working directory.
virtual bool delete_directory(const std::string &path)
Deletes a directory.
virtual bool is_directory(const std::string &path)
Checks if a path is a directory.
virtual std::string documents_directory()
Gets the current user's home directory.
virtual bool create_directory(const std::string &path)
Creates a directory.
virtual bool get_directory_entries(const std::string &path, std::vector< std::string > &result)
Lists directory contents.
virtual std::string normalized_path(const std::string &path)
Normalizes a path.
~Node() override
Node destructor.
virtual std::string get_current_working_directory()
Gets the current working directory.
A smart pointer with reference-counted copy semantics.
Definition: smart_pointer.h:76
Base class of reference-counted objects, to be used with smart pointers.
Common include file, providing basic definitions. Should be included before anything else by all head...
bool copy_file(const std::string &from, const std::string &to)
Copies a file.
std::string absolute_path(const std::string &path)
void get_files(const std::string &path, std::vector< std::string > &result, bool recursive=false)
Lists files in a directory.
bool delete_file(const std::string &path)
Deletes a file.
std::string home_directory()
Gets the current user's home directory.
void terminate()
Terminates the FileSystem library.
bool can_read_directory(const std::string &path)
Tests whether one can read files from a directory.
std::string dir_name(const std::string &path)
Gets a path directory.
std::string get_current_working_directory()
Gets the current working directory.
void get_root(Node *&root)
Gets the root of the file system.
bool rename_file(const std::string &old_name, const std::string &new_name)
Renames or moves a file.
void get_subdirectories(const std::string &path, std::vector< std::string > &result, bool recursive=false)
Lists sub-directories in a directory.
std::string normalized_path(const std::string &path)
Normalizes a path.
bool set_executable_flag(const std::string &filename)
Marks a filename as executable.
bool is_file(const std::string &path)
Checks if a path is a regular file.
bool touch(const std::string &filename)
Modifies the last modification time of a file.
std::string base_name(const std::string &path, bool remove_extension=true)
Gets a path base name.
bool create_directory(const std::string &path)
Creates a directory.
void flip_slashes(std::string &path)
Converts a path to Unix format.
std::string documents_directory()
Gets the current user's home directory.
bool get_directory_entries(const std::string &path, std::vector< std::string > &result)
Lists directory contents.
bool is_directory(const std::string &path)
Checks if a path is a directory.
std::string extension(const std::string &path)
Gets a path extension.
bool can_write_directory(const std::string &path, bool create_missing_directories=false)
Tests whether a directory can be written to.
bool delete_directory(const std::string &path)
Deletes a directory.
void initialize()
Initializes the FileSystem library.
Numeric::uint64 get_time_stamp(const std::string &path)
Gets a file last modification time.
uint64_t uint64
Definition: numeric.h:144
Global Vorpaline namespace.
Definition: basic.h:55
Types and functions for numbers manipulation.
Pointers with automatic reference counting.