Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
posix.cpp
Go to the documentation of this file.
1 #include <xstream/config.h>
2 #include <xstream/posix.h>
3 #include <xstream/except/posix.h>
4 #include <iosfwd>
5 
6 #include <string>
7 #include <ctime>
8 
9 //for read write, etc
10 #include <unistd.h>
11 
12 //for errno
13 #include <errno.h>
14 
15 //for strerror and strerror_r
16 #include <cstring>
17 
18 #include "debug.h"
19 
20 /*
21  * I don't prefix time localtime_r and strftime with std:: because lot's of implementaions are broken and it's an unnecessary purism
22  *
23  */
24 
25 namespace xstream {
26 namespace posix{
27 
28  date_format::date_format(const std::string& f):format(f){};
29 
31  LOG("posix::date_format::now");
32  ::time_t t;
33  ::time(&t);
34 
35  struct std::tm tm;
36 
37  //XXX check error and throw exception
38  ::localtime_r(&t,&tm);
39 
40  //first size of the allocated buffer
41  //if needed it will be reallocated
42  size_t len=512;
43  std::string ret;
44 
45  do {
46  LOG("\tlen=" << len);
47  char buf[len];
48  size_t cret=::strftime(buf,len,format.c_str(),&tm);
49 
50  if (0 != cret && cret <= len) {
51  buf[len - 1] = '\0'; //just in case
52  ret = buf;
53  }
54  len *= 2;
55  } while (0 == ret.size());
56 
57  LOG("\tdate=" << ret);
58  return ret;
59  }
60 
61  void check_return(int code, const std::string& call) {
62  LOG("posix::check_return " << call << " => " << code);
63  if (-1 == code) {
64  //XXX please try to use strerror_r instead
65  const std::string desc(strerror(errno));
66  LOG("\tthrowing " << errno << " => " << desc);
67  throw general_error(call, errno, desc);
68  }
69  }
70 
71 #if HAVE_FD
72  fd::fd(int f, bool c)
73  : fdn(f), dest_close(c)
74  {
75  LOG("posix::fd (" << f << "," << c << ")");
76  }
77 
78  std::streamsize fd::read(char* buf, std::streamsize len)
79  {
80  LOG("posix::fd::read " << len);
81 
82  ssize_t count;
83 
84  do {
85  count = ::read(fdn, buf, len);
86  } while (-1 == count && EINTR == errno);
87 
88  check_return(count, "read");
89 
90  return count;
91  }
92 
93  std::string fd::read(std::streamsize len)
94  {
95  char buf[len];
96  ssize_t count = read(buf, len);
97 
98  return std::string(buf, buf + count);
99  }
100 
101  std::streamsize fd::write(const char* buf, std::streamsize len)
102  {
103  LOG("posix::fd::write " << len);
104 
105  ssize_t count;
106 
107  do {
108  count = ::write(fdn, buf, len);
109  } while (-1 == count && EINTR == errno);
110 
111  check_return(count, "write");
112 
113  return count;
114  }
115 
116  void fd::sync()
117  {
118  LOG("posix::fd::sync");
119  int cret = fsync(fdn);
120  check_return(cret, "fsync");
121  }
122 
123  fd::~fd()
124  {
125  LOG("posix::fd::~fd");
126  if (dest_close) {
127  LOG("\tclosing");
128  int cret = ::close(fdn);
129  check_return(cret, "close");
130  }
131  }
132 #endif
133 
134 }//namespace posix
135 }//namespace xstream
std::string now() const
Definition: posix.cpp:30
debugging/logging support
textOut close()
char string[256]
#define c
exceptions related to POSIX systems calls
TF1 * f
Definition: FitGains.C:21
std::string format
Definition: posix.h:28
void check_return(int code, const std::string &call)
checks the return code of a syscall and raises apropriate exception if needed
Definition: posix.cpp:61
errors in POSIX usage
Definition: except/posix.h:23
POSIX helper objects and functions.
#define LOG(s)
Definition: debug.h:30
date_format(const std::string &format)
construct specifying strftime format
Definition: posix.cpp:28