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
| /* fonction permettant d'extraire un mot dans un fichier texte*/
string read_line (FILE * f)
{
string s;
char c;
fread(&c, 1, 1, f);
while ((c != '\n') && (!feof(f)))
{
s += c;
fread(&c, 1, 1, f);
}
return s;
}
/* on fonction qui cherche un mot dans une ligne*/
bool est_present (string & line, string & cmd)
{
return line.find(cmd) != string::npos;
}
char getChar(string & line, string & cmd)
{
int pos = line.find(cmd);
const char * c = line.c_str() + pos + cmd.length();
return (c);
}
/* fonction qui extrait la réponse*/
string getData (char * input_file, string & cmd)
{
FILE * finput;
finput = fopen(input_file, "r");i
if (finput == NULL)
return -1;
string line;
string output = 0;
while (!feof(finput))
{
line = read_line (finput);
if (est_present(line, cmd))
output += getChar(line, cmd);
}
fclose(finput);
return output;
}
/* la fonction d'entrée de mon programme qui fait appel aux autres fonctions*/
/* my function "entry point" */
test: void Test(vTtyName)
while (1)
{
/*did the fifo already exist? */
test: if ((mkfifo(vTtyName, 0666) < 0) && (errno!= EEXIT))
{
printf("the fifo already exists");
}
/* Read the Tty */
if (open(vTtyName,O_RDWR | O_NDELAY)!=-1)
{
if (read(fd,vMsg, strlen(vMsg)) != 0)
{
string answer = getData(fifo.txt, vMsg);// the answer extracted from the text file
write(vTtyName, &answer, strlen(answer));//write in the fifo
}
else vTtyName++ && goto test;
}
} |
Partager