-
Notifications
You must be signed in to change notification settings - Fork 2
/
tailTalk.cpp
executable file
·152 lines (120 loc) · 3.03 KB
/
tailTalk.cpp
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
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <fstream>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <cstdlib>
#include <list>
#include <vector>
#include <thread>
#include <signal.h>
using namespace std;
void getString(string fileName);
void listen(string FileNamePointer);
void speak();
void cleanup(int bs);
string lastMessage;
vector<string> messageArray;
int main( int argc, const char* argv[] )
{
// Remember to "log off" if things die
signal(SIGTERM, cleanup);
signal(SIGHUP, cleanup);
signal(SIGINT, cleanup);
string fileName = argv[1];
thread listenerThread(listen, fileName);
thread speakerThread(speak);
listenerThread.join();
speakerThread.join();
}
void getString(string fileName)
{
ifstream Test;
Test.open(fileName.c_str(), std::ios_base::ate );//open file and set position to end of the file
string tmp;
int length = 0;
int failsafe = 0;
char c = '\0';
if( Test )
{
length = Test.tellg();//Get file size
// loop backward over the file
for(int i = length-2; i > 0; i-- )
{
Test.seekg(i);
c = Test.get();
if(c == '\n' )//new line?
{
std::getline(Test, tmp);
messageArray.push_back(tmp);
failsafe++;
if(failsafe > 10)
{
break;
}
if(messageArray.back() == lastMessage)
{
messageArray.pop_back();
break;
}
}
}
while(!messageArray.empty())
{
tmp = messageArray.back(); //peek
messageArray.pop_back(); //pop
printf("%s \n",tmp.c_str()); // print it
fflush(stdout); // flush buffers
lastMessage = tmp;
}
}
}
void listen(string fileName)
{
int watch = inotify_init();
const size_t buf_size = sizeof(struct inotify_event);
inotify_add_watch(watch, fileName.c_str(), IN_MODIFY);
char buf[buf_size];
while(read(watch, buf, buf_size)>= 0)
{
getString(fileName);
}
}
void speak()
{
string message;
while(getline(cin , message))
{
if(message != ""){
string escapeString="";
int count=0;
for(int i = 0; i<message.size(); i++){
switch(message[i]){
case '\\':
escapeString.push_back('\\');
escapeString.push_back('\\');
case '\"':
escapeString.push_back('\\');
escapeString.push_back('\"'); break;
default: escapeString.push_back(message[i]);
}
}
message = "~/.myTalk/myt \"" + escapeString + "\"";
system(message.c_str());
}
}
}
void cleanup(int bs)
{
static int isclean;
// Only die once...
if(!isclean)
{
isclean = 1;
// Clean tail, print logoff, cout die
system("~/.myTalk/myt quit: logging off");
system("killall tail 2> /dev/null");
cout << "tailTalk killed" << endl;
}
exit(50);
}