summaryrefslogtreecommitdiffstats
path: root/kppp/logview/loginfo.cpp
blob: 9544f389d85e53180e49f793d7bd3ccb39b19b6c (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * kPPPlogview: a accounting log system for kPPP
 *
 *            Copyright (C) 1998 Mario Weilguni <[email protected]>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "loginfo.h"
#include <stdlib.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <kdebug.h>

char *mystrsep (char **stringp, const char *delim);

LogInfo::LogInfo(QCString data) {
  parse(data);
}

QDateTime LogInfo::from() const {
  QDateTime tm;
  tm.setTime_t(_from);
  return tm;
}

QDateTime LogInfo::until() const {
  QDateTime tm;
  tm.setTime_t(_until);
  return tm;
}

QString LogInfo::connectionName() const {
  return _conname;
}

QString LogInfo::currency() const {
  return _currency;
}

double LogInfo::sessionCosts() const {
  return _session_cost;
}

double LogInfo::totalCosts() const {
  return _total_cost;
}

double LogInfo::bytesIn() const {
  return _bytes_in;
}

double LogInfo::bytesOut() const {
  return _bytes_out;
}

double LogInfo::bytes() const {
  if(bytesIn() == -1 || bytesOut() == -1)
    return -1;
  else
    return bytesIn() + bytesOut();
}

int LogInfo::error() const {
  return errorfield;
}

void LogInfo::parse(QCString s) {
  errorfield = 0;
  char *c = (char *)malloc(s.length() + 1), *csep;
  strcpy(c, s);

  // init data
  _from = _until = 0;
  _conname = "";
  _currency = "";
  _bytes_in = _bytes_out = -1;
  _session_cost = _total_cost = -1;

  // start of connection time
  csep = c;
  char *p = mystrsep(&csep, ":");
  int i = 0;
  while(i < 8 && p != 0) {
    QString token = QString::fromLocal8Bit(p);

    switch(i) {
    case 0:
      _from = token.toULong();
      break;

    case 1:
      _conname = token;
      break;

    case 2:
      _currency = token;
      break;

    case 3:
      _until = token.toULong();
      break;

    case 4:
      _session_cost = token.toFloat();
      break;

    case 5:
      _total_cost = token.toFloat();
      break;

    case 6:
      _bytes_in = token.toLong();
      break;
      
    case 7:
      _bytes_out = token.toLong();
      break;
    }

    i++;
    p = mystrsep(&csep, ":");
  }

  free(c);

  if(i == 8)
    errorfield = 0;
  else
    errorfield = i+1;
}


int LogInfo::duration() const {
  if( _until - _from < 0) 
    return 0;
  else
    return _until - _from;
}

#ifdef MYDEBUG
void LogInfo::dump() {
  kdDebug(5002) << "LOGINFO " << this << endl;
  kdDebug(5002) << "connection name : " << connectionName() << endl;
  kdDebug(5002) << "currency symbol : " << currency() << endl;
  kdDebug(5002) << "begin           : " << ctime(&_from) << endl;
  kdDebug(5002) << "end             : " << ctime(&_until) << endl;
  kdDebug(5002) << "duration        : " << (int)_until - (int)_from << " seconds" << endl;
  kdDebug(5002) << "session cost    : " << sessionCosts() << endl;
  kdDebug(5002) << "total cost      : " << totalCosts() << endl;
  kdDebug(5002) << "bytes in        : " << bytesIn() << endl;
  kdDebug(5002) << "bytes out       : " << bytesOut() << endl;
  kdDebug(5002) << "bytes total     : " << bytes() << endl;
  kdDebug(5002) << endl;
}
#endif

char *mystrsep (char **stringp, const char *delim) {
  char *begin, *end;

  begin = *stringp;
  if (begin == 0L)
    return 0L;

  /* Find the end of the token.  */
  end = strpbrk (begin, delim);
  if (end) {
      /* Terminate the token and set *STRINGP past NUL character.  */
      *end++ = '\0';
      *stringp = end;
  } else
    /* No more delimiters; this is the last token.  */
    *stringp = 0L;

  return begin;
}