Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "my_string.h"
  4. #include "generic.h"
  5.  
  6. typedef struct My_string
  7. {
  8.   char *data;
  9.   int size;
  10.   int capacity;
  11.  
  12. }my_string;
  13.  
  14. MY_STRING my_string_init_default(void)
  15. {
  16.   my_string* ptr=(my_string*) malloc(sizeof(my_string));
  17.   if( ptr == NULL)
  18.     return NULL;
  19.  
  20.   ptr->capacity=7;
  21.   ptr->size=0;
  22.   ptr->data = (char*)malloc(sizeof(char) *ptr->capacity);
  23.   if(!ptr->data)
  24.     {
  25.       free(ptr);
  26.       ptr = NULL;
  27.     }
  28.   return(MY_STRING) ptr;
  29. }
  30.  
  31. void my_string_destroy(MY_STRING* phMy_string)
  32. {
  33.  my_string* ptr=(my_string*) (*phMy_string);
  34.  free(ptr->data);    
  35.  free(ptr);
  36.  *phMy_string = NULL;    
  37. }
  38.  
  39. MY_STRING my_string_init_c_string(const char* c_string)
  40. {
  41.  
  42.   my_string* ptr = (my_string*) malloc(sizeof(my_string));
  43.   if(ptr == NULL)
  44.     return NULL;
  45.   int i;
  46.   for (i=0; c_string[i] != '\0'; i++)
  47.   ptr->size = i + 1;
  48.   ptr->capacity = ptr->size + 1;
  49.   ptr->data = malloc(sizeof(char) * ptr->capacity);
  50.   if(ptr->data == NULL)
  51.     ptr->capacity = ptr->size = 0;
  52.  
  53.   strcpy(ptr->data, c_string);
  54.   return ptr;
  55.  
  56. }
  57.  
  58. int my_string_get_capacity(MY_STRING hMy_string)
  59. {
  60.   return ((my_string*) hMy_string)->capacity;
  61. }
  62.  
  63.  
  64. int my_string_get_size(MY_STRING hMy_string)
  65. {
  66.   return ((my_string*) hMy_string)->size;
  67. }
  68.  
  69. int my_string_compare(MY_STRING hLeft_string, MY_STRING hRight_string)
  70. {
  71.   int i;
  72.   my_string *pLeft_string = (my_string*) hLeft_string;
  73.   my_string *pRight_string = (my_string*) hRight_string;
  74.  
  75.   for(i=0; pLeft_string->data[i] == pRight_string->data[i] && i+1 < pLeft_string->size && i+1 < pRight_string->size; i++){}
  76.   if (pLeft_string->data[i] > pRight_string->data[i])
  77.     {
  78.       return 1;
  79.     }
  80.    
  81.   if (pLeft_string->data[i] < pRight_string->data[i])
  82.     {
  83.       return -1;
  84.     }
  85.   if (pLeft_string->size > pRight_string->size)
  86.     {
  87.       return 1;
  88.     }
  89.   if (pLeft_string->size < pRight_string->size)
  90.     {
  91.       return -1;
  92.     }
  93.   return 0;
  94. }
  95.  
  96. Status my_string_extraction(MY_STRING hMy_string, FILE* fp)
  97. {
  98.   my_string *pMy_string = NULL;
  99.  
  100.   int len = 0;
  101.   char letter;
  102.   char *temp_data;
  103.  
  104.   pMy_string = (my_string*) hMy_string;
  105.   if(!pMy_string)
  106.     {
  107.       return FAILURE;
  108.     }
  109.   while(1)
  110.     {
  111.       letter = fgetc(fp);
  112.       if(((letter == '\n' || letter == ' ') && len > 0) || letter == EOF)
  113.     {
  114.       fseek(fp, -1, SEEK_CUR);
  115.       break;
  116.     }
  117.       if(letter != '\n' && letter != ' ' && len != EOF)
  118.     {
  119.       len++;
  120.     }
  121.  
  122.       if(pMy_string->capacity <= len)
  123.     {
  124.       temp_data = realloc(pMy_string->data, len +1);
  125.       if(!temp_data)
  126.         {
  127.           return FAILURE;
  128.         }
  129.       pMy_string->data = temp_data;
  130.       pMy_string->capacity = len +1;
  131.     }
  132.       if(len)
  133.     {
  134.       pMy_string->data[len - 1] = letter;
  135.     }
  136.     }
  137.   if(len == 0)
  138.     {
  139.       return FAILURE;
  140.     }
  141.   pMy_string->size = len;
  142.  
  143.   return SUCCESS;
  144. }
  145. Status my_string_insertion(MY_STRING hMy_string, FILE* fp)
  146. {
  147.  
  148.   my_string *pMy_string = NULL;
  149.   int i;
  150.  
  151.   pMy_string = (my_string*) hMy_string;
  152.  
  153.   if(pMy_string == NULL || fp == NULL)
  154.     {
  155.       return FAILURE;
  156.     }
  157.  
  158.   for(i = 0; i < pMy_string->size; i++)
  159.     {
  160.       fputc(pMy_string->data[i], fp);
  161.     }
  162.  
  163.   return SUCCESS;
  164. }
  165.  
  166. Status my_string_push_back(MY_STRING hMy_string, char item)
  167. {
  168.   my_string *pMy_string;
  169.   char *temp_data;
  170.  
  171.   pMy_string = (my_string*) hMy_string;
  172.  
  173.   if(pMy_string->size + 1 >= pMy_string->capacity)
  174.     {
  175.       temp_data = (char*) realloc(pMy_string->data, pMy_string->capacity * 2);
  176.       if(!temp_data)
  177.     {
  178.       return FAILURE;
  179.     }
  180.       pMy_string->capacity *= 2;
  181.       pMy_string->data = temp_data;
  182.     }
  183.  
  184.     pMy_string->data[pMy_string->size] = item;
  185.     pMy_string->size++;
  186.     return SUCCESS;
  187. }
  188.  
  189. Status my_string_pop_back(MY_STRING hMy_string)
  190. {
  191.   my_string *pMy_string;
  192.  
  193.   pMy_string = (my_string*) hMy_string;
  194.  
  195.   if(pMy_string->size < 1)
  196.     {
  197.       return FAILURE;
  198.     }
  199.  
  200.   pMy_string->size--;
  201.   return SUCCESS;
  202. }
  203.  
  204. char*  my_string_at(MY_STRING hMy_string, int index)
  205. {
  206.   my_string *pMy_string;
  207.  
  208.   pMy_string = (my_string*) hMy_string;
  209.  
  210.   if(index < 0 || index >= pMy_string->size)
  211.     {
  212.       return NULL;
  213.     }
  214.  
  215.   return &pMy_string->data[index];
  216. }
  217.  
  218. char* my_string_c_str(MY_STRING hMy_string)
  219. {
  220.   my_string* pMy_string;
  221.   char *temp;
  222.  
  223.   pMy_string = (my_string*) hMy_string;
  224.  
  225.   if(pMy_string->size >= pMy_string->capacity)
  226.     {
  227.       temp = (char*) realloc(pMy_string->data,
  228.                  sizeof(char)*pMy_string->capacity);
  229.     if(temp)
  230.       {
  231.         pMy_string->capacity++;
  232.         pMy_string->data = temp;
  233.       }
  234.     }
  235.  
  236.   pMy_string->data[pMy_string->size] = '\0';
  237.   return pMy_string->data;
  238. }
  239.  
  240. Status my_string_concat(MY_STRING hResult, MY_STRING hAppend)
  241. {
  242.   int i;
  243.   int new_size;
  244.   int append_size;
  245.   char *pNewdata;
  246.   my_string *pResult = (my_string*) hResult;
  247.  
  248.   if(hResult == NULL || hAppend == NULL)
  249.   {
  250.     return FAILURE;
  251.   }
  252.  
  253.   append_size = my_string_get_size(hAppend);
  254.   new_size = pResult->size + append_size;
  255.   if(new_size >= pResult->capacity)
  256.     {
  257.       pNewdata = realloc(pResult->data, sizeof(char) * (new_size + 1));
  258.       if(pNewdata == NULL)
  259.     {
  260.       return FAILURE;
  261.     }
  262.       pResult->data = pNewdata;
  263.       pResult->capacity = new_size + 1;
  264.     }
  265.  
  266.   for(i = 0; i < append_size; i++)
  267.     {
  268.       my_string_push_back(hResult, *my_string_at(hAppend, i));
  269.     }
  270.   return SUCCESS;
  271. }
  272.    
  273. Boolean my_string_empty(MY_STRING hMy_string)
  274. {
  275.   my_string* pMy_string;
  276.  
  277.   pMy_string = (my_string*) hMy_string;
  278.  
  279.   if(pMy_string->size == 0)
  280.     {
  281.       return TRUE;
  282.     }
  283.   return FALSE;
  284. }
  285. void my_string_assignment(Item* pLeft, Item Right)
  286. {
  287.    my_string* pMy_string = (my_string*)*pLeft;
  288.    my_string* rMy_string = (my_string*)Right;
  289.     if(*pLeft == NULL){
  290.       *pLeft = my_string_init_default();
  291.     }
  292.     else{
  293.       while(!my_string_empty(*pLeft))
  294.     my_string_pop_back(*pLeft);
  295.     }
  296.    
  297.     my_string_concat(*pLeft, Right);
  298.       //if(pMy_string->data == NULL)
  299.     /*  {
  300.         *pLeft = my_string_init_c_string(rMy_string->data);
  301.     }
  302.         else
  303.     {
  304.         pMy_string->size = rMy_string->size;
  305.         if(pMy_string->size < pMy_string->capacity)
  306.         {
  307.             for (int e = 0; e < pMy_string->size; e++)
  308.             {
  309.                 pMy_string->data[e] = rMy_string->data[e];
  310.             }
  311.         }
  312.             else
  313.         {
  314.             pMy_string->capacity = pMy_string->size*2;
  315.             free(pMy_string->data);
  316.             pMy_string->data = malloc(sizeof(char)*pMy_string->capacity);
  317.             for(int e = 0; e < pMy_string->capacity-1; e++)
  318.             {
  319.                 pMy_string->data[e] = rMy_string->data[e];
  320.             }
  321.         }
  322.     }*/
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement