// HttpHeaderReader.c : Reads HTTP header and counts them
//
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
static int Hash (const char * pSrc, size_t len);
int main(int argc, char* argv[])
{
static const char filename[] = "foo.txt";
static const int MAX_COUNT = 255;
static int frequency[MAX_COUNT] = {0};
FILE* fd = fopen(filename, "r");
if ( fd != NULL )
{
char line[MAX_COUNT + 1];
while ( fgets(line, sizeof line, fd) != NULL )
{
char* p = strchr(line, ':');
if (p != NULL)
{
frequency[Hash(_strlwr(line), (size_t)(p-line))%MAX_COUNT] += 1;
}
}
fclose(fd);
char header[MAX_COUNT + 1] = {0};
printf( "Enter the header:\n " );
scanf("%255s", header);
header[MAX_COUNT] = '\0';
printf( "%s occurs %d times.\n", header, frequency[Hash(_strlwr(header), strlen(_strlwr(header)))%MAX_COUNT] );
}
return 0;
}
static int Hash (const char * pSrc, size_t len)
{
size_t res = 0;
while (len--) res = (res << 1) ^ *pSrc++;
return (int)res;
}
content-length: 10
len = 14, hash = 177
user-agent: test
len = 10, hash = 107
content-length: 14
len = 14, hash = 177
accept: comedy
len = 6, hash = 16
content-length: 100
len = 14, hash = 177
content-encoding: gzip
len = 16, hash = 134
connection: close
len = 10, hash = 50
user-agent: test
len = 10, hash = 107
accept: flash
len = 6, hash = 16
user-agent: test1
len = 10, hash = 107
content-length: 20
len = 14, hash = 177
user-agent: test2
len = 10, hash = 107
user-agent: test3
len = 10, hash = 107
accept: gzip
len = 6, hash = 16
Enter the header:
user-aGENT
user-agent occurs 5 times.
//
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
static int Hash (const char * pSrc, size_t len);
int main(int argc, char* argv[])
{
static const char filename[] = "foo.txt";
static const int MAX_COUNT = 255;
static int frequency[MAX_COUNT] = {0};
FILE* fd = fopen(filename, "r");
if ( fd != NULL )
{
char line[MAX_COUNT + 1];
while ( fgets(line, sizeof line, fd) != NULL )
{
char* p = strchr(line, ':');
if (p != NULL)
{
frequency[Hash(_strlwr(line), (size_t)(p-line))%MAX_COUNT] += 1;
}
}
fclose(fd);
char header[MAX_COUNT + 1] = {0};
printf( "Enter the header:\n " );
scanf("%255s", header);
header[MAX_COUNT] = '\0';
printf( "%s occurs %d times.\n", header, frequency[Hash(_strlwr(header), strlen(_strlwr(header)))%MAX_COUNT] );
}
return 0;
}
static int Hash (const char * pSrc, size_t len)
{
size_t res = 0;
while (len--) res = (res << 1) ^ *pSrc++;
return (int)res;
}
content-length: 10
len = 14, hash = 177
user-agent: test
len = 10, hash = 107
content-length: 14
len = 14, hash = 177
accept: comedy
len = 6, hash = 16
content-length: 100
len = 14, hash = 177
content-encoding: gzip
len = 16, hash = 134
connection: close
len = 10, hash = 50
user-agent: test
len = 10, hash = 107
accept: flash
len = 6, hash = 16
user-agent: test1
len = 10, hash = 107
content-length: 20
len = 14, hash = 177
user-agent: test2
len = 10, hash = 107
user-agent: test3
len = 10, hash = 107
accept: gzip
len = 6, hash = 16
Enter the header:
user-aGENT
user-agent occurs 5 times.
No comments:
Post a Comment