1 /******************************************************************************
2 * to emulate the serial input and output of an 8051 controller *
3 * fileio.cc - file input and output *
4 ******************************************************************************/
18 // make the input fifo
19 if(mkfifo(DEF_INFILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
21 std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
26 // the input fifo - non blocking
27 if ((fdin = open(DEF_INFILE, O_RDONLY|O_NONBLOCK)) == -1) {
28 std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
32 // make the output fifo
33 if(mkfifo(DEF_OUTFILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
35 std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
41 if ((fdout = open(DEF_OUTFILE, O_RDWR|O_NONBLOCK)) == -1) {
42 std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
47 FileIO::FileIO(char *infile, char *outfile)
49 // make the input fifo
50 if(mkfifo(infile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
52 std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno);
57 // the input fifo - non blocking
58 if ((fdin = open(infile, O_RDONLY|O_NONBLOCK)) == -1) {
59 std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno);
63 // make the output fifo
64 if(mkfifo(outfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
66 std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno);
72 if ((fdout = open(outfile, O_RDWR|O_NONBLOCK)) == -1) {
73 std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno);
85 int FileIO::SendByte(char b)
89 if((ret = write(fdout, &b, 1)) != 1)
91 std::cerr << "write(): Error number " << errno << " occourred: " << strerror(errno);
99 int FileIO::RecvByte(char *b)
103 ret = read(fdin, b, 1);
105 if((ret == -1) && (errno != EAGAIN))
107 std::cerr << "read(): Error number " << errno << " occourred: " << strerror(errno);
115 int FileIO::SendStr(char *str)
119 if((ret = write(fdout, str, strlen(str))) != (int)strlen(str))
121 std::cerr << "write(): Error number " << errno << " occourred: " << strerror(errno);
129 int FileIO::RecvStr(char *str)
133 ret = read(fdin, str, MAX_SIZ-1);
136 if((ret == -1) && (errno != EAGAIN))
138 std::cerr << "read(): Error number " << errno << " occourred: " << strerror(errno);