New month parsing algorithm that dynamically adjusts itself so the most cpp
authorDustin Sallings <dustin@spy.net>
Thu Jun 07 02:05:22 2007 -0700 (2 years ago)
branchcpp
changeset 51ed2d915f1f69
parent 5090826de64378
child 5255ca792142e8
New month parsing algorithm that dynamically adjusts itself so the most
recently used month is in the beginning.
logfiles.cpp
     1.1 --- a/logfiles.cpp	Thu Jun 07 01:57:30 2007 -0700
     1.2 +++ b/logfiles.cpp	Thu Jun 07 02:05:22 2007 -0700
     1.3 @@ -147,72 +147,36 @@
     1.4  	return(rv);
     1.5  }
     1.6  
     1.7 +/* A date and a string */
     1.8 +struct date_str {
     1.9 +	char *datestr;
    1.10 +	int val;
    1.11 +};
    1.12 +
    1.13  /* Convert a three character month to the numeric value */
    1.14 -// generated by gensearch.py
    1.15  TESTED_STATIC int parseMonth(const char *input) {
    1.16      int rv=-1;
    1.17 -    const char *p=input;
    1.18 +	struct date_str tmp;
    1.19 +	static struct date_str dates[]={
    1.20 +		{"Jan/", 0}, {"Feb/", 1}, {"Mar/", 2}, {"Apr/", 3}, {"May/", 4},
    1.21 +		{"Jun/", 5}, {"Jul/", 6}, {"Aug/", 7}, {"Sep/", 8}, {"Oct/", 9},
    1.22 +		{"Nov/", 10}, {"Dec/", 11}
    1.23 +	};
    1.24  
    1.25 -    /* Collapsing anything with fewer than 4 paths */
    1.26 -	/* 12 matches for "" */
    1.27 -	switch(p[0]) {
    1.28 -		case 'A':
    1.29 -			/* 2 match(es) for "A" */
    1.30 -			if(memcmp(p, "Apr/", 4) == 0) {
    1.31 -				rv=3;
    1.32 -			} else if(memcmp(p, "Aug/", 4) == 0) {
    1.33 -				rv=7;
    1.34 +	for(int i=0; i<12; i++) {
    1.35 +		if(memcmp(dates[i].datestr, input, 4) == 0) {
    1.36 +			rv=dates[i].val;
    1.37 +			// If it's not already, move it to the front of the list.  It'll
    1.38 +			// probably be in use for a while.
    1.39 +			if(i != 0) {
    1.40 +				tmp=dates[0];
    1.41 +				dates[0]=dates[i];
    1.42 +				dates[i]=tmp;
    1.43  			}
    1.44 -		break;
    1.45 -		case 'D':
    1.46 -			/* 1 match(es) for "D" */
    1.47 -			if(memcmp(p, "Dec/", 4) == 0) {
    1.48 -				rv=11;
    1.49 -			}
    1.50 -		break;
    1.51 -		case 'F':
    1.52 -			/* 1 match(es) for "F" */
    1.53 -			if(memcmp(p, "Feb/", 4) == 0) {
    1.54 -				rv=1;
    1.55 -			}
    1.56 -		break;
    1.57 -		case 'J':
    1.58 -			/* 3 match(es) for "J" */
    1.59 -			if(memcmp(p, "Jan/", 4) == 0) {
    1.60 -				rv=0;
    1.61 -			} else if(memcmp(p, "Jun/", 4) == 0) {
    1.62 -				rv=5;
    1.63 -			} else if(memcmp(p, "Jul/", 4) == 0) {
    1.64 -				rv=6;
    1.65 -			}
    1.66 -		break;
    1.67 -		case 'M':
    1.68 -			/* 2 match(es) for "M" */
    1.69 -			if(memcmp(p, "Mar/", 4) == 0) {
    1.70 -				rv=2;
    1.71 -			} else if(memcmp(p, "May/", 4) == 0) {
    1.72 -				rv=4;
    1.73 -			}
    1.74 -		break;
    1.75 -		case 'O':
    1.76 -			/* 1 match(es) for "O" */
    1.77 -			if(memcmp(p, "Oct/", 4) == 0) {
    1.78 -				rv=9;
    1.79 -			}
    1.80 -		break;
    1.81 -		case 'N':
    1.82 -			/* 1 match(es) for "N" */
    1.83 -			if(memcmp(p, "Nov/", 4) == 0) {
    1.84 -				rv=10;
    1.85 -			}
    1.86 -		break;
    1.87 -		case 'S':
    1.88 -			/* 1 match(es) for "S" */
    1.89 -			if(memcmp(p, "Sep/", 4) == 0) {
    1.90 -				rv=8;
    1.91 -			}
    1.92 -		break;
    1.93 +			break;
    1.94 +		}
    1.95  	}
    1.96 +
    1.97  	return rv;
    1.98  }
    1.99