home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / manipuls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  6.8 KB  |  237 lines

  1. /* 
  2.  * manipulstr.c -- 
  3.  * SCCS Status     : %W%        %G%
  4.  * Author          : Huynh Quoc T. Tung
  5.  * Created On      : Sun Oct 17 16:43:35 1993
  6.  * Last Modified By: Huynh Quoc T. Tung
  7.  * Last Modified On: Sun Oct 17 16:43:38 1993
  8.  * Update Count    : 1
  9.  * Status          : Unknown, Use with caution!
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include "cutil.h"
  16.  
  17. #define LITERAL_KEY1    '"'   
  18. #define LITERAL_KEY2    0x27    /* single quote ' dgg */
  19. #define MAX_WORD_LENGTH 20
  20. #define MAX_LINE_LENGTH 1000 
  21.  
  22. /* HQTT, 5.10.93  */
  23. long number_of_qwords = 0;
  24.  
  25. struct string_node
  26.   char key[4]; 
  27.   struct string_node *next;
  28. };
  29.  
  30. static struct string_node *head_string, *z_string, *t_string;
  31.  
  32. void string_stackinit _AP(());
  33. void string_stackinit()
  34. {
  35.   head_string = (struct string_node *)malloc(sizeof (struct string_node));
  36.   z_string = (struct string_node *)malloc(sizeof(struct string_node));
  37.   head_string->next = z_string; 
  38.   head_string->key[0] = '\0' ; 
  39.   z_string->next = z_string;
  40. }
  41.  
  42. void string_push _AP((char* string));
  43. void string_push(string)
  44.      char* string;
  45. {
  46.   t_string = (struct string_node *)malloc(sizeof(struct string_node));
  47.   s_strncpy(t_string->key, string, strlen(string)+1);
  48.   t_string->next = head_string->next;
  49.   head_string->next = t_string;
  50. }
  51.  
  52. static char* string_pop _AP(());
  53. static char* string_pop()
  54. {
  55.   char* x_string;
  56.  
  57.   t_string = head_string->next;
  58.   head_string->next = t_string->next;
  59.   x_string = t_string->key;
  60.   free(t_string);
  61.   return(x_string);
  62. }
  63.  
  64. static int string_stackempty _AP(());
  65. static int string_stackempty()
  66. {
  67.   return(head_string->next == z_string);
  68. }
  69.  
  70.  
  71. static char* infix_to_postfix _AP((char* line));
  72. static char* infix_to_postfix(line)
  73.      char *line;
  74. {
  75.   char* word;
  76.   char* result;
  77.   char* cpy_line;
  78.   int parentheses_count = 0;
  79.   int operand_count = 0;
  80.   int literal_count = 0;
  81.  
  82.   cpy_line = (char *)s_malloc((strlen(line) + 1) * sizeof(char));
  83.   result = (char *)s_malloc((strlen(line) + 1) * sizeof(char));
  84.   result[0] = '\0' ;
  85.   cpy_line[0] = '\0' ;
  86.   s_strncpy(cpy_line, line, strlen(line)+1);
  87.   word = strtok(cpy_line, " ");
  88.   string_stackinit(); 
  89.   while(word != NULL) {
  90.     if(!strcmp(word, "(")) {
  91.       ++parentheses_count;
  92.       operand_count = 0;
  93.     }
  94.     if(!strcmp(word, ")")) {
  95.       --parentheses_count;
  96.       if(!string_stackempty()) {
  97.         s_strncat(result, string_pop(), MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  98.         s_strncat(result, " ", MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  99.       }
  100.       if((parentheses_count == 0) && !string_stackempty()) {
  101.         s_strncat(result, string_pop(), MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  102.         s_strncat(result, " ", MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  103.       }
  104.       operand_count = 1;
  105.     }
  106.     else if(!strcmp(word, "or") && (literal_count == 0)) {
  107.       if(operand_count == 2) {
  108.         if(!string_stackempty()) {
  109.           s_strncat(result, string_pop(), MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  110.           s_strncat(result, " ", MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  111.           operand_count = 1;
  112.         }
  113.       }
  114.       string_push(word);
  115.     }
  116.     else if(!strcmp(word, "and") && (literal_count == 0)) {
  117.       if(operand_count == 2) {
  118.         if(!string_stackempty()) {
  119.           s_strncat(result, string_pop(), MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  120.           s_strncat(result, " ", MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  121.           operand_count = 1;
  122.         }
  123.       }
  124.       string_push(word);
  125.     }
  126.     else if(!strcmp(word, "not") && (literal_count == 0)) {
  127.       if(operand_count == 2) {
  128.         s_strncat(result, string_pop(), MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  129.         s_strncat(result, " ", MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  130.         operand_count = 1;
  131.       }
  132.       string_push(word); 
  133.     }
  134.     else if (strcmp(word, "(")){
  135.       s_strncat(result, word, MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  136.       s_strncat(result, " ", MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  137.       if(!strcmp(word,"soundex") || !strcmp(word,"phonix")) ;
  138.       else if(((word[0] == LITERAL_KEY1) && 
  139.                (word[strlen(word)-1] == LITERAL_KEY1)) ||
  140.               ((word[0] == LITERAL_KEY2) && 
  141.                (word[strlen(word)-1] == LITERAL_KEY2))) {
  142.         ++operand_count;
  143.         ++number_of_qwords; 
  144.       }
  145.       else if(((word[0] == LITERAL_KEY1) && 
  146.                (word[strlen(word)-1] != LITERAL_KEY1)) ||
  147.               ((word[0] == LITERAL_KEY2) && 
  148.                (word[strlen(word)-1] != LITERAL_KEY2))) {
  149.         literal_count = 1;
  150.       }
  151.       else if(((word[0] != LITERAL_KEY1) &&
  152.                (word[strlen(word)-1] == LITERAL_KEY1) &&
  153.                (literal_count == 1)) ||
  154.               ((word[0] != LITERAL_KEY2) && 
  155.                (word[strlen(word)-1] == LITERAL_KEY2) &&
  156.                (literal_count == 1))) {
  157.         ++operand_count;
  158.         ++number_of_qwords;
  159.         literal_count = 0;
  160.       }
  161.       else {
  162.         if(literal_count == 0) {
  163.           ++operand_count;
  164.           ++number_of_qwords;
  165.         }
  166.       }
  167.     }
  168.     word = strtok(NULL, " ");
  169.   }
  170.   while(!string_stackempty()) { 
  171.     s_strncat(result, string_pop(), MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  172.     s_strncat(result, " ", MAX_LINE_LENGTH, MAX_LINE_LENGTH);
  173.   }
  174.   s_free(cpy_line);
  175.   return(result);
  176.  
  177. }
  178.  
  179. char *string_manipulation(line)
  180.      char *line;
  181. {
  182.   int char_count, char_count_of_newline;
  183.   char ch;
  184. #ifndef WIN32
  185.   char *word;
  186. #endif
  187.   char newline[MAX_LINE_LENGTH+1];
  188.   boolean parentheses = false;
  189.   boolean special_chr = false;
  190.   char_count = char_count_of_newline = 0;
  191.  
  192.   for(ch = line[char_count++]; 
  193.       ch != '\0'; ch = line[char_count++]){
  194.     if(isalnum(ch) && !special_chr) {
  195.       newline[char_count_of_newline++] = ch;
  196.       parentheses = false;
  197.     }
  198.     else if(isspace(ch) || (ch == '\t') || (ch == '\n')) 
  199.       special_chr = true;
  200.     else if(isalnum(ch) && special_chr) {
  201.       newline[char_count_of_newline++] = ' ';
  202.       newline[char_count_of_newline++] = ch;
  203.       special_chr = false;
  204.       parentheses = false;
  205.     }
  206.     else if((ch == '(') || (ch == ')')) {
  207.       newline[char_count_of_newline++] = ' ';
  208.       newline[char_count_of_newline++] = ch;
  209.       parentheses = true;
  210.       special_chr = true;
  211.     }
  212.     else {
  213.       if(parentheses) {
  214.         newline[char_count_of_newline++] = ' ';
  215.         newline[char_count_of_newline++] = ch;
  216.         special_chr = false;
  217.         parentheses = false;
  218.       }
  219.       else {
  220.         if(special_chr) {
  221.           newline[char_count_of_newline++] = ' ';
  222.           newline[char_count_of_newline++] = ch;
  223.         }
  224.         else
  225.           newline[char_count_of_newline++] = ch;
  226.         special_chr = false;
  227.       } 
  228.     }
  229.   }
  230.   newline[char_count_of_newline] = '\0';
  231.   s_strncpy(newline, infix_to_postfix(newline), MAX_LINE_LENGTH);
  232.   return(newline);
  233. }   
  234.  
  235.   
  236.