summaryrefslogtreecommitdiffabout
path: root/include/konforka/util.h
blob: c06edd92a9526173e58f3a53ca4e69f3ef4db87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef __KONFORKA_UTIL_H
#define __KONFORKA_UTIL_H

#include <sys/types.h>
#include <string>
#include <konforka/exception.h>

/**
 * @file
 * @brief miscellaneous utility stuff.
 */

/**
 * @brief The main konforka namespace.
 */
namespace konforka {
    using std::string;

    class restricted_sequence_error : public konforka::exception {
	public:
	    restricted_sequence_error(const string& fi,const string& fu,int l,const string& w)
		: konforka::exception(fi,fu,l,w) { }
    };

    /**
     * normalize_path options enumeration.
     * @see normalize_path()
     */
    enum normalize_path_options {
	/**
	 * Restrict the /../ sequence.
	 */
	restrict_dotdot = 1,
	/**
	 * Strip out the leading slash.
	 */
	strip_leading_slash = 2,
	/**
	 * Strip out the trailing slash.
	 */
	strip_trailing_slash = 4
    };

    /**
     * Normalize pathname by stripping duplicate slashes, etc.
     * @param p the pathname.
     * @param o options.
     * @return the normalized path.
     * @see normalize_path_options
     * @todo TODO: document exceptions.
     */
    string normalize_path(const string& p,int o=(restrict_dotdot|strip_trailing_slash));

    /**
     * Extract the directory part of the filename.
     * @param p the pathname.
     * @return the directory part.
     */
    string dir_name(const string& p);

    class beyond_root_error : public konforka::exception {
	public:
	    beyond_root_error(const string& fi,const string& fu,int l,const string& w)
		: konforka::exception(fi,fu,l,w) { }
    };

    /**
     * combine_path options enumeration.
     * @see combine_path()
     */
    enum combine_path_options {
	/**
	 * The origin is file. Otherwise it is directory.
	 */
	origin_is_file = 1,
	/**
	 * Fail if we've gone up beyond root.
	 */
	fail_beyond_root = 2
    };

    /**
     * Combine path with the relative path.
     * @param orig the origin.
     * @param rel relative path to combine with.
     * @param o options.
     * @return the paths combined.
     * @see combine_path_options
     * @todo TODO: document exceptions.
     */
    string combine_path(const string& orig,const string& rel,int o=origin_is_file);

    /**
     * Create directory and parent directories if needed.
     * @param p the pathname.
     * @param m mode value for the newly created directories.
     */
    void make_path(const string& p,mode_t m);

}

 /* vim:set ft=cpp: */
#endif /* __KONFORKA_UTIL_H */