You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.9 KiB

2 years ago
#include <QFile>
#include <iostream>
#include "LinkedList.h"
#include "Node.h"
#define SENDER 4
#define COMMAND 2
#define RECEIVER 4
#define TIME 11
using namespace std;
/*<============================================================================================================>*/
void LinkedList::qListLinked(Node* newNode, int count){
if(count == 0)
{
newNode->chainLen += 1;
_linkedList.append(newNode);
}
else
{
Node* tmp = _linkedList.last();
tmp->chainLen += 1;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = newNode;
newNode->pre = tmp;
}
}
/*<============================================================================================================>*/
void LinkedList::pushBack(string sender, string receiver, string command, float time, int count) {
Node* newNode = new Node();
newNode->sender = sender;
newNode->command = command;
newNode->receiver = receiver;
newNode->time = time;
newNode->chainLen = 0;
newNode->next = NULL;
newNode->pre = NULL;
qListLinked(newNode, count);
}
/*<============================================================================================================>*/
void LinkedList::qListOmit(string line){
omitedLines.append(line);
}
/*<============================================================================================================>*/
void LinkedList::addToOmitList(int where){
omitedLines.insert(where + 1, "chain");
}
/*<============================================================================================================>*/
void LinkedList::removeOmitLast(){ //remove uncorrect format
omitedLines.removeLast();
}
/*<============================================================================================================>*/
void LinkedList::addTime(float time){ //addTime to null blocks in chain
Node* head = _linkedList.last();
while(head->next != NULL)
{
head = head->next;
}
if(head->time == NULL)
{
head->time = time;
}
else
{
while(head->time != NULL)
{
head = head->pre;
}
head->time = time;
}
}
/*<============================================================================================================>*/
void LinkedList::makeLinkedList(){
string subLine;
string sender;
string command;
string receiver;
string time;
float numFloat;
string lastLine;
int count = 0;
for(int i = 0; i < omitedLines.length(); i++)
{
if(omitedLines[i].find(">>>") != std::string::npos) //line with <<< init
{
subLine = omitedLines[i].substr(omitedLines[i].find(">") + SENDER);
sender = subLine.substr(0, subLine.find("|") - 1);
subLine = omitedLines[i].substr(omitedLines[i].find("|") + COMMAND);
command = subLine.substr(0, subLine.find("=") - 1);
subLine = omitedLines[i].substr(omitedLines[i].find("==>") + RECEIVER);
receiver = subLine.substr(0, subLine.find(","));
pushBack(sender, receiver, command, NULL, count); //add the line with null
//time
count++;
}
else if(omitedLines[i].find("<<<") != std::string::npos)
{
subLine =
omitedLines[i].substr(omitedLines[i].find("exec time:") + TIME);
time = subLine.substr(0, subLine.find(","));
numFloat = std::stof(time);
addTime(numFloat);
}
else if(omitedLines[i].find("chain") != std::string::npos) //add the time to the
//last null time variable
//in the chain
{
count = 0;
}
}
}
/*<============================================================================================================>*/
void LinkedList::chainDetector(){ //a function for finding the chains P.S: chain can
//have one block.
int count = 0;
for(int i = 0; i < omitedLines.length(); i++)
{
if(omitedLines[i].find(">>>") != std::string::npos)
{
count++;
}
if(omitedLines[i].find("<<<") != std::string::npos)
{
count--;
}
if(omitedLines[i] == "chain")
{
continue;
}
if(count == 0)
{
addToOmitList(i);
}
}
if(omitedLines.last() != "chain")
{
while(omitedLines.last() != "chain")
{
removeOmitLast();
}
}
}
/*<============================================================================================================>*/
void LinkedList::fileOpen(const QString& path){ //read the file & add them to a Qlist for easy
//access
fstream newFile;
newFile.open(path.toStdString(), ios::in);
if(newFile.is_open())
{
string line;
string subLine;
string secSubLine;
while(getline(newFile, line))
{
if(line.find(">>>") != std::string::npos)
{
subLine = line.substr(line.find(">"));
secSubLine = subLine.substr(0, subLine.find(","));
qListOmit(secSubLine);
}
else if(line.find("<<<") != std::string::npos)
{
subLine = line.substr(line.find("<"));
secSubLine = subLine.substr(0, subLine.find(","));
qListOmit(secSubLine);
}
}
}
newFile.close();
}
float LinkedList::convertTime(float input)
{
return input * 1000;
}
/*<============================================================================================================>*/
QList<Node*> LinkedList::Initiate(const QString& path){
fileOpen(path);
chainDetector();
makeLinkedList();
return _linkedList;
}
/*<============================================================================================================>*/