Update date in Doxyfile
[anna.git] / source / io / functions.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 #include <poll.h>
13
14 #include <anna/core/util/Tokenizer.hpp>
15 #include <anna/config/defines.hpp>
16 #include <anna/core/functions.hpp>
17
18 #include <anna/io/functions.hpp>
19
20 using namespace std;
21 using namespace anna;
22
23 void io::functions::mkdir(const std::string& path)
24 throw(RuntimeException) {
25   Tokenizer tokenizer;
26   string w;
27   int r;
28   tokenizer.apply(path, "/");
29
30   for(Tokenizer::const_iterator ii = tokenizer.begin(), maxii = tokenizer.end(); ii != maxii; ii ++) {
31     w += "/";
32     w += Tokenizer::data(ii);
33
34     if(io::functions::exists(w) == false) {
35       anna_signal_shield(r, ::mkdir(w.c_str(), S_IRWXU | S_IRWXG));
36
37       if(r == -1)
38         throw RuntimeException(path, errno, ANNA_FILE_LOCATION);
39     }
40   }
41 }
42
43 bool io::functions::exists(const char* path)
44 throw(RuntimeException) {
45   struct stat data;
46   int r;
47   anna_signal_shield(r, stat(path, &data));
48
49   if(r != -1)
50     return true;
51
52   if(errno == ENOENT)
53     return false;
54
55   throw RuntimeException(path, errno, ANNA_FILE_LOCATION);
56 }
57
58 bool io::functions::isADirectory(const char* path)
59 throw(RuntimeException) {
60   struct stat data;
61   int r;
62   anna_signal_shield(r, stat(path, &data));
63
64   if(r != 0)
65     throw RuntimeException(path, errno, ANNA_FILE_LOCATION);
66
67   return ((data.st_mode & S_IFDIR) != 0);
68 }
69
70 ino_t io::functions::getINode(const char* path)
71 throw(RuntimeException) {
72   struct stat data;
73   int r;
74   anna_signal_shield(r, stat(path, &data));
75
76   if(r != 0)
77     throw RuntimeException(path, errno, ANNA_FILE_LOCATION);
78
79   return data.st_ino;
80 }
81
82 /*static*/
83 bool io::functions::waitInput(const int fd, const Millisecond &timeout)
84 throw(RuntimeException) {
85   pollfd waiting;
86   int r;
87   waiting.fd = fd;
88   waiting.events = POLLIN;
89   anna_signal_shield(r, poll(&waiting, 1, timeout));
90
91   if(r == -1) {
92     string msg("io::functions::waitInput: ");
93     msg += anna::functions::asString(fd);
94     throw RuntimeException(msg, errno, ANNA_FILE_LOCATION);
95   }
96
97   return (r != 0);
98 }
99