-rw-r--r-- | include/konforka/util.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/include/konforka/util.h b/include/konforka/util.h new file mode 100644 index 0000000..c06edd9 --- a/dev/null +++ b/include/konforka/util.h | |||
@@ -0,0 +1,103 @@ | |||
1 | #ifndef __KONFORKA_UTIL_H | ||
2 | #define __KONFORKA_UTIL_H | ||
3 | |||
4 | #include <sys/types.h> | ||
5 | #include <string> | ||
6 | #include <konforka/exception.h> | ||
7 | |||
8 | /** | ||
9 | * @file | ||
10 | * @brief miscellaneous utility stuff. | ||
11 | */ | ||
12 | |||
13 | /** | ||
14 | * @brief The main konforka namespace. | ||
15 | */ | ||
16 | namespace konforka { | ||
17 | using std::string; | ||
18 | |||
19 | class restricted_sequence_error : public konforka::exception { | ||
20 | public: | ||
21 | restricted_sequence_error(const string& fi,const string& fu,int l,const string& w) | ||
22 | : konforka::exception(fi,fu,l,w) { } | ||
23 | }; | ||
24 | |||
25 | /** | ||
26 | * normalize_path options enumeration. | ||
27 | * @see normalize_path() | ||
28 | */ | ||
29 | enum normalize_path_options { | ||
30 | /** | ||
31 | * Restrict the /../ sequence. | ||
32 | */ | ||
33 | restrict_dotdot = 1, | ||
34 | /** | ||
35 | * Strip out the leading slash. | ||
36 | */ | ||
37 | strip_leading_slash = 2, | ||
38 | /** | ||
39 | * Strip out the trailing slash. | ||
40 | */ | ||
41 | strip_trailing_slash = 4 | ||
42 | }; | ||
43 | |||
44 | /** | ||
45 | * Normalize pathname by stripping duplicate slashes, etc. | ||
46 | * @param p the pathname. | ||
47 | * @param o options. | ||
48 | * @return the normalized path. | ||
49 | * @see normalize_path_options | ||
50 | * @todo TODO: document exceptions. | ||
51 | */ | ||
52 | string normalize_path(const string& p,int o=(restrict_dotdot|strip_trailing_slash)); | ||
53 | |||
54 | /** | ||
55 | * Extract the directory part of the filename. | ||
56 | * @param p the pathname. | ||
57 | * @return the directory part. | ||
58 | */ | ||
59 | string dir_name(const string& p); | ||
60 | |||
61 | class beyond_root_error : public konforka::exception { | ||
62 | public: | ||
63 | beyond_root_error(const string& fi,const string& fu,int l,const string& w) | ||
64 | : konforka::exception(fi,fu,l,w) { } | ||
65 | }; | ||
66 | |||
67 | /** | ||
68 | * combine_path options enumeration. | ||
69 | * @see combine_path() | ||
70 | */ | ||
71 | enum combine_path_options { | ||
72 | /** | ||
73 | * The origin is file. Otherwise it is directory. | ||
74 | */ | ||
75 | origin_is_file = 1, | ||
76 | /** | ||
77 | * Fail if we've gone up beyond root. | ||
78 | */ | ||
79 | fail_beyond_root = 2 | ||
80 | }; | ||
81 | |||
82 | /** | ||
83 | * Combine path with the relative path. | ||
84 | * @param orig the origin. | ||
85 | * @param rel relative path to combine with. | ||
86 | * @param o options. | ||
87 | * @return the paths combined. | ||
88 | * @see combine_path_options | ||
89 | * @todo TODO: document exceptions. | ||
90 | */ | ||
91 | string combine_path(const string& orig,const string& rel,int o=origin_is_file); | ||
92 | |||
93 | /** | ||
94 | * Create directory and parent directories if needed. | ||
95 | * @param p the pathname. | ||
96 | * @param m mode value for the newly created directories. | ||
97 | */ | ||
98 | void make_path(const string& p,mode_t m); | ||
99 | |||
100 | } | ||
101 | |||
102 | /* vim:set ft=cpp: */ | ||
103 | #endif /* __KONFORKA_UTIL_H */ | ||