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
|
/**
* Whiteboard session file object
*
* Authors:
* David Yip <yipdw@rose-hulman.edu>
*
* Copyright (c) 2005 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <glibmm.h>
#include <glibmm/i18n.h>
#include "util/list-container.h"
#include "jabber_whiteboard/message-utilities.h"
#include "jabber_whiteboard/node-utilities.h"
#include "jabber_whiteboard/typedefs.h"
#include "jabber_whiteboard/session-file.h"
namespace Inkscape {
namespace Whiteboard {
SessionFile::SessionFile(Glib::ustring const& filename, bool reading, bool compress) : _filename(filename), _compress(compress), _reading(reading)
{
try {
if (!reading) {
this->fptr = Glib::IOChannel::create_from_file(filename, "w+");
} else {
this->fptr = Glib::IOChannel::create_from_file(filename, "r");
}
this->_ateof = false;
} catch (Glib::FileError) {
throw;
}
}
SessionFile::~SessionFile()
{
if (!this->_reading) {
this->commit();
}
this->close();
}
gint64
SessionFile::nextMessageFrom(gint64 from, Glib::ustring& buf)
{
try {
Glib::ustring line;
Glib::IOStatus st;
Node part;
gint64 accum = from;
buf = "";
this->fptr->seek(accum, Glib::SEEK_TYPE_SET);
while(part.tag != MESSAGE_COMMIT) {
st = this->fptr->read_line(line);
if (st == Glib::IO_STATUS_EOF) {
break;
} else {
accum += line.bytes();
this->fptr->seek(accum);
MessageUtilities::getFirstMessageTag(part, line);
buf += line;
line.clear();
}
}
if (st == Glib::IO_STATUS_NORMAL) {
// reset eof flag if successful
this->_ateof = false;
return from + buf.bytes();
} else {
if (st == Glib::IO_STATUS_EOF) {
this->_ateof = true;
}
return from;
}
} catch (Glib::IOChannelError e) {
g_warning("Could not read next message due to I/O error (error: %s)!", e.what().data());
} catch (Glib::ConvertError e) {
g_warning("Could not read next message due to charset conversion error (error: %s)!", e.what().data());
}
return from;
}
void
SessionFile::addMessage(Glib::ustring const& message)
{
Glib::ustring msg = message;
this->changes.push_back(msg);
}
void
SessionFile::commit()
{
if (!this->_reading) {
SessionQueue::iterator i = changes.begin();
for(; i != changes.end(); i++) {
try {
fptr->write(*i);
changes.erase(i);
} catch (Glib::IOChannelError e) {
g_warning("Caught I/O exception (error string: %s) while committing change to session file %s. Attempting to commit remaining changes; session file will be inconsistent with whiteboard session history.", e.what().c_str(), this->_filename.c_str());
} catch (Glib::ConvertError e) {
g_warning("Caught character set conversion error (error string: %s) while committing change to session file %s. Attempting to commit remaining changes; session file will be inconsistent with whiteboard session history.", e.what().c_str(), this->_filename.c_str());
}
}
fptr->write("\n");
}
}
void
SessionFile::close()
{
fptr->close(true);
}
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|