#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#define MIN(X,Y) ((X<Y)?X:Y)
#define MAX(X,Y) ((X>Y)?X:Y)
const int MAX_LINE = 256;
class FastReader
{
public:
FastReader();
bool Read(FILE* filePtr, char* buffer, unsigned int* len);
bool Readline(FILE* filePtr, char* buffer, unsigned int* len);
private:
char savebuf[MAX_LINE];
char* saveptr;
unsigned int savecount;
};
FastReader::FastReader()
: savecount(0)
{
}
bool FastReader::Read(FILE* filePtr, char* buffer, unsigned int* len)
{
unsigned int want = *len;
if (savecount)
{
unsigned int ncopy = MIN(want, savecount);
memcpy(buffer, saveptr, ncopy);
savecount -= ncopy;
saveptr += ncopy;
buffer += ncopy;
want -= ncopy;
}
while (want)
{
unsigned int result = fread(savebuf, sizeof(char), MAX_LINE, filePtr);
if (result)
{
unsigned int ncopy= MIN(want, result);
memcpy(buffer, savebuf, ncopy);
savecount = result - ncopy;
saveptr = savebuf + ncopy;
buffer += ncopy;
want -= ncopy;
}
else {
*len -= want;
if (*len)
return true; else
return false; }
}
return true;
}
bool FastReader::Readline(FILE* filePtr, char* buffer, unsigned int* len)
{
unsigned int count = 0;
unsigned int length = *len;
char* ptr = buffer;
unsigned int one = 1;
while ((count < length) && Read(filePtr, ptr, &one))
{
if (('\n' == *ptr) || ('\r' == *ptr))
{
*ptr = '\0';
*len = count;
return true;
}
count++;
ptr++;
}
if (count < length) *len = 0; return false;
}
int main(int argc, char* argv[])
{
FastReader reader;
char buffer[MAX_LINE];
unsigned long line = 0;
unsigned int len = MAX_LINE;
const char* header = "NormSession::ServerUpdateGroupSize";
unsigned int headerLen = strlen(header);
double totalSize = 0.0;
unsigned long sizeCount = 0;
while (reader.Readline(stdin, buffer, &len))
{
line++;
if (!strncmp(buffer, header, headerLen))
{
char* ptr = strstr(buffer, "size:");
if (!ptr) break;
double size;
if (1 != sscanf(ptr, "size: %lf", &size))
{
fprintf(stderr, "sizeAve: Error parsing log file "
"at line: %lu\n", line);
exit(-1);
}
totalSize += size;
sizeCount++;
}
len = MAX_LINE;
}
double sizeAve = 0.0;
if (sizeCount)
{
sizeAve = totalSize / (double)sizeCount;
}
fprintf(stdout, "Average group size estimate:%lf\n", sizeAve);
}