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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/******************************************************************************
* to emulate the serial input and output of an 8051 controller *
* fileio.cc - file input and output *
******************************************************************************/
#include <sys/types.h>
#include <iostream>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "fileio.hh"
FileIO::FileIO()
{
// make the input fifo
if(mkfifo(DEF_INFILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
if(errno != EEXIST) {
std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
exit(-1);
}
}
// the input fifo - non blocking
if ((fdin = open(DEF_INFILE, O_RDONLY|O_NONBLOCK)) == -1) {
std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
exit(-1);
}
// make the output fifo
if(mkfifo(DEF_OUTFILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
if(errno != EEXIST) {
std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
exit(-1);
}
}
// the output fifo
if ((fdout = open(DEF_OUTFILE, O_RDWR|O_NONBLOCK)) == -1) {
std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno) << "\n";
exit(-1);
}
}
FileIO::FileIO(const char *infile, const char *outfile)
{
// make the input fifo
if(mkfifo(infile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
if(errno != EEXIST) {
std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
}
// the input fifo - non blocking
if ((fdin = open(infile, O_RDONLY|O_NONBLOCK)) == -1) {
std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
// make the output fifo
if(mkfifo(outfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) {
if(errno != EEXIST) {
std::cerr << "mkfifo(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
}
// the output fifo
if ((fdout = open(outfile, O_RDWR|O_NONBLOCK)) == -1) {
std::cerr << "open(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
}
FileIO::~FileIO()
{
close(fdin);
close(fdout);
}
int FileIO::SendByte(char b)
{
int ret;
if((ret = write(fdout, &b, 1)) != 1)
{
std::cerr << "write(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
return(ret);
}
int FileIO::RecvByte(char *b)
{
int ret;
ret = read(fdin, b, 1);
if((ret == -1) && (errno != EAGAIN))
{
std::cerr << "read(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
return(ret);
}
// send a string
int FileIO::SendStr(char *str)
{
int ret;
if((ret = write(fdout, str, strlen(str))) != (int)strlen(str))
{
std::cerr << "write(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
return(ret);
}
int FileIO::RecvStr(char *str)
{
int ret;
ret = read(fdin, str, MAX_SIZ-1);
str[MAX_SIZ] = 0;
if((ret == -1) && (errno != EAGAIN))
{
std::cerr << "read(): Error number " << errno << " occourred: " << strerror(errno);
exit(-1);
}
return(ret);
}
|