/***************************************************************************
                          parser.cpp  -  description
                             -------------------
    begin                : Wed Nov 24 1999
    copyright            : (C) 1999 by Henning Burchardt
    email                : Henning.Burchardt@gmx.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "parser.h"
#include "resources.h"
#include <fstream>
#include <cstdio>
#include <stdlib.h>

Parser::Parser() : QObject() {}

Parser::~Parser() {}

ProcParser::ProcParser(QString filename, QString device)
	: Parser(), filename(filename), device(device)
{
}

ProcParser::~ProcParser() {}

ParserBytes ProcParser::getDiffBytes() {
	ParserBytes diffbytes;
	diffbytes.in = bytes.diff_in;
	diffbytes.out = bytes.diff_out;
	return diffbytes;
}

ParserBytes ProcParser::getTotalBytes() {
	ParserBytes totalbytes;
	totalbytes.in = bytes.total_in;
	totalbytes.out = bytes.total_out;
	return totalbytes;
}

ParserPackets ProcParser::getDiffPackets()
{
	ParserPackets diffpackets;
	diffpackets.in = packets.diff_in;
	diffpackets.out = packets.diff_out;
	return diffpackets;
}

ParserPackets ProcParser::getTotalPackets()
{
	ParserPackets totalpackets;
	totalpackets.in = packets.total_in;
	totalpackets.out = packets.total_out;
	return totalpackets;
}

bool ProcParser::setupNetStats () {
	std::ifstream in(filename.ascii());
	char line[200];
	char device_name[10];
	bool device_found = false;
		
	if (!in)
	    return false;

	// skip first two lines
	in.getline (line, 200, '\n');
	in.getline (line, 200, '\n');
	
	while ( in.getline(line, 200, '\n') ) {
		sscanf(line, " %[^ \t\r\n:] %*c %lf %lf %*s %*s %*s %*s%*s %*s %lf %lf %*s %*s %*s %*s %*s %*s",
			device_name, &bytes.total_in, &packets.total_in,
			&bytes.total_out, &packets.total_out);

		if( !strcmp(device_name, device.latin1()) ) {
			device_found = true;
			break;
		}
	}
	
	if ( !device_found )
	    return false;
	
	bytes.last_in = bytes.total_in; bytes.last_out = bytes.total_out;
	packets.last_in = packets.total_in; packets.last_out = packets.total_out;
	bytes.diff_in = 0; bytes.diff_out = 0;
	packets.diff_in = 0; packets.diff_out = 0;

	in.close();
	
	return true;
}

bool ProcParser::updateNetStats () {
	std::ifstream in(filename.ascii());
	char line[200];
	char device_name[10];
	bool device_found = false;
		
	if (!in)
        return false;

	// skip first two lines
	in.getline (line, 200, '\n');
	in.getline (line, 200, '\n');
	
	while ( in.getline(line, 200, '\n') ) {
		sscanf(line, " %[^ \t\r\n:] %*c %lf %lf %*s %*s %*s %*s%*s %*s %lf %lf %*s %*s %*s %*s %*s %*s",
			device_name, &(bytes.total_in), &(packets.total_in),
			&(bytes.total_out), &(packets.total_out));
		
		if( !strcmp(device_name, device.latin1()) ){
			device_found = true;
			break;
		}
	}
	
	if ( ! device_found )
		return false;
			
	if( packets.total_in != packets.last_in ) {
		bytes.diff_in += bytes.total_in - bytes.last_in;
		packets.diff_in += packets.total_in - packets.last_in;
		bytes.last_in = bytes.total_in;
		packets.last_in = packets.total_in;
	}
	
	if( packets.total_out != packets.last_out ) {
		bytes.diff_out += bytes.total_out - bytes.last_out;
		packets.diff_out += packets.total_out - packets.last_out;
		bytes.last_out = bytes.total_out;
		packets.last_out = packets.total_out;
	}

	in.close();

	return true;
}
