libutil++  1.9.3
 All Classes Functions Variables
File.h
1 /*
2 ** libutil++
3 ** $Id: File.h 1669 2016-04-09 20:07:12Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__util__File_H__
10 #define __libutilxx__sella__util__File_H__
11 
12 #include "../../common.h"
13 #include "FileException.h"
14 
15 #include <limits.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <time.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include <iostream>
28 #include <fstream>
29 
30 #include <set>
31 #include <vector>
32 #include <string>
33 
34 namespace sella {
35  namespace util {
36  class File;
37  }
38 }
39 
41  public:
42  static const std::string any; /* .* */
43  static const std::set<uint8_t> all; /* { DT_BLK, DT_CHR, DT_DIR, DT_FIFO, DT_LNK, DT_REG, DT_SOCK, DT_UNKNOWN } */
44  static const std::set<uint8_t> regular; /* { DT_REG } */
45  static const std::set<uint8_t> files; /* { DT_BLK, DT_CHR, DT_FIFO, DT_LNK, DT_REG, DT_SOCK, DT_UNKNOWN } */
46  static const std::set<uint8_t> dirs; /* { DT_DIR } */
47 
48  public:
49  static std::string getBaseName(const std::string &fullpath) throw ();
50  static std::string getDirName(const std::string &fullpath) throw ();
51  static std::string getRealPath(const std::string &fullpath) throw (FileException);
52 
53  static std::vector<std::string> getDirectory(const std::string &path, const std::string &regex = any, const std::set<uint8_t> &types = all) throw (FileException);
54 
55  static std::string read(const std::string &fullpath, size_t limit = 0, std::ios_base::openmode mode = std::ios_base::in) throw (FileException);
56  static std::string readline(const std::string &fullpath, char delim = '\n', std::ios_base::openmode mode = std::ios_base::in) throw (FileException);
57  static std::vector<std::string> readlines(const std::string &fullpath, size_t limit = 0, char delim = '\n', std::ios_base::openmode mode = std::ios_base::in) throw (FileException);
58 
59  static void write(const std::string &fullpath, const std::string &data, std::ios_base::openmode mode = std::ios_base::out) throw (FileException);
60 
61  static bool exists(const std::string &fullpath, bool followSymlink = true) throw ();
62  static bool isReadable(const std::string &fullpath) throw ();
63  static bool isRegularFile(const std::string &fullpath, bool followSymlink = true) throw (FileException);
64  static bool isDirectory(const std::string &fullpath, bool followSymlink = true) throw (FileException);
65  static bool isCharacterDevice(const std::string &fullpath, bool followSymlink = true) throw (FileException);
66  static bool isBlockDevice(const std::string &fullpath, bool followSymlink = true) throw (FileException);
67  static bool isFIFO(const std::string &fullpath, bool followSymlink = true) throw (FileException);
68  static bool isSymLink(const std::string &fullpath) throw (FileException);
69  static bool isSocket(const std::string &fullpath, bool followSymlink = true) throw (FileException);
70 
71  static uid_t getUID(const std::string &fullpath, bool followSymlink = true) throw (FileException);
72  static gid_t getGID(const std::string &fullpath, bool followSymlink = true) throw (FileException);
73  static int64_t getSize(const std::string &fullpath, bool followSymlink = true) throw (FileException); /* in bytes */
74  static int64_t getBlocks(const std::string &fullpath, bool followSymlink = true) throw (FileException);
75  static int64_t getBlockSize(const std::string &fullpath, bool followSymlink = true) throw (FileException);
76  static time_t getATime(const std::string &fullpath, bool followSymlink = true) throw (FileException);
77  static time_t getMTime(const std::string &fullpath, bool followSymlink = true) throw (FileException);
78  static time_t getCTime(const std::string &fullpath, bool followSymlink = true) throw (FileException);
79 
80  static mode_t itomode(int mode) throw (FileException);
81 
82  private:
83  static struct stat getStat(const std::string &fullpath, bool followSymlink = true) throw (FileException);
84  static mode_t getMode(const std::string &fullpath, bool followSymlink = true) throw (FileException);
85 };
86 
87 #endif
88 
89 /*
90 ** vim: noet ts=3 sw=3
91 */