]> git.tdb.fi Git - libs/core.git/blob - source/utils.h
Add is_dir and get_home_dir functions
[libs/core.git] / source / utils.h
1 /* $Id$
2
3 This file is part of libmsppath
4 Copyright © 2006-2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_PATH_UTILS_H_
9 #define MSP_PATH_UTILS_H_
10
11 #include <sys/stat.h>
12 #include <list>
13 #include <string>
14
15 namespace Msp {
16
17 class Path;
18
19 struct Filename
20 {
21         std::string base;
22         std::string ext;
23 };
24
25 Path fix_case(const Path &path);
26
27 /// Creates a directory
28 void mkdir(const Path &path, int mode);
29
30 /// Creates a directory and any required parent directories
31 void mkpath(const Path &path, int mode);
32
33 /// Removes a directory
34 void rmdir(const Path &path);
35
36 /// Removes a directory and anything it contains
37 void rmdirs(const Path &path);
38
39 /// Removes a file
40 void unlink(const Path &path);
41
42 /// Lists the contents of a directory
43 std::list<std::string> list_files(const Path &path);
44
45 Filename splitext(const std::string &);
46 int fnmatch(const std::string &, const Path &);
47
48 /// Makes a path relative to some base path.  That is, base/result==path.
49 Path relative(const Path &path, const Path &base);
50
51 std::string basename(const std::string &);
52
53 /// Tests for existence of a file
54 bool exists(const Path &path);
55
56 /// Tests whether a path refers to an existing directory
57 bool is_dir(const Path &paht);
58
59 /**
60 Gets information about a file.  Returns 0 on success or -1 on error.  This
61 version can be used to check for file existence and get information in one
62 call.
63 */
64 int stat(const Path &fn, struct stat &st);
65
66 /**
67 Returns information about a file.  This version throws an exception if an error
68 occurs.
69 */
70 struct stat stat(const Path &fn);
71
72 Path getcwd();
73
74 /// Returns the user's home directory
75 Path get_home_dir();
76
77 inline bool is_windows_drive(const std::string &p)
78 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
79
80 } // namespace Msp
81
82 #endif