OLD | NEW |
1 /* | 1 /* |
2 ** This C program extracts all "words" from an input document and adds them | 2 ** This C program extracts all "words" from an input document and adds them |
3 ** to an SQLite database. A "word" is any contiguous sequence of alphabetic | 3 ** to an SQLite database. A "word" is any contiguous sequence of alphabetic |
4 ** characters. All digits, punctuation, and whitespace characters are | 4 ** characters. All digits, punctuation, and whitespace characters are |
5 ** word separators. The database stores a single entry for each distinct | 5 ** word separators. The database stores a single entry for each distinct |
6 ** word together with a count of the number of occurrences of that word. | 6 ** word together with a count of the number of occurrences of that word. |
7 ** A fresh database is created automatically on each run. | 7 ** A fresh database is created automatically on each run. |
8 ** | 8 ** |
9 ** wordcount DATABASE INPUTFILE | 9 ** wordcount DATABASE INPUTFILE |
10 ** | 10 ** |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 ** | 73 ** |
74 ** gcc -I. -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ | 74 ** gcc -I. -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ |
75 ** wordcount.c sqlite3.c | 75 ** wordcount.c sqlite3.c |
76 */ | 76 */ |
77 #include <stdio.h> | 77 #include <stdio.h> |
78 #include <string.h> | 78 #include <string.h> |
79 #include <ctype.h> | 79 #include <ctype.h> |
80 #include <stdlib.h> | 80 #include <stdlib.h> |
81 #include <stdarg.h> | 81 #include <stdarg.h> |
82 #include "sqlite3.h" | 82 #include "sqlite3.h" |
| 83 #define ISALPHA(X) isalpha((unsigned char)(X)) |
83 | 84 |
84 /* Return the current wall-clock time */ | 85 /* Return the current wall-clock time */ |
85 static sqlite3_int64 realTime(void){ | 86 static sqlite3_int64 realTime(void){ |
86 static sqlite3_vfs *clockVfs = 0; | 87 static sqlite3_vfs *clockVfs = 0; |
87 sqlite3_int64 t; | 88 sqlite3_int64 t; |
88 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); | 89 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
89 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ | 90 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ |
90 clockVfs->xCurrentTimeInt64(clockVfs, &t); | 91 clockVfs->xCurrentTimeInt64(clockVfs, &t); |
91 }else{ | 92 }else{ |
92 double r; | 93 double r; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 rc = sqlite3_prepare_v2(db, | 386 rc = sqlite3_prepare_v2(db, |
386 "DELETE FROM wordcount WHERE word=?1", | 387 "DELETE FROM wordcount WHERE word=?1", |
387 -1, &pDelete, 0); | 388 -1, &pDelete, 0); |
388 if( rc ) fatal_error("Could not prepare the DELETE statement: %s\n", | 389 if( rc ) fatal_error("Could not prepare the DELETE statement: %s\n", |
389 sqlite3_errmsg(db)); | 390 sqlite3_errmsg(db)); |
390 } | 391 } |
391 | 392 |
392 /* Process the input file */ | 393 /* Process the input file */ |
393 while( fgets(zInput, sizeof(zInput), in) ){ | 394 while( fgets(zInput, sizeof(zInput), in) ){ |
394 for(i=0; zInput[i]; i++){ | 395 for(i=0; zInput[i]; i++){ |
395 if( !isalpha(zInput[i]) ) continue; | 396 if( !ISALPHA(zInput[i]) ) continue; |
396 for(j=i+1; isalpha(zInput[j]); j++){} | 397 for(j=i+1; ISALPHA(zInput[j]); j++){} |
397 | 398 |
398 /* Found a new word at zInput[i] that is j-i bytes long. | 399 /* Found a new word at zInput[i] that is j-i bytes long. |
399 ** Process it into the wordcount table. */ | 400 ** Process it into the wordcount table. */ |
400 if( iMode==MODE_DELETE ){ | 401 if( iMode==MODE_DELETE ){ |
401 sqlite3_bind_text(pDelete, 1, zInput+i, j-i, SQLITE_STATIC); | 402 sqlite3_bind_text(pDelete, 1, zInput+i, j-i, SQLITE_STATIC); |
402 if( sqlite3_step(pDelete)!=SQLITE_DONE ){ | 403 if( sqlite3_step(pDelete)!=SQLITE_DONE ){ |
403 fatal_error("DELETE failed: %s\n", sqlite3_errmsg(db)); | 404 fatal_error("DELETE failed: %s\n", sqlite3_errmsg(db)); |
404 } | 405 } |
405 sqlite3_reset(pDelete); | 406 sqlite3_reset(pDelete); |
406 }else if( iMode==MODE_SELECT ){ | 407 }else if( iMode==MODE_SELECT ){ |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur,iHiwtr); | 537 printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur,iHiwtr); |
537 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, 0); | 538 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, 0); |
538 printf("-- Largest Allocation: %d bytes\n",iHiwtr); | 539 printf("-- Largest Allocation: %d bytes\n",iHiwtr); |
539 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, 0); | 540 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, 0); |
540 printf("-- Largest Pcache Allocation: %d bytes\n",iHiwtr); | 541 printf("-- Largest Pcache Allocation: %d bytes\n",iHiwtr); |
541 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, 0); | 542 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, 0); |
542 printf("-- Largest Scratch Allocation: %d bytes\n", iHiwtr); | 543 printf("-- Largest Scratch Allocation: %d bytes\n", iHiwtr); |
543 } | 544 } |
544 return 0; | 545 return 0; |
545 } | 546 } |
OLD | NEW |