-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastread.cpp
144 lines (119 loc) · 3.21 KB
/
lastread.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
// Copyright (c) 1999 Peter Karlsson
//
// $Id$
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include <fstream.h>
#include <stdio.h>
#include <string>
#ifdef __EMX__
# include <io.h>
#else
# include <unistd.h>
#endif
#include <string.h>
#include "config.h"
#include "lastread.h"
unsigned getlastread(string name, string echo)
{
string fname = LASTREAD + name;
fstream input(fname.c_str(), ios::in);
if (!(input.is_open())) return 0;
char iname[256], iread[256];
while (!input.eof())
{
// File format:
// Line 1: Area tag
// Line 2: Lastread
input.getline(iname, 256);
if (input.eof()) break;
input.getline(iread, 256);
#ifdef UNIX
if ('\r' == iname[strlen(iname) - 1])
iname[strlen(iname) - 1] = 0;
#endif
if (echo == iname)
{
unsigned retval;
input.close();
sscanf(iread, "%d", &retval);
return retval;
}
}
input.close();
return 0;
}
void setlastread(string name, string echo, unsigned msgno)
{
string fname = LASTREAD + name;
string fnamebak = fname + ".bak";
bool found = false;
fstream input, output;
if (access(fname.c_str(), 0))
{
output.open(fname.c_str(), ios::out);
if (!(output.is_open())) return;
}
else
{
if (-1 == rename(fname.c_str(), fnamebak.c_str()))
{
remove(fnamebak.c_str());
return;
}
input.open(fnamebak.c_str(), ios::in);
if (!(input.is_open()))
{
rename(fnamebak.c_str(), fname.c_str());
return;
}
output.open(fname.c_str(), ios::out);
if (!(output.is_open()))
{
input.close();
rename(fnamebak.c_str(), fname.c_str());
return;
}
char iname[256], iread[256];
while (!input.eof())
{
// File format:
// Line 1: Area tag
// Line 2: Lastread
input.getline(iname, 256);
if (input.eof()) break;
output << iname << endl;
input.getline(iread, 256);
#ifdef UNIX
if ('\r' == iname[strlen(iname) - 1])
iname[strlen(iname) - 1] = 0;
#endif
if (echo == iname)
{
output << msgno << endl;
found = true;
}
else
output << iread << endl;
}
}
if (!found)
{
output << echo << endl;
output << msgno << endl;
}
if (input.is_open()) input.close();
output.close();
remove(fnamebak.c_str());
}