/*
list - By Dale Swanson 2005-01-09
takes a list in http://username:password@www.example.com/members format
and generates a html file with a list of the sites
with links to each user name and password.
input file is list.txt, output list.htm.
*/

//Headers

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

//Global Varibals

//buff will be where each line is stored.
string buff;

//site will be used to store the names of sites
//siten is the site name, siteu is the username password
vector<string> siten(1024);
vector<string> siteu(1024);

//urll will list the full urls
vector<string> urll(1024);

//y will be the current site in index of sites.
int y;

//nums will hold the current number of sites.
int nums;

//tempsite will hold the current site being worked with.
string tempsite;

//upass will store the current username and password.
string upass;
  
//at will be the postion of the @.
int at;

//sitend will be the site we are working on
string sitend;

//endsite will be the ending of the url.
int endsite;

//www will be used in testing if the site starts with www.
string www;

// fist and second $ for the list of user pass's
int firsts;
int secs;

// stores temp user and pass for the list generator.
string tempupass;

//url stores the full url
string url;



//this will generate the html file, list.htm
void makehtml()
{
    
    //opens file
    ofstream html ("C:/list.htm");
    
    //tests to see if file opened
    if (!html) 
    {
        cout<<"\nProblem opening C:/list.htm\n";
        return;
    } 
    
    html<<"<html><head><title>List Of Passwords</title></head><body>";
    
    
    //this will go through the list of sites, up to the end.
    for (y = 1; y <= nums; y++)
    {
        //this makes the header for the site
        html<<"\n<h3>"<<siten[y]<<"</h3>";
        
                
        //sets secs to -1 for the first site
        secs = -1;
        
        //I use the break to get out, so loop until that
        while (1==1)
        {
            //sets firsts to secs + 1
            firsts = secs + 1;
            
            //sets secs to the next location of $
            secs = siteu[y].find ("$", firsts + 1);
            
            //sets tempupass to the current site
            tempupass = siteu[y].substr(firsts, secs - firsts);
            
            //if secs < 0 it means we are past the end of user pass's so break
            if (secs < 0)
            {
                break;
            }    
            
            //writes a link for this user pass
            html<<"\n<a href=\"http://"<<tempupass<<"@"<<urll[y]<<"\">"<<tempupass<<"</a><br>";

        }  
    }    
    return;
}    



//will check if a site is allready in the index.
void look()
{

    //this will go through the list of sites, up to the end.
    for (y = 1; y <= nums; y++)
    {

        //checks to see if the site is the same as the current index
        tempsite=siten[y];
        if (tempsite == sitend)
        {
            //if the site is found this adds the user and pass to the end of the list
            siteu[y] = siteu[y] + upass + "$";
            return;
        }    
        
    }    
    
    //if it gets here then it didn't find the site
    //increses number of sites
    nums++;
    
    //adds this site name to last index number
    siten[nums]=sitend;
    
    //adds the url to the list of urls
    urll[nums]=url;
    
    //adds this user and pass for the first entry
    siteu[nums]=upass + "$";
    
    
    return;
}    



int main()
{
    
    //open list.txt names it list
    ifstream list ("C:/list.txt");
    
    //open html.htm names it html
    ofstream html ("C:/html.htm");
    
    //tests to see if file was opened
    if (!list)
    {
        //if it wasn't opened tells you there was an error, and exits.
        cout<<"\nError opening file!\n";
        system("PAUSE");
        return -1;
    }    
    
    
    //reads each line until the End Of File;
    while (! list.eof() )
    {
        //clears the buff
        buff="";
        
        //checks each line until it gets one not empty
        while (buff == "")
        {
            //puts line into buff
            std::getline(list, buff);
        }    
 
        //sets postition of @        
        at = buff.find("@", 0);
        
        //finds the next / after the @
        endsite = buff.find ("/", at);
        
        //sets finds the username and password, puts them in upass
        upass = buff.substr(7, at - 7);
        
        //siteend is the www.example.com without /members...
        sitend = buff.substr(at + 1, endsite - at - 1);
            
        //url is the full url www.example.com/members...
        url = buff.substr(at + 1);
        
        ///puts the first 4 characters in www
        www = sitend.substr(0, 4);
        
 
        //if www isn't "www." then it puts it there  
        if (www != "www.")
        {
            sitend.insert(0, "www.");   
        }   
        
        
        //look checks to see if this site is allready in the index, if not it adds it
        look();
    }
    
    //this generates the html file
    makehtml();

    //pauses system, ends program.

    system("PAUSE");	
    return 0;
}