
/******************************************************************
 *                                                                *
 *                  Computer Science Department                   *
 *                                                                *
 *                        Master's Project                        *
 *                                                                *
 *                Graphical Development Environment               *
 *                                                                *
 *                     for Postgres Database                      *
 *                                                                *
 *                    Designed Especially for                     *
 *                                                                *
 *                 Postgres Developers and Users                  *
 *                                                                *
 *                              At                                *
 *                                                                *
 *                     Concordia University                       *
 *                                                                *
 ******************************************************************
 *                                                                *
 * Designer and Programmer:  Khaled Jababo                        *
 * Email                  :  jababo@cs.concordia.ca               *
 *                                                                *
 * Supervised by          :  Dr. Bipin C. Desai.                  *
 *                           Computer Science Department.         *
 * Email                  :  bcdesai@cs.concordia.ca              *
 *                                                                *
 * Last Modified          :  June 18, 1994                        *
 *                                                                *
 *                                                                *
 *                                                                *
 * Disclaimer:  There are no guarantees as to the accuracy, cu-   *
 *             rrency, quality, or usefulness of these programs   *
 *             or any information that is provided by using it.   *
 *             Furthermore, there is no assumption of responsi-   *
 *             bility for any los or damage which may be caused   *
 *             by their use.                                      *
 *                                                                *
 *                                                                *
 * Forward comments or questions about this report or the GDEP    *
 * system to:                                                     *
 *              bcdesai@cs.concordia.ca                           *
 *              jababo@cs.concordia.ca                            *
 *                                                                *
 ******************************************************************/


/******************************************************************/
/***********************     function.c    ************************/
/*    This file Contains all the functions needed to find a part- */
/*  icular pattern in a string or in a file and returns the string*/
/*  which comes right after the pattern if pattern is found, else */
/*  returns an empty string.                                      */
/******************************************************************/

#include <stdio.h>
#include <ctype.h>
#include "mainfile.h"

#define TRUE                        1
#define FALSE                       0

#define LaMaxRecSize              250


typedef char LaString[LaMaxRecSize] ;


/******************************************************************/
/*                      find_pattern_in_file                      */
/*    Function to get a pattern and a filename then opens the     */
/*  specified file name and look for the pattern in the file, if  */
/*  the pattern is found then returns the string right after the  */
/*  pattern otherwise it returns an empty string.                 */
/*                                                                */
/******************************************************************/
 
char* find_pattern_in_file(pattern,filename)
  LaString pattern,filename ;
 
    { /* begin find_pattern_in_file function */

      FILE *stream_pattern_file ;
      LaString  record ;
      int  i      ;
      int  pattern_length ;
      int  record_length  ;
      int  found = FALSE  ;

      pattern_length = strlen(pattern) ;

      if ((stream_pattern_file = fopen(filename,"r")) == NULL)
        {
          printf("Sorry Can Not Open The Select File: %s\n",filename) ;
          return("\0") ;
        } ;

      while((found == FALSE) &&
            (fgets(record,LaMaxRecSize,stream_pattern_file) != NULL) )
        {
          record_length = strlen(record) - pattern_length;
          for (i=0; i < record_length && found == FALSE ; i++)
            if (strncmp(pattern,&(record[i]),pattern_length) == 0)
              {
                found = TRUE ;
                return(&(record[i + pattern_length])) ;
              }

        } ;
      fclose(stream_pattern_file) ;

      return("\0") ;

    } /* end   find_pattern_in_file function */ 


/******************************************************************/
/*                        MaFpatternInString                      */
/*    Function to find a Pattern in a string and returns the str- */
/*  that appears after the pattern if it exists, otherwise it re- */
/*  turns an empty string.                                        */
/*                                                                */
/******************************************************************/

char *MaFpatternInString (MaLpattern, MaLstring)
  char *MaLpattern, *MaLstring ;

    { /* begin MaFpatternInString function */
 
      int  i      ;
      int  MaLpatternLength ;
      int  MaLrecordLength  ;
      int  found = FALSE    ;
      MaBufferType MaLreturnBuffer ;

      strcpy(MaLreturnBuffer,"\0") ;
      MaLpatternLength = strlen(MaLpattern) ;

      MaLrecordLength = strlen(MaLstring) - MaLpatternLength;
      for (i=0; i < MaLrecordLength && found == FALSE ; i++)
        if (strncmp(MaLpattern,&(MaLstring[i]),MaLpatternLength) == 0)
          {
            found = TRUE ;
            strcpy(MaLreturnBuffer,&(MaLstring[i + MaLpatternLength])) ;
            return(MaLreturnBuffer) ;
          }
      return(MaLreturnBuffer) ;

    } /* end   MaFpatternInString function */


/*****************************************************************/
/*                                                               */
/*                       END OF pattern.c FILE                   */
/*                                                               */
/*****************************************************************/