
/******************************************************************
 *                                                                *
 *                  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 by the main ap- */
/*  plication to  manipulate strings  and  linked lists.  It also */
/*  contains functions to create, and free pointers of a specific */
/*  data structure.                                               */
/******************************************************************/

#include "mainfile.h"

#define MAX_REC_SIZE 200

typedef char str1[MAX_REC_SIZE] ;


/******************************************************************/
/*                             stoi                               */
/*    Function to convert a string to an integer value            */
/*  stoi() converts a string into an integer.                     */
/******************************************************************/

int stoi(s)
  char *s;

  { /* begin stoi function */
    int i, sum;
    /* skip the beginning zeros */
    for (i=0;s[i]!='\0' && s[i]!='\n' && (s[i]=='0'||s[i]==' '); i++);
    for (sum=0; s[i]!='\n' && isdigit(s[i]) && s[i] != '\0'; i++)
      sum = sum*10 + (s[i] - '0');
    return sum ;
  } /* end   stoi function */


/*****************************************************************/
/*                            stof                               */
/*     Function to convert a string to a float value             */
/*  stof() converts a string into a floating point number        */
/*                                                               */
/*****************************************************************/

float stof(s)
  char *s;

  { /* begin stof function */
    float val, power;
    int i;

    /* skip the beginning zeros or blank spaces */
    for (i=0; (s[i]==' ' || s[i]=='0') && s[i] != '\n' && s[i] != '\0' ; i++)
      ;
    for (val=0;s[i]>='0' && s[i]<= '9' &&  s[i] != '\n' && s[i] != '\0' ; i++)
      val = 10*val + s[i] - '0';
    if (s[i]=='.')
      i++;
    for (power=1;s[i]>='0' && s[i]<='9' && s[i] != '\n' && s[i] != '\0' ; i++)
      {
        val = 10 * val + s[i] - '0';
        power *= 10;
      }
    return (val/power);
  } /* end   stof function */


/*****************************************************************/
/*                          sto_lower                            */
/*     Function to convert a string to a string with the same    */
/*  contents but shift the upper case to lower case letters.     */
/*                                                               */
/*****************************************************************/

char * sto_lower(strings)
  char * strings ;

  { /* begin sto_lower function */
    char *np ;
    char *temp_strings ;

    temp_strings = strdup(strings) ;

    for (np = temp_strings; *np != '\0'; np++ )
      {
        if (isupper (*np))
          *np = tolower( *np ) ;
      }
    return temp_strings ;

  } /* end   sto_lower function */


/*****************************************************************/
/*                          sto_upper                            */
/*     Function to convert a string to a string with the same    */
/*  contents but shift the lower case to upper case letters.     */
/*                                                               */
/*****************************************************************/

char * sto_upper(strings)
  char * strings ;

  { /* begin sto_upper function */
    char *np ;
    char *temp_strings ;

    temp_strings = strdup(strings) ;

    for (np = temp_strings; *np != '\0'; np++ )
      {
        if (islower (*np))
          *np = toupper( *np ) ;
      }
    return temp_strings ;

  } /* end   sto_upper function */


/*****************************************************************/
/*                            get_strings                        */
/*     Function to read an input until it finds a return chara-  */
/*  cter, converts it into a string and returns it to the call-  */
/*  ing program.                                                 */
/*                                                               */
/*****************************************************************/

char* get_strings()
  { /* begin get_strings function */
    int i, j ;
    char input_string[600] ;

    input_string[0] = '\n' ;
    for (i=0 ; (input_string[i]=getchar()) != '\n' ; i++) {} ;
      input_string[i] = '\0' ;
    for (j=0 ; input_string[j] == ' ' && j < i ; j++) ;
    if ((input_string[0] == '\n')||(input_string[0]== '\0'))
      return(get_strings()) ;
    else
      {
        return(&(input_string[j])) ;
      }

  } /* end get_strings function */


/*****************************************************************/
/*                          get_first_word                       */
/*     Function that gets a string and returns the first word    */
/*  which appears in the string.                                 */
/*                                                               */
/*****************************************************************/

char* get_first_word(input_string)
  str1 input_string ;

  { /* begin get_first_word function */
    int i, j, k = 0 ;
    str1 result ;

    for (i=0 ; input_string[i] == ' ' ; i++) {} ;

    for (j=i ;
         input_string[j] != ' ' && input_string[j] != '\0' && input_string[j] != '\n' ;
         j++)
      {
        result[k++] = input_string[j] ;
      } ;
    result[k] = '\0' ;

    return(result) ;
  } /* end get_first_word function */


/*****************************************************************/
/*                            get_int                            */
/*     Function to return an integer read from the keyboard.     */
/*                                                               */
/*****************************************************************/

int get_int()
  {
    return(stoi(get_strings())) ;
  } /* end get_int function */


/*****************************************************************/
/*                            get_float                          */
/*     Function to read a float read from the keyboard.          */
/*                                                               */
/*****************************************************************/

float get_float()
  { /* begin get_float function */
    return(stof(get_strings())) ;
  } /* end get_float function */

/******************************************************************/
/*                           print_empty_lines                    */
/* Function to print a number of empty Lines.                     */
/*                                                                */
/******************************************************************/

void print_empty_lines(lines)
  int lines ;

  { /* begin print_empty_lines */
    int i ;
    for (i=0; i<lines; i++)
      printf("\n") ;

  } /* print_empty_lines */


/******************************************************************/
/*                       print_empty_spaces                       */
/*  Function to Insert a number of empty spaces.                  */
/*                                                                */
/******************************************************************/

void print_empty_spaces(spaces)
  int spaces;

  { /* begin print_empty_spaces function */
    int i;
    for (i=0; i<spaces; i++)
      printf(" ") ;
  } /* end   print_empty_spaces function */


/******************************************************************/
/*                             get_mode                           */
/*    Function that take an integer value and returns OFF string  */
/*  if its value is 0, otherwise it return ON string.             */
/*                                                                */
/******************************************************************/

char* get_mode (value)
  int value ;

  { /* begin get_mode function */
    if (value == 0)
      return("OFF") ;
    else
      return("ON") ;

  } /* end   get_mode function */


/******************************************************************/
/*                            get_cwd                             */
/*   Function that gets the current working directory and returns */
/*  its value to the calling program.                             */
/*                                                                */
/******************************************************************/

char* get_cwd()
  { /* begin get_cwd function */
    char *cwd, *getcwd();

    if ((cwd = getcwd((char *)NULL, 256)) == NULL)
      {
         perror ("pwd");
         exit (1);
      }
    return(cwd) ;

  } /* end   get_cwd function */


/******************************************************************/
/*                            wait_keysss                         */
/*   Function that Wait for user to press enter key.              */
/*                                                                */
/******************************************************************/

void wait_keysss()

  { /* begin wait_keysss function */
    str1 in_key ;
    char c ;

    print_empty_lines(5) ;
    in_key[0] = ' ' ;

    printf("Press return key to Continue\n") ;

    fflush(stdin);
    while((c = getchar()) != '\n')
      continue ;
    fflush(stdin);

  } /* end   wait_keysss function */


/******************************************************************/
/*                            wait_key                            */
/*   Function that Wait for user to press enter key.              */
/*                                                                */
/******************************************************************/

void wait_key()

  { /* begin wait_key function */
    char c ;

    print_empty_lines(5) ;
    printf("Press return key to Continue\n") ;

    while ((c = getc(stdin)) != '\n')
      continue ;

  } /* end   wait_key function */


/******************************************************************/
/*                    MaFgetWordNumberInString                    */
/*    Function that takes as argument a string and a number then  */
/*  returns the nth word in the string.                           */
/*                                                                */
/******************************************************************/

char *MaFgetWordNumberInString(MaLstring, MaLn)
  char *MaLstring ;
  int   MaLn      ;

    { /* begin MaFgetWordNumberInString function */
      MaBufferType MaLreturn ;
      int          MaLi, MaLj ;
      int          MaLindex = 0 ;

      if (MaLstring[0] == '\0')
        return("") ;

      if (MaLn > 1)
        {
          MaLi = 1 ;
          while ((MaLi < MaLn) && (MaLi < MaMaxBufSize)
                 && (MaLstring[MaLindex] != '\0'))
            {
              if (MaLstring[MaLindex++] == ' ')
               MaLi++ ;
            }
          if (MaLi < MaLn)
            return("") ;
        }

      MaLi = 0 ;
      while ((MaLi < MaMaxBufSize) && (MaLstring[MaLindex] != ' ')
             && (MaLstring[MaLindex] != '\0'))
        {
          MaLreturn[MaLi++] = MaLstring[MaLindex++] ;
        }
      MaLreturn[MaLi] = '\0' ;

      return(MaLreturn) ;

    } /* end   MaFgetWordNumberInString function */


/******************************************************************/
/*                      MaFgetPtrStringList                       */
/*    Function that generate a new pointer of string_list type,   */
/*  fills it with data, set the next pointer to nil and returns   */
/*  the pointer to the node to the calling program.               */
/*                                                                */
/******************************************************************/

MaStringListStruct MaFgetPtrStringList(MaLdata)
  char *MaLdata ;

    { /* begin MaFgetPtrStringList function */
      MaStringListStruct MaLpNodeList ;

      MaLpNodeList = (MaStringListStruct)
                            malloc (sizeof (struct MaGstringRec)) ;
      strcpy((MaLpNodeList-> MaLlabelName),"")          ;
      strcpy((MaLpNodeList-> MaLlabelType),"")          ;
      strcpy((MaLpNodeList-> MaLstringData),MaLdata)    ;
      MaLpNodeList-> MaLpNextString   = NULL            ;
      return(MaLpNodeList) ;

    } /* end   MaFgetPtrStringList function */


/******************************************************************/
/*                      MaFgetPtrClientData                       */
/*    Function that generate a new pointer of client_data type,   */
/*  initialize its data, set the next pointer to nil and returns  */
/*  the pointer to the node to the calling program.               */
/*                                                                */
/******************************************************************/

MaClientDataStruct MaFgetPtrClientData()

    { /* begin MaFgetPtrClientData function */
      MaClientDataStruct MaLpNodeList ;

      MaLpNodeList = (MaClientDataStruct)
                            malloc (sizeof (struct MaGclientDataRec)) ;
      MaLpNodeList-> MaLobjectIdsList      = NULL ;
      MaLpNodeList-> MaLstringList         = NULL ;
      MaLpNodeList-> MaLtextStringList     = NULL ;
      MaLpNodeList-> MaLchildrenStringList = NULL ;

      strcpy((MaLpNodeList-> MaLexportFilename),"")   ;
      strcpy((MaLpNodeList-> MaLlistLabel),     "")   ;
      strcpy((MaLpNodeList-> MaLclassName),     "")   ;
      strcpy((MaLpNodeList-> MaLparentName),    "")   ;
      strcpy((MaLpNodeList-> MaLchildName),     "")   ;
      strcpy((MaLpNodeList-> MaLnewParentName), "")   ;
      strcpy((MaLpNodeList-> MaLattributeName), "")   ;
      strcpy((MaLpNodeList-> MaLattributeType), "")   ;
      strcpy((MaLpNodeList-> MaLdialogMessage), "")   ;

      MaLpNodeList-> MaLselectionAction    = 0 ;
      MaLpNodeList-> MaLselectionMode      = 0 ;
      MaLpNodeList-> MaLeditableMode       = MaFALSE ;
      MaLpNodeList-> MaLdialogType         = XmDIALOG_MESSAGE ;
      MaLpNodeList-> MaLpreviousOid        = -1 ;
      MaLpNodeList-> MaLcurrentOid         = 0  ;
      MaLpNodeList-> MaLnextOid            = MaMaxOidNumber ;
      MaLpNodeList-> MaLfreeNodeOk         = MaTRUE  ;
      MaLpNodeList-> MaLCreateNewClassMode = MaFALSE ;
      MaLpNodeList-> MaLexportInheritMode  = MaTRUE  ;
      MaLpNodeList-> MaLparentChildConflict= MaFALSE ;

      MaLpNodeList-> MaLpNextClientData = NULL ;

      return(MaLpNodeList) ;

    } /* end   MaFgetPtrClientData function */


/******************************************************************/
/*                       MaFfreeStringList                        */
/*    Function to free all pointers to a list of MaStringList     */
/*  structure.                                                    */
/*                                                                */
/******************************************************************/

MaStringListStruct MaFfreeStringList (MaLpList) 
   MaStringListStruct MaLpList ;

    { /* begin MaFfreeStringList function */
      MaStringListStruct MaLtempPointer ;
      while (MaLpList != NULL)
        {
          MaLtempPointer = MaLpList ;
          MaLpList = MaLpList -> MaLpNextString ;
          free(MaLtempPointer) ;
        }

      return(NULL)  ;

    } /* end   MaFfreeStringList function */


/******************************************************************/
/*                       MaFfreeClientData                        */
/*    Function to free all pointers to a list of MaStringList     */
/*  structure.                                                    */
/*                                                                */
/******************************************************************/

MaClientDataStruct MaFfreeClientData (MaLpClientData) 
   MaClientDataStruct MaLpClientData ;

    { /* begin MaFfreeClientData function */
      MaClientDataStruct MaLtempPointer ;
      while (MaLpClientData != NULL)
        {
          MaLtempPointer = MaLpClientData ;
          MaLpClientData = MaLpClientData->MaLpNextClientData ;

          /***** Free Contents of object Ids list node *****/
          MaLtempPointer->MaLobjectIdsList =
                             MaFfreeStringList(MaLtempPointer->MaLobjectIdsList) ;

          /***** Free Contents of string list node *****/
          MaLtempPointer->MaLstringList    =
                             MaFfreeStringList(MaLtempPointer->MaLstringList) ;

          /***** Free Contents of text string list node *****/
          MaLtempPointer->MaLtextStringList    =
                             MaFfreeStringList(MaLtempPointer->MaLtextStringList) ;

          /***** Free Contents of Children string list node *****/
          MaLtempPointer->MaLchildrenStringList =
                             MaFfreeStringList(MaLtempPointer->MaLchildrenStringList) ;

          /***** Free Pointer to the node *****/
          free(MaLtempPointer) ;
        }
      return(NULL)  ;

    } /* end   MaFfreeClientData function */


/******************************************************************/
/*                     MaFgetSizeStringList                       */
/*    Function to get the number of nodes in a list.              */
/*                                                                */
/******************************************************************/

int MaFgetSizeStringList (MaLpList) 
   MaStringListStruct MaLpList ;

    { /* begin MaFgetSizeStringList function */

      if (MaLpList == NULL)
        return(0) ;
      return(1+ MaFgetSizeStringList(MaLpList->MaLpNextString)) ;

    } /* end   MaFgetSizeStringList function */


/******************************************************************/
/*                      MaFwordInStringList                       */
/*    Function to get a string a pointer to a list the returns    */
/*  True if string exist in any of the nodes of the list, else    */
/*  it returns False.                                             */
/*                                                                */
/******************************************************************/

int MaFwordInStringList (MaLword, MaLpList)
   char              *MaLword ;
   MaStringListStruct MaLpList ;

    { /* begin MaFwordInStringList function */

      if (MaLpList == NULL)
        return(MaFALSE) ;
      if (strcmp(MaLword, MaLpList -> MaLstringData) == 0)
        return(MaTRUE) ;
      return(MaFwordInStringList(MaLword, MaLpList -> MaLpNextString)) ;

    } /* end   MaFwordInStringList function */


/******************************************************************/
/*                     MaFpointerNodeToListAdd                    */
/*    Function that accepts a pointer to a node and a pointer to  */
/*  a list of type MaStringListStruct,  then add the node to the  */
/*  end of the list.                                              */
/******************************************************************/
 
MaStringListStruct MaFpointerNodeToListAdd(MaLnode,MaLlist)
  MaStringListStruct MaLnode ;
  MaStringListStruct MaLlist ;
 
    { /* begin MaFpointerNodeToListAdd function */
      if (MaLlist == NULL)
        return(MaLnode) ;
      else
        {
          MaLlist->MaLpNextString = MaFpointerNodeToListAdd
                                   (MaLnode, MaLlist->MaLpNextString) ;
          return(MaLlist) ;
        } 
 
    } /* end   MaFpointerNodeToListAdd function */
 

/******************************************************************/
/*                    MaFpointerStringToListAdd                   */
/*    Function that accepts a pointer to a node and a pointer to  */
/*  a list of type MaStringListStruct,  then add the node to the  */
/*  end of the list.                                              */
/******************************************************************/
 
MaStringListStruct MaFpointerStringToListAdd(MaLstring,MaLlist)
  char            *MaLstring ;
  MaStringListStruct MaLlist ;
 
    { /* begin MaFpointerStringToListAdd function */

      return(MaFpointerNodeToListAdd(MaFgetPtrStringList(MaLstring),
                                     MaLlist)
            ) ;
 
    } /* end   MaFpointerStringToListAdd function */


/******************************************************************/
/*                  MaFexcludeNodeList1FromList2                  */
/*    Function that accepts two pointers to a MaStringListStruct  */
/*  list type ,  then excludes all nodes from list2 that appears  */
/*  in list1.                                                     */
/******************************************************************/
 
MaStringListStruct MaFexcludeNodeList1FromList2(MaLlist1,MaLlist2)
  MaStringListStruct MaLlist1 ;
  MaStringListStruct MaLlist2 ;
 
    { /* begin MaFexcludeNodeList1FromList2 function */
      MaStringListStruct MaLtempNode ;
      MaStringListStruct MaLreturnList = NULL;

      while (MaLlist2 != NULL)
        {
          MaLtempNode = MaLlist2 ;
          MaLlist2 = MaLlist2 -> MaLpNextString ;
          MaLtempNode -> MaLpNextString = NULL ;

          if (MaFwordInStringList (MaLtempNode -> MaLstringData,
                                   MaLlist1))
            {
              MaLtempNode = MaFfreeStringList(MaLtempNode) ;
            }
          else
            {
              MaLreturnList = MaFpointerNodeToListAdd(MaLtempNode,
                                                      MaLreturnList) ;
            }

        }

      return(MaLreturnList) ;
 
    } /* end   MaFexcludeNodeList1FromList2 function */
 
 
/******************************************************************/
/*                                                                */
/*                       END OF function.c FILE                   */
/*                                                                */
/******************************************************************/ 

