Filesys.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 /*
34  * A lightweight, cross platform solution to accessing filesystems.
35  *
36  * Johnathan Corkery, 2013
37  */
38 
39 #ifndef FILESYS_H_INCLUDED
40 #define FILESYS_H_INCLUDED
41 
42 
43 const int DIRECTORY_SIZE_LIMIT = 256;
44 
45 
46 #include <string>
47 #include <vector>
48 
49 
50 
51 namespace Dynacoe {
52 
53 enum __OS {
54  UNIX_VARIANT,
55  WINDOWS,
56  UNKNOWN
57 };
58 
59 
70 class Filesys {
71  struct Object {
72  bool file;
73  int size;
74  std::string name;
75  Object();
76  };
77 
78  __OS type;
79 
80 
81  std::string currentPath;
82  std::vector<Object> currentPathObjs;
83  std::string home;
84  void _init();
85  std::string FFH(const std::string &, const std::string &, bool);
86  std::vector<std::string> FAFH(const std::string &,
87  const std::string &,
88  std::vector<std::string>);
89 
90  __OS GetType();
91 
92  public:
93 
94 
97  class Directory {
98  std::vector<Object> vec;
99  int numFiles;
100  int pos;
101 
102 
103  public:
108  std::string GetNextName();
109 
112  void ResetPosition();
113 
116  int Size();
117 
120  Directory & operator >> (std::string & str);
121 
124  bool AtEnd();
125 
128  bool operator()();
129 
130 
133  bool Query(const std::string & target);
134 
138  bool IsFile(const std::string & obj);
139 
140  Directory(const std::vector<Object> & src);
141  };
142 
148  Filesys();
149  Filesys(const std::string & initDir);
151 
154  bool ChangeDir(const std::string &);
155 
161  bool GoToChild(const std::string &);
162 
163 
166  bool GoToParent();
167 
170  std::string GetCWD();
171 
175  bool CreateDir(const std::string &);
176 
177 
182 
189  std::string FindFile(const std::string & name);
190 
195  std::string FindDirectory(const std::string & name);
196 
197 
202  std::vector<std::string> FindAllFiles(const std::string & name);
203 
204 
205 
206 
207 
208 };
209 };
210 
211 #include "FilesysPreprocessor.h"
212 
213 #endif
214 
Directory & operator>>(std::string &str)
Equivalent to getNextName().
std::string FindFile(const std::string &name)
Searches for the file of the specified name. The search is done in the current directory and all sub ...
Cross-Platform filesystem access.
Definition: Filesys.h:70
Directory QueryDirectory()
return all the object names in the current working directory as a directory instance. See the Directory object for more details. Special directories are not included
void ResetPosition()
Resets the reading position to the first name on the list.
int Size()
Returns the number of names contained within the directory.
The container for managing directory objects.
Definition: Filesys.h:97
bool ChangeDir(const std::string &)
Change to the specified directory. Returns true on success.
Definition: AssetID.h:37
bool IsFile(const std::string &obj)
Returns whether or nor obj is a file. PROTIP: if the Filesys::Object is not a file, it is most likely a directory!
bool GoToParent()
Change directory to the parent directory.
std::string FindDirectory(const std::string &name)
Searches for the directory of the specified name. THe search is done in the current and all sub direc...
std::string GetCWD()
return the current working directory.
bool Query(const std::string &target)
Returns whether or not target exists.
bool CreateDir(const std::string &)
Creates a directory with the given name. The directory is relative to the current working directory...
std::string GetNextName()
Pulls the next directory name from the list. The order of the elements in the list depends on the ope...
bool operator()()
Equivalent to atEnd()
bool GoToChild(const std::string &)
Change to the specified child directory. NOTICE: you only need to specify the name of the directory E...
bool AtEnd()
Returns if all files have been exhausted.
std::vector< std::string > FindAllFiles(const std::string &name)
Similar to findFile, but returns a vector of paths. The paths coresponding to all instances of name t...