-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
147 lines (136 loc) · 3.19 KB
/
utils.c
File metadata and controls
147 lines (136 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <unistd.h>
#include <utils.h>
/**
* _isdigit - checks if a given character is a digit
* @c: character to check whether is digit
* Return: true if character is a digit false otherwise
*/
bool _isdigit(int c)
{
return (c >= '0' && c <= '9');
}
/**
* _atoi - converts a string pointed to by ptr to an integer
* @ptr: pointer to string to convert to integer
* Return: The converted integer
*/
int _atoi(const char **ptr)
{
int num = 0, base = 1;
while (_isdigit(**ptr)) {
num *= base;
num += **ptr - '0';
base *= 10;
(*ptr)++;
}
return (num);
}
/**
* flush - flushes the buffer to stdout
* @buf: buffer to flush to stdout
* @bufsize: size of the buffer
*/
void flush(char *buf, int *bufsize)
{
write(STDOUT_FILENO, buf, *bufsize);
*bufsize = 0;
}
/**
* _strlen - calculates the length of a string
* @str: string to calculate length of
* Return: The length of the string
*/
int _strlen(const char *str)
{
int len = 0;
while (str[len])
len++;
return (len);
}
/**
* write_zero - writes zeroes to the buffer
* @buf: buffer to write zeroes to
* @bufsize: current size of the buffer
* @count: number of zeroes to write
*/
void write_zero(char *buf, int *bufsize, int count)
{
int i;
for (i = 0; i < count; i++) {
if (*bufsize == LENGTH)
flush(buf, bufsize);
buf[(*bufsize)++] = '0';
}
}
/**
* write_space - writes spaces to the buffer
* @buf: buffer to write spaces to
* @bufsize: current size of the buffer
* @count: number of spaces to write
*/
void write_space(char *buf, int *bufsize, int count)
{
int i;
for (i = 0; i < count; i++) {
if (*bufsize == LENGTH)
flush(buf, bufsize);
buf[(*bufsize)++] = ' ';
}
}
/**
* reverse - reverses a string in place
* @buf: buffer containing the string to reverse
* @start: starting index of the string to reverse
* @end: ending index of the string to reverse
*/
void reverse(char *buf, int start, int end)
{
char temp;
while (start < end) {
temp = buf[start];
buf[start] = buf[end];
buf[end] = temp;
start++;
end--;
}
}
/**
* unumlen - calculates the length of an unsigned number in a given base
* @num: the unsigned number to calculate length of
* @base: the base to use for the calculation
* Return: The length of the number in the specified base
*/
int unumlen(unsigned long long num, int base)
{
int len = 0;
do {
len++;
} while ((num /= base) > 0);
return len;
}
/**
* get_unum - retrieves an unsigned number from the va_list based on the length
* modifier
* @size: the length modifier that indicates how to interpret the argument
* @args: pointer to the va_list containing the arguments
* Return: The unsigned number as an unsigned long long integer.
*/
unsigned long long get_unum(enum LengthModifier size, va_list *args)
{
switch (size) {
case LEN_HH:
return (unsigned long long)(unsigned char)va_arg(*args,
unsigned int);
case LEN_H:
return (unsigned long long)(unsigned short)va_arg(*args,
unsigned int);
case LEN_L:
return (unsigned long long)(unsigned long)va_arg(*args,
unsigned long);
case LEN_LL:
return (unsigned long long)va_arg(*args, unsigned long long);
default:
return (unsigned long long)(unsigned int)va_arg(*args,
unsigned int);
}
}