static void get_line_from_buffer(const char *buffer , unsigned int *n_buffer, int n, t_line *line) { int i; int offset; bool space_state = false; /*Clean str*/ for (int j = 0; j < n + 1; ++j) line->s[j] = '\0'; /*Superficial remove of space in line begin*/ for (offset = 0; offset < n && IS_SEP(buffer[*n_buffer + offset]); ++offset) ; line->n_gaps = 0; /*Line with only spaces*/ if (offset >= n) return ; *n_buffer += offset; /*Actual get_line_from_buffer*/ for (i = 0;i < n && buffer[*n_buffer + i] != '\0'; ++i) { line->s[i] = buffer[*n_buffer + i]; /*Here we count nbr gaps*/ if ((space_state && IS_SEP(line->s[i])) || (!space_state && !IS_SEP(line->s[i]))) continue ; space_state = !space_state; if (!IS_SEP(line->s[i])) ++line->n_gaps; } line->s[i] = '\0'; }
static int find_last_occ(const char *buffer, int n) { /*lookin for last space in dat 0 */ /* ^ ^ */ /*____________________________|____| */ /* 37 42 */ /* i n */ for (int i = n; i >= 0; --i) { if (IS_SEP(buffer[i])) { /*ATTENTION au --i dans le define*/ while (i > 0 && IS_SEP(buffer[--i])) ; return (i + 1); } } return (n); }
/* * IsClientConfigDirectory() -- determine if path matches well-known * client configuration directory. */ #ifdef AFS_NT40_ENV #define IS_SEP(x) ((x) == '\\' || (x) == '/') #else /* AFS_NT40_ENV */ #define IS_SEP(x) ((x) == '/') #endif /* AFS_NT40_ENV */ int _afsconf_IsClientConfigDirectory(const char *path) { const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH; int i, cc, pc; for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) { #ifdef AFS_NT40_ENV cc = tolower(cdir[i]); pc = tolower(path[i]); if (cc == '\\') { cc = '/'; } if (pc == '\\') { pc = '/'; } #else /* AFS_NT40_ENV */ cc = cdir[i]; pc = path[i]; #endif /* AFS_NT40_ENV */ if (cc != pc) { return 0; } } /* hit end of one or both; allow mismatch in existence of trailing slash */ if (cdir[i] != '\0') { if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) { return 0; } } if (path[i] != '\0') { if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) { return 0; } } return 1; }
static int ft_strncat_offset(char *dest, const t_line *orig, int n, int offset) { int spaces_offset; int space_per_word; int step_between_offset_spaces; int j; int i = 0; int current_gap = 0; /*space_per_word is the nbr of spaces to insert between every words of the line*/ /*spaces_offset is the nbr of insertion which take one more spaces*/ space_per_word = offset / (orig->n_gaps); spaces_offset = offset - (space_per_word * orig->n_gaps); if (spaces_offset == 0) step_between_offset_spaces = 0; else step_between_offset_spaces = orig->n_gaps / spaces_offset; for (j = 0; j < n && orig->s[j] != '\0'; ++j) { /*Offset is handle here*/ if (offset > 0 && j != 0 && !IS_SEP(orig->s[j]) && IS_SEP(orig->s[j - 1])) { /*spaces_to_add determine which gaps will get the bonus space*/ for (int k = space_per_word + spaces_to_add(step_between_offset_spaces, current_gap, &spaces_offset); k > 0; --k) { dest[i + j] = SEP; ++i; } ++current_gap; } /*Actual strncat*/ dest[i + j] = orig->s[j]; } return (i + j); }
static void justify(const char *orig, char *dest, unsigned int width) { dest[0] = '\0'; if (width < 1) return ; t_line line; unsigned int size = strlen(orig); unsigned int n_buffer = 0; int end_dest = 0; line.s = (char *)malloc((width + 1) * sizeof(char)); get_line_from_buffer(orig, &n_buffer, width + 1, &line); while (n_buffer < size) { unsigned int n_last_separator; /*To trim end of line*/ n_last_separator = find_last_occ(line.s, width); /*Case 1: All words can be copied*/ if (IS_SEP(line.s[width])) { end_dest += ft_strncat_endl(dest + end_dest, &line, n_last_separator, width - n_last_separator); n_buffer += width + 1; goto next; } /*Case 2: End of file, this is last line*/ else if (line.s[0] != '\0' && line.s[width - 1] == '\0') { ft_strncat_endl(dest + end_dest, &line, width, 0); break ; } /*Case 3: Last word is cutted so we remove from this line*/ else { --line.n_gaps; /*Empty line*/ if (line.s[0] != '\0' || n_last_separator != width) end_dest += ft_strncat_endl(dest + end_dest, &line, n_last_separator, width - n_last_separator); n_buffer += n_last_separator; } next: get_line_from_buffer(orig, &n_buffer, width + 1, &line); } free(line.s); }
char *OS_IsValidDay(const char *day_str) { int i = 0, ng = 0; char *ret; char day_ret[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; const char *(days[]) = { "sunday", "sun", "monday", "mon", "tuesday", "tue", "wednesday", "wed", "thursday", "thu", "friday", "fri", "saturday", "sat", "weekdays", "weekends", NULL }; int days_int[] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8}; /* Must be a valid string */ if (!day_str) { return (NULL); } RM_WHITE(day_str); /* Check for negatives */ if (*day_str == '!') { ng = 1; RM_WHITE(day_str); } while (*day_str != '\0') { i = 0; while (days[i]) { if (strncasecmp(day_str, days[i], strlen(days[i])) == 0) { /* Weekdays */ if (days_int[i] == 7) { day_ret[1] = 1; day_ret[2] = 1; day_ret[3] = 1; day_ret[4] = 1; day_ret[5] = 1; } /* Weekends */ else if (days_int[i] == 8) { day_ret[0] = 1; day_ret[6] = 1; } else { day_ret[days_int[i]] = 1; } break; } i++; } if (!days[i]) { merror(INVALID_DAY, __local_name, day_str); return (NULL); } day_str += strlen(days[i]); if (IS_SEP(day_str)) { RM_SEP(day_str); continue; } else if (*day_str == '\0') { break; } else { merror(INVALID_DAY, __local_name, day_str); return (NULL); } } /* Assign values */ os_calloc(9, sizeof(char), ret); if (ng == 1) { /* Set negative */ ret[7] = '!'; } ng = 0; for (i = 0; i <= 6; i++) { /* Check if some is checked */ if (day_ret[i] == 1) { ng = 1; } ret[i] = day_ret[i]; } /* At least one day must be checked */ if (ng == 0) { free(ret); merror(INVALID_DAY, __local_name, day_str); return (NULL); } return (ret); }
/* * Parse an encoding string, examples: s8, u8, s16, s16le, s24be ... * set *istr to the char following the encoding. Return the number * of bytes consumed. */ int aparams_strtoenc(struct aparams *par, char *istr) { char *p = istr; int i, sig, bits, le, bps, msb; #define IS_SEP(c) \ (((c) < 'a' || (c) > 'z') && \ ((c) < 'A' || (c) > 'Z') && \ ((c) < '0' || (c) > '9')) /* * get signedness */ if (*p == 's') { sig = 1; } else if (*p == 'u') { sig = 0; } else return 0; p++; /* * get number of bits per sample */ bits = 0; for (i = 0; i < 2; i++) { if (*p < '0' || *p > '9') break; bits = (bits * 10) + *p - '0'; p++; } if (bits < BITS_MIN || bits > BITS_MAX) return 0; bps = APARAMS_BPS(bits); msb = 1; le = ADATA_LE; /* * get (optional) endianness */ if (p[0] == 'l' && p[1] == 'e') { le = 1; p += 2; } else if (p[0] == 'b' && p[1] == 'e') { le = 0; p += 2; } else if (IS_SEP(*p)) { goto done; } else return 0; /* * get (optional) number of bytes */ if (*p >= '0' && *p <= '9') { bps = *p - '0'; if (bps < (bits + 7) / 8 || bps > (BITS_MAX + 7) / 8) return 0; p++; /* * get (optional) alignement */ if (p[0] == 'm' && p[1] == 's' && p[2] == 'b') { msb = 1; p += 3; } else if (p[0] == 'l' && p[1] == 's' && p[2] == 'b') { msb = 0; p += 3; } else if (IS_SEP(*p)) { goto done; } else return 0; } else if (!IS_SEP(*p)) return 0; done: par->msb = msb; par->sig = sig; par->bits = bits; par->bps = bps; par->le = le; return p - istr; }