CBT.C 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <mlang.h>
  2. #include <conio.h>
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <btree.h>
  8. #define MODE Compress
  9. #define DATSIZE 0
  10. #define LINE_LEN 4096
  11. #define KEY_LEN 20
  12. typedef char Dat[LINE_LEN];
  13. typedef char Key[KEY_LEN];
  14. typedef Dat * DatPtr;
  15. typedef Key * KeyPtr;
  16. CmpRes comp(const void * a, const void * b) {
  17. int i;
  18. i = strcmp(*((KeyPtr)a),*((KeyPtr)b));
  19. if (i<0) {
  20. return Less;
  21. } else if (i==0) {
  22. return Eq;
  23. } else {
  24. return Greater;
  25. };
  26. }
  27. void key(void * k, const void * d) {
  28. strncpy(*((KeyPtr)k),*((DatPtr)d),sizeof(Key));
  29. }
  30. void main(unsigned argc, const char *argv[]) {
  31. FHandle fH;
  32. IHandle dH,iH;
  33. FILE *f;
  34. Dat d;
  35. int i;
  36. InitModules(Btree$,Pack$,NULL);
  37. if ((argc!=2) || !(f = fopen(argv[1],"rt"))) {
  38. cprintf("Usage:\n cbt infile\n\nSorts text lines from infile to stdout.\n");
  39. return;
  40. }
  41. fH = Btree$Open("cbt.dat",2,MODE,FALSE,FALSE,TRUE);
  42. dH = Btree$OpenData(fH,1,DATSIZE,TRUE);
  43. iH = Btree$OpenIndex(fH,dH,2,comp,key,sizeof(Key),TRUE,TRUE);
  44. while (fgets(d,sizeof(d),f)!=NULL) {
  45. i = strlen(d);
  46. while ((i>0)&&(d[i-1]<=' ')) i--;
  47. d[i] = '\0';
  48. Btree$Add(dH,d,i+1);
  49. }
  50. fclose(f);
  51. Btree$Reset(dH);
  52. while (TRUE) {
  53. if (!Btree$Next(iH,&d)) {
  54. break;
  55. }
  56. cprintf("%s\n",d);
  57. }
  58. Btree$Close(&fH);
  59. return;
  60. }
  61.