home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / hl10osrc.lzh / Lib / EvalExpr.cc < prev    next >
Text File  |  1994-04-23  |  6KB  |  156 lines

  1. /* -*- Mode: C -*- */
  2. /* EvalExpr.cc - Evaluate a -only expression (pre-parsed by ParseExpr)
  3.  * Created by Robert Heller on Sat Feb 29 12:04:08 1992
  4.  *
  5.  * ------------------------------------------------------------------
  6.  * Home Libarian by Deepwoods Software
  7.  * Common Class library implementation code
  8.  * ------------------------------------------------------------------
  9.  * Modification History:
  10.  * ------------------------------------------------------------------
  11.  * Contents:
  12.  * ------------------------------------------------------------------
  13.  * 
  14.  * 
  15.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  16.  *        All Rights Reserved
  17.  * 
  18.  */
  19.  
  20. #include <stream.h>
  21. #include <common.h>
  22. #include <Card.h>
  23. #include <PTree.h>
  24. #include <ctype.h>
  25.  
  26. static Boolean cfstrcmp(char* a,char* b)
  27. {
  28.     int comp;
  29.     char aa, bb;
  30.     do {
  31.         aa = *a++;
  32.         bb = *b++;
  33.         if (islower(aa)) aa = toupper(aa);
  34.         if (islower(bb)) bb = toupper(bb);
  35.         comp = aa - bb;
  36.     } while (comp == 0 && aa != 0 && bb != 0);
  37.     return(comp);
  38. }
  39.  
  40. #define streqp(a,b) (cfstrcmp(a,b) == 0)
  41. #define strnep(a,b) (cfstrcmp(a,b) != 0)
  42. #define strltp(a,b) (cfstrcmp(a,b) <  0)
  43. #define strgtp(a,b) (cfstrcmp(a,b) >  0)
  44. #define strlep(a,b) (cfstrcmp(a,b) <= 0)
  45. #define strgep(a,b) (cfstrcmp(a,b) >= 0)
  46.  
  47. static Boolean Compare(Card* card,Operators op,Node* a,Node* b)
  48. {
  49.     switch (op) {
  50.         case Equal:
  51.             if (a->type != FieldNode) return(Compare(card,op,b,a));
  52.             else {
  53.                 switch (a->value._field) {
  54.                         case TypeField: return(card->type == b->value._ctype);
  55.                         case AuthorField: return(streqp(card->author,b->value._string));
  56.                         case TitleField: return(streqp(card->title,b->value._string));
  57.                         case PublisherField: return(streqp(card->publisher,b->value._string));
  58.                         case CityField: return(streqp(card->city,b->value._string));
  59.                         case VolumeField: return(card->vol == b->value._int);
  60.                         case YearField: return(card->year == b->value._int);
  61.                     }
  62.             }
  63.             break;
  64.         case NotEqual:
  65.             if (a->type != FieldNode) return(Compare(card,op,b,a));
  66.             else {
  67.                     switch (a->value._field) {
  68.                         case TypeField: return(card->type != b->value._ctype);
  69.                         case AuthorField: return(strnep(card->author,b->value._string));
  70.                         case TitleField: return(strnep(card->title,b->value._string));
  71.                         case PublisherField: return(strnep(card->publisher,b->value._string));
  72.                         case CityField: return(strnep(card->city,b->value._string));
  73.                         case VolumeField: return(card->vol != b->value._int);
  74.                         case YearField: return(card->year != b->value._int);
  75.                     }
  76.             }
  77.             break;
  78.         case Less:
  79.             if (a->type != FieldNode) return(Compare(card,Greater,b,a));
  80.             else {
  81.                     switch (a->value._field) {
  82.                         case TypeField: return(card->type < b->value._ctype);
  83.                         case AuthorField: return(strltp(card->author,b->value._string));
  84.                         case TitleField: return(strltp(card->title,b->value._string));
  85.                         case PublisherField: return(strltp(card->publisher,b->value._string));
  86.                         case CityField: return(strltp(card->city,b->value._string));
  87.                         case VolumeField: return(card->vol < b->value._int);
  88.                         case YearField: return(card->year < b->value._int);
  89.                     }
  90.             }
  91.             break;
  92.         case Greater:
  93.             if (a->type != FieldNode) return(Compare(card,Less,b,a));
  94.             else {
  95.                     switch (a->value._field) {
  96.                         case TypeField: return(card->type > b->value._ctype);
  97.                         case AuthorField: return(strgtp(card->author,b->value._string));
  98.                         case TitleField: return(strgtp(card->title,b->value._string));
  99.                         case PublisherField: return(strgtp(card->publisher,b->value._string));
  100.                         case CityField: return(strgtp(card->city,b->value._string));
  101.                         case VolumeField: return(card->vol > b->value._int);
  102.                         case YearField: return(card->year > b->value._int);
  103.                     }
  104.             }
  105.             break;
  106.         case LessEqual:
  107.             if (a->type != FieldNode) return(Compare(card,GreaterEqual,b,a));
  108.             else {
  109.                     switch (a->value._field) {
  110.                         case TypeField: return(card->type <= b->value._ctype);
  111.                         case AuthorField: return(strlep(card->author,b->value._string));
  112.                         case TitleField: return(strlep(card->title,b->value._string));
  113.                         case PublisherField: return(strlep(card->publisher,b->value._string));
  114.                         case CityField: return(strlep(card->city,b->value._string));
  115.                         case VolumeField: return(card->vol <= b->value._int);
  116.                         case YearField: return(card->year <= b->value._int);
  117.                     }
  118.             }
  119.             break;
  120.         case GreaterEqual:
  121.             if (a->type != FieldNode) return(Compare(card,LessEqual,b,a));
  122.             else {
  123.                     switch (a->value._field) {
  124.                         case TypeField: return(card->type >= b->value._ctype);
  125.                         case AuthorField: return(strgep(card->author,b->value._string));
  126.                         case TitleField: return(strgep(card->title,b->value._string));
  127.                         case PublisherField: return(strgep(card->publisher,b->value._string));
  128.                         case CityField: return(strgep(card->city,b->value._string));
  129.                         case VolumeField: return(card->vol >= b->value._int);
  130.                         case YearField: return(card->year >= b->value._int);
  131.                     }
  132.             }
  133.             break;
  134.     }
  135.     return(false);
  136. }
  137.  
  138.  
  139. Boolean EvalExpr(Card* card,Tree* tree)
  140. {
  141.     Node* a = tree->left;
  142.     Node* b = tree->right;
  143.     if (tree == 0) return(true);
  144.     else switch (tree->oper) {
  145.         case Or: return(EvalExpr(card,a->value._tree) ||
  146.                 EvalExpr(card,b->value._tree));
  147.               break;
  148.         case And: return(EvalExpr(card,a->value._tree) &&
  149.                  EvalExpr(card,b->value._tree));
  150.                break;
  151.         case Not: return(!EvalExpr(card,a->value._tree));
  152.               break;
  153.         default: return(Compare(card,tree->oper,a,b));
  154.     }
  155. }
  156.