Important alert: (current site time 7/15/2013 8:38:09 PM EDT)
 

VB icon

Text Counter

Email
Submitted on: 8/21/2002 12:58:56 AM
By: Bhushan-  
Level: Advanced
User Rating: By 12 Users
Compatibility: 5.0 (all versions), Active Perl specific, 4.0 (all versions)
Views: 19245
author picture
(About the author)
 
     Text Hit Counter.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Text Counter
= Description:Text Hit Counter.
= By: Bhushan-
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=355&lngWId=6=for details.=**************************************

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
const char data_dir[] = "/path/to/data/";
const int num_valid_uri = 0;
const char valid_uri[num_valid_uri][128] = { };
const int num_invalid_uri = 0;
const char invalid_uri[num_invalid_uri][128] = { };
const char show_link[] = "http://www.psc.com/";
const int auto_create = 1;
const int show_date = 1;
const int lock_sec = 2;
const int pad_size = 5;
void main() {
char *count_page, *count_file, *lock_file;
char c, date[32];
int i, count, count_page_len;
ifstream count_in;
ofstream count_out;
int check_uri(char []);
void error(char [], char []);
void check_lock(char []);
void create(char []);
void print_count(int);
cout << "Content-type: text/html\n\n";
 
if (!getenv("DOCUMENT_URI") && !getenv("QUERY_STRING"))
error("no_uri","X");
if (strlen(getenv("QUERY_STRING")) > 0)
{
count_page_len = strlen(getenv("QUERY_STRING"));
count_page = new char[count_page_len];
strcat(count_page,getenv("QUERY_STRING"));
}
else
{
count_page_len = strlen(getenv("DOCUMENT_URI"));
count_page = new char[count_page_len];
strcat(count_page,getenv("DOCUMENT_URI"));
}
 
if (!check_uri(count_page))
error("bad_uri","X");
for (i = 0; i < count_page_len; i++)
if (!((count_page[i] > 96 && count_page[i] < 123) ||
 (count_page[i] > 64 && count_page[i] < 91) ||
 (count_page[i] > 47 && count_page[i] < 58)))
count_page[i] = '_';
lock_file = new char[count_page_len + 4];
strcat(lock_file,data_dir);
strcat(lock_file,count_page);
strcat(lock_file,".lck");
check_lock(lock_file);
count_file = new char[count_page_len + strlen(data_dir)];
strcat(count_file,data_dir);
strcat(count_file,count_page);
count_in.open(count_file);
if (!count_in.fail())
{
count_in >> count;
count_in.get(c);
count_in.get(c);
i = 0;
while (!count_in.eof() && c != '\n')
{
date[i] = c;
count_in.get(c);
i++;
}
}
else if (auto_create)
{
count_in.close();
create(count_file);
}
else
{
count_in.close();
error("page_not_found","X");
}
count_in.close();
count++;
 
if (strlen(show_link) > 0)
cout << "<a href=\"" << show_link << "\">";
print_count(count);
if (strlen(show_link) > 0)
cout << "</a>";
if (show_date)
cout << " hits since " << date;
count_out.open(count_file);
if (count_out.fail())
error("could_not_increment",count_file);
else
count_out << count << " " << date;
count_out.close();
unlink(lock_file);
delete count_page;
delete count_file;
delete lock_file;
exit(0);
}
int check_uri(char uri[]) {
int st = 0, uri_check = 0;
int valid_uri_len, invalid_uri_len, i, j;
for (i = 0; i < num_valid_uri; i++)
{
valid_uri_len = strlen(valid_uri[i]);
for (j = 0; j < valid_uri_len; j++)
{
if (valid_uri[i][j] == uri[st])
st++;
else
st = 0;
if (st == valid_uri_len)
{
uri_check = 1;
break;
}
}
}
 
st = 0;
for (i = 0; i < num_invalid_uri; i++)
{
invalid_uri_len = strlen(invalid_uri[i]);
for (j = 0; j < invalid_uri_len; j++)
{
if (invalid_uri[i][j] == uri[st])
st++;
else
st = 0;
if (st == invalid_uri_len)
{
uri_check = 0;
break;
}
}
}
if (!num_valid_uri && !num_invalid_uri)
uri_check = 1;
return uri_check;
}
void create(char count_file[])
{
char *date;
char months[12][10] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
tm *ptm;
time_t *cur_time;
ofstream count_out;
void error(char [], char []);
void print_count(int);
 
cur_time = new time_t;
ptm = new tm;
time(cur_time);
ptm = localtime(cur_time);
count_out.open(count_file);
if (count_out.fail())
error("count_not_created",count_file);
else
count_out << 1 << " " << months[ptm->tm_mon] << " " << ptm->tm_mday << ", " << ptm->tm_year + 1900;
count_out.close();
if (strlen(show_link) > 0)
cout << "<a href=\"" << show_link << "\">";
print_count(1);
if (strlen(show_link) > 0)
cout << "</a>";
if (show_date)
cout << " hits since " << months[ptm->tm_mon] << " " << ptm->tm_mday << ", " << ptm->tm_year + 1900;
;
delete date;
delete ptm;
delete cur_time;
exit(0);
}
void print_count(int count)
{
int i, size = 0;
float count_tmp = count;
while (count_tmp >= 1)
{
count_tmp /= 10;
size++;
}
for (i = 0; i < (pad_size - size); i++)
cout << 0;
cout << count;
}
void error(char error[], char opt_file[])
{
if (strcmp(error,"page_not_found") == 0)
cout << "[TextCounter Fatal Error: This Page Not Found; Auto-Create Option Disabled]";
else if (strcmp(error,"no_uri") == 0)
cout << "[TextCounter Fatal Error: No Document URI or File Flag specified]";
else if (strcmp(error,"bad_uri") == 0)
cout << "[TextCounter Fatal Error: This Page Not In Valid URI]";
else if (strcmp(error,"count_not_created") == 0)
cout << "[TextCounter Fatal Error: Could Not Write to File " << opt_file << "]";
else if (strcmp(error,"could_not_increment") == 0)
cout << "[TextCounter Fatal Error: Could Not Increment Counter File " << opt_file << "]";
exit(0);
}
void check_lock(char lock_file[])
{
int i;
ifstream test_in;
ofstream test_out;
for (i = 1; i <= lock_sec; i++)
{
test_in.open(lock_file);
if (test_in.fail())
{
test_out.open(lock_file);
test_out << 0;
test_out.close();
break;
}
else
sleep(1);
 
test_in.close();
}
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
8/30/2002 9:11:45 AMJennica

Nice Code
Five { ***** } Globes from me,

(If this comment was disrespectful, please report it.)

 
5/1/2003 10:24:27 PMstephen antony

is this done in c++
www.teenarmy.com
(If this comment was disrespectful, please report it.)

 
4/10/2004 1:28:40 PMBrian Battles WS1O

I don't recognize the scripting language, how do you run this on a web site?
(If this comment was disrespectful, please report it.)

 
2/28/2005 2:09:31 PMMatt Squire

It's c++; you need to compile it first. Shouldn't this be in the c++ section?
(If this comment was disrespectful, please report it.)

 
6/11/2005 7:22:08 PMScott Armstrong

I have a basic understanding of both perl and c++. It does indeed belong in the c++ section.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.