Skip to content

Latest commit

 

History

History
720 lines (467 loc) · 15.2 KB

README.MD

File metadata and controls

720 lines (467 loc) · 15.2 KB

LIBMX Documentation

The main idea staying behind this challenge is to create own static library of most common functions uning only some low-level functions, such as: malloc, malloc_size/malloc_usable_size, free, open,read, write, close, exit.

Library is compiled with clang -std=c11 -Wall -Wextra -Werror -Wpedantic.

To compile library simply use make or make libmx.a in your shell. There are also some test cases included, which can be compiled by make test and they executed by ./test.

Feel free to use it if you need, ask questions and so on. As this is mostly educational project, feedback is highly appreciated :)

Enjoy!

Table of contents


Utils pack

Print character

Outputs a single character to the standard output.

void mx_printchar(char c);

Print string

Outputs a string of characters to the standard output.

void mx_printstr(const char *s);

Print array of strings

Outputs:

  • an array of strings arr to the standard output with a delimiter delim between the elements of an array;
  • nothing if arr or delim do not exist;
  • a newline at the end of the output.

arr must be NULL-terminated, in other cases the behavior is undefined.

void mx_print_strarr(char **arr, const char *delim);

Print integer

Outputs integer values to the standard output.

void mx_printint(int n);

Decimal to hex

Converts an unsigned long number into a hexadecimal string.

Returns the number converted to a hexadecimal string.

char *mx_nbr_to_hex(unsigned long nbr);

Bubble sort

Sorts an array of integers in place in ascending order using the bubble sort algorithm.

Returns the number of swap operations.

int mx_bubble_sort(int *arr, int size);

Quick sort

Sorts an array of integers in ascending order using the quick sort algorithm.

Returns:

  • the number of swaps;
  • -1 if arr does not exist.
int mx_quicksort(int *arr, int left, int right);

Integer to ASCII

Takes an integer and converts it to a string.

Returns the number as a NULL-terminated string.

char *mx_itoa(int number);

Print multibyte characters

Outputs ASCII and multibyte characters to the standard output.

void mx_print_unicode(wchar_t c);

Exponentiation

Computes n raised to the power of zero or a positive integer pow.

Returns the result of n to the power of pow.

double mx_pow(double n, unsigned int pow);

Square root

Computes the non-negative square root of x.

Returns the square root of the numberx if it is natural, and 0 otherwise.

int mx_sqrt(int x);

Hex to decimal

Converts a hexadecimal string into an unsigned long number.

Returns the unsigned long number.

unsigned long mx_hex_to_nbr(const char *hex);

For each

Applies the function f for each element of the array arr given size.

void mx_foreach(int *arr, int size, void(*f)(int));

Binary search

Searches the strings in the array arr with the given size of array using the binary search algorithm.

Returns:

  • the index of the found string in the array
  • -1 in case of errors or if the string has not been found
  • assigns the number of required iterations to the count pointer.
int mx_binary_search(char **arr, int size, const char *s, int *count);

Strings pack

String length

Has the same behavior as the corresponding standard libc function strlen.

int mx_strlen(const char *s);

Swap characters

Swaps the characters of a string using pointers. Do nothing if s1 or s2 does not exist.

void mx_swap_char(char *s1, char *s2);

Copy string

Has the same behavior as the standard libc function strcpy.

char *mx_strcpy(char *dst, const char *src);

Compare strings

Has the same behavior as the standard libc function strcmp.

int mx_strcmp(const char *s1, const char *s2);

Concatenate strings

Has the same behavior as the standard libc function strcat.

char *mx_strcat(char *restrict s1, const char *restrict s2);

New string

Allocates memory for a string of a specific size and one additional byte for the terminating '\0'. Initializes each character with '\0'.

Returns the string of a specific size and terminated by '\0' or NULL if creation fails.

char *mx_strnew(const int size);

Duplicate string

Has the same behavior as the standard libc function strdup.

char *mx_strdup(const char *s1);

Join strings

Concatenates strings s1 and s2 into a new string. Terminates the new string with '\0'.

Returns:

  • the string as a result of concatenation s1 and s2;
  • the new copy of non-NULL parameter if one and only one of the parameters is NULL;
  • NULL if the concatenation fails.
char *mx_strjoin(const char *s1, const char *s2);

Delete string

Takes a pointer to a string, frees string memory with free and sets the string to NULL.

void mx_strdel(char **str);

Delete array of strings

Takes a pointer to a NULL-terminated array of strings, deletes the contents of the array, frees array memory with free and sets a pointer to NULL.

void mx_del_strarr(char ***arr);

File to string

Takes a filename as a parameter and reads data from the file into a string.

Returns:

  • NULL-terminated string;
  • NULL in case of any errors.
char *mx_file_to_str(const char *file);

Readline

Reads the line from the given fd into the lineptr until it:

  • reaches a delim character. The delimiter must not be returned with lineptr;
  • reaches the End Of File (EOF);

Returns:

  • the number of bytes that are written into lineptr;
  • -1 if EOF is reached and there is nothing to write in lineptr;
  • -2 in case of errors or fd is invalid.
int mx_read_line(char **lineptr, size_t buf_size, char delim, const int fd);

Copy them all

Has the same behavior as the standard libc function strncpy.

char *mx_strncpy(char *dst, const char *src, int len);

Reverse string

Reverses a string using pointers. Do nothing if a string does not exist.

void mx_str_reverse(char *s);

Duplicate part of string

Has the same behavior as the standard libc function strndup.

char *mx_strndup(const char *s1, size_t n);

Locate substring

Has the same behavior as the standard libc function strstr.

char *mx_strstr(const char *haystack, const char *needle);

Count words

Counts words in a string.

Returns the number of words in the string.

int mx_count_words(const char *str, char c);

Count substrings

Counts the substrings sub in the string str.

Returns:

  • the count of sub in str;
  • 0 if sub is an empty string;
  • -1 if str and/or sub do not exist.
int mx_count_substr(const char *str, const char *sub);

Get character index

Finds the index of the first occurrence of the character c in a string str.

Returns:

  • the index of the first occurrence;
  • -1 if no occurrence is found;
  • -2 if the string does not exist.
int mx_get_char_index(const char *str, char c);

Get substring index

Finds the index of a substring.

Returns:

  • the index of the first character of sub in str;
  • -1 if sub is not found in str;
  • -2 if str or sub does not exist.
int mx_get_substr_index(const char *str, const char *sub);

Trim strings

Takes a string, and creates a new one from it without whitespace characters at the beginning and the end of the string.

Returns:

  • a new trimmed string;
  • NULL if the string str does not exist or string trim fails.
char *mx_strtrim(const char *str);

Clean string

Takes a string, and creates a new one from it without whitespace characters in thebeginning and/or at the end of the string. Separates words in the new string with exactly one space character.

Returns:

  • a new created string;
  • NULL if the string str does not exist or string creation fails.
char *mx_del_extra_spaces(const char *str);

Split string

Converts a strings to a NULL-terminated array of words.

Returns:

  • the NULL-terminated array of strings;
  • NULL if the strings does not exist or conversion fails.
char **mx_strsplit(const char *s, char c);

Replace substrings

Replaces all occurrences of sub in str with replace.

Returns:

  • a new string where substrings are replaced;
  • NULL if sub or str or replace does not exist.
char *mx_replace_substr(const char *str, const char *sub, const char *replace);

Memory pack

Fill memory

Has the same behavior as the standard libc function memset.

void *mx_memset(void *b, int c, size_t len);

Copy memory

Has the same behavior as the standard libc function memcpy.

void *mx_memcpy(void *restrict dst, const void *restrict src, size_t n);

Compare memory

Has the same behavior as the standard stdlib function memcmp.

int mx_memcmp(const void *s1, const void *s2, size_t n);

Reallocate memory

Has the same behavior as the standard stdlib function realloc.

void *mx_realloc(void *ptr, size_t size);

Non-overlapping memory copy

Has the same behavior as the standard libc function memmove.

void *mx_memmove(void *dst, const void *src, size_t len);

Locate byte from end

Similar to the function mx_memchr, except that it searches in the opposite direction from the end of the bytes n points to s instead of directly from the beginning.

void *mx_memrchr(const void *s, int c, size_t n);

Copy memory to ...

Has the same behavior as the standard stdlib function memccpy.

void *mx_memccpy(void *restrict dst, const void* restrict src, int c, size_t n);

Locate byte from start

Has the same behavior as the standard stdlib function memchr.

void *mx_memchr(const void *s, int c, size_t n);

Locate block of bytes

Has the same behavior as the standard libc function memmem.

void *mx_memmem(const void *big, size_t big_len, const void *little, size_t little_len);

List pack

Some functions to work with aingly linked list which contains void pointer.

typedef struct s_list {
  void *data;
  struct s_list *next;
}              t_list;

Create node

Creates a new node of a linked list t_list. The function assigns a parameter data to the list variable data and assigns next to NULL.

t_list *mx_create_node(void *data);

Push front

Inserts a new node of t_list type with the given parameter data at the beginning of the linked list.

void mx_push_front(t_list **list, void *data);

Push back

Inserts a node of t_list type with the given parameter data at the end of the linked list.

void mx_push_back(t_list **list, void *data);

Pop front

Removes the first node of the linked list and frees the memory allocated for the node.

void mx_pop_front(t_list **head);

Pop back

Removes the last node of the linked list and frees the memory allocated for the node.

void mx_pop_back(t_list **head);

Size of list

Calculates the number of nodes in a linked list.

Returns the amount of nodes in the linked list.

int mx_list_size(t_list *list);

Sort list

Sorts a list's contents in ascending order. The function cmp returns true if a > b and false in other cases.

Returns a pointer to the first element of the sorted list.

t_list *mx_sort_list(t_list *lst, bool(*cmp)(void*, void*));

Extra pack

Allocated memory size

Platform-indepent function which return size of previously allocated memory.

Special credits to https://stackoverflow.com/users/1424877/quuxplusone

size_t mx_malloc_size(void *p);

Is digit?

Has the same behaviour as the standard libc function isdigit.

bool mx_isdigit(int c);

Is white-space?

Has the same behaviour as the standard libc function isspace.

bool mx_isspace(int c);

To lower case

Has the same behaviour as the standard libc function tolower.

int mx_tolower(int c);

To upper case

Has the same behaviour as the standard libc function toupper.

int mx_toupper(int c);

Is alphabetic?

Has the same behaviour as the standard libc function isalpha.

bool mx_isalpha(int c);

Locate character

Create a function that has the same behaviour as the standard libc function strchr.

char *mx_strchr(const char *s, int c);

ASCII to integer

Converts an ASCII string to an integer as the standard libc function atoi does.

int mx_atoi(const char *str);

Compare strings N

Has the same behaviour as the standard libc function strncmp.

int mx_strncmp(const char *s1, const char *s2, int n);