Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "s21_string.h"
- #include <float.h>
- #include <math.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <wchar.h>
- s21_size_t s21_strlen(const char *str) {
- return (*str) ? s21_strlen(++str) + 1 : 0;
- }
- void *s21_memchr(const void *str, int c, s21_size_t n) {
- char *ch = s21_NULL;
- for (s21_size_t i = 0; i < n; i++) {
- if (*((char *)str + i) == c) {
- ch = (char *)str + i;
- break;
- }
- }
- return ch;
- }
- int s21_memcmp(const void *str1, const void *str2, s21_size_t n) {
- int ch = 0;
- for (s21_size_t i = 0; i < n; i++) {
- if (*((char *)str1 + i) != *((char *)str2 + i)) {
- ch = *((char *)str1 + i) - *((char *)str2 + i);
- break;
- }
- }
- return ch;
- }
- void *s21_memcpy(void *dest, const void *src, s21_size_t n) {
- for (s21_size_t i = 0; i < n; i++) {
- *((char *)dest + i) = *((char *)src + i);
- }
- return dest;
- }
- void *s21_memmove(void *dest, const void *src, s21_size_t n) {
- char *buff = (char *)calloc(n, sizeof(char));
- s21_memcpy(buff, src, n);
- s21_memcpy(dest, buff, n);
- free(buff);
- return dest;
- }
- void *s21_memset(void *str, int c, s21_size_t n) {
- for (s21_size_t i = 0; i < n; i++) {
- *((char *)str + i) = c;
- }
- return str;
- }
- char *s21_strcat(char *dest, const char *src) {
- s21_size_t size = s21_strlen(dest);
- int i = 0;
- for (; *(src + i) != '\0'; i++) {
- *(dest + size + i) = *(src + i);
- }
- *(dest + size + i) = '\0';
- return dest;
- }
- char *s21_strncat(char *dest, const char *src, s21_size_t n) {
- char *start = dest;
- dest += s21_strlen(dest);
- for (; (n != 0 && *src != '\0'); n--) {
- *dest = *src;
- src++;
- dest++;
- }
- if (*(dest - 1) != '\0') {
- *dest = '\0';
- }
- return start;
- }
- char *s21_strchr(const char *str, int c) {
- char *result_adrr = s21_NULL;
- s21_size_t i;
- for (i = 0; i < s21_strlen(str); i++) {
- if (*((char *)str + i) == c) {
- result_adrr = (char *)str + i;
- break;
- } else {
- result_adrr = 0;
- }
- }
- return (result_adrr != 0) ? result_adrr : (char *)s21_NULL;
- }
- int s21_strcmp(const char *str1, const char *str2) {
- int result = 0;
- for (int i = 0; *((char *)str1 + i) != '\0' || *((char *)str2 + i) != '\0'; i++) {
- result = *(str1 + i) - *(str2 + i);
- if (result != 0) {
- break;
- }
- }
- return (result);
- }
- int s21_strncmp(const char *str1, const char *str2, s21_size_t n) {
- int result = 0;
- for (int i = 0; (*((char *)str1 + i) != '\0' || *((char *)str2 + i) != '\0') && i < (int)n; i++) {
- result = *(str1 + i) - *(str2 + i);
- if (result != 0) {
- break;
- }
- }
- return (result);
- }
- char *s21_strcpy(char *dest, const char *src) {
- char *r = dest;
- while ((*dest++ = *src++)) {
- }
- return r;
- }
- char *s21_strncpy(char *dest, const char *src, s21_size_t n) {
- s21_size_t i = 0;
- int size = s21_strlen(dest);
- for (int j = 0; j < size; j++) {
- *(dest + j) = 0;
- }
- do {
- if (i < n) {
- *(dest + i) = *(src + i);
- i++;
- } else {
- break;
- }
- } while (*((char *)src + i) != '\0');
- return (dest);
- }
- s21_size_t s21_strcspn(const char *str1, const char *str2) {
- s21_size_t i = 0;
- while (i < s21_strlen(str1)) {
- if (s21_memchr(str2, *(str1 + i), s21_strlen(str2)) == s21_NULL) {
- i++;
- } else {
- break;
- }
- }
- return (i);
- }
- char *s21_strerror(int errnum) {
- char *out = (char *)calloc(50, sizeof(char));
- int err_p_max = 107;
- #if MACOS == 1
- err_p_max = 132;
- #endif
- #if LINUX == 1
- err_p_max = 107;
- #endif
- if (errnum < 0 || errnum > err_p_max) {
- #if MACOS == 1
- s21_sprintf(out, "Unknown error: %d", errnum);
- #endif
- #if LINUX == 1
- s21_sprintf(out, "No error information");
- #endif
- } else {
- #if MACOS == 1
- s21_sprintf(out, "%s", ErrorNames1[errnum]);
- #endif
- #if LINUX ==1
- s21_sprintf(out, "%s", ErrorNames[errnum]);
- #endif
- }
- out = (char *)realloc(out, s21_strlen(out));
- return (out);
- }
- char *s21_strpbrk(const char *str1, const char *str2) {
- s21_size_t i = 0;
- char *ch;
- while (*(str1 + i) && s21_strchr(str2, *(str1 + i)) == s21_NULL) i++;
- ch = *(str1 + i) ? (char *)(str1 + i) : s21_NULL;
- return (ch);
- }
- char *s21_strrchr(const char *str, int c) {
- char *ch = s21_NULL;
- for (int i = s21_strlen(str); i >= 0; i--)
- if (str[i] == c) {
- ch = (char *)str + i;
- break;
- }
- return ch;
- }
- s21_size_t s21_strspn(const char *str1, const char *str2) {
- s21_size_t i, j;
- for (i = 0; str1[i] != '\0'; i++) {
- for (j = 0; str2[j] != str1[i]; j++) {
- if (str2[j] == '\0')
- return i;
- }
- }
- return i;
- }
- char *s21_strstr(const char *haystack, const char *needle) {
- int flag;
- for (int i = 0; haystack[i] != '\0'; i++) {
- if (haystack[i] == needle[0])
- flag = 0;
- for (int j = 0; needle[j] != '\0'; j++) {
- if (haystack[i + j] != needle[j]) {
- flag = 1;
- break;
- }
- }
- if (flag == 0)
- return (char *)haystack + i;
- }
- return s21_NULL;
- }
- char *s21_strtok(char *str, const char *delim) {
- static char *buffer;
- char *token = s21_NULL;
- if (str) {
- buffer = str;
- while (*buffer && s21_strchr(delim, *buffer)) {
- *buffer++ = '\0';
- }
- }
- if (buffer && *buffer) {
- str = buffer;
- while (*buffer && !s21_strchr(delim, *buffer)) {
- ++buffer;
- }
- while (*buffer && s21_strchr(delim, *buffer)) {
- *buffer++ = '\0';
- }
- token = str;
- }
- return token;
- }
- void *s21_to_upper(const char *str) {
- char *new_string = (char *)calloc(s21_strlen(str), sizeof(char));
- if (str != s21_NULL) {
- s21_strcpy(new_string, str);
- s21_size_t i = 0;
- for (; i < s21_strlen(new_string); i++) {
- if (new_string[i] >= 97 && new_string[i] <= 122) {
- new_string[i] = new_string[i] - 32;
- }
- }
- } else {
- free(new_string);
- new_string = s21_NULL;
- }
- return new_string;
- }
- void *s21_to_lower(const char *str) {
- char *new_string = (char *)calloc(s21_strlen(str), sizeof(char));
- if (str != s21_NULL) {
- s21_strcpy(new_string, str);
- s21_size_t i = 0;
- for (; i < s21_strlen(new_string); i++) {
- if (new_string[i] >= 65 && new_string[i] <= 90) {
- new_string[i] = new_string[i] + 32;
- }
- }
- } else {
- free(new_string);
- new_string = s21_NULL;
- }
- return new_string;
- }
- void *s21_insert(const char *src, const char *str, s21_size_t start_index) {
- char *new_string = s21_NULL;
- if (src && str && s21_strlen(src) >= start_index) {
- new_string = (char *)calloc((s21_strlen(src) + s21_strlen(str)), sizeof(char));
- s21_strncpy(new_string, src, start_index);
- new_string[start_index] = '\0';
- s21_strcat(new_string, str);
- s21_strcat(new_string, src + start_index);
- }
- return new_string;
- }
- void clean_str(char *str, int size_str) {
- if (size_str > 0) {
- for (int j = 0; j < size_str; j++) {
- *(str + j) = 0;
- }
- }
- }
- int s21_sprintf(char *str, const char *format, ...) {
- parcer_t spec[20];
- char *res = (char *)calloc(1000, sizeof(char));
- parcer(format, spec);
- va_list temp;
- va_start(temp, format);
- int size_format = s21_strlen(format);
- int i = 0;
- for (int j = 0; j < size_format; j++) {
- if (*(format + j) == '%' && spec[i].spec_pos == j) {
- if (spec[i].width_arg_flag == 1) {
- spec[i].width = va_arg(temp, int);
- }
- if (spec[i].accur_arg_flag == 1) {
- spec[i].accur = va_arg(temp, int);
- }
- if (spec[i].specif == 'c') {
- check_ch(&(spec[i]), res, va_arg(temp, int));
- } else if (spec[i].specif == 'd' || spec[i].specif == 'i' || spec[i].specif == 'u') {
- long int ld;
- short int sd;
- unsigned long int uld;
- unsigned short int usd;
- unsigned int ud;
- int d;
- switch (spec[i].lenght) {
- case 'h': {
- if (spec[i].specif == 'u') {
- usd = va_arg(temp, unsigned int);
- ld = (long int)usd;
- } else {
- sd = va_arg(temp, int);
- ld = (long int)sd;
- }
- break;
- }
- case 'l': {
- if (spec->specif == 'u') {
- uld = (long int)va_arg(temp, unsigned long int);
- ld = (long int)uld;
- } else {
- ld = va_arg(temp, long int);
- }
- break;
- }
- default: {
- if (spec->specif == 'u') {
- ud = (long int)va_arg(temp, unsigned int);
- ld = (long int)ud;
- } else {
- d = (long int)va_arg(temp, int);
- ld = (long int)d;
- }
- break;
- }
- }
- d_to_str(res, &spec[i], ld);
- } else if (s21_strchr("feE", spec[i].specif) != s21_NULL) {
- long double d_arg;
- if (spec[i].lenght == 'L') {
- d_arg = va_arg(temp, long double);
- } else {
- d_arg = (long double)va_arg(temp, double);
- }
- float_e_to_str(&(spec[i]), d_arg, res, spec[i].specif);
- } else if (spec[i].specif == 'x' || spec[i].specif == 'X') {
- int_to_oct_hex_str(&(spec[i]), va_arg(temp, long long int), res, 16);
- } else if (spec[i].specif == 'o') {
- int_to_oct_hex_str(&(spec[i]), va_arg(temp, long long int), res, 8);
- } else if (spec[i].specif == 'g' || spec[i].specif == 'G') {
- g_to_str(&(spec[i]), res, temp);
- } else if (spec[i].specif == 'p') {
- pnt_to_str(&(spec[i]), va_arg(temp, long long int), res);
- } else if (spec[i].specif == '%') {
- s21_strcat(res, "%");
- } else if (spec[i].specif == 'n') {
- int *d = va_arg(temp, int *);
- *d = (int)s21_strlen(res);
- } else if (spec[i].specif == 's') {
- str_to_res_str(&(spec[i]), va_arg(temp, char *), res);
- }
- j += spec[i].size_format - 1;
- i++;
- } else {
- *(res + s21_strlen(res)) = *(format + j);
- }
- }
- va_end(temp);
- s21_size_t res_len = s21_strlen(res);
- s21_strcpy(str, res);
- free(res);
- *(str + res_len) = '\0';
- return (int)res_len;
- }
- void check_ch(parcer_t *spec, char *str, int ch) {
- char *temp_str = (char *)calloc(50, sizeof(char));
- char c = ch;
- if (spec->width != 0) {
- char empty = (s21_strchr(spec->flag, '0') == s21_NULL) ? ' ' : '0';
- if (s21_strchr(spec->flag, '-') != s21_NULL) {
- temp_str[0] = c;
- for (int i = 1; i < spec->width; i++)
- *(temp_str + i) = empty;
- } else {
- for (int i = 0; i < spec->width - 1; i++)
- *(temp_str + i) = empty;
- temp_str[spec->width - 1] = c;
- }
- } else {
- temp_str[0] = c;
- }
- s21_strcat(str, temp_str);
- free(temp_str);
- }
- int int_to_str(char *str, long int d) {
- long temp = d;
- int i = 0;
- do {
- temp /= 10;
- i++;
- } while (temp != 0);
- int size = i;
- i--;
- do {
- *(str + i) = d % 10 + '0';
- d /= 10;
- i--;
- } while (d != 0);
- return size;
- }
- int str_to_int(char *str, int size) {
- int sign = check_sign(&str, &size);
- int d = 0;
- for (int i = 0; i < size; i++) {
- if (*(str + i) >= '0' && *(str + i) <= '9') {
- d *= 10;
- d += (*(str + i) - '0');
- }
- }
- d *= sign;
- return d;
- }
- void pnt_to_str(parcer_t *spec, long long int int_p, char *str) {
- int_to_oct_hex_str(spec, int_p, str, 16);
- }
- void str_to_res_str(parcer_t *spec, char *arg_str, char *str) {
- int size = spec->accur < (int) s21_strlen(arg_str) &&
- spec->accur != -1 ? spec->accur : (int) s21_strlen(arg_str);
- int dif_size = spec->width - size;
- if (dif_size > 0 && s21_strchr(spec->flag, '-') == s21_NULL) {
- for (int i = 0; i < dif_size; i++) {
- *(str + s21_strlen(str)) = ' ';
- }
- }
- s21_strncat(str, arg_str, size);
- if (dif_size > 0 && s21_strchr(spec->flag, '-') != s21_NULL) {
- for (int i = 0; i < dif_size; i++) {
- *(str + s21_strlen(str)) = ' ';
- }
- }
- }
- int parcer(const char *format, parcer_t *spec) {
- char flag[] = "-+ 0#";
- char width[] = "1234567890*";
- char accur[] = ".1234567890*";
- char lenght[] = "hlL";
- char specif[] = "cdieEfgGosuxXpn%";
- char *refer_pointer[5] = {flag, width, accur, lenght, specif};
- int counter = 0;
- int counter_buff;
- int ref_point_p = 0;
- int i = 0;
- while (*(format + counter) != 0) {
- if (*(format + counter) == '%' && ref_point_p == 0) {
- spec[i].spec_pos = counter;
- ++counter;
- for (ref_point_p = 0; ref_point_p < 5; ref_point_p++) {
- char *str = (char *)calloc(20, sizeof(char));
- search_spec(&counter_buff, &counter, format, refer_pointer[ref_point_p], str, ref_point_p);
- switch (ref_point_p) {
- case 0: {
- s21_strcpy(spec[i].flag, str);
- break;
- }
- case 1: {
- if (s21_strchr(str, '*') != s21_NULL) {
- spec[i].width_arg_flag = 1;
- } else {
- spec[i].width_arg_flag = 0;
- int size = s21_strlen(str);
- spec[i].width = str_to_int(str, size);
- }
- break;
- }
- case 2: {
- if (s21_strchr(str, '*') != s21_NULL) {
- spec[i].accur_arg_flag = 1;
- } else {
- spec[i].accur = 0;
- spec[i].accur_arg_flag = 0;
- int size = s21_strlen(str);
- if (size == 0) {
- spec[i].accur = -1;
- } else {
- spec[i].accur = str_to_int(str, size);
- }
- }
- break;
- }
- case 3: {
- spec[i].lenght = str[0];
- break;
- }
- case 4: {
- spec[i].specif = str[0];
- break;
- }
- default:
- break;
- }
- free(str);
- }
- ref_point_p = 0;
- if (s21_strchr(specif, spec[i].specif) == s21_NULL) {
- spec[i].specif = '%';
- }
- spec[i].size_format = counter - spec[i].spec_pos;
- i++;
- } else {
- ++counter;
- }
- }
- return i;
- }
- void search_spec(int *counter_buff,
- int *counter,
- const char *format,
- char *refer,
- char *str,
- int ref_point_p) {
- *counter_buff = *counter;
- *counter = check(*counter, format, refer, ref_point_p);
- test_output(*counter, *counter_buff, format, str);
- }
- int check(int counter, const char *str, const char *refer, int ref_point_p) {
- int i = 0;
- while (s21_strchr(refer, (int)*(str + counter)) != s21_NULL) {
- ++counter;
- i++;
- if (ref_point_p == 4 && i == 1) {
- break;
- }
- }
- return counter;
- }
- void test_output(int counter, int counter_buff, const char *format, char *str) {
- int i = 0;
- while (counter_buff < counter) {
- *(str + i) = *(format + counter_buff);
- ++counter_buff;
- i++;
- }
- }
- void int_to_oct_hex_str(parcer_t *spec, long long int d, char *str, int n) {
- char lenght = spec->lenght;
- switch (lenght) {
- case 'h':
- if (spec->specif == 'u') {
- d = (unsigned short int)d;
- } else {
- d = (short int)d;
- }
- break;
- case 'l':
- if (spec->specif == 'u') {
- d = (unsigned long int)d;
- } else {
- d = (long int)d;
- }
- break;
- default:
- break;
- }
- char *buff = (char *)calloc(40, sizeof(char));
- char *temp_str = buff;
- do {
- if (n == 16) {
- switch (d % n) {
- case 10: {
- *temp_str = spec->specif == 'x' || spec->specif == 'p' ? 'a' : 'A';
- break;
- }
- case 11: {
- *temp_str = spec->specif == 'x' || spec->specif == 'p' ? 'b' : 'B';
- break;
- }
- case 12: {
- *temp_str = spec->specif == 'x' || spec->specif == 'p' ? 'c' : 'C';
- break;
- }
- case 13: {
- *temp_str = spec->specif == 'x' || spec->specif == 'p' ? 'd' : 'D';
- break;
- }
- case 14: {
- *temp_str = spec->specif == 'x' || spec->specif == 'p' ? 'e' : 'E';
- break;
- }
- case 15: {
- *temp_str = spec->specif == 'x' || spec->specif == 'p' ? 'f' : 'F';
- break;
- }
- default: {
- *temp_str = (d % n) + '0';
- break;
- }
- }
- } else {
- *temp_str = (d % n) + '0';
- }
- temp_str++;
- d /= n;
- } while (d != 0);
- char empty = ' ';
- s21_size_t size = s21_strlen(buff) > (s21_size_t)spec->accur ? s21_strlen(buff) : (s21_size_t)spec->accur;
- if (s21_strchr(spec->flag, '#') != s21_NULL) {
- switch (spec->specif) {
- case 'o': {
- s21_strcat(str, "0");
- break;
- }
- case 'x': {
- s21_strcat(str, "0x");
- break;
- }
- case 'X': {
- s21_strcat(str, "0X");
- break;
- }
- default:
- break;
- }
- }
- if (spec->specif == 'p') {
- s21_strcat(str, "0x");
- }
- if ((s21_size_t)spec->width > size && s21_strchr(spec->flag, '-') == s21_NULL) {
- for (s21_size_t i = 0; i < spec->width - size; i++) {
- *(str + s21_strlen(str)) = empty;
- }
- }
- if (temp_str - buff < spec->accur) {
- int accur_dif = spec->accur - (temp_str - buff);
- for (int i = 0; i < accur_dif; i++) {
- *(str + s21_strlen(str)) = '0';
- }
- }
- while (temp_str != buff) {
- *(str + s21_strlen(str)) = *(--temp_str);
- }
- if ((s21_size_t)spec->width > size && s21_strchr(spec->flag, '-') != s21_NULL) {
- for (s21_size_t i = 0; i < spec->width - size; i++) {
- *(str + s21_strlen(str)) = empty;
- }
- }
- free(buff);
- }
- void g_to_str(parcer_t *spec, char *str, va_list temp) {
- char specif;
- long double arg_lf;
- if (spec->lenght == 'L') {
- arg_lf = va_arg(temp, long double);
- } else {
- arg_lf = (long double)va_arg(temp, double);
- }
- char buff1[200] = "";
- int st = float_e_to_str(spec, arg_lf, buff1, spec->specif == 'g' ? 'e' : 'E');
- if (spec->accur == -1) {
- spec->accur = 6;
- } else if (spec->accur == 0) {
- spec->accur = 1;
- }
- if (spec->accur > st && st >= -4) {
- spec->accur -= (st + 1);
- specif = 'f';
- } else {
- spec->accur -= 1;
- specif = spec->specif == 'g' ? 'e' : 'E';
- }
- char buff2[200] = "";
- float_e_to_str(spec, arg_lf, buff2, specif);
- s21_strcat(str, buff2);
- }
- int float_e_to_str(parcer_t *spec, long double e, char *str, char specif) {
- char *buff = (char *)calloc(100, sizeof(char));
- char *temp_str = buff;
- int count_l_d = 0;
- if (s21_strchr(spec->flag, '+') != s21_NULL && e >= 0) {
- *temp_str = '+';
- temp_str++;
- }
- if (e < -DBL_EPSILON) {
- *temp_str = '-';
- temp_str++;
- e = -e;
- }
- if (s21_strchr(spec->flag, ' ') != s21_NULL &&
- s21_strchr(buff, '+') == s21_NULL &&
- s21_strchr(buff, '-') == s21_NULL) {
- *temp_str = ' ';
- temp_str++;
- }
- int size;
- int accur;
- if (spec->specif != 'g' && spec->specif != 'G') {
- accur = (spec->accur == -1) ? 6 : spec->accur;
- } else {
- accur = spec->accur;
- }
- if (specif == 'e' || specif == 'E') {
- while (fabsl(e) >= 10 || fabsl(e) < 1) {
- if (fabsl(e) > 0 && fabsl(e) < 1) {
- e *= 10;
- count_l_d--;
- } else if (fabsl(e) >= 10) {
- e /= 10;
- count_l_d++;
- } else {
- break;
- }
- }
- *temp_str = ((char)e) + '0';
- } else if (specif == 'f') {
- size = int_to_str(temp_str, (long int)e);
- temp_str += size - 1;
- }
- if (accur != 0 || s21_strchr(spec->flag, '#') != s21_NULL) {
- *(++temp_str) = '.';
- }
- e -= (int)e;
- e += FLT_EPSILON;
- for (int i = 0; i < accur; i++) {
- e *= 10;
- long double temp;
- e = modfl(e, &temp);
- *(++temp_str) = ((int)temp) + '0';
- }
- if (spec->specif == 'g' || spec->specif == 'G') {
- while (*temp_str == '0') {
- *temp_str = 0;
- temp_str--;
- }
- }
- int temp_count = count_l_d;
- if (s21_strchr("eE", specif) != s21_NULL) {
- *(++temp_str) = (specif == 'e') ? 'e' : 'E';
- *(++temp_str) = temp_count < 0 ? '-' : '+';
- if (abs(temp_count) < 10) {
- *(++temp_str) = '0';
- *(++temp_str) = abs(temp_count) + '0';
- } else {
- *(++temp_str) = abs(temp_count) / 10 + '0';
- temp_count %= 10;
- *(++temp_str) = abs(temp_count) + '0';
- }
- }
- char empty = ' ';
- if (s21_strchr(spec->flag, '0') != s21_NULL) {
- empty = '0';
- }
- if ((s21_size_t)spec->width > s21_strlen(buff) && s21_strchr(spec->flag, '-') == s21_NULL) {
- s21_size_t size = s21_strlen(buff);
- for (s21_size_t i = 0; i < spec->width - size; i++) {
- *(str + s21_strlen(str)) = empty;
- }
- }
- s21_strcat(str, buff);
- if ((s21_size_t)spec->width > s21_strlen(buff) && s21_strchr(spec->flag, '-') != s21_NULL) {
- s21_size_t size = s21_strlen(buff);
- for (s21_size_t i = 0; i < spec->width - size; i++) {
- *(str + s21_strlen(str)) = empty;
- }
- }
- int size_res;
- if (specif == 'e' || specif == 'E') {
- size_res = count_l_d;
- } else {
- size_res = -1;
- }
- free(buff);
- return size_res;
- }
- void d_to_str(char *out1, parcer_t *spec, long int d) {
- char *flags = spec->flag;
- char out[201];
- out[200] = '\0';
- for (int i_tmp = 0; i_tmp < 200; i_tmp++) {
- out[i_tmp] = ' ';
- }
- int w = spec->width;
- int accur = spec->accur;
- accur = accur == -1 ? 0 : accur;
- int accurf = accur;
- if (s21_strchr(flags, '0') != s21_NULL && (spec->accur == -1)) {
- accur = w - 1;
- }
- int ii = 0;
- char sym = (s21_strchr(flags, '+') != s21_NULL && spec->specif != 'u') ? '+' : 0;
- accurf = accur + ((s21_strchr(flags, '+') != NULL) || (d < 0));
- int dig_count = dig_counter(d);
- w = (w < accur) ? accur : w;
- int i_tmp;
- if (s21_strchr(flags, '0') != s21_NULL) {
- if (spec->accur == -1) {
- i_tmp = 0;} else {
- i_tmp = (w - accur);
- }
- for (; i_tmp < w; i_tmp++) {
- out[i_tmp] = '0';
- }
- }
- if (d < (long int)0) {
- sym = '-';
- d = d * -1;
- } else {
- if ((s21_strchr(flags, '+') != NULL) || (s21_strchr(flags, ' ') != NULL)) {
- dig_count++;
- }
- }
- w = (w < dig_count) ? dig_count : w;
- if (w > dig_count || accur > dig_count) {
- ii = w - ((accur < dig_count) ? dig_count : accurf);
- }
- if (s21_strchr(flags, '-')) {
- ii = 0;
- }
- if (s21_strchr(flags, ' ') != 0 && sym == '\0') {
- out[ii] = ' ';
- ii++;
- } else {
- if (sym != '\0') {
- out[ii] = sym;
- ii++;
- }
- }
- dig_count = dig_counter(d);
- long int del = stepen(dig_counter(d));
- long int d_tmp = d;
- get_zero(out, &ii, ii + accur - dig_count);
- for (int i = 0; i < dig_count; i++, ii++) {
- out[ii] = ((long int)d_tmp / del) + 48;
- d_tmp = d_tmp - del * ((long int)d_tmp / del);
- del = del / 10;
- }
- out[ii] = '\0';
- s21_strcat(out1, out);
- }
- long int stepen(long int b) {
- long int result = 1;
- for (long int i = 1; i < b; i++) {
- result = result * 10;
- }
- return result;
- }
- int dig_counter(long int d) {
- int i = 1;
- long int temp = 10;
- if (d < 0) {
- (d = d * -1);
- i++;
- }
- while (1) {
- if (d >= temp) {
- temp = temp * 10;
- i++;
- } else {
- break;
- }
- }
- return (i);
- }
- void get_zero(char *out, int *ii, int stop) {
- for (; *ii < stop; *ii = *ii + 1) {
- out[*ii] = '0';
- }
- }
- int s21_sscanf(const char *str, const char *format, ...) {
- int used_args = 0;
- parcer_t spec[20];
- int num_exp_args = parcer(format, spec);
- int size_src = s21_strlen(str);
- int j = 0;
- va_list list_args;
- va_start(list_args, format);
- for (int i = 0; i < num_exp_args && j < size_src; i++) {
- if (spec[i].specif == 'c') {
- spec[i].width = 1;
- }
- char *buff = (char *)calloc(size_src, sizeof(char));
- int k = 0;
- while (*(str + j) != ' ' && *(str + j) != '\n' && *(str + j) != '\t' && j < size_src) {
- *(buff + k) = *(str + j);
- k++;
- if (spec[i].width != 0 && spec[i].width == k) {
- break;
- }
- j++;
- }
- j++;
- while (*(str + j) == ' ' || *(str + j) == '\n' || *(str + j) == '\t') {
- j++;
- }
- if (spec[i].width_arg_flag == 0) {
- if (spec[i].width == 0) {
- spec[i].width = s21_strlen(buff);
- }
- s21_size_t size = (s21_size_t)spec[i].width < s21_strlen(buff) ?
- (s21_size_t)spec[i].width : s21_strlen(buff);
- if (spec[i].specif == 'c') {
- str_to_ch(buff, &(spec[i]), list_args);
- used_args++;
- } else if (spec[i].specif == 's') {
- sscanf_str_to_res(&(spec[i]), buff, list_args);
- used_args++;
- } else if (s21_strchr("duxXo", spec[i].specif) != s21_NULL) {
- int d;
- if (s21_strchr("xX", spec[i].specif) != s21_NULL) {
- d = 16;
- } else if (spec[i].specif == 'o') {
- d = 8;
- } else {
- d = 10;
- }
- hex_oct_dec_convert(buff, size, &(spec[i]), list_args, d);
- used_args++;
- } else if (spec[i].specif == 'i') {
- hex_oct_str_to_int(&(spec[i]), buff, list_args);
- used_args++;
- } else if (spec[i].specif == 'n') {
- int *n_arg = va_arg(list_args, int *);
- *n_arg = j - k - 2;
- if (*n_arg < 0)
- *n_arg = 0;
- used_args++;
- } else if (s21_strchr("eEfgG", spec[i].specif) != s21_NULL) {
- double_arg_to_str(buff, size, &(spec[i]), list_args);
- used_args++;
- } else if (spec[i].specif == 'p') {
- hex_oct_dec_convert(buff, size, &(spec[i]), list_args, 16);
- used_args++;
- }
- }
- free(buff);
- }
- va_end(list_args);
- return used_args;
- }
- void sscanf_str_to_res(parcer_t *spec, char *src, va_list list_args) {
- if (spec->lenght == 'l') {
- wchar_t *warg = va_arg(list_args, wchar_t *);
- for (s21_size_t i = 0; i < s21_strlen(src); i++) {
- warg[i] = (wchar_t)(*(src + i));
- }
- } else {
- char *s_arg = va_arg(list_args, char *);
- clean_str(s_arg, s21_strlen(s_arg));
- s21_strncat(s_arg, src, (spec->width < (int)s21_strlen(src) ? spec->width : (int)s21_strlen(src)));
- }
- }
- void hex_oct_str_to_int(parcer_t *spec, char *buff, va_list list_args) {
- int size = spec->width < (int)s21_strlen(buff) ? spec->width : (int)s21_strlen(buff);
- if (*buff == '0') {
- if (*(buff + 1) == 'x' || *(buff + 1) == 'X') {
- hex_oct_dec_convert(buff + 2, size - 2, spec, list_args, 16);
- } else {
- hex_oct_dec_convert(buff + 1, size - 1, spec, list_args, 8);
- }
- } else {
- hex_oct_dec_convert(buff, size, spec, list_args, 10);
- }
- }
- void hex_oct_dec_convert(char *buff, int size, parcer_t *spec, va_list list_args, int s_s) {
- int_type int_val;
- unsigned long arg = 0;
- int sign = check_sign(&buff, &size);
- for (int i = 0; i < size; i++) {
- arg *= s_s;
- if (*(buff + i) >= '0' && *(buff + i) <= '9') {
- arg += (*(buff + i) - '0');
- } else if (*(buff + i) >= 'a' && *(buff + i) <= 'f') {
- arg += (*(buff + i) - 'W');
- } else if (*(buff + i) >= 'A' && *(buff + i) <= 'F') {
- arg += (*(buff + i) - '7');
- }
- }
- arg *= sign;
- if (s21_strchr("uxXo", spec->specif) != s21_NULL) {
- if (spec->lenght == 'l') {
- int_val.lu = va_arg(list_args, unsigned long *);
- *(int_val.lu) = (unsigned long)arg;
- } else if (spec->lenght == 'h') {
- int_val.hu = va_arg(list_args, unsigned short *);
- *(int_val.hu) = (unsigned short)arg;
- } else {
- int_val.u = va_arg(list_args, unsigned *);
- *(int_val.u) = (unsigned)arg;
- }
- } else if (s21_strchr("di", spec->specif) != s21_NULL) {
- if (spec->lenght == 'l') {
- int_val.ld = va_arg(list_args, long *);
- *(int_val.ld) = (long)arg;
- } else if (spec->lenght == 'h') {
- int_val.h = va_arg(list_args, short *);
- *(int_val.h) = (short)arg;
- } else {
- int_val.d = va_arg(list_args, int *);
- *(int_val.d) = (int)arg;
- }
- } else if (spec->specif == 'p') {
- void *p_arg = va_arg(list_args, void **);
- int_val.ld = p_arg;
- *(int_val.ld) = (long)arg;
- }
- }
- void str_to_ch(char *buff, parcer_t *spec, va_list list_arg) {
- char *ch;
- wchar_t *wch;
- if (spec->lenght == 'l') {
- wch = va_arg(list_arg, wchar_t *);
- *wch = (wchar_t)*buff;
- } else {
- ch = va_arg(list_arg, char *);
- *ch = *buff;
- }
- }
- int check_sign(char **str, int *size) {
- int sign = 1;
- if (**str == '-') {
- *str += 1;
- *size -= 1;
- sign = -1;
- }
- return sign;
- }
- void double_arg_to_str(char *buff, int size, parcer_t *spec, va_list list_args) {
- double_type doub_val;
- long double f_arg = 0;
- int left = 0;
- int sign = *buff == '-' ? -1 : 1;
- int i = 0;
- if (*buff == '-' || *buff == '+') {
- i++;
- }
- for (; i < size && *(buff + i) != '.'; i++) {
- left *= 10;
- left += (*(buff + i) - '0');
- }
- f_arg = left;
- int count_num = 0;
- int right = 0;
- if (i < size && *(buff + i) == '.') {
- for (++i; i < size && *(buff + i) != 'e' && *(buff + i) != 'E'; i++) {
- right *= 10;
- right += (*(buff + i) - '0');
- count_num++;
- }
- }
- f_arg += right / pow(10, count_num);
- if (i < size && (*(buff + i) == 'e' || *(buff + i) == 'E')) {
- i++;
- int st_sign = *(buff + i) == '-' ? -1 : 1;
- int st = 0;
- count_num = 0;
- for (++i; i < size && count_num < 2; i++) {
- st *= 10;
- st += (*(buff + i) - '0');
- count_num++;
- }
- f_arg *= pow(10, st * st_sign);
- }
- f_arg *= sign;
- if (spec->lenght == 'l') {
- doub_val.doub = va_arg(list_args, double *);
- *(doub_val.doub) = (double)f_arg;
- } else if (spec->lenght == 'L') {
- doub_val.l_doub = va_arg(list_args, long double *);
- *(doub_val.l_doub) = (long double)f_arg;
- } else {
- doub_val.f = va_arg(list_args, float *);
- *(doub_val.f) = (float)f_arg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement