#include "IdanStringPointersLib.h"
#include <stdio.h>

#define ROW 2
#define COL 2


BOOLEAN StringIsSubstringInVec(char * ptr_vec, char * ptr_string) // Lenght vector = COL
{
    char *strings_amount = ptr_vec + (COL * MAX_SIZE_STRING);
    for (;ptr_vec < strings_amount && !(ptr_vec != ptr_string) * StringBInStringA(ptr_vec, ptr_string); ptr_vec += MAX_SIZE_STRING);

    return ((ptr_vec != ptr_string) * StringBInStringA(ptr_vec, ptr_string));
}

BOOLEAN StringIsSubstringInMat(char * ptr_mat, char * ptr_string) // Lenght = ROW
{
    unsigned int step = COL * MAX_SIZE_STRING;
    char *vectors_amount = ptr_mat + (ROW * step);
    for (; ptr_mat < vectors_amount && !((ptr_mat != ptr_string) * StringIsSubstringInVec(ptr_mat, ptr_string)); ptr_mat += step);

    return ((ptr_mat != ptr_string) * StringIsSubstringInVec(ptr_mat, ptr_string));
}

BOOLEAN HaveSubstringInVec(char * ptr_mat, char * ptr_vec) // בודק בווקטור אם אחד האיברים הוא תת מחרוזת במטריצה
{
    char *strings_amount = ptr_vec + (COL * MAX_SIZE_STRING);
    for (; ptr_vec < strings_amount && !(StringIsSubstringInMat(ptr_mat, ptr_vec)); ptr_vec += MAX_SIZE_STRING);

    return (StringIsSubstringInMat(ptr_mat, ptr_vec));
}

BOOLEAN HaveSubstringInMat(char * ptr_mat) // בודק אם קיימת תת מחרוזת במטריצה למחרוזת במטריצה
{
    unsigned int step = COL * MAX_SIZE_STRING;
    char *vectors_amount = ptr_mat + (ROW * step);
    char *ptr_temp_mat = ptr_mat;
    for (; ptr_mat < vectors_amount && !(HaveSubstringInVec(ptr_temp_mat, ptr_mat)); ptr_mat += step);

    return (HaveSubstringInVec(ptr_temp_mat, ptr_mat));
}

//---------------------------------------------------------------------------------------
//                                          EX #3
//                                          -----    
//
//  General : The program gets a matrix of string and checks if one of the string is a 
//			  substring to another string
//
//  Input : matrix of string
//  
//  Process : The program runs on a string and if he is a substring of one of the other 
//			  string till it is out of string to check
//
//  Output : True or False
//
//---------------------------------------------------------------------------------------
//  Programmer : Cohen Idan
//  Student No : 211675038
//  Date: 19.11.2019
//---------------------------------------------------------------------------------------
void main(void)
{
    typedef char string[MAX_SIZE_STRING];
    string mat[ROW][COL] = { {"hello", "world"} , { "idan", "hel"}};

    printf("%hu\n", HaveSubstringInMat(mat));
}