summaryrefslogtreecommitdiff
path: root/trunk/glagen/dll/classes/Data_string.hh
blob: a5f9131e6561ffb3889f38f968d1632d1f1db713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef		DATA_STRING_HH_
# define	DATA_STRING_HH_

#include <iostream>
#include <string>
#include <unistd.h> // Pour write/read

#include <errno.h>

using namespace std;

class Data_string
{
public:
  Data_string() : _str("") {};
  Data_string(const string& str) : _str(str) {};
  
  void	write_data(const int& fd) const
  {
    
    std::cout << "Donnees envoyes" << std::endl << _str << std::endl;
    unsigned int size = _str.size();
    write(fd, &size, sizeof(unsigned int));
    for (unsigned int i = 0; i < size; ++i)
      {
	char car = _str[i];
	write(fd, &car, sizeof(char));
      }
  }
  
  void	read_data(const int& fd)
  {
    unsigned int size = 0;
    do
      {
	errno = 0;
	read(fd, &size, sizeof(size));
      }
    while (errno == 4);

    _str = "";
    for (unsigned int i = 0; i < size; ++i)
      {
	char car;
	do
	  {
	    errno = 0;
	    read(fd, &car, sizeof(char));
	  }
	while (errno == 4);
	_str += car;
      }

    std::cout << "Reception message sur le file descriptor :" << fd
	      << std::endl << _str << std::endl;
  }

private:
  string _str;
};

#endif	// DATA_STRING