/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PARSER_H
#define PARSER_H

#include <qobject.h>
#include <qstring.h>

/** struct for storing amount of bytes (double values).
 * ( total_in, total_out, last_in, last_out,
 * diff_in(=total_in-last_in), diff_out(=total_out-last_out) )
 */
typedef struct {
	double total_in, total_out;
	double last_in, last_out;
	double diff_in, diff_out;
} Bytes;

typedef Bytes Packets;

/** struct for storing two double values */
typedef struct {
	double in, out;
} ParserBytes;

typedef ParserBytes ParserPackets;

/**
 * Abstract class for a parser
 */
class Parser : public QObject {	
	Q_OBJECT
protected:	
	Bytes bytes;
	Packets packets;

public:	
	/** constructor */
	Parser();
	/** destructor */
	virtual ~Parser();
	
	/** gets number of bytes and packets that run in and out the machine */
	virtual bool updateNetStats()=0;
	
	/** nearly the same as 'updateNetStats' */
	virtual bool setupNetStats()=0;

	/** gets number of bytes since the parser is started */
	virtual ParserBytes getDiffBytes()=0;	

	/** gets number of bytes since linux is started */
	virtual ParserBytes getTotalBytes()=0;
	
	virtual ParserPackets getDiffPackets()=0;
	virtual ParserPackets getTotalPackets()=0;
};

/** class for a parser that reads proc/net/dev
 * Opens the file /proc/net/dev and gets the number of bytes and packets that
 * run through a specified device.
 * The code is taken from 'wmnet' by Jesse Off.
 */
class ProcParser : public Parser {
protected:
	QString filename;
	QString device;
	
public:
	ProcParser ( QString filename="/proc/net/dev", QString device="eth0" );
	~ProcParser();
	
	bool updateNetStats();
	bool setupNetStats();	
	ParserBytes getDiffBytes();
	ParserBytes getTotalBytes();
	ParserPackets getDiffPackets();
	ParserPackets getTotalPackets();
	QString getDevice() { return device; }
};

#endif

