View difference between Paste ID: 0vaDDAC0 and GJTUbdu6
SHOW: | | - or go back to the newest paste.
1
#include <stdio.h>
2
#include <string.h>
3
#include <stdlib.h>
4
#include <time.h>
5
6
//#define _countof(a) (sizeof(a)/sizeof(*(a)))
7
8
#define DoBenchmark
9
10
#ifdef DoBenchmark
11-
  #define _Benchmark while (abs(clock()-ttime)<CLOCKS_PER_SEC)
11+
  #define _Benchmark iCount=0; while ((abs(clock()-ttime)<CLOCKS_PER_SEC) && ++iCount)
12
#else
13
  #define _Benchmark 
14
#endif
15
16
typedef struct {
17
    int group;
18
    const char* rule;
19
    const char* ldraw_output;
20
} Rule;
21
22
#include "table.c"
23
24
const Rule* ruleIdx[_countof(rules)];
25
26
//comparator function for qsort
27
int CompareRules( const void* pA , const void* pB ) {
28
	return strcmp( (*((const Rule**)pA))->rule , (*((const Rule**)pB))->rule );
29
}
30
void InitRules() {
31
	//set ptrs for each rules element at initial index
32
	for (int N=0 ; N<_countof(ruleIdx) ; N++ ) { ruleIdx[N]	= &rules[N]; }		
33
	qsort( ruleIdx , _countof(ruleIdx) , sizeof(void*) , CompareRules );
34
	printf("%i rules\n",_countof(ruleIdx));
35
}
36
const Rule* FindRule( char* pzRule ) {
37
	int iBegin = 0 , iEnd = _countof(ruleIdx);
38
	while (iEnd >= iBegin) {
39
		//try item from middle
40
		int idx = (iBegin+iEnd)/2;			
41
		const char* pzEntry = ruleIdx[idx]->rule;
42
		int iResu = strcmp( pzRule , pzEntry );
43
		if (!iResu) { return ruleIdx[idx]; } //found
44
		if (iBegin == iEnd) { return NULL; } //NOT found
45
		//remove the wrong half of possibilities		
46
		if (iResu>0) { iBegin= idx+1; } else { iEnd = idx-1; }		
47
	}
48
	return NULL; //NOT found
49
} 
50
51
void NormalizeInput( char* pzInput ) {	
52
	char WasSpace=1, *pOut = pzInput, C;
53
	while ((C=*pzInput++)) {
54
		switch (C) {
55
		case ' ' : case '\t' : case '\r' : case '\n' : 
56
		  if (!WasSpace) { *pOut++ = ' '; }
57
			WasSpace=1; break;
58
		default: 
59
		  *pOut++ = C; WasSpace=0;
60
		}
61
	}
62
	while (pOut[-1]==' ') { pOut--; }
63
	*pOut = '\0';	
64
}
65
66
int main() {
67
68
    InitRules();
69
    
70
    FILE *outputFile = fopen("output.txt", "a"); // Open the file in append mode
71
72
    while (1) {
73
        char userInput[100];
74
75
        printf("Enter a string (or 'exit' to quit): ");
76
        fgets(userInput, 100, stdin);
77
        userInput[strcspn(userInput, "\n")] = '\0';
78
				NormalizeInput( userInput );
79
				printf("'%s'\n",userInput);
80
				
81
82
        if (strcmp(userInput, "exit") == 0) {
83
            break;  // Exit the loop if the user types 'exit'
84
        }
85
86
				#ifdef DoBenchmark
87
				clock_t ttime = clock();
88
				long long iCount = 0;
89
				#endif				
90
				
91
				const Rule* pFound = NULL;									
92
				#if 1
93
					_Benchmark {
94-
						pFound = FindRule( userInput );					
94+
						pFound = FindRule( userInput );
95-
						iCount++;
95+
96-
					}								
96+
97
					_Benchmark {
98
						for (int N=0 ; N<_countof(rules) ; N++) {
99
							if (!strcmp(userInput , rules[N].rule)) {
100
								pFound = &rules[N]; break; 
101
							}							
102
						}						
103-
						}
103+
104-
						iCount++;
104+
105
				if (pFound) {
106
					printf("%s\n", pFound->ldraw_output);
107
					fprintf(outputFile, "%s\n", pFound->ldraw_output);
108
				} else {
109
					printf("No match found.\n");
110
				}				
111
				
112
				#ifdef DoBenchmark
113
				printf("took %.05fms %lli/s\n",1000.0/iCount,iCount);
114
				#endif
115
				
116
    }
117
118
    fclose(outputFile); // Close the file
119
120
    return 0;
121
		
122
}
123