The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
input_stream.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "global.hpp"
16 
17 #include "input_stream.hpp"
18 
19 #ifndef _WIN32
20 
21 #include <algorithm>
22 #include <iostream>
23 #include <fcntl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <cerrno>
28 
29 #endif
30 
32  fd_(-1),
33  path_(path),
34  data_()
35 {
36 #ifndef _WIN32
37  if(path == "") {
38  return;
39  }
40 
41  const int res = mkfifo(path.c_str(),0660);
42  if(res != 0) {
43  std::cerr << "could not make fifo at '" << path << "' (" << errno << ")\n";
44  }
45 
46  fd_ = open(path.c_str(),O_RDONLY|O_NONBLOCK);
47 
48  if(fd_ == -1) {
49  std::cerr << "failed to open fifo at '" << path << "' (" << errno << ")\n";
50  } else {
51  std::cerr << "opened fifo at '" << path << "'. Server commands may be written to this file.\n";
52  }
53 #endif
54 }
55 
57 {
58 #ifndef _WIN32
59  stop();
60 #endif
61 }
62 
64 {
65 #ifndef _WIN32
66  if(fd_ != -1) {
67  close(fd_);
68  unlink(path_.c_str());
69  fd_ = -1;
70  }
71 #endif
72 }
73 
74 #ifndef _WIN32
76 {
77  if(fd_ == -1) {
78  return false;
79  }
80 
81  const size_t block_size = 4096;
82  char block[block_size];
83 
84  const size_t nbytes = read(fd_,block,block_size);
85  if (nbytes == static_cast<size_t>(-1)) {
86  return false;
87  }
88  std::copy(block,block+nbytes,std::back_inserter(data_));
89 
90  const std::deque<char>::iterator itor = std::find(data_.begin(),data_.end(),'\n');
91  if(itor != data_.end()) {
92  str.resize(itor - data_.begin());
93  std::copy(data_.begin(),itor,str.begin());
94  data_.erase(data_.begin(),itor+1);
95  return true;
96  } else {
97  return false;
98  }
99 }
100 #else
102 {
103  return false;
104 }
105 #endif
input_stream(const std::string &path)
std::string path_
std::deque< char > data_
GLsizei const char ** path
Definition: glew.h:4654
GLuint res
Definition: glew.h:9258
static void block(LexState *ls)
Definition: lparser.cpp:1081
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:400
bool find(E event, F functor)
Tests whether an event handler is available.
bool read_line(std::string &str)
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
GLsizei const GLcharARB ** string
Definition: glew.h:4503