Allolib  1.0
C++ Components For Interactive Multimedia
al_File.hpp
1 #ifndef INCLUDE_AL_FILE_HPP
2 #define INCLUDE_AL_FILE_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  Utilities for file management
42 
43  File author(s):
44  Graham Wakefield, 2010, grrrwaaa@gmail.com
45  Lance Putnam, 2010, putnam.lance@gmail.com
46  Keehong Youn, 2017, younkeehong@gmail.com
47 */
48 
49 #include <stdio.h>
50 
51 #include <algorithm>
52 #include <functional>
53 #include <iostream>
54 #include <list>
55 #include <mutex>
56 #include <string>
57 #include <vector>
58 
59 #ifndef AL_FILE_DELIMITER_STR
60 #ifdef AL_WINDOWS
61 #define AL_FILE_DELIMITER_STR "\\"
62 #else
63 #define AL_FILE_DELIMITER_STR "/"
64 #endif
65 #endif
66 
67 #define AL_FILE_DELIMITER (AL_FILE_DELIMITER_STR[0])
68 
69 #define AL_PATH_MAX (4096)
70 
71 typedef long long int
72  al_nsec;
73 typedef double al_sec;
75 namespace al {
76 
77 std::string demangle(const char *name); // Utility function.
81 class FilePath {
82 public:
83  FilePath() {}
84 
87  FilePath(const std::string &file, const std::string &path);
88 
91  explicit FilePath(const std::string &fullpath);
92 
94  const std::string &file() const { return mFile; }
95 
97  const std::string &path() const { return mPath; }
98 
100  std::string filepath() const { return path() + file(); }
101 
103  bool valid() const { return file() != ""; }
104 
106  FilePath &file(const std::string &v) {
107  mFile = v;
108  return *this;
109  }
110 
112  FilePath &path(const std::string &v) {
113  mPath = v;
114  return *this;
115  }
116 
117 protected:
118  std::string mPath;
119  std::string mFile;
120 };
121 
122 #if 0 /* commenting out FileInfo */
126 class FileInfo{
127 public:
128 
130  enum Type{
131  NOFILE,
132  REG,
133  DIR,
134  CHR,
135  BLOCK,
136  PIPE,
137  LINK,
138  SOCKET,
139  UNKNOWN = 127
140  };
141 
143  FileInfo& type(Type v){ mType=v; return *this; }
144 
146  const Type type() const { return mType; }
147 
149  FileInfo& name(const std::string& v){ mName=v; return *this; }
150 
152  const std::string& name() const { return mName; }
153 
154 private:
155  Type mType;
156  std::string mName;
157 };
158 #endif /* commenting out FileInfo */
159 
161 
166 class File {
167 public:
171  File(std::string path = ".", std::string mode = "r", bool open_ = false);
172  File(const FilePath &path, std::string mode = "r", bool open_ = false);
173 
174  ~File();
175 
177 
181  bool open(const std::string &path, const std::string &mode = "r");
182 
184 
187  bool open();
188 
190  void close();
191 
193 
196  File &mode(const std::string &v) {
197  mMode = v;
198  return *this;
199  }
200 
202  File &path(const std::string &v) {
203  mPath = v;
204  return *this;
205  }
206 
208  int write(const std::string &v) {
209  return write(v.data(), 1, static_cast<int>(v.length()));
210  }
211 
213  int write(const void *v, int itemSizeInBytes, int items = 1) {
214  int itemsWritten = static_cast<int>(fwrite(v, itemSizeInBytes, items, mFP));
215  mSizeBytes += itemsWritten * itemSizeInBytes;
216  return itemsWritten;
217  }
218 
220  int read(void *v, int size, int items = 1) {
221  return static_cast<int>(fread(v, size, items, mFP));
222  }
223 
225  const char *readAll();
226 
228  bool opened() const { return 0 != mFP; }
229 
231  const std::string &mode() const { return mMode; }
232 
234  const std::string &path() const { return mPath; }
235 
237  size_t size() const { return mSizeBytes; }
238 
241  // al_sec modified() const;
242 
245  // al_sec accessed() const;
246 
249  // al_sec created() const;
250 
252  // size_t sizeFile() const;
253 
255  // size_t storage() const;
256 
257  FILE *filePointer() { return mFP; }
258 
260  static std::string read(const std::string &path);
261 
263  static int write(const std::string &path, const void *v, int size,
264  int items = 1);
265 
267  static int write(const std::string &path, const std::string &data);
268 
271  static bool copy(const std::string &srcPath, const std::string &dstPath,
272  unsigned int bufferSize = 1e6);
273 
275  static bool remove(const std::string &path);
276 
278 
281  static std::string conformDirectory(const std::string &dir);
282 
284 
288  static std::string conformPathToOS(const std::string &path);
289 
291  static std::string absolutePath(const std::string &path);
292 
293  static bool isRelativePath(const std::string &path);
294 
298  static std::string currentPath();
299 
300  static bool isSamePath(const std::string &path1, const std::string &path2);
301 
303 
308  static std::string baseName(const std::string &path,
309  const std::string &suffix = "");
310 
312 
316  static std::string directory(const std::string &path);
317 
319 
322  static std::string extension(const std::string &path);
323 
325  static bool exists(const std::string &path);
326 
328  static bool exists(const std::string &name, const std::string &path) {
329  return exists(path + name);
330  }
331 
333  static bool isDirectory(const std::string &path);
334 
336 
344  static bool searchBack(std::string &rootPath, const std::string &matchPath,
345  int maxDepth = 6);
346 
353  static bool searchBack(std::string &path, int maxDepth = 6);
354 
355  static al_sec modificationTime(const char *path);
356  // TODO: Implement these.
357  // static al_sec modified(const std::string& path){ return
358  // File(path).modified(); } static al_sec accessed(const std::string& path){
359  // return File(path).accessed(); } static al_sec created (const std::string&
360  // path){ return File(path).created(); } static size_t sizeFile(const
361  // std::string& path){ return File(path).sizeFile(); } static size_t storage
362  // (const std::string& path){ return File(path).storage(); }
363 
364 protected:
365  // class Impl; Impl * mImpl;
366 
367  std::string mPath;
368  std::string mMode;
369  char *mContent;
370  size_t mSizeBytes;
371  FILE *mFP;
372 
373  void dtor();
374  void freeContent();
375  void allocContent(int n);
376  void getSize();
377 
378  friend class Dir;
379 };
380 
382 public:
383  PushDirectory(std::string directory, bool verbose = false);
384 
385  ~PushDirectory();
386 
387 private:
388  char previousDirectory[4096];
389  bool mVerbose;
390 
391  static std::mutex mDirectoryLock; // Protects all instances of PushDirectory
392 };
393 
397 class Dir {
398 public:
400  // Dir() {};
401 
403  // Dir(std::string dirToOpen): mDirToOpen(dirToOpen) {};
404 
405  // ~Dir() = default;
406 
408 
411  // bool open(const std::string& dirPath);
412 
414 
417  // bool close();
418 
420 
423  // bool read();
424 
426  // const FileInfo& entry() const { return mEntry; }
427 
429  // bool rewind();
430 
432  // static bool make(const std::string& path, bool recursive=true);
433  static bool make(const std::string &path);
434 
436  static bool remove(const std::string &path);
437 
439  static bool removeRecursively(const std::string &path);
440 
441 private:
442  // class Impl; Impl * mImpl;
443  // std::string mDirToOpen;
444  // FileInfo mEntry;
445 };
446 
450 class FileList {
451 public:
452  typedef std::vector<FilePath>::iterator iterator;
453 
454  FileList() : indx(0) {}
455 
457  FilePath &operator()() { return mFiles[indx]; }
458 
460  // FilePath& select(const std::string& filename);
461 
462  FilePath &select(int i) {
463  indx = i % count();
464  return (*this)();
465  }
466  FilePath &next() {
467  ++indx %= count();
468  return (*this)();
469  }
470  FilePath &prev() {
471  --indx;
472  if (indx < 0)
473  indx = count() - 1;
474  return (*this)();
475  }
476 
477  int count() { return static_cast<int>(mFiles.size()); }
478 
479  void sort();
480 
481  void print(std::ostream &stream = std::cout) const;
482 
483  FilePath &operator[](int i) { return mFiles[i]; }
484  iterator begin() { return mFiles.begin(); }
485  iterator end() { return mFiles.end(); }
486 
487  void add(FilePath &fp) { mFiles.push_back(fp); }
488  void add(FilePath &&fp) { mFiles.push_back(fp); }
489  void add(FileList const &fl) {
490  mFiles.insert(mFiles.end(), fl.mFiles.begin(), fl.mFiles.end());
491  }
492  void sort(bool (*f)(FilePath, FilePath)) { std::sort(begin(), end(), f); }
493 
494 protected:
495  int indx;
496  std::vector<FilePath> mFiles;
497 };
498 
502 class SearchPaths {
503 public:
504  typedef std::pair<std::string, bool> searchpath;
505  typedef std::list<searchpath> searchpathlist;
506  typedef std::list<searchpath>::iterator iterator;
507 
508  SearchPaths() {}
509  SearchPaths(const std::string &file);
510  SearchPaths(int argc, char *const argv[], bool recursive = true);
511  SearchPaths(const SearchPaths &cpy);
512 
514  FilePath find(const std::string &filename);
515  // FileList glob(const std::string& regex);
516  FileList filter(bool (*f)(FilePath const &));
517  FileList listAll();
518 
520  void addSearchPath(const std::string &path, bool recursive = true);
521  void addRelativePath(std::string rel, bool recursive = true) {
522  addSearchPath(appPath() + rel, recursive);
523  }
524 
527  void addAppPaths(int argc, char *const argv[], bool recursive = true);
528  void addAppPaths(int argc, const char **argv, bool recursive = true);
529  void addAppPaths(std::string path, bool recursive = true);
530  void addAppPaths(bool recursive = true);
531 
532  const std::string &appPath() const { return mAppPath; }
533 
534  void print(std::ostream &stream = std::cout) const;
535 
536  iterator begin() { return mSearchPaths.begin(); }
537  iterator end() { return mSearchPaths.end(); }
538 
539 protected:
540  std::list<searchpath> mSearchPaths;
541  std::string mAppPath;
542 };
543 
544 // below are original to allolib and did not exist in AlloSystem
545 // TODO: NEED TO CLEAN THESE CALLS...
546 
547 // not recursive, contains dirs
548 FileList itemListInDir(std::string const &dir);
549 
550 // recursive, does not contain dirs
551 FileList fileListFromDir(std::string const &dir);
552 
553 //
554 FilePath searchFileFromDir(std::string const &filename, std::string const &dir);
555 
556 // pass lambda with given signature, returns list of files that f(file) is true
557 FileList filterInDir(std::string const &dir,
558  std::function<bool(FilePath const &)> f,
559  bool recursive = false);
560 
561 // returns true if given file name/path ends in extension
562 bool checkExtension(std::string const &filename, std::string const &extension);
563 bool checkExtension(FilePath const &filepath, std::string const &extension);
564 
565 } // namespace al
566 
567 #endif
static bool make(const std::string &path)
Constructor. This does not attempt to open the directory.
static bool remove(const std::string &path)
Remove a directory.
static bool removeRecursively(const std::string &path)
Remove a directory recursively.
File.
Definition: al_File.hpp:166
static bool searchBack(std::string &path, int maxDepth=6)
static bool remove(const std::string &path)
Delete file from file system.
static bool exists(const std::string &path)
Returns whether a file or directory exists.
static std::string absolutePath(const std::string &path)
Convert relative paths to absolute paths.
static bool exists(const std::string &name, const std::string &path)
Returns whether a file in a directory exists.
Definition: al_File.hpp:328
static int write(const std::string &path, const void *v, int size, int items=1)
Quick and dirty write memory to file.
static std::string read(const std::string &path)
Quick and dirty read of all bytes from file.
File & path(const std::string &v)
Set path of file.
Definition: al_File.hpp:202
static std::string conformPathToOS(const std::string &path)
Conforms path.
const std::string & path() const
Returns path string.
Definition: al_File.hpp:234
void close()
Close file.
static std::string conformDirectory(const std::string &dir)
Returns string ensured to having an ending delimiter.
const std::string & mode() const
Returns file i/o mode string.
Definition: al_File.hpp:231
static std::string baseName(const std::string &path, const std::string &suffix="")
Returns the base name of path.
File(std::string path=".", std::string mode="r", bool open_=false)
static std::string directory(const std::string &path)
Returns the directory part of path.
int write(const std::string &v)
Write string to file.
Definition: al_File.hpp:208
static int write(const std::string &path, const std::string &data)
Quick and dirty write character string to file.
int write(const void *v, int itemSizeInBytes, int items=1)
Write memory elements to file.
Definition: al_File.hpp:213
static std::string currentPath()
const char * readAll()
Returns character string of file contents (read mode only)
int read(void *v, int size, int items=1)
Read memory elements from file.
Definition: al_File.hpp:220
bool open(const std::string &path, const std::string &mode="r")
Open file.
static bool isDirectory(const std::string &path)
Returns true if path is a directory.
static bool copy(const std::string &srcPath, const std::string &dstPath, unsigned int bufferSize=1e6)
File & mode(const std::string &v)
Set i/o mode.
Definition: al_File.hpp:196
static std::string extension(const std::string &path)
Returns extension of file name.
FILE * filePointer()
Return size file (or 0 on failure)
Definition: al_File.hpp:257
static bool searchBack(std::string &rootPath, const std::string &matchPath, int maxDepth=6)
Search for file or directory back from current directory.
bool opened() const
Returns whether file is open.
Definition: al_File.hpp:228
size_t size() const
Returns size, in bytes, of file contents.
Definition: al_File.hpp:237
bool open()
Open file using member variables.
FilePath & operator()()
return currently selected file in list
Definition: al_File.hpp:457
FilePath & select(int i)
find a file in list
Definition: al_File.hpp:462
FilePath(const std::string &fullpath)
const std::string & file() const
Get file name without directory.
Definition: al_File.hpp:94
const std::string & path() const
Get path (directory) of file.
Definition: al_File.hpp:97
bool valid() const
Returns whether file part is valid.
Definition: al_File.hpp:103
FilePath & path(const std::string &v)
Set path (directory) of file.
Definition: al_File.hpp:112
FilePath & file(const std::string &v)
Set file name without directory.
Definition: al_File.hpp:106
FilePath(const std::string &file, const std::string &path)
std::string filepath() const
Get file with directory.
Definition: al_File.hpp:100
FilePath find(const std::string &filename)
find a file in the searchpaths
void addAppPaths(int argc, char *const argv[], bool recursive=true)
void addSearchPath(const std::string &path, bool recursive=true)
add a path to search in; recursive searching is optional
void sort(T &value1, T &value2)
Definition: al_App.hpp:23