| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include <mlang.h>
- #include <conio.h>
- #include <stddef.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <btree.h>
- #define MODE Compress
- #define DATSIZE 0
- #define LINE_LEN 4096
- #define KEY_LEN 20
- typedef char Dat[LINE_LEN];
- typedef char Key[KEY_LEN];
- typedef Dat * DatPtr;
- typedef Key * KeyPtr;
- CmpRes comp(const void * a, const void * b) {
- int i;
- i = strcmp(*((KeyPtr)a),*((KeyPtr)b));
- if (i<0) {
- return Less;
- } else if (i==0) {
- return Eq;
- } else {
- return Greater;
- };
- }
- void key(void * k, const void * d) {
- strncpy(*((KeyPtr)k),*((DatPtr)d),sizeof(Key));
- }
- void main(unsigned argc, const char *argv[]) {
- FHandle fH;
- IHandle dH,iH;
- FILE *f;
- Dat d;
- int i;
- InitModules(Btree$,Pack$,NULL);
- if ((argc!=2) || !(f = fopen(argv[1],"rt"))) {
- cprintf("Usage:\n cbt infile\n\nSorts text lines from infile to stdout.\n");
- return;
- }
- fH = Btree$Open("cbt.dat",2,MODE,FALSE,FALSE,TRUE);
- dH = Btree$OpenData(fH,1,DATSIZE,TRUE);
- iH = Btree$OpenIndex(fH,dH,2,comp,key,sizeof(Key),TRUE,TRUE);
- while (fgets(d,sizeof(d),f)!=NULL) {
- i = strlen(d);
- while ((i>0)&&(d[i-1]<=' ')) i--;
- d[i] = '\0';
- Btree$Add(dH,d,i+1);
- }
- fclose(f);
- Btree$Reset(dH);
- while (TRUE) {
- if (!Btree$Next(iH,&d)) {
- break;
- }
- cprintf("%s\n",d);
- }
- Btree$Close(&fH);
- return;
- }
|