]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/FS.h
added install instructions
[gen-shell.git] / src / libshared / src / FS.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #ifndef INCLUDED_FS
28 #define INCLUDED_FS
29
30 #include <stdio.h>
31 #include <string>
32 #include <vector>
33 #include <sys/stat.h>
34
35 #ifndef PATH_MAX
36 #define PATH_MAX 4096
37 #endif
38
39 class Path
40 {
41 public:
42   Path ();
43   explicit Path (const Path&);
44   Path (const std::string&);
45
46   Path& operator= (const Path&);
47   bool operator== (const Path&);
48   Path& operator+= (const std::string&);
49   operator std::string () const;
50
51   std::string name () const;
52   std::string parent () const;
53   std::string extension () const;
54   bool exists () const;
55   bool is_directory () const;
56   bool is_absolute () const;
57   bool is_link () const;
58   bool readable () const;
59   bool writable () const;
60   bool executable () const;
61   bool rename (const std::string&);
62
63   // Statics
64   static std::string expand (const std::string&);
65   static std::vector<std::string> glob (const std::string&);
66
67 public:
68   std::string _original;
69   std::string _data;
70 };
71
72 class File : public Path
73 {
74 public:
75   File ();
76   explicit File (const Path&);
77   explicit File (const File&);
78   File (const std::string&);
79   virtual ~File ();
80
81   File& operator= (const File&);
82
83   virtual bool create (int mode = 0640);
84   virtual bool remove () const;
85
86   bool open ();
87   void close ();
88
89   bool lock ();
90   void unlock ();
91
92   void read (std::string&);
93   void read (std::vector <std::string>&);
94
95   void append (const std::string&);
96   void append (const std::vector <std::string>&);
97   void write_raw (const std::string&);
98
99   void truncate ();
100
101   virtual mode_t mode ();
102   virtual size_t size () const;
103   virtual time_t mtime () const;
104   virtual time_t ctime () const;
105   virtual time_t btime () const;
106
107   static bool create (const std::string&, int mode = 0640);
108   static bool read (const std::string&, std::string&);
109   static bool read (const std::string&, std::vector <std::string>&);
110   static bool write (const std::string&, const std::string&);
111   static bool write (const std::string&, const std::vector <std::string>&, bool addNewlines = true);
112   static bool remove (const std::string&);
113   static bool copy (const std::string&, const std::string&);
114   static bool move (const std::string&, const std::string&);
115   static std::string removeBOM (const std::string&);
116
117 private:
118   FILE* _fh;
119   int   _h;
120   bool  _locked;
121 };
122
123 class Directory : public File
124 {
125 public:
126   Directory ();
127   explicit Directory (const Directory&);
128   explicit Directory (const File&);
129   explicit Directory (const Path&);
130   Directory (const std::string&);
131
132   Directory& operator= (const Directory&);
133
134   virtual bool create (int mode = 0755);
135   virtual bool remove () const;
136
137   std::vector <std::string> list ();
138   std::vector <std::string> listRecursive ();
139
140   static std::string cwd ();
141   bool up ();
142   bool cd () const;
143
144 private:
145   void list (const std::string&, std::vector <std::string>&, bool);
146   bool remove_directory (const std::string&) const;
147 };
148
149 #endif