free code

c++ php

c++

This is a program that will parse IIS logs, looking for a particular string. In theory, it will work with any logs, but it's tailored to IIS' funky way of constructing file names (2/4/2005 comes out as ex050802.log). This program is called from the command line as follows:

searchlog searchterm 02/01/2005 02/28/2005

...where the dates are obviously the beginning and ending dates of the range that you want to search. On a 800 MHz Windows 2000 box (with 14 programs running) it will process 100 MB of log files in 40 seconds, so it's fairly fast. At this point, it will only look for whole strings separated by whitespace, so you can look for "/myDir/myFile.htm" but not "myFile.htm." It displays an on-screen update for each logfile parsed, and a total. It also appends the searchterm, count, logfile name, and log date to a csv file. I haven't used it with FTP files, but if they are named similarly, it should work on those as well. It's my first c++ program, so I went a little OC trying to make everything a function. I somehow introduced a memory error, so I backed out a bit to make a little more procedural.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

char arrFileName[12];
char startDate[6];
char endDate[6];
char dateStamp[10];
char buf[6];
int intStartDate, intEndDate, intWorkingDate;
long int intDailyCount = 0;
long int intTotal = 0;
string str, searchterm, word;

void checkInput(int argc);
void checkDate(string s);
void buildDate(char arr[], char arr2[], int &i);
void compDates(int &i, int &i2);      
void outFlushClose(ofstream &o, string &s, long int &i, char arr[]);
void displayResult(int i, string s);

int main(int argc, char* argv[]) {
    checkInput(argc);   
    searchterm = argv[1];
  
    checkDate(string(argv[2]));
    checkDate(string(argv[3]));

    buildDate(argv[2], startDate, intStartDate);
    buildDate(argv[3], endDate, intEndDate);
 
    compDates(intStartDate,intEndDate); 

    for (intWorkingDate = intStartDate; intWorkingDate <= intEndDate; intWorkingDate++) {
        str = "ex0";
        itoa(intWorkingDate, buf, 10);
        str += buf;
        str += ".log";

        strcpy(arrFileName, str.c_str());                
 
        ifstream in;
        in.open(arrFileName);
            
        if (in) {
            while(in >> word) {
                if (word == searchterm) {
                   intDailyCount++;
                }
            }
            intTotal += intDailyCount;
            in.close();
            
            ofstream out("hitreport.csv", ios::app);   

            if (!out) {
               cout << "The output file could not be opened. Close \"hitreport.csv\" and hit Enter to resume." << endl;
               cin.ignore();
               ofstream out("hitreport.csv", ios::app);   
               if (!out) {
                  cout << "The output file still can't be opened. Exiting program." << endl;
                  exit(0);
               } else {   
                  outFlushClose(out, searchterm, intDailyCount, arrFileName);
               }
            } else {   
              outFlushClose(out, searchterm, intDailyCount, arrFileName);
            }
             
            cout << "There were " << intDailyCount << " instances of \"" << searchterm << "\" found in " << arrFileName << "." << endl;   
            intDailyCount = 0;
        }
    }
       
    displayResult(intTotal, searchterm);
}

void checkInput(int argc){
    if(argc != 4) {
      cout << "Must provide a searchterm, a start date and an end date:" << endl << "logsearch searchterm mm/dd/yyyy mm/dd/yyyy." << endl;
      exit(1);
    }      
}

void checkDate(string s){
    if (s.length() != 10){
       cout << "Check that you typed your dates in the correct format: mm/dd/yyyy" << endl; 
       exit(1);          
    }     
}

void buildDate(char arr[], char arr2[], int &i){    
    arr2[0] = arr[8];
    arr2[1] = arr[9];
    arr2[2] = arr[0];
    arr2[3] = arr[1];
    arr2[4] = arr[3];
    arr2[5] = arr[4];
    arr2[6] = '\0';
    i = atoi(arr2);
}

void compDates(int &i, int &i2) {
     if (i > i2) {
        cout << "Either you are trying to parse logfiles from a timeframe spanning two different "; 
        cout << "centuries, or your start date is after your end date. I can't handle ";
        cout << "either of those things. Exiting program.";
        exit(1);
     }
}      

void outFlushClose(ofstream &o, string &s, long int &i, char arr[]){    
     dateStamp[0] = arr[4];
     dateStamp[1] = arr[5];
     dateStamp[2] = '/';
     dateStamp[3] = arr[6];
     dateStamp[4] = arr[7];
     dateStamp[5] = '/';
     dateStamp[6] = '2';
     dateStamp[7] = '0';
     dateStamp[8] = arr[2];
     dateStamp[9] = arr[3];
     dateStamp[10] = '\0';    

     o << s << "," << i << "," << arr << "," << dateStamp << "\n" << flush;    
     o.close();
}

void displayResult(int i, string s) {
     cout << "\nThere were " << i << " total instances of \"" << s << "\" found." << endl;        
}

php

This is used to display all of the images on this site. I used to have it set up as a function that would take the number of table columns and directory as a parameter, but I seem to have misplaced that one.

	<table>
	<tr>

	<?php 
		chdir ("timages"); //the name of your thumbnail directory
		$dir = opendir (".");
		$tick=1;
		while ($file=readdir($dir)){
			$count=strlen($file);
			$str=substr($file, ($count-3), 3); //change these to '4' if your _
			file extension is 'jpeg'
			if (eregi("^jpg", $str)) { //change this to match the file _
			extension (gif, png, etc.)
				echo ("<td><center><a href=\"display.php?show=$file\"><img src=\"timages/$file\" border=0></a>");
				echo ("<br />");

				//strip off the file extension, edit to match above
				$pat="\.";		
				$arr=split($pat, $file);
				echo ("$arr[0]");
				//end split code
				echo ("</center>");
				echo ("</td>\n");
				if (is_int($tick/5)){ //edit this int to match the _
				number of cols in your table
					echo ("</tr>\n<tr>\n");
				}
				$tick+=1;
			}	
		}
		if (is_int($tick/5)){ //edit this int to match above
			echo ("<td>  </td>");
			$tick+=1;
		}
		closedir($dir);
	?>

	</tr>
	</table>
This is used to display the images:
<html><head><title></title></head><body>
<p>
<?php
	echo ("<center>");
	echo ("<img src=\"images/$show\">");
	echo ("<p>");
	//strip off the file extension
	$pat="\.";		
	$arr=split($pat, $show);
	echo ("$arr[0]");
	echo ("<br><a href=\"javascript:self.history.back()\"><img src=\"/images/back.gif\" border=\"0\"></a>");
	echo ("</center>");
?>
</body>
</html>