Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "my_string.h"
- #include "generic.h"
- typedef struct My_string
- {
- char *data;
- int size;
- int capacity;
- }my_string;
- MY_STRING my_string_init_default(void)
- {
- my_string* ptr=(my_string*) malloc(sizeof(my_string));
- if( ptr == NULL)
- return NULL;
- ptr->capacity=7;
- ptr->size=0;
- ptr->data = (char*)malloc(sizeof(char) *ptr->capacity);
- if(!ptr->data)
- {
- free(ptr);
- ptr = NULL;
- }
- return(MY_STRING) ptr;
- }
- void my_string_destroy(MY_STRING* phMy_string)
- {
- my_string* ptr=(my_string*) (*phMy_string);
- free(ptr->data);
- free(ptr);
- *phMy_string = NULL;
- }
- MY_STRING my_string_init_c_string(const char* c_string)
- {
- my_string* ptr = (my_string*) malloc(sizeof(my_string));
- if(ptr == NULL)
- return NULL;
- int i;
- for (i=0; c_string[i] != '\0'; i++)
- ptr->size = i + 1;
- ptr->capacity = ptr->size + 1;
- ptr->data = malloc(sizeof(char) * ptr->capacity);
- if(ptr->data == NULL)
- ptr->capacity = ptr->size = 0;
- strcpy(ptr->data, c_string);
- return ptr;
- }
- int my_string_get_capacity(MY_STRING hMy_string)
- {
- return ((my_string*) hMy_string)->capacity;
- }
- int my_string_get_size(MY_STRING hMy_string)
- {
- return ((my_string*) hMy_string)->size;
- }
- int my_string_compare(MY_STRING hLeft_string, MY_STRING hRight_string)
- {
- int i;
- my_string *pLeft_string = (my_string*) hLeft_string;
- my_string *pRight_string = (my_string*) hRight_string;
- for(i=0; pLeft_string->data[i] == pRight_string->data[i] && i+1 < pLeft_string->size && i+1 < pRight_string->size; i++){}
- if (pLeft_string->data[i] > pRight_string->data[i])
- {
- return 1;
- }
- if (pLeft_string->data[i] < pRight_string->data[i])
- {
- return -1;
- }
- if (pLeft_string->size > pRight_string->size)
- {
- return 1;
- }
- if (pLeft_string->size < pRight_string->size)
- {
- return -1;
- }
- return 0;
- }
- Status my_string_extraction(MY_STRING hMy_string, FILE* fp)
- {
- my_string *pMy_string = NULL;
- int len = 0;
- char letter;
- char *temp_data;
- pMy_string = (my_string*) hMy_string;
- if(!pMy_string)
- {
- return FAILURE;
- }
- while(1)
- {
- letter = fgetc(fp);
- if(((letter == '\n' || letter == ' ') && len > 0) || letter == EOF)
- {
- fseek(fp, -1, SEEK_CUR);
- break;
- }
- if(letter != '\n' && letter != ' ' && len != EOF)
- {
- len++;
- }
- if(pMy_string->capacity <= len)
- {
- temp_data = realloc(pMy_string->data, len +1);
- if(!temp_data)
- {
- return FAILURE;
- }
- pMy_string->data = temp_data;
- pMy_string->capacity = len +1;
- }
- if(len)
- {
- pMy_string->data[len - 1] = letter;
- }
- }
- if(len == 0)
- {
- return FAILURE;
- }
- pMy_string->size = len;
- return SUCCESS;
- }
- Status my_string_insertion(MY_STRING hMy_string, FILE* fp)
- {
- my_string *pMy_string = NULL;
- int i;
- pMy_string = (my_string*) hMy_string;
- if(pMy_string == NULL || fp == NULL)
- {
- return FAILURE;
- }
- for(i = 0; i < pMy_string->size; i++)
- {
- fputc(pMy_string->data[i], fp);
- }
- return SUCCESS;
- }
- Status my_string_push_back(MY_STRING hMy_string, char item)
- {
- my_string *pMy_string;
- char *temp_data;
- pMy_string = (my_string*) hMy_string;
- if(pMy_string->size + 1 >= pMy_string->capacity)
- {
- temp_data = (char*) realloc(pMy_string->data, pMy_string->capacity * 2);
- if(!temp_data)
- {
- return FAILURE;
- }
- pMy_string->capacity *= 2;
- pMy_string->data = temp_data;
- }
- pMy_string->data[pMy_string->size] = item;
- pMy_string->size++;
- return SUCCESS;
- }
- Status my_string_pop_back(MY_STRING hMy_string)
- {
- my_string *pMy_string;
- pMy_string = (my_string*) hMy_string;
- if(pMy_string->size < 1)
- {
- return FAILURE;
- }
- pMy_string->size--;
- return SUCCESS;
- }
- char* my_string_at(MY_STRING hMy_string, int index)
- {
- my_string *pMy_string;
- pMy_string = (my_string*) hMy_string;
- if(index < 0 || index >= pMy_string->size)
- {
- return NULL;
- }
- return &pMy_string->data[index];
- }
- char* my_string_c_str(MY_STRING hMy_string)
- {
- my_string* pMy_string;
- char *temp;
- pMy_string = (my_string*) hMy_string;
- if(pMy_string->size >= pMy_string->capacity)
- {
- temp = (char*) realloc(pMy_string->data,
- sizeof(char)*pMy_string->capacity);
- if(temp)
- {
- pMy_string->capacity++;
- pMy_string->data = temp;
- }
- }
- pMy_string->data[pMy_string->size] = '\0';
- return pMy_string->data;
- }
- Status my_string_concat(MY_STRING hResult, MY_STRING hAppend)
- {
- int i;
- int new_size;
- int append_size;
- char *pNewdata;
- my_string *pResult = (my_string*) hResult;
- if(hResult == NULL || hAppend == NULL)
- {
- return FAILURE;
- }
- append_size = my_string_get_size(hAppend);
- new_size = pResult->size + append_size;
- if(new_size >= pResult->capacity)
- {
- pNewdata = realloc(pResult->data, sizeof(char) * (new_size + 1));
- if(pNewdata == NULL)
- {
- return FAILURE;
- }
- pResult->data = pNewdata;
- pResult->capacity = new_size + 1;
- }
- for(i = 0; i < append_size; i++)
- {
- my_string_push_back(hResult, *my_string_at(hAppend, i));
- }
- return SUCCESS;
- }
- Boolean my_string_empty(MY_STRING hMy_string)
- {
- my_string* pMy_string;
- pMy_string = (my_string*) hMy_string;
- if(pMy_string->size == 0)
- {
- return TRUE;
- }
- return FALSE;
- }
- void my_string_assignment(Item* pLeft, Item Right)
- {
- my_string* pMy_string = (my_string*)*pLeft;
- my_string* rMy_string = (my_string*)Right;
- if(*pLeft == NULL){
- *pLeft = my_string_init_default();
- }
- else{
- while(!my_string_empty(*pLeft))
- my_string_pop_back(*pLeft);
- }
- my_string_concat(*pLeft, Right);
- //if(pMy_string->data == NULL)
- /* {
- *pLeft = my_string_init_c_string(rMy_string->data);
- }
- else
- {
- pMy_string->size = rMy_string->size;
- if(pMy_string->size < pMy_string->capacity)
- {
- for (int e = 0; e < pMy_string->size; e++)
- {
- pMy_string->data[e] = rMy_string->data[e];
- }
- }
- else
- {
- pMy_string->capacity = pMy_string->size*2;
- free(pMy_string->data);
- pMy_string->data = malloc(sizeof(char)*pMy_string->capacity);
- for(int e = 0; e < pMy_string->capacity-1; e++)
- {
- pMy_string->data[e] = rMy_string->data[e];
- }
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement