-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeFunc.c
133 lines (113 loc) · 2.15 KB
/
writeFunc.c
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
#include "main.h"
/**
* string_to_integer - Converts a string to an integer.
* @str: The string to be converted.
*
* Return: 0 if no numbers in the string, converted number otherwise.
* -1 on error.
*/
int string_to_integer(char *str)
{
int i = 0;
unsigned long int result = 0;
if (*str == '+')
str++;
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
result *= 10;
result += (str[i] - '0');
if (result > INT_MAX)
return (-1);
}
else
return (-1);
}
return (result);
}
/**
* custPrintecimal - Prints a decimal (integer) number (base 10).
* @input: The input.
* @fd: The file descriptor to write to.
*
* Return: Number of characters printed.
*/
int custPrintecimal(int input, int fd)
{
int (*output_char)(char) = _putchar;
int i, count = 0;
unsigned int absolute_value, current;
if (fd == STDERR_FILENO)
output_char = print_error_char;
if (input < 0)
{
absolute_value = -input;
output_char('-');
count++;
}
else
{
absolute_value = input;
}
current = absolute_value;
for (i = 1000000000; i > 1; i /= 10)
{
if (absolute_value / i)
{
output_char('0' + current / i);
count++;
}
current %= i;
}
output_char('0' + current);
count++;
return (count);
}
/**
* number_to_string - Converts a number to a string.
* @num: Number.
* @base: Base.
* @flags: Argument flags.
*
* Return: String.
*/
char *number_to_string(long int num, int base, int flags)
{
static char *digits;
static char buffer[50];
char sign = 0;
char *ptr;
unsigned long n = num;
if (!(flags & 2) && num < 0) /*2 = unsigned, 1 = lowercase*/
{
n = -num;
sign = '-';
}
digits = flags & 1 ? "0123456789abcdef" : "0123456789ABCDEF";
ptr = &buffer[49];
*ptr = '\0';
do {
*--ptr = digits[n % base];
n /= base;
} while (n != 0);
if (sign)
*--ptr = sign;
return (ptr);
}
/**
* rmStringComment - Replaces the first instance of '#' with '\0'.
* @buffer: Address of the string to modify.
*
* Return: Always 0.
*/
void rmStringComment(char *buffer)
{
int i;
for (i = 0; buffer[i] != '\0'; i++)
if (buffer[i] == '#' && (!i || buffer[i - 1] == ' '))
{
buffer[i] = '\0';
break;
}
}