added new IPv4Address class
[libasyncio] / utils / asyncio_system_tools.hpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /**
21  * @file
22  * @brief some functions to wrap some system functions
23  *
24  * @author Reinhard Pfau \<Reinhard.Pfau@gmx.de\>
25  *
26  * @copyright &copy; Copyright 2009 by Intra2net AG
27  * @contact Intra2net Opensource Team \<opensource@intra2net.com\>
28  */
29
30
31 #ifndef _ASYNCIO_SYSTEM_TOOLS_HPP_
32 #define _ASYNCIO_SYSTEM_TOOLS_HPP_
33
34 #include <string>
35 #include <sys/types.h>
36
37
38 namespace AsyncIo
39 {
40 namespace Utils
41 {
42
43 /**
44  * @brief removed a file entry from file system (if existing).
45  * @param path path to be removed.
46  * @return @a true if the file was successfully deleted.
47  */
48 bool unlink( const std::string& path );
49
50
51 /**
52  * @brief represents a file(/path) status.
53  *
54  * This is basically a wrapper around the "stat" call.
55  */
56 class FileStat
57 {
58     public:
59         FileStat( const std::string& path, bool follow_symlinks= true );
60         ~FileStat();
61
62         /**
63          * @brief stat the path again and updates the values.
64          */
65         void refresh();
66
67         /**
68          * @brief returns if the stat values are valid.
69          * @return @a true if the stat values are valid.
70          */
71         bool is_valid() const { return m_is_valid; }
72
73         operator bool() const { return m_is_valid; }
74
75         mode_t mode() const { return m_mode; }
76
77         bool is_regular_file() const { return m_is_regular_file; }
78         bool is_directory() const { return m_is_directory; }
79         bool is_character_device() const { return m_is_character_device; }
80         bool is_block_device() const { return m_is_block_device; }
81         bool is_fifo() const { return m_is_fifo; }
82         bool is_symbolic_link() const { return m_is_symbolic_link; }
83         bool is_link() const { return m_is_symbolic_link; }
84         bool is_socket() const { return m_is_socket; }
85
86     private:
87         std::string m_path;
88         bool m_follow_symlinks;
89
90         bool m_is_valid;
91
92         mode_t m_mode;
93
94         bool m_is_regular_file      :1;
95         bool m_is_directory         :1;
96         bool m_is_character_device  :1;
97         bool m_is_block_device      :1;
98         bool m_is_fifo              :1;
99         bool m_is_symbolic_link     :1;
100         bool m_is_socket            :1;
101
102 }; // end of FileStat
103
104
105 /*
106  * IPv4 support
107  */
108
109 class IPv4Address
110 {
111     public:
112         IPv4Address();
113         IPv4Address( uint32_t _address );
114
115         bool is_valid() const {return m_valid;}
116
117         uint32_t get_address() const { return m_address; }
118
119         uint32_t get_address_nbo() const;
120
121     private:
122
123         uint32_t m_address;
124         bool m_valid;
125 }; // end of IPv4Address
126
127
128 } // end of namespace Utils
129 } // end of namespace AsyncIo
130
131
132 #endif