1 /*
2 ** Copyright 2012-2013 Centreon
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 **
16 ** For more information : contact@centreon.com
17 */
18 
19 #include <cstdlib>
20 #include <iostream>
21 #include "com/centreon/exceptions/basic.hh"
22 #include "com/centreon/io/file_entry.hh"
23 #include "com/centreon/io/file_stream.hh"
24 
25 unsigned int const DATA_SIZE = 42;
26 
27 using namespace com::centreon;
28 
29 /**
30  *  Create temporary file.
31  *
32  *  @param[in] path  The temporary file path.
33  */
create_fake_file(std::string const & path)34 static void create_fake_file(std::string const& path) {
35   if (!io::file_stream::exists(path)) {
36     io::file_stream fs;
37     fs.open(path, "w");
38     fs.close();
39   }
40 }
41 
42 /**
43  *  Check path information.
44  *
45  *  @return EXIT_SUCCESS on success.
46  */
main()47 int main() {
48   int ret(EXIT_FAILURE);
49 
50   std::string p1("/tmp/test.ext");
51   std::string p2("/tmp/.test");
52   std::string p3("/tmp/test");
53 
54   try {
55     create_fake_file(p1);
56     create_fake_file(p2);
57     create_fake_file(p3);
58 
59     io::file_entry e1(p1);
60     if (e1.base_name() != "test")
61       throw(basic_error() << "invalid base name");
62     if (e1.file_name() != "test.ext")
63       throw(basic_error() << "invalid file name");
64     if (e1.directory_name() != "/tmp")
65       throw(basic_error() << "invalid directory name");
66 
67     io::file_entry e2(p2);
68     if (e2.base_name() != ".test")
69       throw(basic_error() << "invalid base name");
70     if (e2.file_name() != ".test")
71       throw(basic_error() << "invalid file name");
72     if (e2.directory_name() != "/tmp")
73       throw(basic_error() << "invalid directory name");
74 
75     io::file_entry e3(p3);
76     if (e3.base_name() != "test")
77       throw(basic_error() << "invalid base name");
78     if (e3.file_name() != "test")
79       throw(basic_error() << "invalid file name");
80     if (e3.directory_name() != "/tmp")
81       throw(basic_error() << "invalid directory name");
82 
83     ret = EXIT_SUCCESS;
84   } catch (std::exception const& e) {
85     std::cerr << e.what() << std::endl;
86   }
87 
88   io::file_stream::remove(p1);
89   io::file_stream::remove(p2);
90   io::file_stream::remove(p3);
91 
92   return (ret);
93 }
94