// LinksList.cpp
// creates an object holding a list of links

#include "linkslist.h"

// Constructor, sets the links limiting domain
LinksList::LinksList( string limit ){
		//cout << "\nInitializing LinksList . . . ";
		linksLimit = limit;
		current = &links;							// set current to start of queue
		//cout << "LinksList Initialization Succesful!";
}

// Adds a new link, returns 1 if added, 0 if not
int LinksList::add( string newLink ) {
		// conditional - as long as "http://" or "file:/" is at the start of the
		// new link, and ".." is not a part of the string, and the limiting domain
		// is found in the string, it is added.
		
		//cout << "\nTry to add: " << newlink;
		//cin.get();
		
		//if ((( newlink.find("http://") == string::npos ) && (newlink[0] != '#') && ( newlink.find("..") == string::npos ) && ( newlink[0] != '/' ) && (newlink.find("mailto:") == string::npos)) || ( newlink.find(linksLimit) != string::npos )) {
		string newlink = newLink;
		convertCase(newlink);
		if (( newlink.find(linksLimit) != string::npos ) && ( newlink.find(".htm") != string::npos)) {	
			links.add(newLink); 
			return 1;
		}
		else {
		//	cout << "\n Did not enter loop";
		//	cout << "\nlinksLimit: " << linksLimit;
		//	cout << "newlink.find(linksLimit) = " << newlink.find(linksLimit);
		//	cin.get();
			return 0;
		}
}

// Returns the next item in the queue and then moves current to the next item
string LinksList::getNext() {
		if (current != NULL) {
			string ret = current->getName();
			current = current->getNext();
			return ret;
		}
		else return "";
}

// Changes limit
int LinksList::assignLimit( string newLimit ) {
		linksLimit = newLimit;
		return 1;
}

void LinksList::testList() {
		cout << "\nList of links:\n";
		links.testList();
		cout << "\n";
		cin.get();
}

void LinksList::convertCase( string &line ) {
	//cout << "\nCalled ConverCase";
	for (int x = 0; x < line.length(); x++) {
		line[x] = tolower(line[x]);
	}
}
