Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion home_tasks/CString/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/Test/test_CString",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"cwd": "/usr/src/glibc/glibc-2.36",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
Expand Down
18 changes: 17 additions & 1 deletion home_tasks/CString/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@
"unity.h": "c",
"unity_fixture.h": "c",
"string": "c",
"big_strings.h": "c"
"big_strings.h": "c",
"string_t.h": "c",
"memory.h": "c",
"array": "c",
"deque": "c",
"unordered_map": "c",
"vector": "c",
"string_view": "c",
"initializer_list": "c",
"unity_memory.h": "c",
"time.h": "c",
"tear_down_messages.h": "c",
"strcat.h": "c",
"initiator_string.h": "c",
"stdbool.h": "c",
"memcheck.h": "c",
"system_error": "c"
},
"makefile.launchConfigurations": [
{
Expand Down
9 changes: 5 additions & 4 deletions home_tasks/CString/CString/big_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
char get_random_char() {
static const int ALPHABET_SIZE = 256;
srand(clock());
return 1 + rand() % (ALPHABET_SIZE - 1);
return 1 + (size_t)rand() % (ALPHABET_SIZE - 1);
}

// Return C-string size 'number_of_letter' if memory can be allocated else NULL
char *create_big_string(size_t number_of_letters) {
int length = 0;
char *big_string;
if ( (big_string = malloc(1 + number_of_letters)) == NULL) { // '+ 1' for null-terminator
if ( (big_string = malloc((1 + number_of_letters) * sizeof(char))) == NULL) { // '+ 1' for null-terminator
return NULL;
}
for (; length < number_of_letters; ++length) {
VALGRIND_MAKE_MEM_NOACCESS(big_string, str_len(big_string));
// big_string = memset(big_string, 0, number_of_letters); // for valgrind
for (int length = 0; length < number_of_letters; ++length) {
big_string[length] = get_random_char();
}
return big_string;
Expand Down
4 changes: 3 additions & 1 deletion home_tasks/CString/CString/big_strings.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef BIGSTRINGS
#define BIGSTRINGS

typedef struct string_s string_t;
typedef struct strings_array_s strings_array_t;
#include "string.h"
#include "initiator_string.h"

char get_random_char();
char *create_big_string(size_t number_of_letters);

#endif //BIGSTRINGS
#endif //BIGSTRINGS
119 changes: 47 additions & 72 deletions home_tasks/CString/CString/initiator_string.c
Original file line number Diff line number Diff line change
@@ -1,110 +1,85 @@
#include "initiator_string.h"
#include "string_t.h"

int strlen_cp(char *cstring) {
if (cstring == NULL) {
return -1;
}
int count = 0;
while (cstring[count] != '\0') {
++count;
}
return count;
}
#include <memory.h>

int strlen_ccp(const char *cstring) {
VALGRIND_MAKE_MEM_DEFINED(cstring, 1);
if (cstring == NULL) {
return -1;
}
int count = 0;
VALGRIND_MAKE_MEM_UNDEFINED(cstring[count], 1);
while (cstring[count] != '\0') {
++count;
VALGRIND_MAKE_MEM_DEFINED(cstring[count], 1);
}
return count;
}

int strlen_sp(string_t *string) {
if (string == NULL) {
return -1;
}
return string->length;
strlen_ccp(string->str);
}

/*---------------------------------------------------------*/

void init_string_spcp(string_t *to, char *from) {
to->str = malloc(sizeof(char)); // allocating few bytes because memcpy is used with allocated strings
memcpy(to, from);
}

void init_string_spccp(string_t *to, const char *from) {
to->str = malloc(sizeof(char)); // allocating few bytes because memcpy is used with allocated strings
memcpy(to, from);
to->allocated = 0;
str_cpy(to, from);
}

void init_string_spsp(string_t *to, string_t *from) {
to->str = malloc(sizeof(char)); // allocating few bytes because memcpy is used with allocated strings
memcpy(to, from);
to->allocated = 0;
str_cpy(to, from);
}

void init_string_sps(string_t *to, string_t from) {
to->str = malloc(sizeof(char)); // allocating few bytes because memcpy is used with allocated strings
memcpy(to, &from);
to->allocated = 0;
str_cpy(to, &from);
}

/*---------------------------------------------------------*/


/*
Is used for freeing 'string' and copy the 'cstring' content into it

Note: 'string' have to be allocated
*/
void memcpy_ccp(string_t *string, const char *cstring) {
int length = strlen(cstring);
free(string->str);
string->str = NULL;
if (length != -1) { // if 'second' is NULL
string->str = malloc(length + 1);
}
string->length = length;
for (int i = 0; i < length + 1; ++i) {
string->str[i] = cstring[i];
void strcpy_ccp(string_t *string, const char *cstring) {
int length = str_len(cstring);
if (length == -1) { // if 'second' is NULL
string->str = NULL;
string->length = -1;
} else {
if (string->allocated == 0) {
string->str = malloc(length + 1);
string->allocated = length + 1;
}
else if (length >= string->allocated) {
string->str = realloc(string->str, length + 1);
string->allocated = length + 1;
}
string->str = memcpy(string->str, cstring, length + 1);
string->length = length;
string->str[length] = '\0';
}
}

/*
Is used for freeing 'string' and copy the 'cstring' content into it
void strcpy_sp(string_t *first, string_t *second) {
strcpy_ccp(first, second->str);
}

/*---------------------------------------------------------*/

void strfree_s(string_t string) {
free(string.str);
}

Note: 'string' have to be allocated
*/
void memcpy_cp(string_t *string, char *cstring) {
int length = strlen(cstring);
void strfree_sp(string_t *string) {
free(string->str);
string->str = NULL;
if (length != -1) { // if 'second' is NULL
string->str = malloc(length + 1);
}
string->length = length;
for (int i = 0; i < length + 1; ++i) {
string->str[i] = cstring[i];
}
free(string);
}

/*
Is used for freeing 'first' and copy the 'second' content into it
/*---------------------------------------------------------*/

Note: 'first' have to be allocated
*/
void memcpy_sp(string_t *first, string_t *second) {
int length = second->length;
free(first->str);
first->str = NULL;
if (length != -1) { // if 'second' is NULL
first->str = malloc(length + 1);
}
first->length = length;
for (int i = 0; i < length + 1; ++i) {
first->str[i] = second->str[i];
}
void freestrarr_sap(strings_array_t *array) {
free(array->container);
free(array);
}

void freestrarr_sa(strings_array_t array) {
free(array.container);
}
44 changes: 27 additions & 17 deletions home_tasks/CString/CString/initiator_string.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef INIT_STR
#define INIT_STR

typedef struct string_s string_t;
typedef struct strings_array_s strings_array_t;
#include "string_t.h"
#include <stdlib.h>

Expand All @@ -9,40 +11,48 @@

Note: 'string' have to be non-allocated
*/
#define init_string(to, from) _Generic( (from), \
#define str_init(to, from) _Generic( (from), \
const char * : init_string_spccp, \
char * : init_string_spccp, \
string_t * : init_string_spsp, \
string_t : init_string_sps \
)(to, from)

#define strlen(string) _Generic( (string), \
const char * : strlen_ccp, \
char * : strlen_cp, \
string_t : strlen_sp \
#define str_len(string) _Generic( (string), \
const char * : strlen_ccp, \
char * : strlen_ccp, \
string_t * : strlen_sp \
)(string)

/*
Is used for freeing 'string' and copy the 'cstring' content into it

Note: 'string' have to be allocated
*/
#define memcpy(string, cstring) _Generic( (cstring), \
const char * : memcpy_ccp, \
char * : memcpy_cp, \
string_t * : memcpy_sp \
#define str_cpy(string, cstring) _Generic( (cstring),\
const char * : strcpy_ccp, \
char * : strcpy_ccp, \
string_t * : strcpy_sp \
)(string, cstring)

void init_string_spcp(string_t *to, char *from);
#define str_free(string) _Generic( (string), \
string_t * : strfree_s, \
string_t : strfree_s \
)(string)

#define free_str_arr(strings_array) _Generic( (strings_array), \
strings_array_t * : freestrarr_sap, \
strings_array_t : freestrarr_sa \
)(strings_array)

void init_string_spccp(string_t *to, const char *from);
void init_string_spsp(string_t *to, string_t *from);
void init_string_sps(string_t *to, string_t from);
int strlen_cp(char *string);
int strlen_ccp(const char *string);
int strlen_sp(string_t *string);
int strlen_sp(string_t *string);
void memcpy_ccp(string_t *string, const char *cstring);
void memcpy_cp(string_t *string, char *cstring);
void memcpy_sp(string_t *string, string_t *cstring);
void strcpy_ccp(string_t *string, const char *cstring);
void strcpy_sp(string_t *string, string_t *cstring);
void strfree_sp(string_t *string);
void strfree_s(string_t string);
void freestrarr_sap(strings_array_t *array);
void freestrarr_sa(strings_array_t array);

#endif //INIT_STR
26 changes: 26 additions & 0 deletions home_tasks/CString/CString/strcat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "strcat.h"
#include <stdlib.h>
#include <memory.h>

string_t strcat_ccp(string_t *to, const char *from) {
int start = str_len(to);
int length = str_len(from);
if (to->str != NULL && from != NULL) {
to->str = memcpy(to->str + start, from, length) - start;
to->length = length += start;
to->str[length] = '\0';
to->allocated = to->allocated < length ? length : to->allocated;
} else {
to->str = NULL;
to->length = -1;
}
return *to;
}

string_t strcat_sp(string_t *to, string_t *from) {
return strcat_ccp(to, from->str);
}

string_t strcat_s(string_t *to, string_t from) {
return strcat_ccp(to, from.str);
}
19 changes: 19 additions & 0 deletions home_tasks/CString/CString/strcat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef STRCAT
#define STRCAT

typedef struct string_s string_t;
typedef struct strings_array_s strings_array_t;
#include "string_t.h"

#define str_cat(to, from) _Generic( (from),\
char * : strcat_ccp, \
const char * : strcat_ccp, \
string_t * : strcat_sp, \
string_t : strcat_s \
)(to, from)

string_t strcat_ccp(string_t *to, const char *from);
string_t strcat_sp(string_t *to, string_t *from);
string_t strcat_s(string_t *to, string_t from);

#endif //STRCAT
23 changes: 22 additions & 1 deletion home_tasks/CString/CString/strcmp.c
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
#include "strcmp.h"
#include "strcmp.h"

bool strcmp_spcp(string_t *first, char * second) {
char *safety_first_ptr = first->str;
for (int i = 0; *safety_first_ptr != '\0' && *second != '\0'; ++safety_first_ptr, ++second) {
if (*safety_first_ptr != *second) {
return false;
}
}
if (*safety_first_ptr != *second) {
return false;
}
return true;
}

bool strcmp_spsp(string_t *first, string_t *second) {
return strcmp_spcp(first, second->str);
}

bool strcmp_sps(string_t *first, string_t second) {
return strcmp_spcp(first, second.str);
}
Loading